var url = "../controls/gestione_memorizzazioni.asp";
var chiamaPagina = null;
var gloabalDivID = null;
var xmlhttp = null;

/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                    BOX MODIFICA INSERIMENTO CARRELLO                                                    *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/

// POST NON funziona con Mozilla e II6 !!!

function insCarrello(divID,codiceProdotto) {
	gloabalDivID = divID;
	var qta = document.getElementById(["qta" + gloabalDivID]).value;
	var colore = document.getElementById(["colori" + gloabalDivID]).value;
	var procedi = 0;

	if(colore != "nn") { //nn = il prodotto non ha colori
		if (colore == "") {
			alert("E' necessario scegliere il colore");
			procedi = 0;
		}
		else {
			procedi = 1;
		}
	}
	else {
		procedi = 1;
	}

	if(procedi == 1) {
		chiamaPagina = creaXHMHttpRequest(riempiCarrello);
		//Numero casuale x ingannare explorer che altrimenti mostra sempre il solito risulato preso dalla cache
		//window.open(url + "?cmd=inscart&_idprod=" + codiceProdotto + "&qta=" + qta + "&clr=" + colore + "&rand=" + escape(Math.random()));
		chiamaPagina.open("GET", url + "?cmd=inscart&_idprod=" + codiceProdotto + "&qta=" + qta + "&clr=" + colore + "&rand=" + escape(Math.random()), true);
		chiamaPagina.send(null);
	}
}

function dropCarrello(divID,codiceProdotto) {
	if (divID == "") {
		//svuoto carrello
		chiamaPagina = creaXHMHttpRequest(svuotaCarrello);
		//Numero casuale x ingannare explorer che altrimenti mostra sempre il solito risulato preso dalla cache
		chiamaPagina.open("GET", url + "?cmd=emptycart&rand=" + escape(Math.random()), true);		
		chiamaPagina.send(null);
	}
	else {
		// Elimino un prodotto
		gloabalDivID = divID;
		chiamaPagina = creaXHMHttpRequest(rimuoviCarrello);
		//Numero casuale x ingannare explorer che altrimenti mostra sempre il solito risulato preso dalla cache
		chiamaPagina.open("GET", url + "?cmd=dropcart&_idprod=" + codiceProdotto + "&rand=" + escape(Math.random()), true);
		chiamaPagina.send(null);
	}
}


function saveOrdAll(idlinea,idcategoria,idazione,numeroallegati,comando) { /* Salva ordinamento allegati */
	gloabalDivID = "boxAlegati";
	var strOrdinamento = "";
	var strIdAllegati = "";
	for(var x=1; x<numeroallegati+1; x++) {
		strOrdinamento = strOrdinamento == "" ? document.getElementById(["ord_" + x]).value  : strOrdinamento + "," + document.getElementById(["ord_" + x]).value;
		strIdAllegati = strIdAllegati == "" ? document.getElementById(["idall_" + x]).value  : strIdAllegati + "," + document.getElementById(["idall_" + x]).value;		
	}
	
	if(strOrdinamento != "") {
		chiamaPagina = creaXHMHttpRequest(mostraOrdinamento);
		//Numero casuale x ingannare explorer che altrimenti mostra sempre il solito risulato preso dalla cache
		//window.open(url + "?cmd=inscart&_idprod=" + codiceProdotto + "&qta=" + qta + "&clr=" + colore + "&rand=" + escape(Math.random()));
		chiamaPagina.open("GET", url + "?cmd="+comando+"&_strord="+strOrdinamento+"&_strall="+strIdAllegati+"&_idlin="+idlinea+"&_idcat="+idcategoria+"&_idaz="+idazione+"&_numall="+numeroallegati+"&rand=" + escape(Math.random()), true);
		chiamaPagina.send(null);
	}
}


function mostraOrdinamento() {

	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		//document.getElementById([gloabalDivID]).innerHTML = chiamaPagina.responseText;
		window.location.reload();
	}
}



/********************************* AJAX *********************************/

// Creo un oggetto XHMHttpRequest che fa richieste http in background
function creaXHMHttpRequest(eventoUtente) {
	//var xmlhttp = null;	

	if (window.XMLHttpRequest) {
	   xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	   alert('Impossibile continuare: il tuo browser non supporta XMLHttpRequest');
	}

	xmlhttp.onreadystatechange = eventoUtente;
	return xmlhttp;
}
/********************************* AJAX *********************************/

/*
readyState:
0 = XMLHttpRequest object has been created, but not initialized. The open() method has not been called.
1 = The object has been created and initialized, but the send() method has not been called.
2 = The send() method has been called, but the status and headers are not available.
3 = Some data has been received, but invoking the responseXML produces null, because response headers and status are not completely 
	available. Invoking responseText produces partial data.
4 = All the data has been received.
*/

function riempiCarrello() {
	
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		document.getElementById(["qta" + gloabalDivID]).value = "1";
		//alert("Prodotto inserito");
		window.location.reload();
	}
}

function rimuoviCarrello() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		//alert("Prodotto eliminato");
		window.location.reload();
	}
}

function svuotaCarrello() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		//alert("Carrello svuotato");
		window.location.reload();		
	}
}

function aggiornatotale() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		var totale = chiamaPagina.responseText;
		document.getElementById([gloabalDivID]).innerHTML = totale;	
	}
}


function ricalcolaTotale(divID,tipoPagamento,codutente,azione) {
	gloabalDivID = divID;
	chiamaPagina = creaXHMHttpRequest(aggiornatotale);
	//Numero casuale x ingannare explorer che altrimenti mostra sempre il solito risulato preso dalla cache
	//window.open(url + "?cmd=rictot&_tipopag=" + tipoPagamento + "&_codutente=" + codutente + "&rand=" + escape(Math.random()), "get");	
	chiamaPagina.open("GET", url + "?cmd=rictot&_tipopag=" + tipoPagamento + "&_codutente=" + codutente + "&_az="+azione+"&rand=" + escape(Math.random()), true);
	chiamaPagina.send(null);
}

