
//setup
(function(){
  var onl = function(){
    var links = document.getElementsByTagName('a');
    for (var i=0; i < links.length; ++i)
      if (links[i].attributes.rel && links[i].attributes.rel.value == 'new-window')
        links[i].onclick = function(){ window.open(this.href); return false; };
  };
  var prec=window.onload;
  window.onload=(prec?function(){try{prec();}catch(e){}; onl();}:onl);
})();


function validateFields(regform) {
  //var regform = document.getElementById('regform');
  if(!regform) regform = document.forms[0];

  var strErrorMsg='';
  var strnome=regform.nome.value;
  if ((strnome.length < 1) || (strnome.length > 50))  {
    strErrorMsg+='      * Nome \n';
  }

  var strcognome=regform.cognome.value;
  if ((strcognome.length < 1) || (strcognome.length > 50))  {
    strErrorMsg+='      * Cognome\n';
  }

  var strindirizzo=regform.indirizzo.value;
  if ((strindirizzo.length < 1) || (strindirizzo.length > 100))  {
    strErrorMsg+='      * Via e numero civico\n';
  }

  var strcitta=regform.citta.value;
  if ((strcitta.length < 1) || (strcitta.length > 40))  {
    strErrorMsg+='      * Cittą di residenza\n';
  }

  var strcap=regform.cap.value;
  if (!verifyCap(strcap)) {
    strErrorMsg+='      * CAP\n';
  }

  if (regform.provincia.selectedIndex == 0) {
    strErrorMsg+='      * Provincia \n';
  }

  if (verifyEmail(regform.email.value)) {
  }
  else {
    strErrorMsg+='      * Indirizzo E-mail\n';
  }

  var strtelefono=regform.telefono.value;
  if ((!regform.telefono.value))  {
    strErrorMsg+='      * Recapito telefonico\n';
  }

  var strnote=regform.note.value;
  if ((!regform.note.value))  {
    strErrorMsg+='      * Il tipo di servizio: CEPU o GRANDI SCUOLE\n';
  }

  var strprivacy=regform.privacy.value;
    if (regform.privacy.checked == false) {
    strErrorMsg+='      * Consenso all\'informativa al trattamento dei dati\n';
  }

  if (strErrorMsg!='') {
    strErrorMsg='I campi di seguito non sono stati compilati correttamente\n'+strErrorMsg;
    alert(strErrorMsg);
    return false;
  } else {
    return true;
  }
}

function submitForm(regform) {
  return validateFields(regform);
}

function verifyEmail(s) {
  var chrs = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-@';
  var sLen = s.length; var i=0, c=0, cCnt=0, step=0;
  if (sLen < 6) return false;
  while (i < sLen){
    c=s.charAt(i);
    if (!(chrs.indexOf(c)>=0 || (c=='_' && step<1))) return false;
    if (c=='.') { if (cCnt<1) return false; cCnt=0; }
    if (c=='@') { if (step>0) return false; if (cCnt<1) return false; step++; cCnt=0; }
    cCnt=cCnt+1; i++;
  }
  if (cCnt < 3 || cCnt > 5 || step==0 || (s.indexOf(".")<0) ) return false;
  return true;
}

function verifyNumeric(strInput) {
  if (strInput != '') {
    if (!strInput.match(/[^0-9]/)) return true;
  }
}

function verifyCap(strCap) {
  var bValid = false;
  var intLength = strCap.length;
  if (verifyNumeric(strCap) && intLength ==5)
    bValid = true;
  return bValid;
}
