function showTable()
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("table").innerHTML=xmlhttp.responseText;
			showCount();
		}
	}
	
	xmlhttp.open("GET","list.php");
	xmlhttp.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
	xmlhttp.send();
}

function addItem(item)
{
	if(item == "-1")
	{
		alert("Please select an option from the drop down menu first");
	}
	else
	{
		if (window.XMLHttpRequest)
		{
			xmlhttp=new XMLHttpRequest();
		}
		else
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				showCount();
			}
		}
		xmlhttp.open("POST","add.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send("id="+item);
	}
}

function removeItem(item)
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			showCart();
		}
	}
	xmlhttp.open("POST","remove.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("id="+item);
}

function showCount()
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("menu").innerHTML = xmlhttp.responseText;
		}
	}
	
	xmlhttp.open("GET","count.php");
	xmlhttp.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
	xmlhttp.send();
}

function showCart()
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("contentcart").innerHTML = xmlhttp.responseText;
			showCount();
		}
	}
	
	xmlhttp.open("GET","printcart.php");
	xmlhttp.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
	xmlhttp.send();
}

function setTotal()
{
	document.orderform.totalAmount.value = document.cartform.total.value;
}

function showCartCheckout()
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("contentcart").innerHTML = xmlhttp.responseText;
			showCount();
			setTotal();
			document.orderform.shipAddress.value = document.cartform.addy.value;
			document.orderform.shipZip.value = document.cartform.zip.value;
		}
	}
	
	
	xmlhttp.open("GET","printcheckout.php");
	xmlhttp.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
	xmlhttp.send();
}

function updateItem(item, qty)
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			showCart();
		}
	}
	xmlhttp.open("POST","update.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(item+"="+qty);
}

function updateShipping(addy, zip, method)
{
	if(addy == "" || zip == "")
	{
		alert("Please enter both Address and Zip code to calculate shipping accurately.\nThank you");
		return;
	}
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			showCart();
		}
	}
	xmlhttp.open("POST","shipping.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("addy="+addy+"&zip="+zip+"&method="+method);
}

function updateShippingCheckout(zip, method)
{
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			showCartCheckout();
		}
	}
	xmlhttp.open("POST","shipping.php",true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send("zip="+zip+"&method="+method);
}

function checkShipping()
{
	if (typeof document.cartform == "undefined" || typeof document.cartform.shipcost == "undefined" || typeof document.cartform.shipcost.value == "undefined" || document.cartform.shipcost.value == 0)
	{
		alert("Please calculate shipping. If you continue having problems please call 1 (908) 276-8484");
		document.cartform.addy.focus();
		return false;
	}
	window.location.href = "checkout.html";
}
