/*

v1.0 2008-03-06

the original form asks users to select from dynamically generated pulldown menus: product, flavor and size.
after making these three choices, the form then fills in the UPC code,
from data in productdata.js.

these functions enable the reverse of that: if a user fills in the UPC code first,
they will figure out which product, flavor and size corresponds 
to that UPC code, and then 
(1) selects the product from the menu (that menu is always generated on page),
(2) populates and then selects the flavor and size pulldowns

*/

function populatePulldownsFromUPC(upc)
	{
	var f = document.contactform;
	
	var upcData = new Array();
	// retrieve data product name, product flavor, and product size
	// associated with a UPC code.
	upcData = getUPCData(upc);

	if (upcData)
		{

		// select chosen product pulldown
		f.product.options[  upcData["productIndex"]  ].selected = true;
		
		// create flavor pulldowns
		replaceChildElements('flavor',flavors[ upcData["productName"] ],'OPTION', true); 
		showHideEST(f.product); 
		showHideDate(f.product); 
		prdchg(upcData["productName"]);		
		
		// select chosen flavor pulldown
		
		// f.flavor.options[  upcData["flavorIndex"]  ].selected = true;
		
		f.flavor.options.selectedIndex = -1;
		f.flavor.options.selectedIndex = upcData["flavorIndex"];
		
		
		// create size pulldowns
		replaceChildElements('size',sizes[ upcData["flavorName"] ],'OPTION', true);
		flavchg(upcData["flavorName"]);
		
		// select chosen flavor pulldown
		
		//f.size.options[  upcData["sizeIndex"]  ].selected = true;
		f.size.options.selectedIndex = -1;
		f.size.options.selectedIndex = upcData["sizeIndex"];
		
		
		// turn off upc error message
		document.getElementById('upcWarning').style.display="none";
		}
	else 
		{
		// user has input unknown UPC code;
		// clear pulldowns
		// give error "upc code unknown" 
		// reset product to first option
		f.product.options[0].selected = true;
		// remove all options from flavor and size
		removeAllChildNodes(document.getElementById('flavor'),true);
		removeAllChildNodes(document.getElementById('size'),true);
		// turn on upc error message (unless upc code is blank)
		displayStyle = (f.upccode.value!="") ? "inline" : "none";
		document.getElementById('upcWarning').style.display=displayStyle;
		
		}
	}


// -------------------------------------------------


function getUPCData(upc)
	{
	for(var product in products) 
		{
		// get product from productdata.js
		var thisProduct = products[product];
		for(var flavor in flavors[thisProduct])
			{
			// get flavor of product
			var thisFlavor = flavors[thisProduct][flavor];
			for(var size in sizes[thisFlavor]) 
				{
				// get size and upc code from flavor
				var sizeAndUPC = sizes[thisFlavor][size];
				thisSize = sizeAndUPC.split("|")[0];
				thisUPC = sizeAndUPC.split("|")[1];
				// if the user input valid upc code
				// gather up current product, flavor and size and the index of each
				// and send it back; else send back "false"
				if (thisUPC==upc)
					{
					var thisData = new Array();
					thisData["productName"] = thisProduct;
					thisData["productIndex"] = product*1+1;

					thisData["flavorName"] = thisFlavor;
					thisData["flavorIndex"] = flavor*1+1;
					
					thisData["sizeName"] = thisSize;
					thisData["sizeIndex"] = size*1+1;
					
					return thisData;
					}
				}
			}
		}
	return false;
	}
