jQuery(document).ready(function(){
	/* Fill double drop-downs for search forms. */
	fillBottleCategory();
	fillClosureCategory();

	/* Link in small images for Bottle/Closure web apps. */
	jQuery(".BottleDataListItem").each(function() {
		jQuery(".BottleDataListLink", this).attr("href", jQuery(".BottleDataListName a", this).attr("href"));
	});
	jQuery(".ClosureDataListItem").each(function() {
		jQuery(".ClosureDataListLink", this).attr("href", jQuery(".ClosureDataListName a", this).attr("href"));
	});

	/* Hide product enquiry form. */
	jQuery('#BottleDataDetailForm').hide();
	jQuery('#ClosureDataDetailForm').hide();
	
	/* When button is clicked topggle the product enquiry form. */
	jQuery('#ProductEnquriy').click(function(){
		jQuery('#BottleDataDetailForm').toggle(400);
	});
	jQuery('#ProductEnquriy').click(function(){
		jQuery('#ClosureDataDetailForm').toggle(400);
	});
});

/****************************************************/

function printNeckOptions(associatedNecks){
	var neckOptionsArray = associatedNecks.split(",");
	var noLength = neckOptionsArray.length;
	var lastItem = noLength - 1;

	for( var i = 0, noLen = noLength; i < noLen; ++i ){
		currentNeck = neckOptionsArray[i];
		if( i == lastItem ){
			document.write('<a href="#" id="NeckGroup_' + i + '" onclick="showNeckGroup('+ i + "," + noLength +'); return false;">' + currentNeck + '</a>');
		}else{
			document.write('<a href="#" id="NeckGroup_' + i + '" onclick="showNeckGroup('+ i + "," + noLength +'); return false;">' + currentNeck + '</a>, ');
		}
	}
}

function createNeckGroups(associatedNecks){
	var associatedNecksArray = associatedNecks.split(",");
	var anLength = associatedNecksArray.length;
	
	for( var i = 0, anLen = anLength; i < anLen; ++i ){
		document.write('<div id="neckGrouping_' + i +'" class="HiddenDiv">Selected Neck: ' + associatedNecksArray[i] + '<br /></div>');
	}
}

function showNeckGroup(neckGroup,totalGroups){
	jQuery("#PostGrouping").addClass("HiddenDiv");
	
	for( var i = 0; i < totalGroups; ++i ){
		var divGroupID = '#neckGrouping_' + i;
		jQuery( divGroupID ).addClass('HiddenDiv');
	}
	
	var thisDivID = '#neckGrouping_' + neckGroup;
	jQuery( thisDivID ).removeClass('HiddenDiv');
}

function addToNeckGrouping(associatedNecks,productNecks,productLink){
	var productNecksArray = productNecks.split(',');
	var pnLength = productNecksArray.length;
	var associatedNecksArray = associatedNecks.split(",");
	var anLength = associatedNecksArray.length;
	
	for( var i = 0, anLen = anLength; i < anLen; ++i ){
		for( var j = 0, pnLen = pnLength; j < pnLen; ++j ){
			if( (associatedNecksArray[i] == productNecksArray[j]) ){
				divGroupID = 'neckGrouping_' + i;
				document.getElementById(divGroupID).innerHTML += productLink + '<br />';
			}
		}
	}
}

function groupingComplete(){
	jQuery("#PreGrouping").addClass("HiddenDiv");
	jQuery("#PostGrouping").removeClass("HiddenDiv");
}

/****************************************************/

function fillBottleCategory(){ 
	// this function is used to fill the category list on load
	addBottleOption(document.search_bottle_data.BottleCategory, "Type &gt; Bottle", "Type > Bottle", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Type &gt; Shaker Pak", "Type > Shaker Pak", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Type &gt; Jar", "Type > Jar", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Type &gt; Jerry Can", "Type > Jerry Can", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Agriculture", "Industry > Agriculture", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Industry &gt; Automotive", "Industry > Automotive", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Industry &gt; Chemical", "Industry > Chemical", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Industry - Food and Beverage", "Industry - Food and Beverage", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Pharm", "Industry > Pharmaceutical", "");
}

