function doPurchase(sFormId)
{
	http.open("POST", "./custom/cartInterface.php?action=doPurchase", true);
	http.onreadystatechange = viewProcessor;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	sFormData = null;
	if(sFormId != null)
		sFormData = encodeForm(sFormId);
	http.send(sFormData);
}

function continuePurchase(sFormId)
{
	document.sProcessor = "./custom/cartInterface.php";
	http.open("POST", "./custom/cartInterface.php?action=continuePurchase", true);
	http.onreadystatechange = viewProcessor;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	sFormData = null;
	if(sFormId != null)
		sFormData = encodeForm(sFormId);
	http.send(sFormData);
}

function prevPurchaseStep()
{
	http.open("POST", "./custom/cartInterface.php?action=prevPurchase", true);
	http.onreadystatechange = viewProcessor;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function processPayment()
{
	document.updateCart = 1;

	changeElement('checkoutBody', "Please wait while we process your payment. This could take a few moments to complete.");
	http.open("POST", "./custom/cartInterface.php?action=processPayment", true);
	http.onreadystatechange = viewProcessor;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http.send(null);
}

function viewProcessor()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseText != null)
		{
			eElement = document.getElementById("checkoutBody");
			if(eElement != null)
				eElement.innerHTML = http.responseText;
		}

		if(document.updateCart)
		{
			document.updateCart = 0;
			window.setTimeout('getCartView()',100);
		}
	}
}

function ChangeCart(nId, nAmount)
{
	if(nAmount.substr(0,1) == "+")
		sAction = "add";
	else if(nAmount.substr(0,1) == "-")
		sAction = "remove";
	else
		sAction = "set";
	if(Math.round(nAmount) != nAmount)
	{
		alert("Please note: You must enter whole numbers. " + nAmount + " changed to " + Math.ceil(nAmount));
		nAmount = Math.ceil(nAmount);
	}

	// create AJAX request and send
	http.open("POST", "./custom/cartInterface.php?action=" + sAction + "&item=" + nId + "&amount=" + nAmount, true);
	http.onreadystatechange = updateCart;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function updateCart()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseXML != null)
		{
			xmlData = http.responseXML.documentElement;
			eNode = xmlData.firstChild;

			sItem = xmlData.getElementsByTagName('item')[0] != null?xmlData.getElementsByTagName('item')[0].getAttribute('value'):"";
			nQty = xmlData.getElementsByTagName('qty')[0] != null?xmlData.getElementsByTagName('qty')[0].getAttribute('value'):"";
			fPrice = xmlData.getElementsByTagName('price')[0] != null?xmlData.getElementsByTagName('price')[0].getAttribute('value'):0;
			bShowCart = xmlData.getElementsByTagName('checkout')[0] != null?xmlData.getElementsByTagName('checkout')[0].getAttribute('value')!=0:0;

			eItem = document.getElementById("Qty" + sItem);
			if(eItem != null)
				eItem.value = nQty;

			eItem = document.getElementById("subTotal" + sItem);
			if(eItem != null)
			{
				fTotal = nQty * fPrice;
				eItem.innerHTML = fTotal < 1?(fTotal*100)+"c":"$"+fTotal.toFixed(2);
			}

			if(bShowCart)
				showElement('tab_checkout');
			else
				hideElement('tab_checkout');

			//alert(document.body.id);
			window.setTimeout('getCartView()',10);

		}
	}
}

function ToggleFavorite(nId)
{
	http.open("POST", "./custom/cartInterface.php?action=fav&item=" + nId, true);
	http.onreadystatechange = updateFavorite;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function updateFavorite()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseXML != null)
		{
			xmlData = http.responseXML.documentElement;
			eNode = xmlData.firstChild;

			sItem = xmlData.getElementsByTagName('item')[0] != null?xmlData.getElementsByTagName('item')[0].getAttribute('value'):"";
			bFav = xmlData.getElementsByTagName('fav')[0] != null?xmlData.getElementsByTagName('fav')[0].getAttribute('value'):"";

			eItem = document.getElementById("fav" + sItem);
			if(eItem != null)
				eItem.innerHTML = bFav==1?"Remove Favorite":"Add to favorites";

			setTimeout("getFavView()",100);

		}
	}
}

function getCartView()
{
	http.open("POST", "./content/listProducts.php", true);
	http.onreadystatechange = displayCartView;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);

}

function displayCartView()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseText != null)
		{
			eCart = document.getElementById("cartView");
			if(eCart != null)
				eCart.innerHTML = http.responseText;
		}
	}
}

function toggleInstructions()
{
	bHide = getElementValue('Will_you_be_home') == "Yes";

	if(bHide)
		hideElement("Instructions");
	else
		showElement("Instructions");

}

function toggleNotes()
{
	bHide = getElementValue('Hire_Cooler_Box') == "No";

	if(bHide)
		hideElement("Notes");
	else
		showElement("Notes");

}

function toggleCCDetails()
{
	bHide = getElementValue('Payment_Method') == "COD";

	if(bHide)
		hideElement("CCDetails");
	else
		showElement("CCDetails");
}
