/*Le varibili di errore per poterle scrivere anche in lingua*/
if(lng=="1"){
	var err_dataArrivo = "Campo 'GIORNO DELL'ARRIVO' e' obbligatorio."
	var err_nome = "Campo 'NOME DELL'AGENZIA VIAGGI O DEL GRUPPO' e' obbligatorio."
	var err_indirizzo = "Campo 'INDIRIZZO' e' obbligatorio."
	var err_cap = "Campo 'CAP' e' obbligatorio."
	var err_citta = "Campo 'CITTA' e' obbligatorio."
	var err_prov = "Campo 'PROVINCIA' e' obbligatorio."
	var err_cfVuoti = "Campo 'C.F.' per privati o aziende e' obbligatorio"
	var err_pivaVuoto = "Campo 'P.IVA' e' obbligatorio per aziende"
	var err_pivaErrato = "Campo 'P.IVA' non corretto."
	var err_cfErrato = "Campo 'CF' non corretto."
	var err_tel = "Campo 'TELEFONO' e' obbligatorio."
	var err_mailVuota = "Campo 'E-MAIL' e' obbligatorio."
	var err_mailErrata = "Indirizzo e-mail non corretto."
	var err_capogruppo = "Campo 'NOME E COGNOME CAPOGRUPPO' e' obbligatorio."
	var err_cellulare = "Campo 'CELLULARE' e' obbligatorio."
	var err_specAltro = "Specificare il tipo di gruppo."
	var err_paxVuoti = "Specificare il numero di persone."
	var err_accetto = "Si deve dare il consenso al trattamento dei dati personali."
	var erPIVA = "Errore piva"
	var erCF = "Errore cf"
	var errGardaChiuso = "Gardaland e' chiuso nella data selezionata"
	var errSeaChiuso = "SeaLife e' chiuso nella data selezionata"
	var errTuttiChiuso = "I parchi sono chiusi nella data selezionata"
}else{
	var err_dataArrivo = "The field 'ARRIVAL DAY' is required."
	var err_nome = "The field 'TRAVEL AGENCY/GROUP NAME' is required."
	var err_indirizzo = "The field 'ADDRESS' is required."
	var err_cap = "The field 'ZIP CODE' is required."
	var err_citta = "The field 'CITY' is required."
	var err_prov = "The field 'COUNTRY' is required."
	var err_tel = "The field 'PHONE' is required."
	var err_mailVuota = "The field 'E-MAIL ADDRESS' is required."
	var err_mailErrata = "The e-mail address you have provided is not correct, please ensure you have entered the e-mail address correctly."
	var err_capogruppo = "The field 'GROUP LEADER' is required."
	var err_cellulare = "The field 'MOBILE PHONE' is required."
	var err_specAltro = "Please, specify the type of group."
	var err_paxVuoti = "Please, specify the number of people."
	var err_accetto = "Please, give your consent to the processing of your personal data."
	var errGardaChiuso = "Gardaland is closed on the date selected"
	var errSeaChiuso = "SeaLife is closed on the date selected"
	var errTuttiChiuso = "The parks are closed on the date selected"
}

function isCF(valore)
{
	//Codice Fiscale
	var cfins = valore;
	var cf = cfins.toUpperCase(); 
	var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/; 

	if (cfins != "")
	{
		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;			
		}
	}
}

function ControllaPIVA(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;
}

