
function FixDecimali(obj){
  var srich;
  srich = "." ;
  var valore;
  valore = String(obj);
  var pos_punto;
  pos_punto = valore.lastIndexOf(srich)+1;
  last_punto = valore.length - pos_punto;
  // se non c'è il punto
  if(pos_punto<=0) {
      valore=valore.concat(".00");
      return(valore);
     }
  // se il punto è l'ultimo carattere
  if(last_punto==0) {
      valore = valore.concat("00");
      return(valore);
     }
  // se il punto è il penultimo carattere (1 decimale)
  if(last_punto==1) {
      valore = valore.concat("0");
      return(valore);
     }
 return(valore);
 }

function VerificaNumero(obj)
  {
    var msg1="Il valore deve essere numerico con un solo separatore di decimali";
    var msg2="Il valore puo' contenere al massimo due decimali";
    if (obj.value!='')
      {
       obj.value=obj.value.replace(",", ".");
       if (isNaN(obj.value))
          {
           window.alert(msg1);
           obj.value="";
     obj.focus();
           return false;
           }
       var srich = "." ;
       var strzero = "0" ;
       var pos_zero = obj.value.indexOf(strzero);
       if(pos_zero==0){
         obj.value = obj.value*1;
         }
       var pos_punto = obj.value.indexOf(srich);
       if(pos_punto>=0)
         {
    if(obj.value.length-pos_punto > 3)
       {
        window.alert(msg2);
        obj.focus();
          return false;
       }
    if(obj.value.length==pos_punto+1)
       { obj.value=obj.value.replace(".", ""); }
          // if( (obj.value == "0.") || (obj.value == "0.0") || (obj.value == ".0") || (obj.value == ".00"))
          //   { obj.value="0.00"; }
         }
      }
    return true;
  }

function VerificaNumeroIntero(obj)
  {
    var msg1="Il valore deve essere numerico intero";
    if (obj.value!='')
      {
       obj.value=obj.value.replace(",", ".");
       if (isNaN(obj.value))
          {
           window.alert(msg1);
           obj.value="";
           obj.focus();
           return false;
           }
       var srich = "." ;
       var strzero = "0" ;
       var pos_zero = obj.value.indexOf(strzero);
       if(pos_zero==0){
         obj.value = obj.value*1;
         }
       var pos_punto = obj.value.indexOf(srich);
       if(pos_punto>=0)
         {
           window.alert(msg1);
           obj.value="";
           obj.focus();
           return false;
         }
      }
    return true;
  }

//  verifica che un valore sia numerico intero di 4 cifre
function VerificaAnno(obj)
    {
     var msg1="Il valore deve essere numerico intero di 4 cifre";
     if (obj.value!='')
        {
        obj.value=obj.value.replace(",", ".");
        if (isNaN(obj.value))
           {
            window.alert(msg1);
            obj.value="";
            obj.focus();
            return false;
            }
        // cerca se ci sono decimali
        var srich = "." ;
        var strzero = "0" ;
        var pos_zero = obj.value.indexOf(strzero);
        if(pos_zero==0){
          obj.value = obj.value*1;
            }
        var pos_punto = obj.value.indexOf(srich);
        if(pos_punto>=0)
            {
            window.alert(msg1);
            obj.value="";
            obj.focus();
            return false;
            }
        //  verifica il range di validità del dato (anno a 4 cifre)
        if (obj.value < 1000 || obj.value > 9999)
           {
            window.alert(msg1);
            obj.value="";
            obj.focus();
            return false;
            }
        }
     return true;
    }

 function VerificaImportoPagamento(obj)
  {
  var msg1="L'importo deve essere numerico con un solo separatore di decimali";
  var msg2="L'importo puo' contenere al massimo due decimali";
  var msg3="L'importo deve essere maggiore di 0.";
  obj.value=obj.value.replace(",", ".");
  if (isNaN(obj.value))
    {
     window.alert(msg1);
     obj.value="";
     obj.focus();
     return false;
     }
  var srich = "." ;
  var strzero = "0" ;
  var pos_zero = obj.value.indexOf(strzero);
  if(pos_zero==0)
   {
     obj.value = obj.value*1;
   }
  var pos_punto = obj.value.indexOf(srich);
  if(pos_punto>=0)
   {
    if(obj.value.length-pos_punto > 3)
     {
      window.alert(msg2);
      obj.focus();
      return false;
     }
    if(obj.value.length==pos_punto+1)
     {
      obj.value=obj.value.replace(".", "");
     }
   }
  if(obj.value <= 0)
   {
     window.alert(msg3);
     obj.value="0";
     obj.focus();
     return false;
   }
   obj.value = FixDecimali(obj.value);
    return true;
  }

function VerificaAnnoIci(obj) {
    var msg="Il valore inserito non è valido";
    msg= msg + "\nEs. 2003";
    if ( isNaN(obj.value) || (parseInt(obj.value) > 2099) )
      {
       window.alert(msg);
       obj.value="";
       obj.focus();
       return false;
      }
    if (parseInt(obj.value) < 1993)
      {
       window.alert("L'anno deve essere successivo al 1992");
       obj.value="";
       obj.focus();
       return false;
      }
   }

