function showCaricamento(id, div) {
	if (!div || div == 'undefined' || div == null)
		div = 'updating';
	Element.show(div);
}

function hideCaricamento(id, div) {
	if (!div || div == 'undefined' || div == null)
		div = 'updating';
	setTimeout(
		function() { Element.hide(div); }
	, 100);
}

function changeQuantita(riga, iva) {
	var iva; if (parseInt(iva) == 0) iva = 20;
	var perc_iva = iva / 100;

	var righe = $('tb_carrello').rows;
	var riga = riga - 1;
	if (righe[riga]) {
		var celle = righe[riga].cells;

		if (celle[0].getElementsByTagName("input")[0]) {
			var quantita = parseInt(celle[0].getElementsByTagName("input")[2].value);
			if (quantita == undefined || quantita == 0) {
				celle[0].getElementsByTagName("input")[2].value
				quantita = 0;
			}
			var id_carrello = celle[0].getElementsByTagName("input")[0].value;
			var prezzo = parseFloat(celle[0].getElementsByTagName("input")[1].value);
			var totale = prezzo * quantita;

			// calcolo l'iva
			var val_iva = totale * perc_iva;
			celle[0].getElementsByTagName("input")[4].value = val_iva;
			/*val_iva = formatta_numero(val_iva, 2, ",");
			$('tot_iva').innerHTML = val_iva + " &euro;";*/

			celle[0].getElementsByTagName("input")[3].value = totale;

			celle[2].innerHTML = formatta_numero(totale, 2, ",") + " &euro;";

			// tramite ajax aggiorno il carrello
			var url = "xml_upd_carrello.php";
			var pars = "id="+escape(id_carrello)+"&qta="+escape(quantita);
			var myAjax = new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: pars,
					onLoading: showCaricamento,
					onComplete: hideCaricamento
				}
			);
		}
	}

	var tot_iva = 0;
	for (i=0;i<righe.length;i++) {
		var celle = righe[i].cells;
		tot_iva += parseFloat(celle[0].getElementsByTagName("input")[4].value);
	}
	$('tot_iva').innerHTML = formatta_numero(tot_iva, 2, ",") + " &euro;";

	totale = 0;
	for (i=0;i<righe.length;i++) {
		var celle = righe[i].cells;
		totale += parseFloat(celle[0].getElementsByTagName("input")[3].value);
	}

	// totale imponibile
	$('imponibile').innerHTML = formatta_numero(totale, 2, ",") + " &euro;";

	// totale ordine
	totale = totale + tot_iva;
	totale = formatta_numero(totale, 2, ",");
	$('td_tot').innerHTML = totale + " &euro;";
}

function mostraSpese(id) {
	// azzero le spese
	$('speseSpedizione').value = 0;
	$('txt_spese_spedizione').innerHTML = "-,00 &euro;";
	$('div_metodo').style.backgroundColor = "#FFFFFF";

	var righe = $('tb_carrello').rows;
	var tot_iva = 0;
	for (i=0;i<righe.length-1;i++) {
		var celle = righe[i].cells;
		tot_iva += parseFloat(celle[0].getElementsByTagName("input")[4].value);
	}
	$('tot_iva').innerHTML = formatta_numero(tot_iva, 2, ",") + " &euro;";

	totale = 0;
	for (i=0;i<righe.length;i++) {
		var celle = righe[i].cells;
		totale += parseFloat(celle[0].getElementsByTagName("input")[3].value);
	}

	// totale imponibile
	$('imponibile').innerHTML = formatta_numero(totale, 2, ",") + " &euro;";

	// totale ordine
	totale = totale + tot_iva;
	totale = formatta_numero(totale, 2, ",");
	$('td_tot').innerHTML = totale + " &euro;";

	var url = "xml_aggiorna_spese_pagamento.php";
	var pars = "id=" + id;
	var MyAjax = new Ajax.Updater(
		{success: 'div_spese'},
		url,
		{
			method: 'post',
			parameters: pars,
			onSuccess: function () {
				$('div_spese').style.display = "block";
				hideCaricamento(this, 'updating_spese');
			},
			onLoading: showCaricamento(this, 'updating_spese'),
			onFailure: errore('div_spese')
		}
	);
}

// cambia le spese
function cambiaSpese(spese) {
	$('speseSpedizione').value = spese;
	txtspese = formatta_numero(spese, 2, ",");
	$('txt_spese_spedizione').innerHTML = txtspese + " &euro;";
	$('div_spese').style.backgroundColor = "#FFFFFF";

	var righe = $('tb_carrello').rows;
	var tot_iva = 0;
	for (i=0;i<righe.length;i++) {
		var celle = righe[i].cells;
		tot_iva += parseFloat(celle[0].getElementsByTagName("input")[4].value);
	}
	$('tot_iva').innerHTML = formatta_numero(tot_iva, 2, ",") + " &euro;";

	totale = 0;
	for (i=0;i<righe.length;i++) {
		var celle = righe[i].cells;
		totale += parseFloat(celle[0].getElementsByTagName("input")[3].value);
	}

	// totale imponibile
	$('imponibile').innerHTML = formatta_numero(totale, 2, ",") + " &euro;";

	// totale ordine
	totale = totale + tot_iva + spese;
	totale = formatta_numero(totale, 2, ",");
	$('td_tot').innerHTML = totale + " &euro;";
}