var bgt = new biglietti()
$(document).ready(function(){
	
	dp_cal  = new Epoch('epoch_popup','popup',document.getElementById('dataarrivo'));
	//$("#nome").bind("focusin", function(){alert(controllaChiusura($("#dataarrivo").val(), arrSeaChiusure, arrGardaChiusure))})
	$("#imgdata").click(function(){$("#dataarrivo").click()})
	
	//Creo un nuovo oggetto biglietti, durante la compilazione dei campi setto i vari parametri
	bgt.setGratuita(gratuitadefault)
	bgt.setPrezzo(prezzodefault)

	if (sbianca) 
	{	
		//Al document ready svuoto tutti i campi per evitare errori
		svuotaTutto();
		$("#dataarrivo").val(dt)
		/*Imposto inizialmente i radio button 'tipogruppo' con CRAL predefinito e la textbox disanilitata*/
		$("#cral_false").attr("checked","checked")
		$("#spec_altro").attr("disabled","disabled")
		
		/*Imposto inizialmente i radio button 'tipoingresso' e i menu gardaland abilitati*/
		$("#tutti_tutti").attr("checked", "checked")
		menuGardaland("")
		//menuSealife("disabled")
	}
	else
	{
		compilaBiglietti();
		compilaMenuMix();
		compilaMenuVeloce();
		compilaMenuGran();
		//compilaMenuSeahorse();
		//compilaMenuTurtle();
		//compilaMenuShark();
		//compilaMenuStarfish();
		scriviTotale();
	}
	
	$("#cfb2c").bind("blur", function(){
		if($("#cfb2c").val()!=""){
			$("#cfb2b").val("");
			$("#piva").val("");
			$("#ec").attr("checked", "checked")
		}
	})
	$("#cfb2b").bind("blur", function(){
		if($("#cfb2b").val()!=""){
			$("#cfb2c").val("")
			$("#fatt").attr("checked", "checked")
		}
	})
	$("#piva").bind("blur", function(){
		if($("#piva").val()!=""){
			$("#cfb2c").val("")
			$("#fatt").attr("checked", "checked")
		}
	})
	/*Controllo il tipogruppo per abilitare o disabilitare la textbox corrispondente*/
	controlloTipoGruppo();
	controlloTipoIngresso();

	$("#pax").bind("change", function(){compilaBiglietti();scriviTotale();})
	
	//Quando modifico il numero di persone in ogni menu aggiorno i relativi dati
	$("#menumix").bind("change", function(){compilaMenuMix();scriviTotale();})
	$("#menuveloce").bind("change", function(){compilaMenuVeloce();scriviTotale();})
	$("#menugran").bind("change", function(){compilaMenuGran();scriviTotale();})
	
	$("#menuseahorse").bind("change", function(){compilaMenuSeahorse();scriviTotale();})
	$("#menuturtle").bind("change", function(){compilaMenuTurtle();scriviTotale();})
	$("#menushark").bind("change", function(){compilaMenuShark();scriviTotale();})
	$("#menustarfish").bind("change", function(){compilaMenuStarfish();scriviTotale();})
	
	//Nei campi numerici viene disattivattiva la digitazione di lettere 
	//tranne queste nell'array
	var editingKeys = {
		'8'  : 'backspace',
		'9'	 : 'horizontal tab',
		'46' : 'canc',
		'37' : 'leftarrow',
		'39' : 'rightarrow'
	};
	$("#dataarrivo").bind("keydown", function(e){return false;})
	$("#cellulare").bind('keydown', function(e) {
		var key = e.keyCode;
		var keynum = (key > 47) && (key < 58);   
		var keypad  = (key > 95) && (key < 106);  // tastiera numerica
		if (!keynum && !keypad) {
		return (key in editingKeys);
		}
	});
	$("#tel").bind('keydown', function(e) {
		var key = e.keyCode;
		var keynum = (key > 47) && (key < 58);   
		var keypad  = (key > 95) && (key < 106);  // tastiera numerica
		if (!keynum && !keypad) {
		return (key in editingKeys);
		}
	});
	$("#fax").bind('keydown', function(e) {
		var key = e.keyCode;
		var keynum = (key > 47) && (key < 58);   
		var keypad  = (key > 95) && (key < 106);  // tastiera numerica
		if (!keynum && !keypad) {
		return (key in editingKeys);
		}
	});
	//In inglese il cap è alfanumerico e maggiore di 5 caratteri
	if($("#lingua").val()=="1"){
		$("#cap").bind('keydown', function(e) {
			var key = e.keyCode;
			var keynum = (key > 47) && (key < 58);   
			var keypad  = (key > 95) && (key < 106);  // tastiera numerica
			if (!keynum && !keypad) {
			return (key in editingKeys);
			}
		});
		$("#cap").bind("keydown", function(e){
			var key = e.keyCode;
			var digit = $("#cap").val().length
			if(digit>4){return (key in editingKeys);}							   
		});
	}
})

