var xmlHttp

function recalculate_cart(x) { 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }

	// punem cantitatile intr-un string
	var arr = x.split("~");
	
	var cant_prod="";
	for (i=1; i<arr[0]; i++)
	{
		var nr_buc = 'nr_buc['+ i +']';
	 	cant_prod += (cant_prod != "" ? "_" : "") + document.getElementById(nr_buc).value;
	}
	
	 
	
	var url="recalculate_cart.php"
	url=url+"?buc="+cant_prod+"&trans="+arr[1]
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChangedCart
	xmlHttp.open("GET",url,true)
	 
	xmlHttp.send(null)
}


function reloadpage_ajax(type) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 		 
	var frm=document.getElementById("basket");
	//save mode:
	frm.reload_page.value = type;
	submitFormAjax(frm, "basket_ajax.php", stateChangedInfo);
}


function stateChangedInfo(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	 	var xmlDoc = xmlHttp.responseXML;
		if (xmlDoc) {
			var response = xmlDoc.getElementsByTagName("basket_ajax")[0];
			//if the document requests a redirect, process only that
			if (response.getElementsByTagName("redirect_url")[0]){
			   var redirect_url = response.getElementsByTagName("redirect_url")[0].childNodes[0].nodeValue			   
			   document.location = redirect_url;
			   return;
			}
			
			//execute javascript commands received:
			if (response.getElementsByTagName("javascript")[0]){
			   var command = response.getElementsByTagName("javascript")[0].childNodes[0].nodeValue;
			   eval(command);
			}			
			if (response.getElementsByTagName("basket_tabs")[0]) {
				var tabs = response.getElementsByTagName("basket_tabs")[0].childNodes;
				for (i=0;i<tabs.length;i++){
					document.getElementById("tab_"+tabs[i].nodeName).innerHTML = tabs[i].childNodes[0].nodeValue;
				}
			}
		} 
		else { alert ("xmldoc is null!") }
		
	}
} 

function stateChangedCart() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
	 
	 	//document.getElementById("scateg").innerHTML=xmlHttp.responseText
	 	//xmlDoc = xmlHttp.responseText; 
	 	xmlDoc = xmlHttp.responseXML;
		
		
		x = xmlDoc.getElementsByTagName("item");
		//alert(x.length);
		for(i=0;i<x.length;i++){
			var id=i+1;
			var tag1 = 'product_total_price[' + id + ']';
			var tag2 = 'stock_limit[' + id + ']';
			var tag3 = 'nr_buc[' + id + ']';
			var tag4 = 'qty_zero[' + id + ']';
			container_product_total_price = document.getElementById(tag1);
			container_total_price = document.getElementById('total_price');
			container_total_cmd_value = document.getElementById('total_cmd_value');
			container_timbru_verde_total = document.getElementById('timbru_verde_total');
			container_transport_value = document.getElementById('transport_value');
			container_taxes_value = document.getElementById('total_taxes_value');
			container_stock_limit = document.getElementById(tag2);
			container_cart_quantity = document.getElementById('cart_quantity');
			container_product_qty = document.getElementById(tag3);
			container_qty_zero = document.getElementById(tag4);
			
			//alert(tag_products);
			//container_product_total_price.innerHTML = "";
			//container_total_price.innerHTML = "";
			//container_total_cmd_value.innerHTML = "";
						
			container_product_total_price.innerHTML =  x[i].childNodes[0].firstChild.nodeValue ;
			container_total_price.innerHTML =  x[i].childNodes[1].firstChild.nodeValue ;
			container_total_cmd_value.innerHTML =  x[i].childNodes[2].firstChild.nodeValue ;
			container_timbru_verde_total.innerHTML =  x[i].childNodes[3].firstChild.nodeValue ;
			container_transport_value.innerHTML =  x[i].childNodes[4].firstChild.nodeValue ;
			container_taxes_value.innerHTML =  x[i].childNodes[5].firstChild.nodeValue ;
			if (x[i].childNodes[6].childNodes.length != 0){
				container_stock_limit.innerHTML =  x[i].childNodes[6].firstChild.nodeValue ;
			}
			container_cart_quantity.innerHTML =  x[i].childNodes[7].firstChild.nodeValue ;
			container_product_qty.value =  x[i].childNodes[8].firstChild.nodeValue ;
			if (x[i].childNodes[9].childNodes.length != 0){
				container_qty_zero.innerHTML =  x[i].childNodes[9].firstChild.nodeValue ;
			}	 
			id=0;
		}
	 } 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	return xmlHttp;
}


function submitFormAjax(frm, actionUrl, callbackFunction) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }
	 		 	
	//create a string with all form's paramters
	var params = "";	
	for (var i=0; i<frm.elements.length; i++)
	{
		var val;
		//TBD: add special code for lists with multiple selections if needed
		if (frm.elements[i].type == "checkbox" || frm.elements[i].type == "radio") {
			//save the value only if it is checked
			if (!frm.elements[i].checked) continue;
			val = (frm.elements[i].checked) ? frm.elements[i].value : "";
		} else {
			val = frm.elements[i].value;
		}
	   params += (params != "" ? "&" : "") + frm.elements[i].name + "=" + encodeURI(val);	   
	}
	
	//set function that will process the results
	xmlHttp.onreadystatechange=callbackFunction
	
	//open connection, set headers and send parameters
	xmlHttp.open("POST", actionUrl, true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");	 
	xmlHttp.send(params)
}

