function doPurchase()
{
	document.sProcessor = "./custom/cartInterface.php";
	http.open("POST", "./custom/cartInterface.php?action=doPurchase", true);
	http.onreadystatechange = viewProcessor;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http.send(null);
}

function continuePurchase(sFormId)
{
	if(document.checkingVoucher)
	{
		setTimeout("continuePurchase('"+sFormId+"')",100);
		return;
	}

	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()
{
	if(document.checkingVoucher)
	{
		setTimeout("prevPurchaseStep()",100);
		return;
	}


	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()
{
	changeElement('siteProcessor', "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 ChangeCart(nId, nAmount)
{
	if(nAmount.substr(0,1) == "+")
		sAction = "add";
	else if(nAmount.substr(0,1) == "-")
		sAction = "remove";
	else
		sAction = "set";

	// 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 ToggleCart(nId)
{
	sAction = "cart";
	// create AJAX request and send
	http.open("POST", "./custom/cartInterface.php?action=" + sAction + "&item=" + nId, true);
	http.onreadystatechange = updateCart;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

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("dfav" + sItem);
			if(eItem != null)
				eItem.innerHTML = bFav==1?"Remove Favorite":"Add to favorites";

			eItem = document.getElementById("fav" + sItem);
			if(eItem != null)
				eItem.innerHTML = bFav==1?"Remove Favorite":"Add to favorites";

			// setTimeout("getFavView()",100);

		}
	}
}

function ToggleMail(nId)
{
	http.open("POST", "./custom/cartInterface.php?action=mail&item=" + nId, true);
	http.onreadystatechange = updateMail;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function updateMail()
{
	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'):"";
			bMail = xmlData.getElementsByTagName('mail')[0] != null?xmlData.getElementsByTagName('mail')[0].getAttribute('value'):"";

			eItem = document.getElementById("dmail" + sItem);
			if(eItem != null)
				eItem.src = bMail==1?"images/symbols/mail.jpg":"images/symbols/nomail.jpg";

			eItem = document.getElementById("mail" + sItem);
			if(eItem != null)
				eItem.src = bMail==1?"images/symbols/mail.jpg":"images/symbols/nomail.jpg";

			/*
			if(bMail==1)
				setTimeout("getPage('viewFriendsList')",100);
			*/

		}
	}
}

function sendToFriend()
{
	http.open("POST", "./content/viewFriendsList.php", true);
	http.onreadystatechange = viewPage;
	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'):"";

			eItem = document.getElementById("ditem" + sItem);

			if(eItem != null)
				eItem.value = nQty;

			eItem = document.getElementById("item" + sItem);
			if(eItem != null)
				eItem.innerHTML = nQty;

			eItem = document.getElementById("dcart" + sItem);
			if(eItem != null)
				eItem.innerHTML = nQty>0?"Remove from Cart":"Add to Cart";

			eItem = document.getElementById("cart" + sItem);
			if(eItem != null)
				eItem.innerHTML = nQty>0?"Remove from Cart":"Add to Cart";

			eTotal = document.getElementById("cartSummary");

			if(eTotal != null)
			{
				setTimeout("updatePricing()",1);
			}
		}
	}
}

function updatePricing()
{
	http.open("POST", "./custom/cartInterface.php?action=retotal", true);
	http.onreadystatechange = updateCartTotal;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function updateCartTotal()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseXML != null)
		{
			xmlData = http.responseXML.documentElement;
			eNode = xmlData.firstChild;

			fTotal = xmlData.getElementsByTagName('total')[0] != null?xmlData.getElementsByTagName('total')[0].getAttribute('value'):"";
			nTotal = xmlData.getElementsByTagName('items')[0] != null?xmlData.getElementsByTagName('items')[0].getAttribute('value'):"";

			eTotal = document.getElementById("cartCount");
			if(eTotal != null)
				eTotal.innerHTML = nTotal;

			eTotal = document.getElementById("cartTotal");
			if(eTotal != null)
				eTotal.innerHTML = "$ " + fTotal + ".00";


		}
	}
}

function updateFreight()
{
	sSubTotal = getElementValue('Sub_Total');
	sFreight = getElementValue('Freight');

	fSubTotal = LTrim(sSubTotal.replace(/\$/,""));
	fFreight =  LTrim(sFreight.replace(/\$/,""));

	sTotal = "$ " + (Number(fSubTotal) + parseInt(fFreight));
	setElementValue('Total_Due', sTotal);
	setElementValue('Amount_To_Redeem', sTotal);
}
