// JavaScript Document
var ORDTab = 1;
var ORDAll = false;
var PRCodicePromo = false // Array(tipo, valore)
var ZonaSelez = 'in';

function ORDCambiaTab(inID) {
	if(ORDAll) {
		ORDNascondiTutti(4);
	}
	document.getElementById('ORDVoce'+ORDTab).className = 'hidden';
	document.getElementById('ORDTab'+ORDTab).className = '';
	document.getElementById('ORDVoce'+inID).className = '';
	document.getElementById('ORDTab'+inID).className = 'sel';
	ORDTab = inID;
	return false;
}

function ORDVisualizzaTutti(inTot) {
	for(var i=1; i<=inTot; i++) {
		document.getElementById('ORDVoce'+i).className = '';
		document.getElementById('ORDBottoni'+i).style.display = 'none';
		document.getElementById('ORDNome'+i).className = '';
		document.getElementById('ORDTab'+ORDTab).className = '';
	}
	document.getElementById('ORDNome2a').className = '';
	document.getElementById('ORDBottoni2a').style.display = 'none';
	ORDAll = true;
}

function ORDNascondiTutti(inTot) {
	for(var i=1; i<=inTot; i++) {
		document.getElementById('ORDVoce'+i).className = 'hidden';
		document.getElementById('ORDBottoni'+i).style.display = '';
		document.getElementById('ORDNome'+i).className = '';
		document.getElementById('ORDTab'+ORDTab).className = 'hidden';
	}
	document.getElementById('ORDNome2a').className = 'none';
	document.getElementById('ORDBottoni2a').style.display = '';
	ORDAll = true;
}

function ORDlunghezzaMax(ele, tot) {
   if(ele.value.length > tot)
      ele.value = ele.value.substring(0, tot);
   document.getElementById('totCaratteri').value = (tot-ele.value.length);
}

function cambiaTracking(ele) {
	document.getElementById('trackingTr').style.display = (ele.options[ele.selectedIndex].value == "1" ? '' : 'none');
}

function checkPagamento(id, print) {
   if(!print) {
      if(id.options[id.selectedIndex].value == 'cc' || id.options[id.selectedIndex].value == 'bt') {
         alert(TRADSUPERSEC_js_no_otp);
         document.ordine.g_metodo_pagamento.selectedIndex = 2;
         return false;
      }
   }
   return true;
}

function checkVaso(id) {
	if(id.options[id.selectedIndex].value == '1')
	{
		if (document.ordine.g_dove.options[document.ordine.g_dove.selectedIndex].value == 'in' || document.ordine.g_dove.options[document.ordine.g_dove.selectedIndex].value == 'out_hin')
		{
			document.getElementById('promozione_table_vaso').style.display = '';
		}
		else
		{
			alert('Il vaso è disponibile nella sola città di Prato e nell\'Hinterland');
         	id.selectedIndex = 0;
		}
	}
	else
	{
		document.getElementById('promozione_table_vaso').style.display = 'none';
	}
	return PRUpdateStatePrices();
}

function cambiaZona(ele, hinterland, world) {
	var id = ele.options[ele.selectedIndex].value;
	var nome = ele.options[ele.selectedIndex].text;
	if(isNaN(id)) {
		if(hinterland) {
			ele.selectedIndex = 0;
			document.ordine.g_dove.selectedIndex = 2;
		}
		document.getElementById('zona_'+ZonaSelez).style.display = 'none';
		ZonaSelez = id;
		document.getElementById('zona_'+ZonaSelez).style.display = '';
	}
	if(world) {
		var content = nome.split(' (only ');
		if(content.length == 2) {
			document.ordine.g_out_mon_consegna_citta.value = content[1].slice(0, -1);
			document.ordine.g_out_mon_consegna_citta.readOnly = true;
			document.ordine.g_out_mon_consegna_provincia.value = content[1].slice(0, -1);
			document.ordine.g_out_mon_consegna_provincia.readOnly = true;
		} else {	
			document.ordine.g_out_mon_consegna_citta.value = '';
			document.ordine.g_out_mon_consegna_citta.readOnly = false;
			document.ordine.g_out_mon_consegna_provincia.value = '';
			document.ordine.g_out_mon_consegna_provincia.readOnly = false;
		}
	}
	if (ZonaSelez == 'out_ita' || ZonaSelez == 'out_mon')
	{
		if (document.ordine.g_vaso_add.type != 'hidden')
		{
			if (document.ordine.g_vaso_add.options[document.ordine.g_vaso_add.selectedIndex].value == 1)
			{
				alert('Il vaso è disponibile nella sola città di Prato e nell\'Hinterland; per questo motivo sarà rimosso dal carrello.');
	         	document.ordine.g_vaso_add.selectedIndex = 0;
				document.getElementById('promozione_table_vaso').style.display = 'none';
			}
		}
	}
	
	return PRUpdateStatePrices();
}

