var tempo_mesi = new Array();
tempo_mesi[1] = "1 mese";
tempo_mesi[2] = "2 mesi";
tempo_mesi[3] = "3 mesi";
tempo_mesi[4] = "4 mesi";
tempo_mesi[5] = "5 mesi";
tempo_mesi[6] = "6 mesi";
tempo_mesi[7] = "7 mesi";
tempo_mesi[8] = "8 mesi";
tempo_mesi[9] = "9 mesi";
tempo_mesi[10] = "10 mesi";
tempo_mesi[11] = "11 mesi";
tempo_mesi[12] = "1 anno";
tempo_mesi[24] = "2 anni";
tempo_mesi[36] = "3 anni";
tempo_mesi[48] = "4 anni";
tempo_mesi[60] = "5 anni";

var tempo_anno = new Array();
tempo_anno[12] = "1 anno";
tempo_anno[24] = "2 anni";
tempo_anno[36] = "3 anni";
tempo_anno[48] = "4 anni";
tempo_anno[60] = "5 anni";

function prezzo(valore) {
	valore	= (Math.round(valore)).toFixed(2);
	return valore;
	//valore = valore.toString();
	//var prezzo = valore.split(".");
	//if (prezzo[1]==null || prezzo[1]=="undefined") prezzo[1] = "00";
	//else if (prezzo[1].length<2) prezzo[1] += "0";
	//return prezzo[0] + "." + prezzo[1];	
}

function calcola () {
	var piano, parti, ordine, tot = 0, mm = document.phcsord;
	for (var c=0; c<ORDINI.length; c++) {
		ordine = ORDINI[c];
		piano	= mm["piano"+ordine].value;

		var totale_piano 		= 0;
		var totale_estensione 	= 0;
		//piano selezionato
		if (piano!="") {
			parti		= piano.split ("%");
			parti[1]	= parseFloat (parti[1]);

			// se il select del tempo si tratta di un servizio a consumo
			mesi = (mm["tempo"+ordine]!=null)
				? mm["tempo"+ordine].value
				: 12;

			//se manca l'estensione non è un dominio
			delta_tra_estensioni = (mm["tld"+ordine]!=null)
				? DOMINI_PREZZI[mm["tld"+ordine].value]-DOMINI_PREZZI["it"]
				: 0;

			totale_estensione = (mesi/12)*delta_tra_estensioni;	//AGGIUNTA COSTO ESTENSIONE
			totale_piano = ((mesi/12)*parti[1]) - totale_estensione;
		} 
		mm["tot"+ordine].value = prezzo(totale_piano);
		
		//se esiste il campo per il sovrapprezzo della TLD allora aggiornalo
		if (mm["prezzotld"+ordine]!=null) mm["prezzotld"+ordine].value = prezzo(totale_estensione);	
		tot += totale_piano + totale_estensione;
	}
	mm.totale.value = 		prezzo(tot);
	mm.iva.value	= 		prezzo(tot*aliquota_iva/100);
	mm.totaleIva.value	= 	prezzo(tot*(1+(aliquota_iva/100)));
}

function populateTempo(g) {
	var piano, mm = document.phcsord;
	
	// se non ha il select del tempo, salta direttamente la funzione
	if (mm["tempo"+g]==null) return;
	if (mm["piano"+g].value=="") return;
	
	piano	= mm["piano"+g].value;
	parti	= piano.split ("%");
	var selected = mm["tempo"+g].value;
	var tt = ((PIANI['p'+parti[0]]==1)) ? tempo_mesi : tempo_anno;
	select_clear("phcsord", "tempo"+g);
	var oo = mm["tempo"+g].options;
	if (oo!=null) {
		for(obj in tt) oo[oo.length] = new Option(tt[obj],obj);
		mm["tempo"+g].value = selected;
		if (mm["tempo"+g].value == "") mm["tempo"+g].value = tt[0];
	}
	calcola();
}
function controlla() {
	var piano, parti, tot = 0, mm = document.phcsord;

	for (c=0; c<ORDINI.length; c++) {
		piano	= mm["piano"+ORDINI[c]].value;
		if (piano=="") {
			alert("Non hai selezionato il servizio che desideri per tutti gli ordini presenti nel tuo carrello.\n\nSe non sei interessato ad alcuni ordini puoi rimuoverli mediante l'apposito bottone");
			return false;
		}
	}
	return true;
}
