//object detection to return the correct object depending upon broswer type.
function getNewHttpObject() {
    var objType = false;
    try {
        objType = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            objType = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            objType = new XMLHttpRequest();
        }
    }
    return objType;
}

//Function used to update page content with new xhtml fragments by using a javascript object, the dom, and http.
function getAXAH(url,elementContainer){
	//document.getElementById(elementContainer).innerHTML = '<img src=\"/loading.gif\" alt=\"Laddar\">';
	var theHttpRequest = getNewHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);

		function processAXAH(elementContainer){
		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {
				   document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
			   } else {
				   document.getElementById(elementContainer).innerHTML="error";
			   }
		   }
		}

}
function getAXAH5(url,elementContainer){
	//document.getElementById(elementContainer).innerHTML = '<img src=\"/loading.gif\" alt=\"Laddar\">';
	var theHttpRequest = getNewHttpObject();
	theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);

		function processAXAH(elementContainer){
		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {
				   document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
			   } else {
				   document.getElementById(elementContainer).innerHTML="error";
			   }
		   }
		}

}

function addItem2(id)
{
	var theHttpRequest = getNewHttpObject();
	
	//document.getElementById("status" + id).innerHTML = "<img src='/loading.gif' alt='loading' />";
	
	var quantity = document.forms['items'].elements['item' + id].value;
	var url = "/system/wsEngine.php?s=add&product=" + id + "&quantity=" + quantity;
	theHttpRequest.onreadystatechange = function() {processAXAH("item" + id, quantity)};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);
		
		function processAXAH(elementContainer, quantity){
		   if (theHttpRequest.readyState == 4) {
		   
		   	   //For no apparent reason, this check returns undefined in Safari on mac OS/X
			   //if (theHttpRequest.status == 200) {
			 
				   if(quantity > 0)
				   {
				     //document.getElementById("status" + id).innerHTML = "<img src='/icon_various3.gif' alt='icon' />";
				  	 giveFocus(id);
					 updatePrice2();
				   }else{
				     //document.getElementById("status" + id).innerHTML = "<img src='/icon_various1.gif' alt='icon' />";
				   	 looseFocus(id);
					 updatePrice2();
				   }
				   
			   //} else {
				   //alert("Something went wrong!");
			  //}
			  
		   }
		}

}
function addItem3(id)
{
	var theHttpRequest = getNewHttpObject();
	
	//document.getElementById("status" + id).innerHTML = "<img src='/loading.gif' alt='loading' />";
	
	var quantity = document.forms['items'].elements['item' + id].value;
	var url = "/system/wsEngine.php?s=add&product=" + id + "&quantity=" + quantity;
	theHttpRequest.onreadystatechange = function() {processAXAH("item" + id, quantity)};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);
		
		function processAXAH(elementContainer, quantity){
		   if (theHttpRequest.readyState == 4) {
		   
		   	   //For no apparent reason, this check returns undefined in Safari on mac OS/X
			   //if (theHttpRequest.status == 200) {
			 
				   if(quantity > 0)
				   {
				     //document.getElementById("status" + id).innerHTML = "<img src='/icon_various3.gif' alt='icon' />";
				  	 giveFocus2(id);
					 updatePrice3();
				   }else{
				     //document.getElementById("status" + id).innerHTML = "<img src='/icon_various1.gif' alt='icon' />";
				   	 looseFocus2(id);
					 updatePrice3();
				   }
				   
			   //} else {
				   //alert("Something went wrong!");
			  //}
			  
		   }
		}

}

function addItem(id)
{
	var theHttpRequest = getNewHttpObject();
	
	document.getElementById("status" + id).innerHTML = "<img src='/loading.gif' alt='loading' />";
	
	var quantity = document.forms['items'].elements['item' + id].value;
	var url = "/system/wsEngine.php?s=add&product=" + id + "&quantity=" + quantity;	
	
	theHttpRequest.onreadystatechange = function() {processAXAH("item" + id, quantity)};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);
		
		function processAXAH(elementContainer, quantity){
		   if (theHttpRequest.readyState == 4) {
		   
		   	   //For no apparent reason, this check returns undefined in Safari on mac OS/X
			   //if (theHttpRequest.status == 200) {
			 
				   if(quantity > 0)
				   {
				     document.getElementById("status" + id).innerHTML = "<img src='/icon_various3.gif' alt='icon' />";
				  	 giveFocus(id);
					 updatePrice();
				   }else{
				     document.getElementById("status" + id).innerHTML = "<img src='/icon_various1.gif' alt='icon' />";
				   	 looseFocus(id);
					 updatePrice();
				   }
				   
			   //} else {
				   //alert("Something went wrong!");
			  //}
			  
		   }
		}

}

