var sizes = new Array();
var upcs = new Array();

jQuery(function()
{
	jQuery("#productLine").change(productLineChanged);
	
	jQuery("#productType").change(productTypeChanged);
	
	jQuery("form#frmFindIt input").focus(function()
	{
		jQuery(this).val(jQuery(this).val() == "Zip code" ? "" : jQuery(this).val());
	});
	
	jQuery("form#frmFindIt").submit(submitForm);
	
	jQuery("form#frmFindIt img").click(submitForm);
}
);

function productLineChanged()
{
	jQuery.post("/GetXml.do?productLineName=" + jQuery("#productLine option:selected").val(), "", function(data)
	{
		var option = "<option value=''>Select a Form<\/option>";
		
		jQuery("#productType").empty();
		
		jQuery("#productScent").empty();
		
		if (jQuery("ProductType", data).length > 1)
		{
			jQuery(option).appendTo("#productType");
		}
		
		if (jQuery("#productLine option:selected").val() != "")
		{
			jQuery("ProductType", data).each(function()
			{
				option = "<option value='" + jQuery(this).children("product:first-child").attr("name") + "'>" + jQuery(this).attr("name") + "<\/option>"; //new Option(jQuery(this).attr("name"), jQuery(this).children("product:first-child").attr("name"));
				
				jQuery(option).appendTo("#productType");
			});
		}
		
		if (jQuery("ProductType", data).length == 1)
		{
			productTypeChanged();
		}
	}, "xml");
}

function productTypeChanged()
{
	sizes = new Array();
	upcs = new Array();
	
	jQuery.post("/GetXml.do?productLineName=" + jQuery("#productLine option:selected").val(), "", function(data)
	{
		var option = "<option value=''>Select a Scent<\/option>";
		
		jQuery("#productScent").empty();
		
		jQuery(option).appendTo("#productScent");
		
		if (jQuery("#productType option:selected").val() != "")
		{
			jQuery("ProductType[name='" + jQuery("#productType option:selected").text() + "'] product", data).each(function()
			{
				if (!upcs[jQuery(this).attr("scent")])
				{
					option = "<option value='" + jQuery(this).attr("imagePath") + "'>" + jQuery(this).attr("scent") + "<\/option>";
					
					jQuery(option).appendTo("#productScent");
					
					upcs[jQuery(this).attr("scent")] = "";
					
					sizes[jQuery(this).attr("scent")] = "";
				}
				
				upcs[jQuery(this).attr("scent")] = upcs[jQuery(this).attr("scent")] + jQuery(this).attr("upc") + "|";
				
				sizes[jQuery(this).attr("scent")] = sizes[jQuery(this).attr("scent")] + " | " + jQuery(this).attr("size");
			});
			
			if (jQuery("#productScent option").length == 2) // 2 = one option + "Select a Scent"
			{
				jQuery("#productScent option:first").remove();
			}
		}
	}, "xml");
}

function submitForm()
{
	if (jQuery("#productLine option:selected").val() == "" || jQuery("#productType option:selected").val() == "" || jQuery("#productScent option:selected").val() == "")
	{
		var msg = document.createElement("div");
		
		msg.className = "errorMsg";
		
		msg.innerHTML = "Please complete the form.";
		
		jQuery(msg).insertBefore("form#frmFindIt h2");
		
		jQuery("form#frmFindIt div.errorMsg").fadeIn(1000);
		
		setTimeout(removeErrorMsg, 2000);
		
		return false;
	}
	
	jQuery("div.productContainer").addClass("active");
	
	swfobject.embedSWF(jQuery("#productScent option:selected").val(), "flash", "40%", "95%", "8.0.0", "", {}, {wmode: "transparent"}, {});
	
	jQuery("div.productContainer h4").html(jQuery("#productType option:selected").val());
	
	jQuery("div.productContainer h5").html(jQuery("#productType option:selected").text() + " | " + jQuery("#productScent option:selected").text());
	
	jQuery("div.productContainer h6 span").html(sizes[jQuery("#productScent option:selected").text()].substring(3));
	
	if (jQuery("#cii_sZip").val() != "Zip code" && jQuery("#cii_sZip").val() != "")
	{
		jQuery("div.buyFind").removeClass("noRetailer");
		
		jQuery("div.buyFind").addClass("withRetailer");
		
		document.getElementsByTagName("iframe")[1].contentWindow.location.replace("/wheretobuy/findResultsFrame.jsp?cii_nSCID=15&cii_nCTID=15&cii_sSKU=&cii_nIID=-1&cii_nRGID=1547&cii_nVID=&cii_nLocID=&cii_sCountry=US&cii_nRadius=15&cii_sSort=&cii_sSortOrder=&cii_nPanHorizontal=0&cii_nPanVertical=0&cii_nSize=0&cii_nZoomFactor=2&cii_sCT=&cii_nPGID=&cii_nStoresByDealer=0&cii_sZip=" + jQuery("#cii_sZip").val());
	}
	else
	{
		jQuery("div.buyFind").removeClass("withRetailer");
		
		jQuery("div.buyFind").addClass("noRetailer");
	}
	
	document.getElementsByTagName("iframe")[0].contentWindow.location.replace("/wheretobuy/buyResultsFrame.jsp?upcs=" + upcs[jQuery("#productScent option:selected").text()]);
	
	return false;
}

function removeErrorMsg()
{
	jQuery("form#frmFindIt div.errorMsg").stop().fadeOut(2000, function()
	{
		jQuery("form#frmFindIt div.errorMsg").remove();
	});
}