var xmlhttp;

function CheckShipping()
{

	if(document.getElementById("pemail").value == "") { document.getElementById("pemail").focus(); SendAlert(); return false; }
	if(document.getElementById("pname").value == "") { document.getElementById("pname").focus(); SendAlert(); return false; }
	if(document.getElementById("address").value == "") { document.getElementById("address").focus(); SendAlert(); return false; }
	if(document.getElementById("pcity").value == "") { document.getElementById("pcity").focus(); SendAlert(); return false; }
	if(document.getElementById("state").value == "") { document.getElementById("state").focus(); SendAlert(); return false; }
	if(document.getElementById("postcode").value == "") { document.getElementById("postcode").focus(); SendAlert(); return false; }
	if(document.getElementById("country").value == "") { document.getElementById("country").focus(); SendAlert(); return false; }

	return true;
}

function SendAlert()
{
	alert("Required field not supplied!");
	return;
}

function DisplayStore(target)
{
	var url = "id=0&type=ShowStore";
	DataHandler(url,target,0);
}

function addCartItem(id,itemid,price,type,target)
{
	var url = "id="+id+"&itemid="+itemid+"&price="+price+"&type="+type;
	DataHandler(url,target,0);
}

function getContent(str,target,type)
{
	var url="id="+str+"&type="+type;
	DataHandler(url,target,0);
}

function checkout(target,type)
{
	var url="id=0&type="+type;
	DataHandler(url,target,0);
}

function RemlCartItem(id,target,type)
{
	var url="id="+id+"&type="+type;
	DataHandler(url,target,1);
}

function UpdateBalace(target,type)
{
	var url="id=0&type="+type;
	DataHandler(url,target,0);
}

function PlaceStoreOrder(email,name,address,city,state,postcode,country,target,type)
{
	var url="id=0&email="+email+"&name="+name+"&address="+address+"&city="+city+"&state="+state+"&postcode="+postcode+"&country="+country+"&type="+type;
	DataHandler(url,target,0);
}

function DataHandler(str,target,act)
{

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	xmlhttp.action= act;
	xmlhttp.id = target;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST","shopfront.php",true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(str);
}

function stateChanged()
{
	if (xmlhttp.readyState==4)
	{
		var id = this.id;
		var act = this.action;

		document.getElementById(id).innerHTML=xmlhttp.responseText;

		switch (act)
		{
			case 1: UpdateBalace("cart","updateCartBalance"); break;
		}
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
}
