
	function closeBuyTool () {
		document.getElementById("photo_preview").innerHTML = "";
		document.getElementById("photo_order").innerHTML = "";
		document.getElementById("photobuytool").style.display = "none";	
		document.getElementById("cover").style.display = "none";	
	}

	function togglePhotoBuyTool(photoid) {
		// Chiudo il tool, se per caso è aperto
		closeBuyTool();
	
		// Attivo la foto
		document.getElementById("photo_preview").innerHTML = '<img src="pages/showphoto.php?psi=' + phpsessid + '&id=' + photoid + '" />';
	
		// Aggiorno i cosi del coso
		updateBuyTool(photoid);
	
		// Infine mostro il tool
		if (document.body.clientHeight) {
			posH = (document.body.clientHeight / 2) - 210;
			document.getElementById("cover").style.height = document.body.clientHeight + "px";
		} else {
			posH = (window.innerHeight / 2) - 210;
		}
	
		if (document.body.clientWidth) {
			posW = (document.body.clientWidth / 2) - 420; 	
			document.getElementById("cover").style.width = document.body.clientWidth + "px";
		} else {
			posW = (window.innerWidth / 2) - 420; 
		}
		document.getElementById("photobuytool").style.top = posH + "px";
		document.getElementById("photobuytool").style.left = posW + "px";
		document.getElementById("cover").style.display = "block";
		document.getElementById("photobuytool").style.display = "block";

	}

	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 updateBuyTool(photo_id){

		var xhr = getNewHttpObject();
		xhr.onreadystatechange = function() {
									document.getElementById("photo_order").innerHTML = "Operazione in corso ...";
									updateBuyToolText();
											};
		xhr.open("POST", "pages/cartstatus.php?entry_id="+ photo_id +"&psi=" + phpsessid);
		xhr.send(false);

		function updateBuyToolText() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					document.getElementById("photo_order").innerHTML = xhr.responseText;
				} else {
					document.getElementById("photo_order").innerHTML = "Errore, pagina non caricata.";
				}
			}
		}
	}

	function updateRightOrder(){

		var xhr = getNewHttpObject();
		xhr.onreadystatechange = function() {
										updateRight();
										
									};
		xhr.open("POST", "includes/other.inc.php?psi=" + phpsessid);
		xhr.send(false);

		function updateRight() {
		  if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					
			    document.getElementById("aux").innerHTML = xhr.responseText;
				} 
			}
		}
	}

	function updateCart (photo_id, format, add) {
		
		var xhr = getNewHttpObject();
		xhr.onreadystatechange = function () {
				if (xhr.readyState == 4) {
					//if (xhr.status == 200) {
						
            //document.getElementById("debug").innerHTML = "pages/updatecart.php?entry_id="+ photo_id +"&psi=" + phpsessid + "&format=" + format + "&add=" + add;
            updateBuyTool(photo_id, phpsessid);
						updateRightOrder();
						
					//} 
				}	
				
				

		}					
		xhr.open("POST", "pages/updatecart.php?entry_id="+ photo_id +"&psi=" + phpsessid + "&format=" + format + "&add=" + add);
		xhr.send(false);
	
	}
	
	function updateCartAlert (photo_id, format, add) {
		
		//alert("Attenzione, non sei registrato oppure non ti sei autenticato, per evadere un ordine è necessario registrarsi prima di confermare il carrello.");
		var xhr = getNewHttpObject();
		xhr.onreadystatechange = function () {
				if (xhr.readyState == 4) {
					if (xhr.status == 200) {
						updateBuyTool(photo_id, phpsessid);
						updateRightOrder();
						
					} 
				}	
				
				

		}					
		xhr.open("POST", "pages/updatecart.php?entry_id="+ photo_id +"&psi=" + phpsessid + "&format=" + format + "&add=" + add);
		xhr.send(false);
	
	}


	function updateCartAndReload (photo_id, format, add) {
		
		var xhr = getNewHttpObject();
		xhr.onreadystatechange = function () {
				if (xhr.readyState == 4) {
					if (xhr.status == 200) {
						window.location = "index.php?loadpage=cart";
						
					} 
				}	
				
				

		}					
		xhr.open("POST", "pages/updatecart.php?entry_id="+ photo_id +"&psi=" + phpsessid + "&format=" + format + "&add=" + add);
		xhr.send(false);
	
	}
	
	function resetCart(photo_id) {
		var xhr = getNewHttpObject();
		xhr.onreadystatechange = function () {
				if (xhr.readyState == 4) {
					if (xhr.status == 200) {
						updateBuyTool(photo_id, phpsessid);
						updateRightOrder();
						document.getElementById("photo_order").innerHTML += xhr.responseText;
						closeBuyTool();
					} 
				}	
				
				

		}					
		xhr.open("POST", "pages/resetcart.php?entry_id="+ photo_id +"&psi=" + phpsessid);
		xhr.send(false);
	}

