
function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}


function form_check_priv(){
var cognome, nome, anno, societa, codfis, indirizzo, cap, citta, provincia, telefono, email, ran, ran1, accetto, credito, bonifico, credito2, bonifico2, sesso;
var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
var espressione2 = /^[_a-zA-Z0-9+-\.\'\s]+$/;
var espressione3 = /^[0-9+-\.\s]+$/;
var espressione4 = /^[0-9]+$/;
var espressione5 = /^[_a-zA-Z0-9]+$/;
var ck = new Array();
ck[1] = "a7f1c8";
ck[2] = "b2r3ac";
ck[3] = "q2et89";
ck[4] = "p1y5h3";
ck[5] = "mh3g77";
ck[6] = "nf33t4";
ck[7] = "l2ep77";
ck[8] = "nu55h3";
ck[9] = "nu55h3";
ck[10] = "yh3na2";
ck[11] = "yv77e8";
ck[12] = "psa662";
ck[13] = "wf43l1";
ck[14] = "cxz234";
ck[15] = "ke2p37";

i=0;
cognome = trim(document.getElementById('id_cognome').value);
nome = trim(document.getElementById('id_nome').value);
anno = trim(document.getElementById('id_anno').value);
societa = trim(document.getElementById('id_societa').value);
//codfis = trim(document.getElementById('id_codfis').value);
telefono = trim(document.getElementById('id_telefono').value);
email = trim(document.getElementById('id_email').value);
indirizzo = trim(document.getElementById('id_indirizzo').value);
anno = trim(document.getElementById('id_anno').value);
cap = trim(document.getElementById('id_cap').value);
citta = trim(document.getElementById('id_citta').value);
provincia = trim(document.getElementById('id_provincia').value);

if (document.getElementById('id_sesso_m').checked == true){
sesso = "M";
}
else
{
sesso = "F";
}

//ran1 = document.getElementById('ran1').value;
ran = new String(trim(document.getElementById('id_ran').value)); 
ran1 = document.getElementById('id_ran1').value; 
accetto = document.getElementById('id_accetto').checked;
//credito = document.getElementById('id_pag_credito').checked;
//bonifico = document.getElementById('id_pag_bonifico').checked;

/*
  if (credito == false){
  credito2 = "no";
  bonifico2 = "si";
  }
  else
  {
  credito2 = "si";
  bonifico2 = "no";
  }
*/

  if ( cognome == "" || !espressione2.test(cognome) || cognome.length < 3 ){
  document.getElementById('id_cognome').style.background = "#f498ee";
  i++;
  }

  if (email == ""){
  document.getElementById('id_email').style.background = "#f498ee";
  i++;
  }
  else
  {
  	
	if (!espressione.test(email))
	{
	document.getElementById('id_email').style.background = "#f498ee";
	i++;
	}
  }
  
  if (nome == "" || !espressione2.test(nome) || nome.length < 3 ){
  document.getElementById('id_nome').style.background = "#f498ee";
  i++;
  }
  
  if (anno == "" || !espressione4.test(anno) || !(anno.length == 4) || anno < 1918 || anno > 2002){
  document.getElementById('id_anno').style.background = "#f498ee";
  i++;
  }  
  
  if ( societa == "" || !espressione2.test(societa) || societa.length < 3 ){
  document.getElementById('id_societa').style.background = "#f498ee";
  i++;
  }

  
/*
  if (codfis == ""){
  document.getElementById('id_codfis').style.background = "#f498ee";
  i++;
  }
*/
  if (telefono == "" || !espressione3.test(telefono) || telefono.length < 6){
  document.getElementById('id_telefono').style.background = "#f498ee";
  i++;
  }
  
  if (indirizzo == "" || !espressione2.test(indirizzo) || indirizzo.length < 3){
  document.getElementById('id_indirizzo').style.background = "#f498ee";
  i++;
  }
  
  if (cap == "" || !espressione4.test(cap) || cap.length < 3){
  i++;
  document.getElementById('id_cap').style.background = "#f498ee";
  }
  
  if (citta == "" || !espressione2.test(citta) || citta.length < 3){
  document.getElementById('id_citta').style.background = "#f498ee";
  i++;
  }
  
  if (provincia == "" || !espressione2.test(provincia) || provincia.length < 2){
  document.getElementById('id_provincia').style.background = "#f498ee";
  i++;
  }

  if (ran == ""){
  document.getElementById('id_ran').style.background = "#f498ee";
  i++;
  }
  else
  {
    if ( !((ran == ck[ran1]) || (ran == ck[ran1].toUpperCase())) ){
    document.getElementById('id_ran').style.background = "#f498ee";
    i++;
    }
  }
  
  
  if (accetto == false){
  //document.getElementById('id_ran').style.background = "#f498ee";
  i++;
  }
   
  if (i > 0){
  cc('id_errore', 'form_errore.php');
  }
  else
  {
  par = "cognome=" + cognome + "&nome=" + nome + "&anno=" + anno + "&societa=" + societa + "&citta=" + citta + "&telefono=" + telefono + "&email=" + email + "&indirizzo=" + indirizzo + "&cap=" + cap + "&provincia=" + provincia  + "&ran1=" + ran1 + "&ran=" + ran + "&sesso=" + sesso;
  //alert(par);  
  cc('corpo2', 'form_ver_priv_real.php', 'post', par);
  }

}

function form_del_campo(val){
  if (document.getElementById(val)){
  document.getElementById(val).style.background = "#d2d2d2";
  }
cc('id_errore', 'form_errore_vuoto.php');  
}
