/******************************
*    Form Control Script      *
*  developed by Ecoquadro.com *
*  script vers. 1.2 - 11/2009 *
******************************/

// Inizializzo delle semplici variabili di controllo

var advent = true;
var cont_camp = false;
var cont_picc = false;

// Inizializzo alcuni componenti del form della pagina

function firstpass() {

  document.getElementById("zerodue").disabled = true;
  document.getElementById("chkculla").disabled = true;
  document.getElementById("tredodici").disabled = true;
  document.getElementById("tredicisette").disabled = true;

}

// Abilito i componenti disabilitati in caso di prenotazioni con figli

function abilcamp() {

  document.getElementById("zerodue").disabled = false;
  document.getElementById("chkculla").disabled = false;
  document.getElementById("tredodici").disabled = false;
  document.getElementById("tredicisette").disabled = false;

}

// Simulo la nota funzione trim() utilizzando il prototype

String.prototype.trim = function() {

  return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	
}

// Creo la funzione empty() per controllare se una variabile è vuota, nulla, undefined, etc...

function empty( mixed_var ) {

  return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  mixed_var === undefined || mixed_var.length === 0  );
	
}

// Sub funzioni: Controllo Accettazione Privacy

function Cont_Priv() {

  var rb_scelto = false;

  if ( document.frmPrenota.scelta.checked ) {

    Cprivacy( document.frmPrenota.scelta.value );

    rb_scelto = true;
			
  }
			
  for ( i = 0; i < document.frmPrenota.scelta.length; i++ ) {
		
    if ( document.frmPrenota.scelta[i].checked ) {
	
      Cprivacy( document.frmPrenota.scelta[i].value );

      rb_scelto = true;
				
    }
			
  }
			
  if ( ! ( rb_scelto ) ) {
      
    alert("E' Obbligatorio Leggere e Accettare l'Informativa sulla Privacy.");

    return false;
      
  }

  if ( cont_camp === true ) {
  
    return true;
  
  }

}

function Cprivacy( valrad ) {

  switch ( valrad ) {
  
    case 'no':
    
      alert("E' Obbligatorio Leggere e Accettare l'Informativa sulla Privacy.");
    
      cont_camp = false;
    
    break;
    
    case 'si':

      cont_camp = true;
    
    break;
  
  }

  return ( cont_camp );

}

// Controllo Anagrafica Cliente

function Canag ( field ) {

  if ( empty( field ) ) {

    document.getElementById("anagrafica").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 2 ) {

    document.getElementById("anagrafica").style.border = "1px solid #FF0000";
    
    return false;
       
  }

  document.getElementById("anagrafica").style.border = "1px solid #C7C8BB";

  return true;

}

// Controllo Indirizzo E-Mail

