function updater_standard(a,b,c,d) {
c=unescape(c);
new Ajax.Updater(a,b, {method:'post',postBody:c,evalScripts:true,success:a, asynchronous:false});
}
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 collapse(div) {
  visibile=$(div).style.display;
  if(visibile=="") {$(div).style.display='inline';}
  visibile=$(div).style.display;
//  alert(visibile)
  if(visibile=="inline") {
    $(div).hide();
  } else {
    $(div).appear();
  }
}
function pxx(lingua){
  var msg="";
  var campo=document.dati.elements;
  msg=controllo_username(msg,campo,lingua);
  msg=controllo_password(msg,campo,lingua);
  msg=controllo_fname(msg,campo,lingua);
  msg=controllo_lname(msg,campo,lingua);
  msg=controllo_address1(msg,campo,lingua);
  msg=controllo_city(msg,campo,lingua);
  msg=controllo_nazione(msg,campo,lingua);
  msg=controllo_zip(msg,campo,lingua);
  msg=controllo_Professione(msg,campo,lingua);
  msg=controllo_email(msg,campo,lingua);
  msg=controllo_phone_day(msg,campo,lingua);
  msg=controllo_codicefiscale(msg,campo,lingua);
  invio_messaggio(msg,campo,lingua);
}

function controllo_phone_day(msg,campo,lingua) {
  if(!campo.phone_day.value||campo.phone_day.value.length<6) {
    if(lingua =="it_IT") {
      msg=msg+'Telefono\n';
    } else {
      msg=msg+'Phone Number\n';
    }
  }
  return msg;
}
function controllo_codicefiscale(msg,campo,lingua) {
  if(campo.nazione.value=="Italy")  {
    if(campo.codice_fiscale.value=="" && campo.piva.value =="" )  {
      if(lingua =="it_IT") {
        msg=msg+'Codice Fiscale o Partita Iva per cittadini italiani\n';
      } else {
        msg=msg+'Fiscal Code od VAT number for italia citizen\n';
      }
    }
  }
    return msg;
}
function controllo_nazione(msg,campo,lingua) {
  if(campo.nazione.value=="--")  {
    if(lingua =="it_IT") {
      msg=msg+'Nazione\n';
    } else {
      msg=msg+'Country\n';
    }
  }
    return msg;
}

function controllo_username(msg,campo,lingua) {
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
  var checkStr = document.dati.mv_username.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) if (ch == checkOK.charAt(j)) break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid) {
    if(lingua =="it_IT") {
      msg=msg+'Caratteri alfabetici o numerici per il nome utente, non sono accettati gli spazi\n';
    } else {
      msg=msg+'Only a-z,A-Z and 0-9 chars for username, spaces not allowed \n';
    }
  }
  if(document.dati.mv_username.value.length<1 || document.dati.mv_username.value.length>16) {
    if(lingua =="it_IT") {
      msg=msg+'Lunghezza Nome Utente min 2 e max 16 caratteri\n';
    } else {
      msg=msg+'Username\'s length between 2 and 16 chars\n';
    }
  }
    return msg;
}

