var xmlHttp;

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

/*-------------------------------------------------------------------------------*/



/*-------------------------------------------------------------------------------*/



/*-------------------------------------------------------------------------------*/

function recalculateCart(id)
{
	var xmlHttp2;	
	xmlHttp2=GetXmlHttpObject2();
	if (xmlHttp2==null){
		alert ("Brak obslugi xmlHttpRequest");
		return;
	}
	var ilosc = document.getElementById("nilosc_" + id).value;	
	document.getElementById("modify_" + id).innerHTML="<img src=\"/img/szkielet/pasek_postepu.gif\" width=\"75\">";
	var url="/xmlrecalculatecart.php?id=" + id +"\&ilosc=" + ilosc;
	xmlHttp2.onreadystatechange = function(){recalculateCart_stateChanged(xmlHttp2)};
	xmlHttp2.open("GET", url , true);
	xmlHttp2.send(null);
}

function recalculateCart_stateChanged(http_request)
{
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			var xmldoc 		= http_request.responseXML;
			var root_node 	= xmldoc.documentElement;
			var id			= root_node.getElementsByTagName("id")[0].childNodes[0].data;
			var quantity	= root_node.getElementsByTagName("quantity")[0].childNodes[0].data;
			var sum			= root_node.getElementsByTagName("sum")[0].childNodes[0].data;
			var total		= root_node.getElementsByTagName("total")[0].childNodes[0].data;
			if(total > 0){
				var row	= document.getElementById("rowid_" + id);
				if(quantity >0){
					row.cells[2].innerHTML = quantity;
					row.cells[4].innerHTML = sum + "zł.";
					row.cells[5].innerHTML = "<form><input type=\"text\" size=\"2\" id=\"nilosc_" + id + "\" name=\"nilosc\" value=\"" + quantity + "\"><input type=\"submit\" value=\"Przelicz\" onClick=\"recalculateCart(" + id + ")\"><br><input type=\"submit\" value=\"Skasuj\" onClick=\"removeFromCart(" + id + ")></form>";
					document.getElementById("total_costs").innerHTML = total + " zł. brutto";
				}
				else{
					document.getElementById("cart_table").deleteRow(row.rowIndex);
					document.getElementById("total_costs").innerHTML = total + " zł. brutto";
				}
			}
			else{
				document.getElementById("cart_content").innerHTML = "<h3>Nie dodałeś jeszcze żadnego produktu do koszyka</h3>";
			}
			przeladujInfoKoszyk()
		}
		else{
			alert('Coś się nie powiodło!');
		}
	}
}

/*-------------------------------------------------------------------------------*/

function removeFromCart(id)
{
	var xmlHttp2;	
	xmlHttp2=GetXmlHttpObject2();
	if (xmlHttp2==null){
		alert ("Brak obslugi xmlHttpRequest");
		return;
	}
	document.getElementById("modify_" + id).innerHTML="<img src=\"/img/szkielet/pasek_postepu.gif\" width=\"75\">";
	var url="xmlremovefromcart.php?id=" + id;
	xmlHttp2.onreadystatechange = function(){removeFromCart_stateChanged(xmlHttp2)};
	xmlHttp2.open("GET", url , true);
	xmlHttp2.send(null);
}

function removeFromCart_stateChanged(http_request)
{
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			var xmldoc 		= http_request.responseXML;
			var root_node 	= xmldoc.documentElement;
			var id			= root_node.getElementsByTagName("id")[0].childNodes[0].data;
			var error		= root_node.getElementsByTagName("error")[0].childNodes[0].data;
			var positions	= root_node.getElementsByTagName("positions")[0].childNodes[0].data;
			var total		= root_node.getElementsByTagName("total")[0].childNodes[0].data;
			
			if(positions > 0){
				if(error == 0){
					var row	= document.getElementById("rowid_" + id);
					document.getElementById("cart_table").deleteRow(row.rowIndex);
					document.getElementById("total_costs").innerHTML = total + " zł. brutto";
				}
			}
			else{
				document.getElementById("cart_content").innerHTML = "<h3>Nie dodałeś jeszcze żadnego produktu do koszyka</h3>";
			}
			
			przeladujInfoKoszyk()
		}
		else{
			alert('Coś się nie powiodło!');
		}
	}
}

/*-------------------------------------------------------------------------------*/

function GetXmlHttpObject2()
{ 
	var http_request;
	if (window.XMLHttpRequest){ // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType){
			http_request.overrideMimeType('text/xml');
        }
	}else if (window.ActiveXObject){ // IE
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){}
		}
	}
	return http_request;
}
