
function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function cambiaPrezzo(prodotto,prezzo,prezzo_extra,prezzo_spremuta,unita) {
	//prex = prezzo_extra.replace(/,/, ".");
	//prex = prex * 1;
	//ar presp = prezzo_spremuta.replace(/,/, ".");
	//presp = presp * 1;
	//var prest = prezzo.replace(/,/, ".");
	//prest = prest * 1;
	
	var prex = prezzo_extra;
	var presp = prezzo_spremuta;
	var prest = prezzo;

	qu = document.form_prodotto.Qualita.options[document.form_prodotto.Qualita.selectedIndex].value;
	if (qu == "extra")
	{
		//pr2 = Math.round(prex*100)/100;
		pr2 = prex;
	}
	if (qu == "spremuta")
	{
		pr2 = presp;
	}
	if (qu == "standard")
	{
		pr2 = prest;
	}
	alert("Il prezzo di "+prodotto+", qualità "+qu+", è pari a:\nEuro "+pr2+" / "+unita);
	document.getElementById("prezzo_prodotto").innerHTML = "€ "+pr2+" / "+unita;
	//document.getElementById("Prezzo").value = pr2;
}

function controllaQuantita(nomeForm) {
	for (var i=0;i<document.getElementById(nomeForm).elements.length;i++) {
			var nome = document.getElementById(nomeForm).elements[i].name;
			if(nome.indexOf('Quantita') != -1) {
				var quantita = document.getElementById(nomeForm).elements[i].value;
				if(quantita == "" || isNaN(quantita)==true || quantita.indexOf('.') != -1 || quantita.indexOf(',') != -1) {
					alert("Inserisci correttamente la quantità");
					document.getElementById(nomeForm).elements[i].focus();
					return false;
				}
				else {
					return true
				}
			}
		}
}

function controllaQuantitaCarrello() {
	for (var i=0;i<document.form_carrello.elements.length;i++) {
		var nome = document.form_carrello.elements[i].name;
		var qnt = document.form_carrello.elements[i].value;
		if(nome.indexOf('qt-') != -1) {
			if(qnt == "" || isNaN(qnt)==true || qnt.indexOf('.') != -1 || qnt.indexOf(',') != -1) {
				alert("Inserisci correttamente la quantità");
				document.form_carrello.elements[i].focus();
				event.returnValue=false;
			}
		}
	}
	document.form_carrello.action='carrello.asp?azione=update';
	document.form_carrello.submit();
}