function validaData(ele) {
   if(ele.value > 0 && ele.value < 10)
      ele.value = '0'+ele.value;
   else if(ele.value == '')
      ele.select();
}

function cambiaGG(ele) {
	var d = new Date();
   if(isNaN(ele.value))
      ele.value = ele.value.substr(0, ele.value.length-1);
   if((ele.value <= 0 || ele.value >= 32) && ele.value != '')
      ele.value = (ele.value.length > 0 ? ele.value.substr(0, ele.value.length-1) : '');
}

function cambiaMM(ele) {
   var d = new Date();
   if(isNaN(ele.value))
      ele.value = ele.value.substr(0, ele.value.length-1);
   if((ele.value <= 0 || ele.value >= 13) && ele.value != '')
      ele.value = (ele.value.length > 0 ? ele.value.substr(0, ele.value.length-1) : '');
}

function cambiaAAAA(ele) {
	var d = new Date();
   if(isNaN(ele.value))
      ele.value = ele.value.substr(0, ele.value.length-1);
}

function openFrasi(url) {
   window.open(url, 'frasi', 'width=450,height=220,scrollbars=yes');
   return false;
}

function PRcambiaNumeroCell(ele) {
	// Verifico la validità dei dati inseriti per la quantità
	if(isNaN(ele.value)) {
	   ele.value = ele.value.substr(0, ele.value.length-1);
		ele.select();
	}
}

function PROutNumeroCell(ele) {
	if(ele.value.length == 7)
		return true;
	alert(TRADSUPERSEC_js_tracking_numer_errore);
	ele.select();
}

function PRUpdateStatePrices() {
	var ele = document.ordine.g_dove;
	
	switch(ele.options[ele.selectedIndex].value) {
		case 'in':
			PRInfo[3] = fixed(0.00);
			PRInfo[4] = fixed(0.00);
			break;
		case 'out_hin':
			var inStato = document.ordine.g_out_hin_citta.value;
			PRInfo[3] = fixed(ORDPrezziStati[inStato][0]);
			PRInfo[4] = fixed(ORDPrezziStati[inStato][1]);
			break;
		case 'out_ita':
			var inStato = document.ordine.g_out_ita_stato.value;
			PRInfo[3] = fixed(ORDPrezziStati[inStato][0]);
			PRInfo[4] = fixed(ORDPrezziStati[inStato][1]);
			break;
		case 'out_mon':
			var inStato = document.ordine.g_out_mon_stato.value;
			PRInfo[3] = fixed(ORDPrezziStati[inStato][0]);
			PRInfo[4] = fixed(ORDPrezziStati[inStato][1]);
			break;
	}
	
	document.getElementById('riepCostoConsegna').innerHTML = PRInfo[3];
	document.getElementById('riepPrezzoTotale').innerHTML = PRGetFinalDiscount();
	if(PRInfo[4] > 1) {
		document.getElementById('riepPrezzoMinimo').style.display = '';
		document.getElementById('riepPrezzoMinimoValue').innerHTML = PRInfo[4];
	} else {
		document.getElementById('riepPrezzoMinimo').style.display = 'none';
	}
}

function PRRemoveStatePrices() {
	PRInfo[3] = 0.00;
	PRInfo[4] = 0.00;
	document.getElementById('riepCostoConsegna').innerHTML = '0.00';
	document.getElementById('riepPrezzoTotale').innerHTML = PRGetFinalDiscount();
	document.getElementById('riepPrezzoMinimo').style.display = 'none';
}

function PRcambiaQuantita(ele) {
	// Verifico la validità dei dati inseriti per la quantità
	if(isNaN(ele.value)) {
	   ele.value = ele.value.substr(0, ele.value.length-1);
		if(ele.value == '') {
			ele.value = 1;
			ele.select();
		}
	} else if(ele.value == '0') {
	   ele.value = PRInfo[2];
	} else if(ele.value == '') {
		ele.value = PRInfo[2];
		ele.select();
	}
	PRInfo[6] = ele.value;
	PRInfo[5] = PRGetFinalPrice();
	document.getElementById('riepPrezzoQuantPerUnit').innerHTML = PRInfo[5];
	document.getElementById('riepPrezzoTotale').innerHTML = PRGetFinalDiscount();
}