function changeItem(id)
{
	var theHttpRequest = getNewHttpObject();
	
	document.getElementById("status" + id).innerHTML = "<img src='/loading.gif' alt='loading' />";
	
	var quantity = document.forms['items'].elements['item' + id].value;
	var url = "/system/wsEngine.php?s=add&product=" + id + "&quantity=" + quantity;	
	
	theHttpRequest.onreadystatechange = function() {processAXAH("item" + id, quantity)};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);
		
		function processAXAH(elementContainer, quantity){
		   if (theHttpRequest.readyState == 4) {
		   
		   	   //For no apparent reason, this check returns undefined in Safari on mac OS/X
			   //if (theHttpRequest.status == 200) {
			 
				   if(quantity > 0)
				   {
				     document.getElementById("status" + id).innerHTML = "<img src='/icon_various3.gif' alt='icon' />";
				  	 //giveFocus(id);
					 getAXAH("/system/getCheckoutSum.php","checkoutSum");
				   }else{
				     document.getElementById("status" + id).innerHTML = "<img src='/icon_various1.gif' alt='icon' />";
					 getAXAH("/system/getCheckoutSum.php","checkoutSum");
					 removeElement(id);
				   }
				   
			   //} else {
				   //alert("Something went wrong!");
			  //}
			  
		   }
		}

}

function purgeBasket()
{
	var theHttpRequest = getNewHttpObject();
	
	var url = "/system/wsEngine.php?s=purgeBasket";	
	theHttpRequest.onreadystatechange = function() {processAXAH();};
	theHttpRequest.open("GET", url);
	theHttpRequest.send(false);
		
		function processAXAH(){
		   if (theHttpRequest.readyState == 4) {
			   if (theHttpRequest.status == 200) {
					alert("Kundvagnen tömdes!");
					window.location="/";
			   } else {
				    alert("Kundvagnen kunde inte tömmas!");
					//Skicka vidare till frågor och svar: webbläsaren
					//window.location="/kassan/";
			   }
		   }
		}

}


function removeElement(id) {
  var d = document.getElementById("items");
  var olddiv = document.getElementById("item" + id);
  d.removeChild(olddiv);
}


function giveFocus2(id)
{
	document.getElementById("itemRow" + id).attributes.getNamedItem("class").value = "itemRowActive";
}

function looseFocus2(id)
{
	document.getElementById("itemRow" + id).attributes.getNamedItem("class").value = "itemRow";
}

function giveFocus(id)
{
	document.getElementById("item" + id).attributes.getNamedItem("class").value = "itemActive";
}

function looseFocus(id)
{
	document.getElementById("item" + id).attributes.getNamedItem("class").value = "item";
}

function updatePrice()
{
		getAXAH("/system/getShopFooter.php","shopFooter");
}
function updatePrice2()
{
		//Hanna ändrar
		//getAXAH("/system/getShopFooter.php","half");
		getAXAH("/system/getShopFooter2.php","half");
}
function updatePrice3()
{
		//Hanna ändrar
		//getAXAH("/system/getShopFooter.php","half");
		getAXAH("/system/getShopFooter3.php","half");
}




function checkOut(){
	var checkRequest = getNewHttpObject();
	checkRequest.onreadystatechange = function() {processAXAH();};
	checkRequest.open("GET", "/system/getBasket.php");
	checkRequest.send(false);
	
		function processAXAH(){
		   if (checkRequest.readyState == 4) {
			   if (checkRequest.status == 200) {
			   		if(checkRequest.responseText=="basket_true")
					{
				    	window.location="/kassan/";
					}else{
						alert("Du måste lägga till minst en vara i kundvagnen för att forsätta.");
					}
			   } else {
				   alert("Du kan inte fortsätta pga tekniska problem.");
			   }
		   }
		}

}