function FormSubmit(){
	if(verifica()){
		document.form1.submit();
	}
	//$("#form1").submit();
}

function contatore(val)
{
  var max = 200
  if (val.note.value.length > max) {
    val.note.value = val.note.value.substring(0,max)
    rest = 0
  } 
  else
  {
    rest = max - val.note.value.length
  }
  $("#carMancanti").html(rest)
}


//dato il giorno scelto, l'array delle chiusure di sea e l'array delle chiusure di Gardaland
//ritorna un valore che può essere:
// S     = SeaLife in quel giorno è chiuso
// G     = Gardaland in quel giorno è chiuso
// SG    = Entrambi in quel giorno sono chiusi
// vuoto = sono tutti aperti
function controllaChiusura(ggScelto, arrSea, arrGarda){
	var ret = ""
	for(i=0; i<arrSea.length; i++) {
		if(arrSea[i].toUpperCase() == ggScelto.toUpperCase()){
			ret = "S"
		}
	}
	for(i=0; i<arrGarda.length; i++) {
		if(arrGarda[i].toUpperCase() == ggScelto.toUpperCase()){
			ret = ret + "G"
		}
	}
	return ret;
}

function controlloGiorniPrima(ggScelto, nrGiorni){
	var d = new Date();
	var p = new Date(ggScelto.split("/")[1] + "/" + ggScelto.split("/")[0] + "/" + ggScelto.split("/")[2]);
	if (parseInt((p-d)/86400000)>=nrGiorni){
		return false;	
	}
	return true;
}

function bloccaAdvBook(){
	if ($("#advBook_grd").attr("checked")==true){
		$("#advBook_grd").removeAttr("checked")
		$("#misti_grd").attr("checked", "checked")
	}
	if($("advBook_tutti").attr("checked")==true){
		$("#advBook_tutti").removeAttr("checked")
		$("#tutti_tutti").attr("checked", "checked")
	}
	$("#advBook_grd").attr("disabled", "disabled")
	$("#advBook_tutti").attr("disabled", "disabled")
}

function creaDivSopra(idSotto){
	var larg = $("#" + idSotto).width()
	var alt = $("#" + idSotto).height()
	var off = $("#" + idSotto).offset()
	var t = off.top
	var s = off.left
	$("<div id='copre_" + idSotto + "' style='width:" + larg + "px; height:" + alt + "px; position:absolute; background-color: white; filter:alpha(opacity=80); opacity:0.8;'><div>").insertAfter("body").offset({top: t, left: s})
}

function bloccaParchi(tipo){
	//Per prima elimino tutti i DIV
	$("#copre_selezioneSea").detach()
	$("#copre_selezioneGarda").detach()
	$("#copre_selezioneTutti").detach()
	//Reimposto il check su gardaland e sollevo l'evento click
	$("#misti_grd").attr("checked", "checked")
	$("#misti_grd").click()
	$("#pax").attr("disabled", "")
	menuGardaland("")
	$("#btnSubmit").attr("disabled", "")
	switch(tipo){
		case "S": //blocco Sea
			alert(errSeaChiuso)
			creaDivSopra("selezioneSea")
			//creaDivSopra("selezioneTutti")
			break;
		case "G": //blocco Gardaland
			alert(errGardaChiuso)
			creaDivSopra("selezioneGarda")
			//creaDivSopra("selezioneTutti")
			$("#misti_sea").attr("checked", "checked")
			$("#misti_sea").click()
			break;
		case "SG": //blocco Gardaland e Sea
			alert(errTuttiChiuso)
			creaDivSopra("selezioneGarda")
			creaDivSopra("selezioneSea")
			//creaDivSopra("selezioneTutti")
			menuGardaland("disabled")
			$("#btnSubmit").attr("disabled", "disabled")
			$("#pax").val("0")
			$("#pax").change()
			$("#pax").attr("disabled", "disabled")
			break;
		default: //Non blocco nessuno
	}
}