function PRConfermaMinimoQuantita(ele) {
	if(!PRInfo[2])
		return true;
	if(ele.value >= PRInfo[2])
		return true;
	alert(TRADSUPERSEC_js_pezzi_minimo+' '+PRInfo[2]);
	ele.value = PRInfo[2];
	PRInfo[6] = ele.value;
	PRInfo[5] = PRGetFinalPrice();
	document.getElementById('riepPrezzoQuantPerUnit').innerHTML = PRInfo[5];
	document.getElementById('riepPrezzoTotale').innerHTML = PRGetFinalDiscount();
	return true;
}

function PRCheckRemoteCode(ele) {
	req = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  req = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   req = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   req = false;
	  }
	 }
	@end @*/
	if (!req && typeof XMLHttpRequest != 'undefined') {
	  req = new XMLHttpRequest();
	}
	if(req) {
		req.open("GET", REAL_DOMAIN+"/public/"+LABEL+"/_exec/checkCode.xml.php?c="+escape(document.ordine.g_promozione.value)+"&e="+escape(document.ordine.g_email.value), true);
		req.onreadystatechange = PRCheckRemoteResponse;
		req.send(null);
	}
	return false;
}

function PRCheckRemoteResponse() {
	// Ricevuta la risposta dal server, la esamino
	if(req.readyState == 4) {
		if (req.status == 200) {
			var status = req.responseXML.getElementsByTagName('result')[0].childNodes[0].nodeValue;
			if(status == 1) {
				PRCodicePromo = new Array();
				PRCodicePromo[0] = req.responseXML.getElementsByTagName('result')[0].getAttribute('type');
				PRCodicePromo[1] = (1.00 * req.responseXML.getElementsByTagName('result')[0].getAttribute('value'));
			}
			PRGestisciRemoteCode(status);
		} else {
			// Risposta non consona alle attese.
			problemiLetturaXML();
		}
	}
}

function PRGestisciRemoteCode(code) {
	// Elaborazione della risposta
	switch(code) {
		case '1':
			alert(TRADSUPERSEC_js_sconto_1);
			document.ordine.g_promozione_checked.value = document.ordine.g_promozione.value;
			document.getElementById('liSconto').style.display = 'none';
			document.getElementById('promozione_table_info').style.display = '';
			document.getElementById('riepCostoSconto').innerHTML = (PRCodicePromo[0] == 'e' ? '&euro; '+PRCodicePromo[1] : PRCodicePromo[1]+' %');
			document.getElementById('riepPrezzoTotale').innerHTML = PRGetFinalDiscount();
			break;
		case '2':
			alert(TRADSUPERSEC_js_sconto_2);
			document.ordine.g_promozione.style.background = '#D68C8A';
			document.ordine.g_promozione.value = '';
			break;
		case '3':
			alert(TRADSUPERSEC_js_sconto_3);
			document.ordine.g_promozione.style.background = '#D68C8A';
			document.ordine.g_promozione.value = '';
			break;
		case '4':
			alert(TRADSUPERSEC_js_sconto_4);
			document.ordine.g_promozione.style.background = '#D68C8A';
			document.ordine.g_promozione.value = '';
			break;
	}
}

function PRGetTotalProducts() {
	var tmp = 1.00 * PRInfo[6];
	return fixed(tmp);
}

function PRGetPrice() {
	var tmp = 1.00 * PRInfo[0];
	return fixed(tmp);
}

function PRGetFinalPrice() {
	var tmp = 1.00 * (PRInfo[6] * PRInfo[0]);
	return fixed(tmp);
}

function PRGetPriceState() {
	var tmp = 1.00 * PRInfo[3];
	return fixed(tmp);
}

function PRGetVasoPrice()
{
	if (document.ordine.g_vaso_add.type == 'hidden')
		return 0;
	if (document.ordine.g_vaso_add.options[document.ordine.g_vaso_add.selectedIndex].value == '0')
		return 0;
	return PRInfo[7];
}

function PRGetFinalDiscount() {
	if(PRCodicePromo) {
		if(PRCodicePromo[0] == 'e') {
			var tot = 1.00 * (PRGetFinalPrice() - PRCodicePromo[1]);
		} else {
			var tot = 1.00 * (PRGetFinalPrice() - (PRGetFinalPrice() / 100) * PRCodicePromo[1]);
		}
	} else {
		var tot = 1.00 * PRGetFinalPrice();
	}
	tot = tot + (1.00 * PRGetPriceState()) + PRGetVasoPrice();
	return fixed(tot);
}


