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

	/* 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, "Bottle", "Bottle", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Cube (or Drum)", "Cube (or Drum)", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Jar", "Jar", "");
	addBottleOption(document.search_bottle_data.BottleCategory, "Jerry Can", "Jerry Can", "");
}

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 == '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 == 'Cube (or Drum)'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"Cube", "Cube");
	}
	if(document.search_bottle_data.BottleCategory.value == '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 == '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 == 'Dairy'){
		addBottleOption(document.search_bottle_data.BottleSubCat,"D MB", "Milk Bottles");
		addBottleOption(document.search_bottle_data.BottleSubCat,"Yoghurt Containers", "Yoghurt Containers");
			}
	
	SelectBottleCapacity();
}

function SelectBottleCapacity(){
	if( (document.search_bottle_data.BottleCategory.value == 'Bottle') || (document.search_bottle_data.BottleCategory.value == 'Cube (or Drum)') || (document.search_bottle_data.BottleCategory.value == '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 == '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);
}

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

function fillIndustryCategory(){ 
	// this function is used to fill the category list on load
	addIndustryOption(document.search_bottle_data.Industry, "Agriculture", "Agriculture", "");
	addIndustryOption(document.search_bottle_data.Industry, "Diary", "Diary", "");
	addIndustryOption(document.search_bottle_data.Industry, "Honey", "Honey", "");
	addIndustryOption(document.search_bottle_data.Industry, "Pharmaceutical", "Pharmaceutical", "");
}

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

	removeAllIndustryOptions(document.search_bottle_data.IndustrySubCat);
	addIndustryOption(document.search_bottle_data.IndustrySubCat, "*", "-- All --", "");
	
	removeAllIndustryOptions(document.search_bottle_data.IndustryCapacity);
	addIndustryOption(document.search_bottle_data.IndustryCapacity, "*", "-- All --", "");

	if(document.search_bottle_data.IndustryCategory.value == 'Agriculture'){	
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Hose On Sprayers", "Hose On Sprayers");
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Jerry Cans", "Jerry Cans");
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Shaker Paks", "Shaker Paks");
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Trigger Sprayers", "Trigger Sprayers");
		}

	if(document.search_bottle_data.IndustryCategory.value == 'Dairy'){	
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Milk Bottles", "Milk Bottles");
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Yoghurt Containers", "Yoghurt Containers");
		}

	if(document.search_bottle_data.IndustryCategory.value == 'Honey'){	
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Jars", "Jars");
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Squeeze Bottles", "Squeeze Bottles");
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Updside Down Squeeze Bottles", "Updside Down Squeeze bottles");
		}

	if(document.search_bottle_data.IndustryCategory.value == 'Pharmaceutical'){	
		addIndustryOption(document.search_bottle_data.IndustrySubCat,"Small Pharmaceutical", "Small Pharmaceutical");
		}

	SelectBottleCapacity();
}

function SelectBottleCapacity(){
	if( (document.search_bottle_data.BottleCategory.value == 'Bottle') || (document.search_bottle_data.BottleCategory.value == 'Cube (or Drum)') || (document.search_bottle_data.BottleCategory.value == '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 == '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 removeAllIndustryOptions(selectbox)
{
	var i;
	for( i=selectbox.options.length-1;i>=0;i-- ){
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}

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

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