/************************** AJAX FUNCTIONS ********************************/
var xmlhttp;
 var urlBase = "http://"+document.domain+"/scripts/cartAction.php"	//Modify this if the cartAction.php file is moved 

function addFullCartItem(newItem, qty, price){
        xmlhttp=GetXmlHttpObject();
        httpFailCheck(xmlhttp);
        var url=urlBase;
        url=url+"?item="+newItem+"&qty="+qty+"&price="+price+"&act=19"+"&sid="+Math.random();
        xmlhttp.onreadystatechange=autoUpdate;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
}


function addItem(newItem, qty, price) 
{
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?item="+newItem+"&qty="+qty+"&price="+price+"&act=1"+"&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
  return true;
}

function remItem(oldItem, qty, price) {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?item="+oldItem+"&qty="+qty+"&price="+price+"&act=2"+"&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function upgradeItem(oldItem, qty, price, newItem, newQty, newPrice) {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?item="+oldItem+"&qty="+qty+"&price="+price+"&newItem="+newItem+"&newQty="+newQty+"&newPrice="+newPrice+"&act=5"+"&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function emptyCart() {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?act=3"+"&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function applyPromo(promoID) {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?promoID="+promoID+"&act=6&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function setShipping(method) {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?method="+method+"&act=7&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}

function setCountry(country) {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?country="+country+"&act=9&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}

function loginUser(user,pwd) {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?act=10&user="+user+"&pwd="+pwd+"&sid="+Math.random();
	xmlhttp.onreadystatechange=autoUpdate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);		
}

function printMC() {
	xmlhttp2=GetXmlHttpObject();
	httpFailCheck(xmlhttp2);
	var url=urlBase;
	url=url+"?act=18&sid="+Math.random();
	xmlhttp2.onreadystatechange=updateMC;
	xmlhttp2.open("GET",url,true);
	xmlhttp2.send(null);		
}

function updateMC() {
	if (xmlhttp2.readyState==4)  {
		answer=xmlhttp2.responseText;
		document.getElementById("MCart").innerHTML = answer;
	}
}

function printLB() {
	xmlhttp=GetXmlHttpObject();
	httpFailCheck(xmlhttp);
	var url=urlBase;
	url=url+"?act=17&sid="+Math.random();
	xmlhttp.onreadystatechange=UpdateLB;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);		
}

function UpdateLB() {
	if (xmlhttp.readyState==4)  {
		answer=xmlhttp.responseText;
		document.getElementById("LBuddie").innerHTML = answer;
	}
}

/********** THE AUTOUPDATE FUNCTION ************/

function autoUpdate() {
	if (xmlhttp.readyState==4)  {
		var respHtml = new Array();
		var elements = new Array("miniCart","bigCart","loginBuddy");
		answer=xmlhttp.responseText;
		respHtml = answer.split("###");
		for (x=0;x<elements.length;x++) 
		{
			//alert(respHtml[x]);
			if (document.getElementById(elements[x]) != null) 
			{
				document.getElementById(elements[x]).innerHTML=respHtml[x];
			}//end if
		}// end for
		/***** only for cart.php page *****/
		if(respHtml[3] != undefined)	
		{
			var displayButtons = '';
			if(parseInt(respHtml[3]) == 1)
				displayButtons = 'none';
			var imgs = getByName('cont_btn');

			for(var i=0; i<imgs.length; i++)
			{
				imgs[i].style.display = displayButtons;	
			}
		}
	}
}

function httpFailCheck(xmlhttp) {
	if (xmlhttp==null)  {
	  //do something about no HTTP request support OTHER than a freaking alert!!!!! 	**************** TO IMPLEMENT **********************
	  alert("Your browser does not support HTTP Request! Please update to the latest Internet Explorer, Mozilla Firefox, Opera, Safari or other browser of your choice which supports HTTP Requests");
	}
}

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;
}