function SelectBottleSubCat(){
	// ON selection of category this function will work


	removeAllBottleOptions(document.search_bottle_data.BottleSubCat);
	addBottleOption(document.search_bottle_data.BottleSubCat, "*", "-- All --", "");
	
	removeAllBottleOptions(document.search_bottle_data.BottleCapacity);
	addBottleOption(document.search_bottle_data.BottleCapacity, "*", "-- All --", "");

	if(document.search_bottle_data.BottleCategory.value == 'Type &gt; Bottle'){
		
		addBottleOption(document.search_bottle_data.BottleSubCat,"Boston", "Boston");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Dairy", "Dairy");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Flat", "Flat");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Fuel", "Fuel Additive");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Honey", "Honey");
		addBottleOption(document.search_bottle_data.BottleSubCat,"H-Series", "H-Series");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Juice", "Juice");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Phial", "Phial");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Poison Round", "Poison Round");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Round", "Round");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Shaker Pak", "Shaker Pak");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Slant", "Slant");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Small", "Small");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Spray", "Spray");		
		addBottleOption(document.search_bottle_data.BottleSubCat,"Sports Drink", "Sports Drink");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Square", "Square");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Twin Neck", "Twin Neck");
			}
	if(document.search_bottle_data.BottleCategory.value == 'Type &gt; Cube (or Drum)'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Cube", "Cube");
	}
	if(document.search_bottle_data.BottleCategory.value == 'Type &gt; Jar'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Boston Base Jar", "Boston Base Jar");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Poison Jar", "Poison Jar");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Round Jar", "Round Jar");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Honey Jar", "Honey Jar");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Pharmaceutical", "PharmaceuticalJar");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Phial", "Phial");
			}
	if(document.search_bottle_data.BottleCategory.value == 'Type &gt; Jerry Can'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Jerry Can", "Jerry Can");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Jerry Can Slant", "Jerry Can Slant");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Poison Jerry Can", "Poison Jerry Can");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Poison Jerry Can Slant", "Poison Jerry Can Slant");
	}

	if(document.search_bottle_data.BottleCategory.value == 'Agriculture'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Bottles", "Bottles");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Hose-On Sprayer", "Hose-On Sprayer");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Jerry Cans", "Jerry Cans");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Shaker Paks", "Shaker Paks");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Trigger Sprayer", "Trigger Sprayers");
	}

	if(document.search_bottle_data.BottleCategory.value == 'Industry &gt; Automotive'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Bottles", "Bottles");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Jerry Cans", "Jerry Cans");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Triggers", "Triggers");
	}

	if(document.search_bottle_data.BottleCategory.value == 'Industry &gt; Chemical'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Bottles", "Bottles");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Hose-On Sprayer", "Hose-On Sprayer");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Jerry Cans", "Jerry Cans");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Shaker Paks", "Shaker Paks");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Triggers", "Triggers");
	}

	if(document.search_bottle_data.BottleCategory.value == 'Industry - Food and Beverage'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Dairy", "Dairy");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Dairy and Juice", "Dairy and Juice");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Honey", "Honey");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Others", "Others");
	}

	if(document.search_bottle_data.BottleCategory.value == 'Pharm'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Bottles", "Bottles");
		addBottleOption(document.search_bottle_data.BottleSubCat,"HDPE", "HDPE");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Jars and Phials", "Jars and Phials");
		addBottleOption(document.search_bottle_data.BottleSubCat,"PET", "PET");
	}
	
	SelectBottleCapacity();
}

function SelectBottleCapacity(){
	if( (document.search_bottle_data.BottleCategory.value == 'Type &gt; Bottle') || (document.search_bottle_data.BottleCategory.value == 'Type &gt; Cube (or Drum)') || (document.search_bottle_data.BottleCategory.value == 'Type &gt; Jerry Can') ){
		addBottleOption(document.search_bottle_data.BottleCapacity,"0,500", "0-500ml");
		addBottleOption(document.search_bottle_data.BottleCapacity,"501,1000", "> 500ml - 1L");
		addBottleOption(document.search_bottle_data.BottleCapacity,"1001,5000", "> 1L - 5L");
		addBottleOption(document.search_bottle_data.BottleCapacity,"5001,20000", "> 5L");

	}
	if( document.search_bottle_data.BottleCategory.value == 'Type &gt; Jar' ){
		addBottleOption(document.search_bottle_data.BottleCapacity,"0,500", "0-500gm");
		addBottleOption(document.search_bottle_data.BottleCapacity,"501,1000", "> 500gm - 1kg");
		addBottleOption(document.search_bottle_data.BottleCapacity,"1001,5000", "> 1kg - 5kg");
		addBottleOption(document.search_bottle_data.BottleCapacity,"5001,20000", "> 5 kg");
		
	}
}