function checkCampiSpese() {
	if (!$('speseSpedizione') || $F('speseSpedizione') == undefined || $F('speseSpedizione') == 0) {
		new Effect.Highlight('div_metodo', {startcolor:'#FFFFFF', endcolor:'#FF3300', restorecolor:'#FF9900'});
		new Effect.Highlight('div_spese', {startcolor:'#FFFFFF', endcolor:'#FF3300', restorecolor:'#FF9900'});
		alert("Devi selezionare il metodo di pagamento e le spese di spedizione");
		return false;
	}
	return true;
}

function svuotaCarrello() {
	var url = "xml_svuota_carrello.php";
	var pars = "";
	var MyAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: gotoProdotti
		}
	);
}

function gotoProdotti() {
	window.location = "prodotti.php";
}

// formatta un numero
function formatta_numero(numero, decimali, sep_decimali) {
	if (decimali == 1) {
		numero = Math.round(numero * 10) / 10;
	} else if (decimali == 2) {
		numero = Math.round(numero * 100) / 100;
	} else if (decimali == 3) {
		numero = Math.round(numero * 1000) / 1000;
	}

	// formattiamo una valuta
	if (isNaN(numero))
		return undefined;

	if (decimali < 0)
		return undefined;

	if (decimali == undefined)
		decimali = 2;

	if (sep_decimali == undefined)
		sep_decimali = '.';

	var returned = numero.toString().split('.');
	var str_begin = returned[0];
	var str_after = returned[1];
	if (str_after == undefined)
		str_after = "0";
	var i = 0;
	// decimali
	if (decimali > 0) {
		if (str_after.length < decimali || str_after.length == undefined) {
			while (str_after.length < decimali) {
				str_after += '0';
			}
		}
	}
	if(decimali>0) {
		returned = str_begin + sep_decimali + str_after;
	} else {
		returned = str_begin;
	}
	return returned;
}

/* Contatti agenti*/
function swap(regione) {
	/* rollover cartina */
	$('italia').src = "immagini/it/italia.gif";
	var imm = "immagini/it/" + regione + ".gif";
	$('italia').src = imm;
	$('italia').alt = imm.toString();
	$('italia').title = imm.toString();
	$('italia').style.cursor = 'hand';
}

function swapDe(regione) {
	$('germania').src = "immagini/de/germany.gif";
	var imm = "immagini/de/" + regione + ".gif";
	$('germania').src = imm;
	$('germania').alt = imm.toString();
	$('germania').title = imm.toString();
	$('germania').style.cursor = 'hand';
}

function swapBack() {
	$('italia').src = "immagini/it/italia.gif";
	$('italia').style.cursor = '';
}

function swapBackDe() {
	$('germania').src = "immagini/de/germany.gif";
	$('germania').style.cursor = '';
}

// link al click sulla regione
function go(regione) {
	// pesco i dati via AJAX
	var url = "xml_cerca_provincia.php";
	var pars = "regione=" + regione;
	var MyAjax = new Ajax.Updater(
		{success: 'rivenditori'},
		url,
		{
			method: 'post',
			parameters: pars,
			onFailure: errore('rivenditori'),
			onSuccess: function() {
				setTimeout(
					function() {
						new Effect.ScrollTo('rivenditori', {offset: 100});
					}, 100
				);
			}
		}
	);
}

function goDe(regione) {
	// pesco i dati via AJAX
	var url = "xml_cerca_provincia.php";
	var pars = "regione=" + regione + "&stato=germania";
	var MyAjax = new Ajax.Updater(
		{success: 'rivenditori'},
		url,
		{
			method: 'post',
			parameters: pars,
			onFailure: errore('rivenditori'),
			onSuccess: function() {
				setTimeout(
					function() {
						new Effect.ScrollTo('rivenditori', {offset: 100});
					}, 100
				);
			}
		}
	);
}

// chiamata di gestione dell'errore Ajax
function errore(request, div) {
	if ($(div)) {
		$(div).innerHTML = "Errore nella chiamata Ajax";
	}
}

//nella pagina dei rivenditori pesca le province di uno stato
function cercaProvincia() {
	if ($('rivenditori').hasChildNodes()) {
		for (var i=0; i<$('rivenditori').childNodes.length;i++) {
			$('rivenditori').removeChild($('rivenditori').childNodes[i])
		}
	}
	var url = "xml_seleziona_province.php";
	var pars = 'src='+$('stato').options[$('stato').selectedIndex].value;
	var myAjax = new Ajax.Updater(
		'td_provincia',
		url,
		{
			method: 'post',
			parameters: pars
		}
	);
}

//nella pagina dei rivenditori pesca tutti i rivenditori della provincia
function cercaRivendirore() {
	var url = "xml_seleziona_rivenditore.php";
	var pars = "src="+$('provincia').options[$('provincia').selectedIndex].value;
	var myAjax = new Ajax.Updater(
		'rivenditori',
		url,
		{
			method: 'post',
			parameters: pars
		}
	);
}