function PRCheckCampi(ele) {
	// Gestione della richiesta di registrazione ordine
	var d = new Date();
	var pr5 = 1.00 * PRInfo[5];
	var pr4 = 1.00 * PRInfo[4];
	if(pr5 < pr4) {
		alert(TRADSUPERSEC_js_superareMinimo+' \u20AC '+fixed(pr4));
		return false;
	}

	// Verifico i campi del mittente
   if(checkIfEmpty(ele.g_nome, TRADSUPERSEC_js_campo_vuoto)) return false;
   if(checkIfEmpty(ele.g_cognome, TRADSUPERSEC_js_campo_vuoto)) return false;
   if(checkIfEmpty(ele.g_tel, TRADSUPERSEC_js_campo_vuoto)) return false;
   if(checkIfEmpty(ele.g_email, TRADSUPERSEC_js_campo_vuoto)) return false;
   if(checkIfEmailIsWrong(ele.g_email, TRADSUPERSEC_js_email_non_valida)) return false;

	// Numero per il tracking
	if(ele.g_tracking_number.value != '')
		if(ele.g_tracking_number.value.length < 6) {
			alert(TRADSUPERSEC_js_tracking_numer_errore);
			return false;
		}

	// Verifico i campi del destinatario
   if(checkIfEmpty(ele.g_consegna_nome, TRADSUPERSEC_js_campo_vuoto)) return false;
   if(checkIfEmpty(ele.g_consegna_cognome, TRADSUPERSEC_js_campo_vuoto)) return false;
   if(checkIfEmpty(ele.g_consegna_tel, TRADSUPERSEC_js_campo_vuoto)) return false;
	switch(ele.g_dove.options[ele.g_dove.selectedIndex].value) {
		case 'in':
		case 'out_hin':
			break;
		case 'out_ita':
		   if(checkIfEmpty(ele.g_out_ita_consegna_citta, TRADSUPERSEC_js_campo_vuoto)) return false;
		   if(checkIfEmpty(ele.g_out_ita_consegna_provincia, TRADSUPERSEC_js_campo_vuoto)) return false;
			break;
		case 'out_mon':
		   if(checkIfEmpty(ele.g_out_mon_consegna_citta, TRADSUPERSEC_js_campo_vuoto)) return false;
		   if(checkIfEmpty(ele.g_out_mon_consegna_provincia, TRADSUPERSEC_js_campo_vuoto)) return false;
			break;
	}
   if(checkIfEmpty(ele.g_consegna_via, TRADSUPERSEC_js_campo_vuoto)) return false;

	// Verifico i dati per la consegna
   if((ele.g_consegna_data_mm.value < (1+d.getMonth())) && (ele.g_consegna_data_aaaa.value == d.getFullYear())) {
		alert(TRADSUPERSEC_js_data_errata);
		ele.g_consegna_data_mm.focus();
		return false;
	} else if((ele.g_consegna_data_gg.value < d.getDate()) && (ele.g_consegna_data_mm.value == (1+d.getMonth())) && (ele.g_consegna_data_aaaa.value == d.getFullYear())) {
	   alert(TRADSUPERSEC_js_data_errata);
		ele.g_consegna_data_gg.focus();
		return false;
	}
	
	if((d.getMonth()+1) == 3 && d.getFullYear() == 2007) {
		if(d.getDate() == 8) {
			if(ele.g_consegna_data_gg.value == 8 && ele.g_consegna_data_mm.value == 3 && ele.g_consegna_data_aaaa.value == 2007) {
				alert("A causa dell\'elevato numero di ordini ricevuti, la prossima data di consegna valida e' Venerdi' 9 Marzo!\nCi scusiamo per il disagio arrecato.");
				return false;
			} 
		}
	}
	
	// No Domenica 13 Maggio 2007
	if(ele.g_consegna_data_gg.value == 13 && ele.g_consegna_data_mm.value == 5 && ele.g_consegna_data_aaaa.value == 2007) {
		alert("A causa di un eccessivo quantitativo di ordini, non e\' piu\' possibile indicare la domenica come data di consegna. Inserisci come data Sabato 12 Maggio.");
		return false;
	}

	// Controllo le condizioni di vendita
   if(!ele.g_condizioni.checked) {
      alert(TRADSUPERSEC_js_non_accettato);
      return false;
   }

	ele.type_submit.disabled = true;
	ele.type_submit.value = 'Attendere ...';

   return true;
}