function removeAllBottleOptions(selectbox)
{
	var i;
	for( i=selectbox.options.length-1;i>=0;i-- ){
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}

function addBottleOption(selectbox, value, text){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

/****************************************************/

function fillClosureCategory(){ 
	// this function is used to fill the category list on load
	addClosureOption(document.search_closure_data.ClosureCategory, "Caps & lids", "Caps & lids", "");
	addClosureOption(document.search_closure_data.ClosureCategory, "Plugs", "Plugs", "");
	addClosureOption(document.search_closure_data.ClosureCategory, "Pumps", "Pumps", "");
	addClosureOption(document.search_closure_data.ClosureCategory, "Sprayers", "Sprayers", "");
	addClosureOption(document.search_closure_data.ClosureCategory, "Taps", "Taps", "");
}

function SelectClosureSubCat(){
	// ON selection of category this function will work

	removeAllClosureOptions(document.search_closure_data.ClosureSubCat);
	addClosureOption(document.search_closure_data.ClosureSubCat, "*", "-- All --", "");

	if(document.search_closure_data.ClosureCategory.value == 'Caps & lids'){
		addClosureOption(document.search_closure_data.ClosureSubCat,"Child Resistant - 1 Piece", "Child Resistant - 1 Piece");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Child Resistant - 2 Piece", "Child Resistant - 2 Piece");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Dispensing Caps - Disc (Press)", "Dispensing Caps - Disc (Press)");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Dispensing Caps - Dome Dropper", "Dispensing Caps - Dome Dropper");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Dispensing Caps - Flip Top", "Dispensing Caps - Flip Top");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Dispensing Caps - Nozzle", "Dispensing Caps - Nozzle");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Dispensing Caps - Ring Seal", "Dispensing Caps - Ring Seal");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Dispensing Caps - Witches Hat", "Dispensing Caps - Witches Hat");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Other", "Other");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Other - MBSL", "Other - MBSL");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Other - Overcap", "Other - Overcap");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Other - Sports Drink Cap", "Other - Sports Drink Cap");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Shaker Pak Closure - Press", "Shaker Pak Closure - Press");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Shaker Pak Closure - Pull Tab", "Shaker Pak Closure - Pull Tab");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Shaker Pak Closure - Strainer & Lid", "Shaker Pak Closure - Strainer & Lid");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Standard Closure - Cap", "Standard Closure - Cap");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Standard Closure - Lid", "Standard Closure - Lid");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Tamper - Tell", "Tamper - Tell");
	}
	if(document.search_closure_data.ClosureCategory.value == 'Plugs'){
		addClosureOption(document.search_closure_data.ClosureSubCat,"Plug", "Plug");
	}
	if(document.search_closure_data.ClosureCategory.value == 'Pumps'){
		addClosureOption(document.search_closure_data.ClosureSubCat,"Finger Pump", "Finger Pump");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Jumbo Pump", "Jumbo Pump");
	}
	if(document.search_closure_data.ClosureCategory.value == 'Sprayers'){
		addClosureOption(document.search_closure_data.ClosureSubCat,"Finger Sprayer", "Finger Sprayer");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Jumbo Sprayer", "Jumbo Sprayer");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Trigger Sprayer - Adjustable Nozzle", "Trigger Sprayer - Adjustable Nozzle");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Trigger Sprayer - CR Safety Clip", "Trigger Sprayer - CR Safety Clip");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Trigger Sprayer - Foamer", "Trigger Sprayer - Foamer");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Trigger Sprayer - On/Off Nozzle", "Trigger Sprayer - On/Off Nozzle");
		addClosureOption(document.search_closure_data.ClosureSubCat,"Wetter Soak Sprayer", "Wetter Soak Sprayer");
	}
	if(document.search_closure_data.ClosureCategory.value == 'Taps'){
		addClosureOption(document.search_closure_data.ClosureSubCat,"Other", "Other");
	}
}

function removeAllClosureOptions(selectbox)
{
	var i;
	for( i=selectbox.options.length-1;i>=0;i-- ){
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}

function addClosureOption(selectbox, value, text){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
