retailerCodeMatcher = "";
retailerShortName = "";
retailerCodeHint = "";
retailerAutoLookup = 0;

function selectRetailer()
{
	document.getElementById("product_hint").innerHTML = "Getting store details.....";
	xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			xml = getResponseXMLDocumentElement(xmlHttp);
			retailerCodeMatcher = xml.getAttribute("retailer_code_matcher");
			retailerShortName = xml.getAttribute("retailer_short_name");
			retailerCodeHint = xml.getAttribute("retailer_code_hint");
			retailerAutoLookup = xml.getAttribute("retailer_auto_lookup");
			document.getElementById("product_hint").innerHTML = retailerCodeHint;
			document.getElementById("go_to_reviews").style.display = "none";
			if (retailerShortName)
			{
				document.getElementById("product_code").focus();
			}
			document.getElementById("product_code").value = "";				
		}
	}
	selectedRetailer = encodeURIComponent(document.getElementById("retailer_id").value);
	xmlHttp.open("GET", "/ops/retailer_xml.php?retailer_id=" + selectedRetailer, true);
	xmlHttp.send(null);
}

var lastProductCode = "";

function determineProductName()
{
	disableButton("look_up", true);
	var productCode = lastProductCode;
	document.getElementById("product_hint").innerHTML = "Fetching product name.....";
	xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			enableButton("look_up");
			xml = getResponseXMLDocumentElement(xmlHttp);
			source = xml.getAttribute("source");
			prefix = "";
			if (source == "web" || source == "session")
			{
				prefix = "<b>NEW:</b>";
			}
			if (productName = xml.getAttribute("product_name"))
			{
				document.getElementById("product_hint").innerHTML = prefix + " " + productName;
				document.getElementById("go_to_reviews").style.display = "";
			}
			else
			{
				retailer = document.getElementById("retailer_id");
				retailerName = retailer.options[retailer.selectedIndex].text;
				document.getElementById("product_hint").innerHTML = "Unknown product at " + retailerName;
			}
			document.getElementById("look_up").style.display = "none";
		}
	}
	productCode = encodeURIComponent(productCode);
	xmlHttp.open("GET", "/ops/product_xml.php?retailer_id=" + selectedRetailer + "&product_code=" + productCode, true);
	xmlHttp.send(null);
	return false;
}

function checkProductCode(event)
{
	if (event.keyCode == 13)
	{
		if (document.getElementById("go_to_reviews").style.display == "")
		{
			review();
		}
		if (document.getElementById("look_up").style.display == "")
		{
			determineProductName();
		}
		return;
	}
	productCode = document.getElementById("product_code").value;
	productCode = productCode.replace(/[^\w]/g, "");
	if (productCode == lastProductCode) return;
	lastProductCode = productCode;
	document.getElementById("product_hint").innerHTML = retailerCodeHint;
	document.getElementById("go_to_reviews").style.display = "none";
	if (retailerCodeMatcher)
	{
		regExp = new RegExp(retailerCodeMatcher);
		if (!retailerAutoLookup)
		{
			document.getElementById("look_up").style.display = productCode.match(regExp) ? "" : "none";
		}
		else
		{
			if (productCode.match(regExp))
			{
				determineProductName();
			}
		}	
	}
}

function review()
{
	disableButton("go_to_reviews", true);
	retailerID = encodeURIComponent(document.getElementById("retailer_id").value);
	productCode = document.getElementById("product_code").value;
	productCode = productCode.replace(/[^\w]/g, "");
	productCode = encodeURIComponent(productCode);
	window.location = "/reviews/" + retailerShortName + "/" + productCode + "/";
	return false;
}
