/****************
* METODI AJAX
*****************/
//Creazione oggetto XMLHttpRequest
function createXMLHttpRequest() {
	var ua;
	if(window.XMLHttpRequest) {
		try {
			ua = new XMLHttpRequest();
		}
		catch(e) {
			ua = false;
		}
	}
	else if(window.ActiveXObject) {
		try {
			ua = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e) {
			ua = false;
		}
	}
	return ua;
}
//INVIO RICHIESTA
//Invio generico della richiesta
function sendAjaxGetRequest(parametri){
	req.open('get', 'ajax_cart.php?' + parametri);
	req.onreadystatechange = refreshCart;
	req.send(null);
}
//Invio generico della richiesta Post
function sendAjaxPostRequest(parametri) {
	req.open('POST', 'ajax_cart.php');
	//Send the proper header information along with the request
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//req.setRequestHeader("Content-length", params.length);
	//req.setRequestHeader("Connection", "close");
	req.onreadystatechange = refreshCart;
	//req.send(null);
	req.send(parametri);
}
//Gestione della risposta
function refreshCart() {
	if(req.readyState == 4){
		var response = req.responseText;
		if (response.length>0){
			var array = response.split("||");
			reloadElements(array);
		}
		//else
		//	alert("Cacchio");
	}
	else{
		//Mostriamo le finestre di loading
	}
}
//FUNZIONE AUSILIARIA DI RIDISEGNO INTERFACCIA IN BASE AI CODICI
function reloadElements(array){
	var i=0, contenuto, aux;
	for (i in array){
		if (array[i].indexOf("<=>")>-1){
			//aux = htmlEntityReplace(array[i]);
			aux = array[i];
			contenuto = aux.split("<=>");
			switch(contenuto[0]){
				case "errore":
					alert("Errore: "+contenuto[1]);
					break;
				case "messaggio":
					alert(contenuto[1]);
					break;
				case "logout":
					window.location = "index.php";
					return false;
					break;
				case "lista_carrello":
					document.getElementById("cart_list").innerHTML = contenuto[1];
					break;
				case "totpz":
					//document.getElementById("totpz").innerHTML = contenuto[1];
					var totpz = parseInt(contenuto[1]);
					if (!isNaN(totpz) && totpz>0){
						if (document.getElementById("cart").className=="menu_footer_vuoto")
							document.getElementById("cart").className = "menu_footer";
						if (document.getElementById("link_cart").className=="hidden")
							document.getElementById("link_cart").className = "riep_cart";
					}
					else if (!isNaN(totpz) && totpz<1){
						if (document.getElementById("cart").className=="menu_footer")
							document.getElementById("cart").className = "menu_footer_vuoto";
						if (document.getElementById("link_cart").className=="riep_cart")
							document.getElementById("link_cart").className = "hidden";
					}
					break;
				case "totprice":
					document.getElementById("totprice").innerHTML = contenuto[1];
					break;
				case "varianti":
					//document.getElementById("id_prodotto").value = "0";
					document.getElementById("lista_varianti").innerHTML = contenuto[1];
					$("#apri_scelta_variante").fancybox().trigger('click');
					//document.getElementById("scelta_variante").style.display = "block";
					break;
				case "descrizione_indirizzo_spedizione":
					document.getElementById("descrizione_indirizzo_spedizione").innerHTML = contenuto[1];
					break;
				default:
					salvaErrore("script=cart.js&errore=ricevuta caso non atteso dalla richiesta ajax("+contenuto[0]+")");
					//alert("Errore: ricevuta risposta non attesa dalla richiesta ajax("+contenuto[0]+")");
			}
		}
		else
		//	alert(array[i]);
		salvaErrore("script=cart.js&errore=ricevuta risposta non attesa dalla richiesta ajax("+array[i]+")");
	}
}
//
var req = createXMLHttpRequest();
/**********************
* Altri METODI E VARIABILI
***********************/
function aggiungiAlCarrello(id_prodotto_scheda,id_prodotto){
	//var quantita = document.getElementById("qt_prodotto").value;
	if (id_prodotto>0){
		//sendAjaxGetRequest("tipo=addtocart&id_prodotto="+id_prodotto+"&qt="+quantita);
		sendAjaxGetRequest("tipo=addtocart&id_prodotto="+id_prodotto+"&qt=1");
		annullaAggiungiVariante();
	}
	else{
		sendAjaxGetRequest("tipo=showvariants&id_prodotto_scheda="+id_prodotto_scheda);
	 }
}
function mostraVarianti(id_prodotto_scheda){
	sendAjaxGetRequest("tipo=showvariants&id_prodotto_scheda="+id_prodotto_scheda);
}
function rimuoviDalCarrello(id_item_carrello){
	sendAjaxGetRequest("tipo=cancellaitemcarrello&id_item_carrello="+id_item_carrello);
}
function annullaAggiungiVariante(){
	//document.getElementById("id_prodotto").value = "0";
	document.getElementById("lista_varianti").innerHTML = "";
	//document.getElementById("scelta_variante").style.display = "none";
	$.fancybox.close()
	variante_sel = "";
}
var variante_sel = "";
function selezionaVariante(id_prodotto,div){
	document.getElementById("id_prodotto").value = id_prodotto;
	if (variante_sel!="")
		variante_sel.className = "riquadro_variante_nonsel";
	div.className = "riquadro_variante_sel";
	variante_sel = div;
}
function aggiungiVariante(){
	id_prodotto = document.getElementById("id_prodotto").value;
	if (id_prodotto=="0" || id_prodotto=="")
		alert("Selezionare la variante desiderata prima di aggiungere al carrello");
	else
		aggiungiAlCarrello(0,id_prodotto);
}
function ricaricaIndirizzo(){
	var id_indirizzo = document.getElementById("indirizzo").value;
	if (id_indirizzo>0)
		sendAjaxGetRequest("tipo=ricaricaindirizzo&id_indirizzo="+id_indirizzo);
	else
		alert("Id Indirizzo vale 0!!");
}