function VerificaPercentuale(obj)
 {
  var msg="Il valore deve essere numerico, compreso tra 0 e 100";
  var msg2="Sono ammessi 2 decimali";

  if (obj.value!='')
    {
       obj.value=obj.value.replace(",", ".");
       if (isNaN(obj.value) || obj.value>100 || obj.value <0 ) {
          window.alert(msg);
          obj.value="";
    obj.focus();
          return false;
          }
       var srich = "." ;
       var pos_punto = obj.value.indexOf(srich);
       if (pos_punto==-1){
    pos_punto = 0;
    }
       if(obj.value.length-pos_punto > 3) {
           window.alert(msg2);
     obj.focus();
           return false;
     }
       if(obj.value.length==pos_punto+1) {
           obj.value=obj.value.replace(".", "");
           }

    }
 }

//controlla il formato dell'indirizzo e-mail
function controlla_mail(obj){
      var mail=obj.value;
      if (mail.length > 0) {
        var msg = "L'indirizzo e-mail non è valido.";
        var srich1 = "@" ;
  var srich2 = "." ;
  var pos1 = mail.indexOf(srich1);
  var pos2 = mail.lastIndexOf(srich2);
  if ( (pos1 <= 0) || (pos2 <= (pos1+1)) || (pos2 > (mail.length-2)) )
           {
             alert (msg);
             obj.focus();
             return false;
     }
      }
      return true;
    }

//controlla il formato data gg/mm/aaaa
function controlla_data(obj) {
     var data = obj.value;
     if (data.length > 0) {
       var ok = true;
       var first = false;
       var second = false;
       var g = 0;
       var m = 0;
       var giorno;
       var mese;
       var anno;
       var msg = "La data non è valida: controllare i valori ed il carattere di separazione\n es: GG/MM/AAAA!";
       for(i=0;i<data.length;i++) {
         if (data.charAt(i) != '/' && (data.charAt(i) < '0') || (data.charAt(i) > '9')) ok = false;
       }
       if (!ok){
         alert(msg);
         obj.focus();
         return false;
       }
       for(i=0;i<data.length;i++) {
         if (data.charAt(i) == '/' && !first) {;
           first = true;
           g = i;
         }
         else if (data.charAt(i) == '/' && first) {
           second = true;
           m = i;
         }
       }
       if (!first || !second) ok = false;
       if (!ok){
         alert(msg);
         obj.focus();
         return false;
       }
       giorno = data.substring(0,g);
       mese   = data.substring(g+1,m);
       anno   = data.substring(m+1,data.length);
       var oggi = new Date();
       if (anno < 1890){ok = false;}
       if (mese < 1 || mese > 12) ok = false;
       if (giorno < 1 || giorno > 31) ok = false;
       if (giorno > 30 && mese == 4) ok = false;
       if (giorno > 30 && mese == 6) ok = false;
       if (giorno > 30 && mese == 9) ok = false;
       if (giorno > 30 && mese == 11) ok = false;
       if (giorno > 29 && mese == 2) ok = false;
       if (giorno == 29 && mese == 2) {
         if (anno % 4 == 0) {
           if (anno % 100 == 0) {
             if (anno % 400 != 0) ok = false;
           }
         }
         else ok = false;
       }
       if (!ok){
         alert(msg);
         obj.focus();
         return false;
       }
     }
     return true;
   }

//controlla il formato dell'ora OO:MM
function controlla_ora(obj){
      var ora=obj.value;
      var msg = "L'ora indicata non è valida: controllare i valori ed il carattere di separazione\n es: OO:MM!";
      var srich1 = ":" ;
      var pos1 = ora.indexOf(srich1);
      if ( ora.length > 0 ) {
      if ( (pos1 != 1 && pos1 != 2) || ora.length != 3+pos1 )
         {
         alert (msg);
         obj.value="";
         obj.focus();
         return false;
      }
      var hh = ora.substr(0, pos1);
      var mm = ora.substr(pos1+1, 2);
      if ( isNaN(hh) || isNaN(mm) )
         {
         alert (msg);
         obj.value="";
         obj.focus();
         return false;
      }
      if ( parseInt(hh) > 23 || parseInt(mm) > 59 )
         {
         alert (msg);
         obj.value="";
         obj.focus();
         return false;
      }
    }

      return true;
    }


//controlla il numero di giorni di un anno
function controlla_ngiorni(obj)
  {
    var msg="Il valore deve essere compreso tra 1 e 365 senza decimali";
    if (obj.value!='')
      {
       // obj.value=obj.value.replace(".", ",");
       if (isNaN(obj.value) || obj.value.indexOf(".") > -1)
          {
           window.alert(msg);
           obj.value="";
         obj.focus();
           return false;
          }
       if (obj.value < 1 || obj.value > 365)
          {
           window.alert(msg);
         obj.focus();
           return false;
          }
       }
  }
