// definizione delle regexp per la validazione dei campi
PAT_ANY        ="";
PAT_INT        ="/^[0-9]+$/";
PAT_FLOAT      ="/^[0-9]*[.]{0,1}[0-9]+$/";
PAT_PHONE      ="/^[+]{0,1}[ 0-9\\-\/.]+$/";
PAT_ALPHA      ="/^[, A-Za-z0-9\(\)\?_.-]+$/";
PAT_TIME       ="/^[0-2]*[0-9]:[0-5][0-9]$/";
PAT_DATE       ="date/^([0-9]{2})(-)([0-9]{2})(-)([0-9]{4})$/";
PAT_EMAIL      ="/^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+[.][A-Za-z]{2,6}$/";
PAT_MULTIEMAIL ="/^([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+[.][A-Za-z]{2,6} *; *)*[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+[.][A-Za-z]{2,6}$/";
PAT_FIELD_NAME ="/^[A-Za-z0-9]+$/";
PAT_WEB        ="";

// valori ammessi per il parametro nullok della check_field
YES_NULL =true;
NO_NULL  =false;

// controlla la sintassi del campo rispetto alla regexp passata
// se il nullok e' true il campo puo' essere vuoto
function check_field(fld,pat,nullok)
{
  // se e' permesso il valore null e il campo e' vuoto
  if (nullok==true && fld.value.length==0)
     return true;

  // se non e' permesso il valore null e' il campo e' vuoto
  if (nullok==false && fld.value.length==0) {
     alert('Il campo \''+fld.id+'\' e\' obbligatorio.');
     return false;
  }

  // altrimenti controllo il contenuto con la regular expression
  // se questa e' nulla accetto qualsiasi valore
  if (pat!='') {

     // se la regexp inizia con 'date' controllo che sia una data con l'apposita funzione
     if (pat.substring(0,4) != 'date') {
        eval("var regexp="+pat+";");
        if (regexp.test(fld.value)==false) {
           alert('Il campo \''+fld.id+'\' contiene un valore errato.');
           return false;
        }
     } else
        return(check_date(fld,pat));
  }

  return true;
}


// controlla la validita' di una data nel formato specificato
function check_date(fld,pat) {

  // tolgo i primi 4 caratteri dal pattern che mi hanno passato
  // perche' contengono la stringa 'date'
  eval("var datePat = "+pat.substring(4,pat.length)+";");

  var matchArray = fld.value.match(datePat); // is the format ok?
  if (matchArray == null) {
     alert('Il campo \''+fld.id+'\' contiene una data non valida.');
     return false;
  }
  day = matchArray[1]; // parse date into variables
  month = matchArray[3];
  year = matchArray[5];
  
  if (month < 1 || month > 12) { // check month range
     alert('Nel campo \''+fld.id+'\' il mese deve essere compreso tra 1 e 12.');
     return false;
  }
  if (day < 1 || day > 31) {
     alert('Nel campo \''+fld.id+'\' il giorno deve essere compreso tra 1 e 31.');
     return false;
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
     alert('Nel campo \''+fld.id+'\' il mese e\' errato (non ha 31 giorni).');
     return false;
  }
  if (month == 2) { // check for february 29th
     var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
     if (day>29 || (day==29 && !isleap)) {
        alert('Nel campo \''+fld.id+'\' il mese e\' errato (febbraio '+year+' non ha '+day+' giorni).');
        return false;
     }
  }
  return true;
}