var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;
		
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp = false;
		}
	}
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlHttp = false;
		}
	}
		
	if(!xmlHttp)
	{
		return false;
	}
	else
	{
		return xmlHttp;
	}
}

function updatePricing()
{
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
		var postString = "go=1";
		
		xmlHttp.open("POST", "/order/tools/pricing_query.php", true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		postString += "&key=I2hc5F9G";
		postString += "&shipping_state_id=" + document.getElementById("shipping_state_id").value;
		postString += "&wine_club_member=" + document.getElementById("wine_club_member").checked;
		//postString += "&discount_code=" + document.getElementById("discount_code").value;
		
		var inputs = document.getElementsByTagName("input");
		
		for(var i = 0; i < inputs.length; i++)
		{
			if(inputs[i].id.indexOf("product_") == 0) postString += "&" + inputs[i].id + "=" + inputs[i].value;
		}

		xmlHttp.send(postString);
	}
	else
	{
		setTimeout("updatePricing()", 1000);
	}
}

function handleServerResponse()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			
			//get the product pricing info
			productArray = xmlDocumentElement.getElementsByTagName("product");
			
			//update the product pricing info
			for(var i = 0; i < productArray.length; i++)
			{
				productId = productArray[i].getElementsByTagName("product_id")[0].firstChild.nodeValue;
				document.getElementById("wo_product_" + productId + "_price").innerHTML = "$" + productArray[i].getElementsByTagName("price")[0].firstChild.nodeValue;
				document.getElementById("wo_product_" + productId + "_club_member_price").innerHTML = "$" + productArray[i].getElementsByTagName("club_member_price")[0].firstChild.nodeValue;
				if(document.getElementById("product_" + productId) != null)
				{
					max_quantity = productArray[i].getElementsByTagName("max_quantity")[0].firstChild.nodeValue;
					if(parseInt(document.getElementById("product_" + productId).value) > max_quantity)
					{
						document.getElementById("product_" + productId).value = max_quantity;
					}
					
				}
				
			}
			
			//update the subtotal
			subtotal = xmlDocumentElement.getElementsByTagName("subtotal");
			document.getElementById("wo_subtotal").innerHTML = "$" + subtotal[0].firstChild.nodeValue;
			
			//update the discount
			//discount               = xmlDocumentElement.getElementsByTagName("discount");
			//discount_code_discount = xmlDocumentElement.getElementsByTagName("discount_code_discount");
			total_discount = xmlDocumentElement.getElementsByTagName("total_discount");
			totalDiscount = total_discount[0].firstChild.nodeValue;
			document.getElementById("wo_discount").innerHTML = "($" + totalDiscount + ")";
			
			/*//update the status field
			discount_code_valid = xmlDocumentElement.getElementsByTagName("discount_code_valid");
			discount_code_valid = discount_code_valid[0].firstChild.nodeValue;
			
			if(document.getElementById('discount_code').value != "")
			{
				if(discount_code_valid != "1")
				{
					document.getElementById('discount_code_status').innerHTML = "<strong><span style='color:#FF0000;'>(Not Valid)</span></strong>&nbsp;&nbsp;";
				} else {
					document.getElementById('discount_code_status').innerHTML = "<strong><span style='color:#5F9E3C;'>(Valid)</span></strong>&nbsp;&nbsp;";
				}
			} else {
				document.getElementById('discount_code_status').innerHTML = "";
			}*/

			if(totalDiscount > 0)
			{
				document.getElementById("wo_discount_row").style.display = "";
			} else {
				document.getElementById("wo_discount_row").style.display = "none";
			}
			
			//update the tax
			tax = xmlDocumentElement.getElementsByTagName("tax");
			document.getElementById("wo_tax").innerHTML = "$" + tax[0].firstChild.nodeValue;
			
			//update the shipping
			shipping = xmlDocumentElement.getElementsByTagName("shipping");
			document.getElementById("wo_shipping").innerHTML = "$" + shipping[0].firstChild.nodeValue;
			
			//update the grand total
			grand_total = xmlDocumentElement.getElementsByTagName("grand_total");
			document.getElementById("wo_grand_total").innerHTML = "$" + grand_total[0].firstChild.nodeValue;
		}
		else
		{
			return false;
		}
	}
}