function controllaForm(nomeForm) {
	var errEmail = false;
	var errCfManca = false;
	var errCfErrato = false;
	var errPivaManca = false;
	var errPivaErrato = false;
	var errGen = false;
	var errPrivacy = false;
	var errContratto = false;
	var errMagg = false;
	var errPaga = false;
	var pagaChecked = false;

	for (var i=0;i<document.getElementById(nomeForm).elements.length;i++) {
			var idCampo = document.getElementById(nomeForm).elements[i].id;
			var nomeCampo = document.getElementById(nomeForm).elements[i].name;
			var valoreCampo = document.getElementById(nomeForm).elements[i].value;
			
			/*
			if(nomeCampo == "cfFatt" && valoreCampo == "") {
				errCfManca = true;
			}
			if(nomeCampo == "pivaFatt" && valoreCampo == "") {
				errPivaManca = true;
			}
			if(nomeCampo == "cfFatt" && valoreCampo != "" && codiceFISCALE(valoreCampo) == false) {
				errCfErrato = true;
			}
			if(nomeCampo == "pivaFatt" && valoreCampo != "" && partitaIVA(valoreCampo) == false) {
				errPivaErrato = true;
			}
			*/
			
			if(idCampo.indexOf('@') != -1) {
				switch(nomeCampo){
					case "cfFatt":
					if(valoreCampo == "") {
						errCfManca = true;
					}
					break;
					
					case "pivaFatt":
					if(valoreCampo == "") {
						errPivaManca = true;
					}
					break;

					case "email":
					if(valoreCampo == "" || valoreCampo.indexOf('.') == -1 || valoreCampo.indexOf('@') == -1) {
						errEmail = true;
					}
					break;
					
					case "pagamento":
					for (ii=0; ii<document.getElementById(nomeForm).pagamento.length; ii++) {
						if (document.getElementById(nomeForm).pagamento[ii].checked==true) {
							pagaChecked=true;
						}
					}
					if(pagaChecked==false) {
						errPaga=true;
					}
					break;

					case "privacy":
					if(document.getElementById(nomeForm).privacy.checked == false) {
						errPrivacy = true;
					}
					break;
					
					case "contratto":
					if(document.getElementById(nomeForm).contratto.checked == false) {
						errContratto = true;
					}
					break;
					
					case "magg":
					if(document.getElementById(nomeForm).magg.checked == false) {
						errMagg = true;
					}
					
					break;
					default:
					if(valoreCampo == "" || valoreCampo.indexOf('substring') != -1) {
						errGen = true;
					}
					break;
				}
			}
		}
		if (errGen == true) {
			alert("Compilare tutti i campi obbligatori");
			return false;
		}
		else if (errEmail == true) {
			alert("Inserire un valido indirizzo email");
			return false;
		}
		else if (errCfManca == true && errPivaManca == true) {
			alert("Inserire il codice fiscale o la p. iva");
			return false;
		}
		else if (errCfErrato == true && errPivaErrato == true) {
			alert("Inserire correttamente il codice fiscale o la p. iva");
			return false;
		}
		else if (errCfErrato == true && errPivaManca == true) {
			alert("Inserire correttamente il codice fiscale");
			return false;
		}
		else if (errPivaErrato == true && errCfManca == true) {
			alert("Inserire correttamente la p. iva");
			return false;
		}
		/*
		else if (errPiva == true) {
			alert("Inserire correttamente la p. iva");
			return false;
		}
		*/
		else if (errPaga == true) {
			alert("Scegliere un metodo di pagamento");
			return false;
		}
		else if (errPrivacy == true) {
			alert("Per continuare occorre accettare le condizioni di trattamento dei dati personali");
			return false;
		}
		else if (errContratto == true) {
			alert("Per continuare occorre leggere ed accettare le condizioni di vendita, ed essere maggiorenni");
			return false;
		}
		else if (errMagg == true) {
			alert("Per continuare occorre essere maggiorenni");
			return false;
		}
		else {
			return true;
		}
}

function controllaRapido() {
	var cont = 0;
	for (var i=0;i<document.rapido.elements.length;i++) {
		var id = document.rapido.elements[i].id;
		var qnt = document.rapido.elements[i].value;
		if(id == "prodotto" && qnt != "") {
			cont++;
		}
	}
	if (cont == 0) {
		alert("Scegliere almeno un prodotto");
		return false;
	}
	else {
		return true;
	}
}

function vaiSchede() {
	categoria = document.form_prod.categoria.options[document.form_prod.categoria.selectedIndex].value;
	document.location.href = "schede_prodotti.asp?cat="+categoria;
}

function codiceFISCALE(cfins) {
   var cf = cfins.toUpperCase();
   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   if (!cfReg.test(cf))
      return false;
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var s = 0;
   for( i = 1; i <= 13; i += 2 )
      s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   for( i = 0; i <= 14; i += 2 )
      s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
      return false;
   return true;
}

function partitaIVA(pi) {
	if( pi == '' )  return false;
	if( pi.length != 11 )
		return false;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return false;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return false;
	return true;
}

function controllaCambio() {
	if(document.form_acq.nome.value != document.form_acq.nome_utente.value || document.form_acq.cognome.value || document.form_acq.cognome_utente.value && document.form_acq.indirizzo.value || document.form_acq.indirizzo_utente.value ) {
		document.getElementById("biglietto").style.display = "block";
	}
	else {
		document.getElementById("biglietto").style.display = "none";
	}
}

function vediBiglietto(cosa) {
	if(cosa == "si") {
		document.getElementById("biglietto").style.display = "block";
	}
	else if(cosa == "no") {
		document.getElementById("biglietto").style.display = "none";
	}
}

//Controlli dei campi per le puntate
function isNumberKey(evt) {
	// 31=vuoto; 48=0; 57=9;
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
	    return false;
	}
	return true;
}



	