var req;

function aggiungi_prodotto() {

	var prodotto = document.getElementById('prodotto');
	var qnt = document.getElementById("qnt");
		
	if (qnt.value == "") {
		alert("Nessuna Quantità Inserita");
		qnt.focus();
	}
		else if (isNaN(qnt.value)) {
			alert("La quantità inserita non è un numero");
			qnt.focus();
		}
			else if (qnt.value <= 0) {
				alert("La quantità non deve essere minore o uguale a 0");
				qnt.focus();
			}
				else if (qnt.value.indexOf(".") != "-1") {
					alert("La quantità deve essere solo un numero intero");
					qnt.focus();
				}
				else {	 
						req = assegna_req();
						var url="ordine_funzioni.php?id_prodotto="+prodotto.value;
						
						if (req != null) {
							req.open("GET", url, true);
							req.onreadystatechange = function() {
							if (req.readyState == 4 && req.status == 200) { 

								var tokens = req.responseText.split("__");
								var prezzo = tokens[0] * qnt.value;
								var peso_alpezzo = (tokens[2] * 10) * qnt.value / 10;
								
								//var prezzo = req.responseText.replace("__alpezzo", "") * qnt.value
								var delete_riga = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' onclick='cancella_riga(\"riga_" + prodotto.value + qnt.value + "\")'><font size='1'>ELIMINA</font></a>";		
								
								if(tokens[1] == "alpezzo") {
									var hidden = "<input type='hidden' id='prodotto_alpezzo' name='prodotto_alpezzo[]' value=\"" + prodotto.value.replace("_", "'") + "\" >\n<input type='hidden' name='qnt_alpezzo[]' value='" + qnt.value + "' >\n<input type='hidden' name='prezzo_alpezzo[]' value='" + prezzo + "' >\n";								
									var nuova_riga = "<div id=\"riga_" + prodotto.value + qnt.value + "\">" + hidden + "<b>" + prodotto.value.replace("_", "'") + " - (Quantit&agrave; " + qnt.value +  ") - " + peso_alpezzo +  " -Kg. - " + prezzo + " &euro;" + delete_riga + "</b></div>\n\n";
								}
								else {
									var hidden = "<input type='hidden' id='prodotto_alkg' name='prodotto_alkg[]' value=\"" + prodotto.value.replace("_", "'") + "\" >\n<input type='hidden' name='qnt_alkg[]' value='" + qnt.value + "' >\n<input type='hidden' name='prezzo_alkg[]' value='" + prezzo + "' >\n";
									var nuova_riga = "<div id=\"riga_" + prodotto.value + qnt.value + "\">" + hidden + "<b>" + prodotto.value.replace("_", "'") + " - (Quantit&agrave; " + qnt.value +  ") - " + qnt.value +  " Kg. - " + prezzo + " &euro;" + delete_riga + "</b></div>\n\n";
								}	
								
								document.getElementById('zona').innerHTML = document.getElementById('zona').innerHTML + nuova_riga;
								qnt.value = "1";
								button_submit(1);
								
							    }
							}
							req.send(null);
						}
					}
		
}

function cancella_riga(id) {
	
	var div = document.getElementById(id);
	if (conferma()) div.parentNode.removeChild(div);
	
}

function button_submit(tipo) {

	if(tipo == 0) document.getElementById("prosegui").innerHTML = "<br />&nbsp;<br />";
		else if(tipo == 1) document.getElementById("prosegui").innerHTML = "<br /><input type='submit' class='Campiinvio' name='prosegui' value=\"Prosegui con l'acquisto\" >";

}

function conferma() {
	return confirm('Sei sicuro di voler procedere?');
} 

function controllaform() {

	if (document.getElementById("prodotto_alkg") || document.getElementById("prodotto_alpezzo")) {}
	else {
		alert("Nessun prodotto selezionato");
		return false;
	}

	if (document.getElementById("nome").value=="")
	{
		alert("Inserire Nome");
		document.getElementById("nome").focus();
		return false;
	}

	if (document.getElementById("cognome").value=="")
	{
		alert("Inserire Cognome");
		document.getElementById("cognome").focus();
		return false;
	}

	if (document.getElementById("mail").value=="")
	{
		alert("Inserire E-Mail");
		document.getElementById("mail").focus();
		return false;
	}

	if (document.getElementById("indirizzo").value=="")
	{
		alert("Inserire Indirizzo");
		document.getElementById("indirizzo").focus();
		return false;
	}

	if (document.getElementById("cap").value=="")
	{
		alert("Inserire C.A.P.");
		document.getElementById("cap").focus();
		return false;
	}

	if (document.getElementById("citta").value=="")
	{
		alert("Inserire Città");
		document.getElementById("citta").focus();
		return false;
	}

	if (document.getElementById("provincia").value=="")
	{
		alert("Inserire Provincia");
		document.getElementById("provincia").focus();
		return false;
	}

	if (document.getElementById("autorizza").checked == false)
	{
		alert("Devi autorizzare il trattamento dei dati");
		document.getElementById("autorizza").focus();
		return false;
	}

}

function assegna_req() {

	try { 
	 req = new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e) {
	  try {	
		req = new ActiveXObject("Microsoft.XMLHTTP"); 
	  }
	  catch(oc) {
		req = null; 
	  }
	}

	if (!req && typeof XMLHttpRequest != "undefined")  req = new XMLHttpRequest();

	return req;
}