function svuotaTutto(){
	$("input[type=text]").val("");
	$("select").val("0");
	$("#accetto").attr("checked", "");
}
function compilaMenuMix(){
	if($("#menumix :selected").val()!="0"){
		var menuMixGrd = new biglietti() 
		menuMixGrd.setGratuita(parseInt($("#gratuita_rist").val()))
		menuMixGrd.setPrezzo(parseInt($("#prezzoMenuMix").val()))
		menuMixGrd.setNrPersone(parseInt($("#menumix :selected").val()))
		$("#grtMix").html(menuMixGrd.getGratis())
		$("#pgtMix").html(menuMixGrd.getPaganti())
		$("#totMix").html(menuMixGrd.getTotale())
	}else{
		$("#grtMix").html("")
		$("#pgtMix").html("")
		$("#totMix").html("")
	}
}
function compilaMenuVeloce(){
	if($("#menuveloce :selected").val()!="0"){
		var menuVeloceGrd = new biglietti()
		menuVeloceGrd.setPrezzo(parseInt($("#prezzoMenuVeloce").val()))
		menuVeloceGrd.setGratuita(parseInt($("#gratuita_rist").val()))
		menuVeloceGrd.setNrPersone(parseInt($("#menuveloce :selected").val()))
		$("#grtVeloce").html(menuVeloceGrd.getGratis())
		$("#pgtVeloce").html(menuVeloceGrd.getPaganti())
		$("#totVeloce").html(menuVeloceGrd.getTotale())
	}else{
		$("#grtVeloce").html("")
		$("#pgtVeloce").html("")
		$("#totVeloce").html("")
	}
}
function compilaMenuGran(){
	if($("#menugran :selected").val()!="0"){
		var menuGranGrd = new biglietti()
		menuGranGrd.setGratuita(parseInt($("#gratuita_rist").val()))
		menuGranGrd.setPrezzo(parseInt($("#prezzoMenuGran").val()))
		menuGranGrd.setNrPersone(parseInt($("#menugran :selected").val()))
		$("#grtGran").html(menuGranGrd.getGratis())
		$("#pgtGran").html(menuGranGrd.getPaganti())
		$("#totGran").html(menuGranGrd.getTotale())
	}else{
		$("#grtGran").html("")
		$("#pgtGran").html("")
		$("#totGran").html("")
	}
}
function compilaMenuSeahorse(){
	if($("#menuseahorse :selected").val()!="0"){
		var menuSeahorseSea = new biglietti()
		menuSeahorseSea.setPrezzo(parseInt($("#prezzoMenuSeahorse").val()))
		menuSeahorseSea.setGratuita(parseInt($("#gratuita_sea").val()))
		menuSeahorseSea.setNrPersone(parseInt($("#menuseahorse :selected").val()))
		$("#grtSeahorse").html(menuSeahorseSea.getGratis())
		$("#pgtSeahorse").html(menuSeahorseSea.getPaganti())
		$("#totSeahorse").html(menuSeahorseSea.getTotale())
	}else{
		$("#grtSeahorse").html("")
		$("#pgtSeahorse").html("")
		$("#totSeahorse").html("")
	}
}
function compilaMenuTurtle(){
	if($("#menuturtle :selected").val()!="0"){
		var menuTurtleSea = new biglietti()
		menuTurtleSea.setPrezzo(parseInt($("#prezzoMenuTurtle").val()))
		menuTurtleSea.setGratuita(parseInt($("#gratuita_sea").val()))
		menuTurtleSea.setNrPersone(parseInt($("#menuturtle :selected").val()))
		$("#grtTurtle").html(menuTurtleSea.getGratis())
		$("#pgtTurtle").html(menuTurtleSea.getPaganti())
		$("#totTurtle").html(menuTurtleSea.getTotale())
	}else{
		$("#grtTurtle").html("")
		$("#pgtTurtle").html("")
		$("#totTurtle").html("")
	}
}
function compilaMenuShark(){
	if($("#menushark :selected").val()!="0"){
		var menuSharkSea = new biglietti()
		menuSharkSea.setPrezzo(parseInt($("#prezzoMenuShark").val()))
		menuSharkSea.setGratuita(parseInt($("#gratuita_sea").val()))
		menuSharkSea.setNrPersone(parseInt($("#menushark :selected").val()))
		$("#grtShark").html(menuSharkSea.getGratis())
		$("#pgtShark").html(menuSharkSea.getPaganti())
		$("#totShark").html(menuSharkSea.getTotale())
	}else{
		$("#grtShark").html("")
		$("#pgtShark").html("")
		$("#totShark").html("")
	}
}
function compilaMenuStarfish(){
	if($("#menustarfish :selected").val()!="0"){
		var menuStarfishSea = new biglietti()
		menuStarfishSea.setGratuita(parseInt($("#gratuita_sea").val()))
		menuStarfishSea.setPrezzo(parseInt($("#prezzoMenuStarfish").val()))
		menuStarfishSea.setNrPersone(parseInt($("#menustarfish :selected").val()))
		$("#grtStarfish").html(menuStarfishSea.getGratis())
		$("#pgtStarfish").html(menuStarfishSea.getPaganti())
		$("#totStarfish").html(menuStarfishSea.getTotale())
	}else{
		$("#grtStarfish").html("")
		$("#pgtStarfish").html("")
		$("#totStarfish").html("")
	}
}
function compilaBiglietti(){
	bgt.setNrPersone(parseInt($("#pax :selected").val()))
	$("#gratuita").html(bgt.getGratis())
	$("#paganti").html(bgt.getPaganti())
	$("#przUnitario").html(bgt.getPrezzo())
	$("#nrPaganti").html(bgt.getPaganti())
	$("#totale").html(bgt.getTotale())
}
function scriviTotale(){
	var totBgt = parseFloat($("#totale").html())
	var totMix = parseFloat($("#totMix").html())
	var totVeloce = parseFloat($("#totVeloce").html())
	var totGran = parseFloat($("#totGran").html())
	var totSeahorse = parseFloat($("#totSeahorse").html())
	var totShark = parseFloat($("#totShark").html())
	var totTurtle = parseFloat($("#totTurtle").html())
	var totStarfish = parseFloat($("#totStarfish").html())
	if(isNaN(totBgt)){totBgt=0}
	if(isNaN(totMix)){totMix=0}
	if(isNaN(totVeloce)){totVeloce=0}
	if(isNaN(totGran)){totGran=0}
	if(isNaN(totSeahorse)){totSeahorse=0}
	if(isNaN(totShark)){totShark=0}
	if(isNaN(totTurtle)){totTurtle=0}
	if(isNaN(totStarfish)){totStarfish=0}
	$("#totaleGen").html((totBgt+totMix+totVeloce+totGran+totSeahorse+totShark+totTurtle+totStarfish).toFixed(2))
}

