/******************************
*    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;

// 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.frmContacts.scelta.checked ) {

    Cprivacy( document.frmContacts.scelta.value );

    rb_scelto = true;
			
  }
			
  for ( i = 0; i < document.frmContacts.scelta.length; i++ ) {
		
    if ( document.frmContacts.scelta[i].checked ) {
	
      Cprivacy( document.frmContacts.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("anag").style.border = "1px solid #FF0000";
    
    return false;
       
  }
   
  if ( field.length < 2 ) {

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

  document.getElementById("anag").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;

}

function Cont_Mex ( field ) {

  if ( empty( field ) ) {

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

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

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

  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("anag").value.trim();
  var mymail = document.getElementById("mail").value.trim();
  var mymex = document.getElementById("note").value.trim();

  if( !( Cont_Priv() ) ) {
  
    check = false;
  
  }
  
  if( !( Canag ( mycontact ) ) ) {

    check = false;

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

    check = false;

  }
  
  if( !( Cont_Mex( mymex ) ) ) {

    check = false;
      
  }

  if ( check === true ) {

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