function controllo_password(msg,campo,lingua) {
  if(document.dati.mv_password.value.length<1 || document.dati.mv_password.value.length>20 ) {
    if(lingua =="it_IT") {
      msg=msg+'Lunghezza Password min 2 e max 20 caratteri\n';
    } else {
      msg=msg+'Password\'s length between 2 and 20 chars \n';
    }
  }
  if(document.dati.mv_password.value!=document.dati.mv_verify.value) {
    if(lingua =="it_IT") {
      msg=msg+'Password e verifica Password non corrispondono\n ';
    } else {
      msg=msg+'Password and Password Verify doesn\'t match \n';
    }
  }
    return msg;
}
function controllo_fname(msg,campo,lingua) {
  if(!campo.fname.value||campo.fname.value.length<2) {
    if(lingua =="it_IT") {
      msg=msg+'Nome\n';
    } else {
      msg=msg+'First Name\n';
    }

  }
    return msg;
}
function controllo_lname(msg,campo,lingua) {
  if(!campo.lname.value||campo.lname.value.length<2) {
    if(lingua =="it_IT") {
      msg=msg+'Cognome\n';
    } else {
      msg=msg+'Last Name\n';
    }
  }
    return msg;
}
function controllo_address1(msg,campo,lingua) {
  if(!campo.address1.value||campo.address1.value.length<4) {
    if(lingua =="it_IT") {
      msg=msg+'Indirizzo\n';
    } else {
      msg=msg+'Address\n';
    }
  }
    return msg;
}
function controllo_city(msg,campo,lingua) {
  if(!campo.city.value||campo.city.value.length<3) {
    if(lingua =="it_IT") {
      msg=msg+'Citta\'\n';
    } else {
      msg=msg+'City\n';
    }
  }
    return msg;
}
function controllo_zip(msg,campo,lingua) {
  if(!campo.zip.value||campo.zip.value.length<2) {
    if(lingua =="it_IT") {
      msg=msg+'Cap\n';
    } else {
      msg=msg+'Zip\n';
    }
  }
    return msg;
}
function controllo_Professione(msg,campo,lingua) {
  if(campo.Professione.value||campo.strumento.value||campo.Categoria.value||campo.voce.value||campo.Continente.value) {
    test1='0'
    test2='0'
    if(campo.biografia.value.length < 100 ) {
      test1='1';
    }
    if(campo.biografiaita.value.length < 100 ) {
      test2='1';
    }
    if (test1=='1' && test2=='1') {
      if(lingua =="it_IT") {
//        msg=msg+'Curriculum Incompleto e\' necessario compilare il campo Curriculum\n';
      } else {
//        msg=msg+'Incomplete curriculum infos is necessary complete some note in Curriculum Field\n';
      }
    }
  }
    return msg;
}
function controllo_email(msg,campo,lingua) {
  test_email=0;
  avviso1="";
  avviso2="";
  avviso3="";
  avviso4="";
  avviso5="";
  avviso6="";
  avviso7="";
  var emailStr=campo.email.value;
  if (campo.email.value.length < 7) {
    test_email++;
    avviso1="lunghezza";
  } else {
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
    test_email++;
    avviso2="2";
    } else {
      var user=matchArray[1]
      var domain=matchArray[2]
      if (user.match(userPat)==null) {
        // user is not valid
    test_email++;
    avviso3="3";
      }
      var IPArray=domain.match(ipDomainPat)
      if (IPArray!=null) {
        // this is an IP address
        for (var i=1;i<=4;i++) {
          if (IPArray[i]>255) {
    test_email++;
    avviso4="4";
          }
        }
      return true
      }
      var domainArray=domain.match(domainPat)
      if (domainArray==null) {
    test_email++;
    avviso5="5";
      }
      var atomPat=new RegExp(atom,"g")
      var domArr=domain.match(atomPat)
      var len=domArr.length
      if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
    test_email++;
    avviso6="6";
      }
      if (len<2) {
    test_email++;
    avviso7="7";
      }
    }
  }
 //  alert('test_email ' + test_email + '\nalert1 ' + avviso1 + ' \nalert2 ' + avviso2 + ' \nalert3 ' + avviso3 + ' \nalert4 ' + avviso4 + ' \nalert5 ' + avviso5 + ' \nalert6 ' + avviso6 + ' \nalert7 ' + avviso7);
  if(test_email >0) {
     msg=msg+'Email\n';
   } 
  return msg;
}

function invio_messaggio(msg,campo,lingua) {

  if (msg.length>0){

 if(lingua =="it_IT") {
    msg="Errori di compilazione nei seguenti campi:\n\n" + msg;
  } else {
    msg="There is some errors is the following fields:\n\n" +msg ;
  }

    alert(msg)
    return false;
  } else {

      if(lingua =="it_IT") {
    var agree=confirm("Tutti i controlli sono stati effettuati\neventuali modifiche o inserimento di dati verranno archiviati.\nLa pressione sul tasto OK implica esplicita accettazione delle\nnorme sulla tutela privacy attualmente in vigore.");
      } else {
    var agree=confirm("All checks has benn performed\n all the inserted info will be archivied.\nPressing over the Ok button is explicit manifestation of acceptance about privacy policy. ");
      }
    if (agree) {
    document.dati.submit();
// alert('ok');
    } else {
    return false;
    }
  }
}