/*Controllo il tipogruppo per abilitare o disabilitare la textbox corrispondente*/
function controlloTipoGruppo(){
	$("input[name='tipogruppo']").bind(
	   "click",
	   function(){
			//Per ogni tipo che viene cliccato porto tutto alla situazione iniziale
			$("#misti_grd").removeAttr("disabled")
			$("#misti_sea").removeAttr("disabled")
			$("#misti_grd").attr("checked", "checked")
			$("#misti_grd").click()
			var id = $(this).attr("id")
			id = id.split("_")
			if(id[1]=="true"){
				$("#spec_altro").attr("disabled","")
				if(id[0]=="parrocchia"){
					alert(strparrocchia)
					//ho scelto parrocchia, disabilito i misti,
					//metto il check sul prezzo x parrocchia,
					//imposto il prezzo a 21 euro
					/*$("#parrocchie_grd").attr("checked", "checked")
					$("#misti_grd").attr("disabled", "disabled")
					$("#misti_sea").attr("disabled", "disabled")
					bgt.setPrezzo(21)*/
				}
			}else{
				$("#spec_altro").val("")
				$("#spec_altro").attr("disabled","disabled")
				if($("#lingua").val()=="2"){
					if(id[0]=="comune"){
						alert(strparrocchia)
						/*$("#parrocchie_grd").attr("checked", "checked")
						$("#misti_grd").attr("disabled", "disabled")
						$("#misti_sea").attr("disabled", "disabled")
						bgt.setPrezzo(21)*/
					}
				}
		}
	})
}
function controlloTipoIngresso(){
	$("input[name='tipoingresso']").bind(
									"click",
									function(){
											var id = $(this).attr("id")
											var prz = $(this).attr("prz")
											//Setto il prezzo per la classe biglietti
											bgt.setPrezzo(parseFloat(prz))
											//id = id.split("_")
											//Abilito e disabilito i menu sottostanti
											/*switch(id[1]){
												case "grd":
													menuGardaland("")
													//menuSealife("disabled")
													bgt.setGratuita(parseInt($("#gratuita_grd").val()))
													break;
												case "sea":
													menuGardaland("disabled")
													//menuSealife("")
													bgt.setGratuita(parseInt($("#gratuita_sea").val()))
													break;
												case "tutti":
													menuGardaland("")
													//menuSealife("")
													bgt.setGratuita(parseInt($("#gratuita_tutti").val()))
													break;
											}*/
											//se ho i pax selezionati ricalcolo tutti i biglietti in base alla selezione
											if($("#pax :selected").val()!="0"){
												compilaBiglietti()
												scriviTotale()
											}
													
	})
}

