var xmlHttp

//opdaterKurv('116491','92147d65402dfb4840b2d10385aa4abc',document.getElementById("p116491").value)
function opdaterKurv(produktid,md5value,antal){ 
//	alert(produktid+','+md5value+','+antal);
	//document.getElementById("kurvliniestatus").innerHTML="opdaterKurv()";
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	if(antal==-1 && produktid==0){
		var str= 'productId='+produktid + '&actiontype=delete&antal='+ antal +'&md5value='+md5value;
	
		 
	}
	else if(antal==-1){
		var str= 'productId='+produktid + '&actiontype=remove&antal='+ antal +'&md5value='+md5value;

	 
	}
	else{
		var str= 'productId='+produktid + '&actiontype=add&antal='+ antal +'&md5value='+md5value;
	}

	var url="xmldata.asp"
	url=url+"?"+str
	url=url+"&sid="+Math.random()
	
	//document.getElementById("kurvliniestatus").innerHTML=url;
	//alert('Do:'+url);
	xmlHttp.onreadystatechange=function(){
		stateChanged(produktid,md5value);
	}
	
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	//alert('slut:'+url);
	
}
 
function stateChanged(produktid,md5value){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		xmlDoc=xmlHttp.responseXML;
		
		//alert('for '+xmlDoc);
		//alert('Debugging '+xmlDoc.xml);
		// alert('Debugging responseText '+xmlHttp.responseText);
		//document.getElementById("kurvliniestatus").innerHTML=xmlHttp.status;
		//document.getElementById("kurvliniestatus").innerHTML='stateChanged';
		
		var produktid=xmlDoc.getElementsByTagName("produktid")[0].childNodes[0].nodeValue;

		var produktnavn=xmlDoc.getElementsByTagName("produktnavn")[0].childNodes[0].nodeValue;
		var antal=xmlDoc.getElementsByTagName("antal")[0].childNodes[0].nodeValue;
		var liniepris=xmlDoc.getElementsByTagName("liniepris")[0].childNodes[0].nodeValue;
	
		updKurvProduktLinie(produktid,produktnavn,antal,liniepris,md5value);
		updateTotalPrice();
	}
} 

function GetXmlHttpObject(){ 
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function removeByID(id){
var el;
if((el=document.getElementById(id)) && el.parentNode){
el.parentNode.removeChild(el);
}
}

//Sletter alle rækker i en tabel minus den første (i>-1) 
function deleteallRows(mytable)
{
   for(i=mytable.rows.length-1; i > 0; i--)
       mytable.deleteRow(i); 
}
	
	

function updKurvProduktLinie(produktid, produktnavn, antal, liniepris,md5value){
//document.getElementById("kurvliniestatus").innerHTML="updKurvProduktLinie()";
	
	//alert(produktid+','+ produktnavn+','+ antal+','+ liniepris);
		var itemBox = document.getElementById('shopping_cart_items');

	if(md5value.length<=3){
				
		alert('Du har desværre ikke aktiveret cookies, som indkøbskurven kræver\n\nDu kan aktiverer cookies på følgende måde:\n\n1.) Vælg Internetindstillinger i menuen Funktioner. \n\n2.) Klik på fanen Beskyttelse af personlige oplysninger for at ændre dine cookie-indstillinger.\n\n3.) Klik på knappen Standard, eller flyt skyderen til indstillingen -Mellem-\n\n4.) Når du ser siden igen skal du trykke opdater eller "F5" for at genindlæse siden.');
	}
	else if(antal<=0 && produktid==0){
		deleteallRows(itemBox);
		
		alert('Kurven er blevet tømt');
	}
	else if(antal<=0){
		var productRow = document.getElementById('shop_produkt' + produktid);
		var numberOfItemCell = productRow.cells[1];
		//alert(numberOfItemCell.innerHTML);
		
		productRow.parentNode.removeChild(productRow);			
		
		
	}
	else{

		if(document.getElementById('shop_produkt' + produktid)){	// A product with this id is allready in the basket - just add number items
			var row = document.getElementById('shop_produkt' + produktid);
			row.cells[0].innerHTML = produktnavn.substring(0,12)+'...';
			row.cells[1].innerHTML = antal;
			row.cells[2].innerHTML = liniepris;

			//document.getElementById("kurvliniestatus").innerHTML="Opdateret";
		}
		else{

			var tr = itemBox.insertRow(-1);
			tr.id = 'shop_produkt' + produktid

			var td = tr.insertCell(-1);
			td.className ='kurvprodukt'; 
			td.title =produktnavn;
			td.innerHTML = produktnavn.substring(0,12)+'...'; 	// Description

			var td = tr.insertCell(-1);
			td.className ='kurvantal'; 
			td.innerHTML = antal; 	// Number of items

			var td = tr.insertCell(-1);
			td.className ='kurvpris'; 
			td.innerHTML = liniepris;

			var td = tr.insertCell(-1);
			td.innerHTML = '<a href="#" onClick="opdaterKurv(\'' + produktid + '\',\''+ md5value +'\','+ -1 +');return false;"><img src="images/remove.gif"></a>';	

			//document.getElementById("kurvliniestatus").innerHTML="Ny" + produktid +','+ md5value;
		}
	}
}

