// global functions

function bTestEmail()
{
  var txtEmail = document.frm.mail.value;
  sEmail = new String(txtEmail);
if ((sEmail.indexOf("@") == -1) || (sEmail.indexOf(".") == -1) || (sEmail.length < 6)) {
    window.alert("L'indirizzo email non è valido.");
    document.frm.mail.value="";
    //document.frm.mail.focus();
    return(false);
  } else {
    return(true);
  }
}


function validatedateA(){
if (
(document.frm.gg_a.value!="---") &&
(document.frm.mm_a.value!="---") &&
(document.frm.year_a.value!="---")
)
{
validateA(document.frm);
}
};

function validatedateP(){
if (
(document.frm.gg_p.value!="---") &&
(document.frm.mm_p.value!="---") &&
(document.frm.year_p.value!="---")
)
{
validateP(document.frm);
}
};


function validateA(_form)
{
   var chk    = 0;
   var maxDay = 0;
   var dd = _form.gg_a.value; // day
   var mm = _form.mm_a.value; // month
   var yy = _form.year_a.value; // year

   var _dd=parseInt(dd);
   var _mm=parseInt(mm);
   var _yy=parseInt(yy);

   if(dd!=_dd){_form.gg_a.value="";}
   if(mm!=_mm){_form.mm_a.value="";}
   if(yy!=_yy){_form.year_a.value="";}

   // calling function to get maximum day for this month
   maxDay = max_day(mm, yy);  
   if(dd!=_dd || mm!=_mm || yy!=_yy)   { chk = 1;}
   else if((dd <= 0) || (dd > maxDay)) { chk = 1;}
   else if((mm <= 0) || (mm > 12))     { chk = 1;}
   else if((yy <= 0))                  { chk = 1;} 

   if(chk) { alert('La data di arrivo inserita è sbagliata!');
document.frm.year_a.selectedIndex=0;
document.frm.mm_a.selectedIndex=0;
document.frm.gg_a.selectedIndex=0;
}
  else {
if (
(document.frm.gg_a.value!="---") &&
(document.frm.mm_a.value!="---") &&
(document.frm.year_a.value!="---") &&
(document.frm.gg_p.value!="---") &&
(document.frm.mm_p.value!="---") &&
(document.frm.year_p.value!="---")
)
{// inizio istruzioni
difdate();
}
}
}

function validateP(_form)
{
   var chk    = 0;
   var maxDay = 0;
   var dd = _form.gg_p.value; // day
   var mm = _form.mm_p.value; // month
   var yy = _form.year_p.value; // year

   var _dd=parseInt(dd);
   var _mm=parseInt(mm);
   var _yy=parseInt(yy);

   if(dd!=_dd){_form.gg_p.value="";}
   if(mm!=_mm){_form.mm_p.value="";}
   if(yy!=_yy){_form.year_p.value="";}

   // calling function to get maximum day for this month
   maxDay = max_day(mm, yy);  
   if(dd!=_dd || mm!=_mm || yy!=_yy)   { chk = 1;}
   else if((dd <= 0) || (dd > maxDay)) { chk = 1;}
   else if((mm <= 0) || (mm > 12))     { chk = 1;}
   else if((yy <= 0))                  { chk = 1;} 

   if(chk) { alert( 'La data di partenza inserita è sbagliata!');
document.frm.year_p.selectedIndex=0;
document.frm.mm_p.selectedIndex=0;
document.frm.gg_p.selectedIndex=0; 
}
  else {
if (
(document.frm.gg_a.value!="---") &&
(document.frm.mm_a.value!="---") &&
(document.frm.year_a.value!="---") &&
(document.frm.gg_p.value!="---") &&
(document.frm.mm_p.value!="---") &&
(document.frm.year_p.value!="---")
)
{// inizio istruzioni
difdate();
}
}
}

// function for calculating maximum day 
function max_day(mn, yr)
{
  var mDay;
  if((mn == 4) || (mn == 6) || (mn == 9) || (mn == 11))
  { 
    mDay = 30;
  }
  else if(mn == 2)
  {
    //calling leap year function 
    mDay = isLeapYear(yr) ? 29 : 28;    
  }
  else
  {
    mDay = 31;
  }
  return mDay; 
}

// function to check leap year
function isLeapYear(yr)
{
  if      (yr % 4 != 0)   return false;
  else if (yr % 400 == 0) return true;
  else if (yr % 100 == 0) return false;
  else                    return true;
}

// function to check difference

function difdate(){

 var y_p = document.frm.year_p.value;
 var y_a = document.frm.year_a.value;

 var m_p = document.frm.mm_p.value;
 var m_a = document.frm.mm_a.value;

 var g_p = document.frm.gg_p.value;
 var g_a = document.frm.gg_a.value;

var diff_years= y_p - y_a;
var diff_mm= m_p - m_a;
var diff_gg= g_p - g_a;

if (diff_years < 0) {
alert('Attenzione! L\'anno di partenza è precedente all\'anno di arrivo');
document.frm.year_p.focus();
document.frm.year_p.selectedIndex=0;
return false;
}

if ((diff_years == 0) && (diff_mm < 0)) {
alert('Attenzione! Il mese di partenza è precedente al mese di arrivo');
document.frm.mm_p.focus();
document.frm.mm_p.selectedIndex=0;
return false;
} // end if mm


if ((diff_years == 0) && (diff_mm == 0) && (diff_gg< 0) ) {
alert('Attenzione! Il giorno di partenza è precedente al giorno di arrivo');
document.frm.gg_p.focus();
document.frm.gg_p.selectedIndex=0;
return false;
} // end if gg

else {return true;}

}// end check difference

function validate() {

 if (document.frm.nome.value==""){
 alert("per favore scrivi il nome");
 document.frm.nome.focus();
 return false;
 }
 if (document.frm.cognome.value==""){
 alert("per favore scrivi il cognome");
 document.frm.cognome.focus();
 return false;
 }
 if (document.frm.tel.value==""){
 alert("per favore scrivi il Telefono");
 document.frm.tel.focus();
 return false;
 }
 if (document.frm.mail.value==""){
 alert("per favore scrivi la Mail");
 document.frm.mail.focus();
 return false;
 }
if (document.frm.gg_a.value=="---"){
 alert("per favore scrivi il giorno della partenza");
 document.frm.gg_a.focus();
 return false;
 }
if (document.frm.mm_a.value=="---"){
 alert("per favore scrivi il mese della partenza");
 document.frm.mm_a.focus();
 return false;
 }
if (document.frm.year_a.value=="---"){
 alert("per favore scrivi l'anno della partenza");
 document.frm.year_a.focus();
 return false;
 }
if (document.frm.gg_p.value=="---"){
 alert("per favore scrivi il giorno dell'arrivo");
 document.frm.gg_p.focus();
 return false;
 }
if (document.frm.mm_p.value=="---"){
 alert("per favore scrivi il mese dell'arrivo");
 document.frm.mm_p.focus();
 return false;
 }
if (document.frm.year_p.value=="---"){
 alert("per favore scrivi l'anno dell'arrivo");
 document.frm.year_p.focus();
 return false;
 }
 if (document.frm.tipo_camera.value=="---"){
 alert("per favore scrivi il tipo di camera");
 document.frm.tipo_camera.focus();
 return false;
 }
 if (document.frm.camere.value=="---"){
 alert("per favore scrivi il numero delle camere");
 document.frm.camere.focus();
 return false;
 }
 if (document.frm.informativa.checked==false){
 alert("l'invio della mail necessita il consenso sulla privacy");
 return false;
 }

}
// end functions