function Cmail ( field ) {

  if ( empty( field ) ) {

    document.getElementById("mail").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 2 ) {

    document.getElementById("mail").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( !field.match ( /^[a-z0-9_\-\.]+@[a-z0-9]+[a-z0-9_\-\.]*\.[a-z]{2,}$/ ) || field.length > 96 ) {

    document.getElementById("mail").style.border = "1px solid #FF0000";
    
    return false;
   
  }

  document.getElementById("mail").style.border = "1px solid #C7C8BB";

  return true;

}

// Controllo la Select delle Province

function Cont_Prov ( myprov ) {

  if ( empty( myprov ) ) {
  
    document.getElementById("provincia").style.border = "1px solid #FF0000";
    return false;
       
  }
  
  document.getElementById("provincia").style.border = "1px solid #C7C8BB";
  return true;

}

// Controllo il campo Telefono

function Cont_Tel( myphone ) {

  if ( empty( myphone ) ) {
  
    document.getElementById("telefono").style.border = "1px solid #FF0000";
    return false;
       
  }
  
  if ( myphone.length < 6 && myphone.length > 0 ) {
  
    document.getElementById("telefono").style.border = "1px solid #FF0000";
    return false;
    
  }
  
  if ( myphone.match(/[^0-9]/) ) {
  
    document.getElementById("telefono").style.border = "1px solid #FF0000";
    return false;
     
  }
  
  document.getElementById("telefono").style.border = "1px solid #C7C8BB";
  return true;

}

// Controllo la Select dei Trattamenti

function Cont_Trat ( mytrat ) {

  if ( empty( mytrat ) ) {
  
    document.getElementById("trattamento").style.border = "1px solid #FF0000";
    return false;
       
  }
  
  document.getElementById("trattamento").style.border = "1px solid #C7C8BB";
  return true;

}

// Controllo il campo delle Camere

function Cont_Room ( field ) {

  if ( empty( field ) ) {

    document.getElementById("ncamere").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 1 ) {

    document.getElementById("ncamere").style.border = "1px solid #FF0000";
    
    return false;
       
  }

  document.getElementById("ncamere").style.border = "1px solid #C7C8BB";

  return true;

}

// Controllo il campo degli Adulti

function Cont_Adults ( field ) {

  if ( empty( field ) ) {

    document.getElementById("nadulti").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 1 ) {

    document.getElementById("nadulti").style.border = "1px solid #FF0000";
    
    return false;
       
  }

  document.getElementById("nadulti").style.border = "1px solid #C7C8BB";

  return true;

}

// Controllo se ci sono bambini

function Cont_Pargoli() {

  var rb_pupo = false;

  if ( document.frmPrenota.piccini.checked ) {

    CPargolo( document.frmPrenota.piccini.value );

    rb_pupo = true;
			
  }
			
  for ( i = 0; i < document.frmPrenota.piccini.length; i++ ) {
		
    if ( document.frmPrenota.piccini[i].checked ) {
	
      CPargolo( document.frmPrenota.piccini[i].value );

      rb_pupo = true;
				
    }
			
  }

  if ( cont_picc === true ) {
  
    return true;
  
  }

}

function CPargolo( valradp ) {

  switch ( valradp ) {
  
    case 'no':

      cont_picc = true;
    
    break;
    
    case 'si':
    
      var myzerodue = document.getElementById("zerodue").value.trim();
      var mytredodici = document.getElementById("tredodici").value.trim();
      var mytredici = document.getElementById("tredicisette").value.trim();
    
      if( !( Cont_Zerod( myzerodue ) ) ) {
  
        no_error = false;
  
      }
      
      if( !( Cont_Tred( mytredodici ) ) ) {
  
        no_error = false;
  
      }
      
      if( !( Cont_treDici( mytredici ) ) ) {
  
        no_error = false;
  
      }
      
      if ( no_error === true ) {
      
        cont_picc = true;
      
      } else {
      
        cont_picc = false;
      
      }
    
    break;
  
  }

  return ( cont_picc );

}

function Cont_Zerod ( field ) {

  if ( empty( field ) ) {

    document.getElementById("zerodue").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 1 ) {

    document.getElementById("zerodue").style.border = "1px solid #FF0000";
    
    return false;
       
  }

  document.getElementById("zerodue").style.border = "1px solid #C7C8BB";

  return true;

}

function Cont_Tred ( field ) {

  if ( empty( field ) ) {

    document.getElementById("tredodici").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 1 ) {

    document.getElementById("tredodici").style.border = "1px solid #FF0000";
    
    return false;
       
  }

  document.getElementById("tredodici").style.border = "1px solid #C7C8BB";

  return true;

}

function Cont_treDici ( field ) {

  if ( empty( field ) ) {

    document.getElementById("tredicisette").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 1 ) {

    document.getElementById("tredicisette").style.border = "1px solid #FF0000";
    
    return false;
       
  }

  document.getElementById("tredicisette").style.border = "1px solid #C7C8BB";

  return true;

}

function controllo_data( stringa ) {

	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;

		if ( !espressione.test( stringa ) ){

			return false;
    
    } else {
    
    	anno = parseInt( stringa.substr(6) ,10 );
    
      mese = parseInt( stringa.substr(3, 2), 10 );
    
      giorno = parseInt( stringa.substr(0, 2), 10 );
        
      var data = new Date( anno, mese-1, giorno );
      
      if( data.getFullYear() == anno && data.getMonth()+1 == mese && data.getDate() == giorno ) {

	      return true;

      } else {
      
      	return false;
      
      }
    
		}

}

function confronta_data( data1, data2, data3 ) {
	
	// controllo validità formato data

	if ( controllo_data( data1 ) && controllo_data( data2 ) && controllo_data ( data3 ) ) {

	//trasformo le date nel formato aaaammgg (es. 20081103)
		
		data1str = data1.substr(6) + data1.substr(3, 2) + data1.substr(0, 2);

		data2str = data2.substr(6) + data2.substr(3, 2) + data2.substr(0, 2);
		
		data3str = data3.substr(6) + data3.substr(3, 2) + data3.substr(0, 2);

	//controllo se la seconda data è successiva alla prima

  	if ( ( data2str - data1str ) <= 0 ) {
  	
  		alert("La data di partenza deve essere successiva alla data di arrivo");
  		return false;

  	}
  	
  	if ( ( ( data1str - data3str ) < 0 ) || ( ( data1str - data3str ) == 0 ) ) {
  	
  		alert("La data di arrivo deve essere successiva alla data attuale");
  		return false;

  	}

  } else {
        
  	alert("Il formato della data deve essere gg/mm/aaaa");
  	return false;
  	
  }
	
	return true;
	
}

// Funzione madre che richiamando altre sub funzioni controlla se ci sono o meno problemi

function cont_form() {

  if( advent ) advent = false;
		
    else {
		
      return false;
			
    }
  
  var check = true;
  
  // Assegno il contenuto dei campi di testo a delle variabili temporanee ed elimino gli spazi nei loro valori tramite la funzione trim()
  
  var mycontact = document.getElementById("anagrafica").value.trim();
  var mymail = document.getElementById("mail").value.trim();
  var myprov = document.getElementById("provincia").value.trim();
  var mytel = document.getElementById("telefono").value.trim();
  
  var mytrat = document.getElementById("trattamento").value.trim();
  var myroom = document.getElementById("ncamere").value.trim();
  var myadult = document.getElementById("nadulti").value.trim();

  var datar = document.getElementById("darrivo").value.trim();
  var datap = document.getElementById("dpartenza").value.trim();
  var datac = document.getElementById("dact").value.trim();

  if( !( Cont_Priv() ) ) {
  
    check = false;
  
  }
  
  if ( !( confronta_data( datar, datap, datac ) ) ) {
  
  	check = false;
  
  }
  
  if( !( Canag ( mycontact ) ) ) {

    check = false;

  }
  
  if( !( Cmail ( mymail ) ) ) {

    check = false;

  }
  
  if( !( Cont_Prov( myprov ) ) ) {

    check = false;
      
  }
  
  if( !( Cont_Tel( mytel ) ) ) {

    check = false;
      
  }

  if( !( Cont_Trat( mytrat ) ) ) {

    check = false;
      
  }

  if( !( Cont_Room( myroom ) ) ) {

    check = false;
      
  }
  
  if ( !( Cont_Adults( myadult ) ) ) {
  
    check = false;
  
  }

  if( !( Cont_Pargoli() ) ) {
  
    check = false;
  
  }
  
  if ( check === true ) {

    document.frmPrenota.submit();
    
    check = false;
    
  }
  
  if(!check) advent = true;
		
    return(check);
	
}	