function menuGardaland(cmd){
	if(cmd=="disabled"){
		$("#menumix").val("0")
		$("#menuveloce").val("0")
		$("#menugran").val("0")
		compilaMenuMix()
		compilaMenuVeloce()
		compilaMenuGran()
	}
	$("#menumix").attr("disabled", cmd)
	$("#menuveloce").attr("disabled", cmd)
	$("#menugran").attr("disabled", cmd)
}
function menuSealife(cmd){
	if(cmd=="disabled"){
		$("#menuseahorse").val("0")
		$("#menushark").val("0")
		$("#menuturtle").val("0")
		$("#menustarfish").val("0")
		compilaMenuSeahorse()
		compilaMenuShark()
		compilaMenuTurtle()
		compilaMenuStarfish()
	}
	$("#menuseahorse").attr("disabled", cmd)
	$("#menushark").attr("disabled", cmd)
	$("#menuturtle").attr("disabled", cmd)
	$("#menustarfish").attr("disabled", cmd)
}

function verifica()
{
	/*Controllo che i primi campi siano stati scritti*/
	if($("#dataarrivo").val()==""){
		alert(err_dataArrivo);
		$("#nome").focus();
		return false;
	}
	if($("#nome").val()==""){
		alert(err_nome);
		$("#nome").focus();
		return false;
	}
	if($("#indirizzo").val()==""){
		alert(err_indirizzo);
		$("#indirizzo").focus();
		return false;
	}
	if($("#cap").val()==""){
		alert(err_cap);
		$("#cap").focus();
		return false;
	}
	if($("#citta").val()==""){
		alert(err_citta);
		$("#citta").focus();
		return false;
	}
	if($("#prov").val()==""){
		alert(err_prov);
		$("#prov").focus();
		return false;
	}
	
	if($("#tel").val()==""){
		alert(err_tel);
		$("#tel").focus();
		return false;
	}
	
	/*if($("#fax").val()==""){
		alert("Campo 'FAX' e' obbligatorio.");
		$("#fax").focus();
		return false;
	}*/
	
	if($("#email").val()==""){
		alert(err_mailVuota);
		$("#email").focus();
		return false;
	}else{
		var mail=$("#email").val();
		re = /@/i;
		r = mail.search(re);
		if (r < 1){
			alert(err_mailErrata);
			$("#email").focus();
			return false;
		}
	}
	
	if($("#capogruppo").val()==""){
		alert(err_capogruppo);
		$("#capogruppo").focus();
		return false;
	}
	if($("#cellulare").val()==""){
		alert(err_cellulare);
		$("#cellulare").focus();
		return false;
	}
	
	/*Se seleziono i campi Parrocchia o Altro anche la textbox deve essere obbligatoria*/
	if($("#spec_altro").attr("disabled")==false){
		if($("#spec_altro").val()==""){
			alert(err_specAltro);
			$("#spec_altro").focus();
			return false;
		}
	}
	
	/*Controllo che pax sia diverso da vuoto*/
	if($("#pax").val()=="0"){
		alert(err_paxVuoti);
		$("#pax").focus();
		return false;
	}
	
	/*Controllo dell'accettazione finale*/
	if($("#privacy").attr("checked")==false&&sbianca){
		alert(err_accetto);
		$("#privacy").focus();
		return false;
	}
	
	//Se il campo CF Privati è vuoto controllo le organizzazioni
	if($("#cfb2c").val()==""){
		//Se non è settato parrocchie/scuole
		if($("#parrocchia_true").attr("checked")==false&&$("#altro_true").attr("checked")==false){
			if($("#cfb2b").val()==""){
				alert(err_cfVuoti);
				$("#cfb2c").focus();
				return false;
			}else{
				if($("#piva").val()==""){
					alert(err_pivaVuoto)
					$("#piva").focus();
					return false;
				}else{
					//controllo se la partita iva è una partita iva
					if(ControllaPIVA($("#piva").val())==false){
						alert(err_pivaErrato)
						$("#piva").focus();
						return false;
					}
					//controllo se il codice fiscale aziende è una partita iva o un codice fiscale
					if(ControllaPIVA($("#cfb2b").val())==false){
						if(isCF($("#cfb2b").val())==false){
							alert(err_cfErrato)
							$("#cfb2b").focus();
							return false;
						}
					}
				}
			}
		}else{
			//Se sono vuoti entrambi do un'errore
			if($("#cfb2b").val()==""&&$("#piva").val()==""){
				alert(err_cfVuoti)
				return false;
			}else{
				//Se uno dei due non è vuoto faccio i controlli per vedere se è corretto
				if($("#cfb2b").val()!=""){
					if(ControllaPIVA($("#cfb2b").val())==false){
						if(isCF($("#cfb2b").val())==false){
							alert(err_cfErrato)
							$("#cfb2b").focus();
							return false;
						}
					}
				}
				if($("#piva").val()!=""){
					if(ControllaPIVA($("#piva").val())==false){
						alert(err_pivaErrato)
						$("#piva").focus();
						return false;
					}
				}
			}
		}
	}else{
		//Se non è vuoto controllo se il CF è corretto
		if(isCF($("#cfb2c").val())==false){
			alert(err_cfErrato)
			$("#cfb2c").focus();
			return false;
		}
	}
	
	return true;
}

/* ''''''''CLASSE BIGLIETTI ''''''''''''*/
//Costruttore della classe biglietti
function biglietti(gratuita, prezzo, nrPersone){
	_gratuita = gratuita
	_prezzo = prezzo
	_nrPersone = nrPersone
	
	this.setGratuita = function(grt){
		_gratuita = parseInt(grt)+1
	}	
	
	this.setPrezzo = function(prz){
		_prezzo = prz.toFixed(2)
	}

	this.setNrPersone = function(nrPers){
		_nrPersone = nrPers
	}
	
	this.getPrezzo = function(){
		return parseFloat(_prezzo).toFixed(2)	
	}
	this.getGratis = function(){
		if(_nrPersone==20){
			return 0
		}else{
			return Math.floor(_nrPersone/_gratuita)
		}
	}
	this.getPaganti = function(){
		return (_nrPersone - this.getGratis());
	}
	this.getTotale = function(){
		return parseFloat(this.getPaganti()*_prezzo).toFixed(2)
	}
}
/* ''''''''CLASSE BIGLIETTI ''''''''''''*/
