//***************************************************************************
//  Questo file contiene le funzioni Javascript per il controllo
//  sintattico dei campi contenuti nelle form per la generazione delle
//  LAR   :
//
//  nomeOK()   controlla la sintassi del nome e del cognome
//  annoOK()   controlla l'anno di nascita
//  Data()     setta il campo con la data odierna
//  ValidateSocieta():     controlla i campi della LAR per le Società al Submit 
//  ValidateLiberi():      controlla i campi della LAR per Liberi Professionisti
//  ValidateAssociazioni():controlla i campi della LAR per le Associazioni 
//  ValidateEnti()        :controlla i campi della LAR per gli Enti Pubblici
//  ValidatePersone()     : controlla i campi della LAR per le Persone Fisiche
//
// Created by:   arianna Del Soldato
// Last changed: 10/05/2001   A. Del Soldato 
//***************************************************************************


//******************************************************************
//                             IsNumber(num)
//
// Controlla che la stringa passata come parametro sia composta
// da soli caratteri numerici.
//
//******************************************************************
function IsNumber(num)
{
   var i = 0;
   var letter;

   while (i < num.length)
   {
      letter = num.charAt(i);

      if ((letter == '0') || (letter == '1') || (letter == '2') ||
          (letter == '3') || (letter == '4') || (letter == '5') ||
          (letter == '6') || (letter == '7') || (letter == '8') ||
          (letter == '9'))
      {
         i++;
      }
      else
      {
         return false;
      }
   }

   return true;
}

//******************************************************************
//                      IsLetter(str)
//
// Controlla che la stringa passata come parametro sia composta
// da soli caratteri alfabetici.
//
//******************************************************************
function IsLetter(str)
{
   var i = 0;
   var letter = "";
   var lowerstr = str.toLowerCase();

   while (i < lowerstr.length)
   {
      letter = lowerstr.charAt(i);

      if ((letter == 'a') || (letter == 'b') || (letter == 'c') ||
          (letter == 'd') || (letter == 'e') || (letter == 'f') ||
          (letter == 'g') || (letter == 'h') || (letter == 'i') ||
          (letter == 'l') || (letter == 'm') || (letter == 'n') ||
          (letter == 'o') || (letter == 'p') || (letter == 'q') ||
          (letter == 'r') || (letter == 's') || (letter == 't') ||
          (letter == 'u') || (letter == 'v') || (letter == 'z') ||
          (letter == 'w') || (letter == 'y') || (letter == 'j') ||
          (letter == 'k') || (letter == 'x'))
      {
         i++;
      }
      else
      {
         return false;
      }
   }

   return true;
}


//******************************************************************
//                      NomeOK(campo)
//
// Controlla che la stringa contenuta nel campo passato come
// parametro contenga solo caratteri alfabetici, spazi e apostrofi
// e che eventuali apostrofi non siano isolati.
//
// IN:  campo: campo della form da controllare
// OUT: true : se la stringa contenuta nel campo è vuota o
//             se la sintassi è giusta
//      messaggio di errore, se la sintassi è sbagliata
//******************************************************************
function nomeOK(campo)
{
   var data  = campo.value;
   var size  = data.length;
   var str   = '';
   var index = -1;
   var ReturnValue = true;
   var space = 0;

   if (IsEmpty(data))
   {
      return true;
   }

   // aggiunge uno spazio in fondo alla stringa
   data = data + ' ';
   size++;

   // cerca il primo spazio
   var space = data.indexOf(' ', index);

   // fintantoche ci sono degli spazi e non c'è un errore
   while ((space != -1) && (space != '') && (ReturnValue == true))
   {
      // legge la sottostringa
      str = data.substring(index + 1, space);
      var i = 0;

      // controlla che sia formata di caratteri alfabetici con
      // l'aggiunta del carattere "'"
      while ((i < str.length) && (ReturnValue == true))
      {
         var letter = str.charAt(i);

         if (IsLetter(letter) || (letter == '\''))
         {
            i++;
         }
         else
         {
            ReturnValue = false;
         }
      }

      // controlla che non ci sia un carattere "'" seguito o
      // preceduto da uno spazio o da un altro carattere "'"
      var indexcom = str.indexOf('\'');

      if (indexcom != -1)
      {
          var substr1 = str.substring(indexcom - 1, indexcom);
          var substr2 = str.substring(indexcom + 1, indexcom + 2);

          if ((substr2 == '\'') || (substr1 == '\'') || (substr1 == ''))
             ReturnValue = false;
      }

      if ((str != '') && (ReturnValue == true))
      {
         index = space;
         space = data.indexOf(' ', index + 1);
      }
      else
      {
         ReturnValue = false;
      }
   }

   if (ReturnValue == true)
   {
      return true;
   }
   else
   {
      alert ("Valore non valido!");
      campo.value = "";
      campo.focus();
      campo.select();

      return false;
   }
}


//******************************************************************
//                      annoOK(campo)
//
// Controlla che la stringa contenuta nel campo passato come
// parametro sia formata da 4 caratteri numerici
//
// IN:  campo: campo della form da controllare
// OUT: true : se la stringa contenuta nel campo è vuota o
//             se la sintassi è giusta
//      messaggio di errore, se la sintassi è sbagliata
//******************************************************************

function annoOK(campo)
{
   var str = campo.value;
   var OK  = true;
   var char = '';
   var len  = str.length;

   if (len != 4)
   {
      OK = false;
   }
   else
   {
      var i = 0;
      while ((i < 4) && OK)
      {
         char = str.charAt(i);
         OK = IsNumber(char);
         i++;
      }
   }

   if (OK == true)
   {
      return true;
   }
   else
   {
      alert ("Valore non valido nel campo 'anno'!");
      campo.value = "";
      campo.focus();
      campo.select();

      return false;
   }

}


//*******************************************************************
//                           IsEmpty()
//
// Controlla che la stringa passata come parametro contenga almeno un
// carattere numerico o alfabetico.
//
// IN  - str: stringa da controllare
//*******************************************************************
function IsEmpty(str)
{
   var i    = 0;
   var ok   = false;
   var tmpchar = "";

   // se la stringa non è vuota
   if (str != "")
   {
      // controlla che ci sia almeno una lettera o un numero
      while ((i < str.length) && (!ok))
      {
         tmpchar = str.charAt(i);
         if (IsLetter(tmpchar) || IsNumber(tmpchar))
         {
            ok = true;
         }

         i++;
      }
   }

   if (ok)
   {
      return false;
   }
   else
   {
      return true;
   }
}


//******************************************************************
//                              Data()
//
// Calcola la data odierna nel formato: gg-mm-aaaa
//
//******************************************************************
function Data()
{
   var mydate = "";
   var now    = new Date();

   var day    = now.getDate();
   var month  = now.getMonth() + 1;
   var year   = now.getYear();

   // correzione per gestione millennium bag
   year += (year < 2000) ? 1900 : 0;

   mydate += ((day < 10)?"0":"") + day + '-';
   mydate += ((month < 10)?"0":"") + month + '-';
   mydate += year;
 
   return mydate;
}

function FixNA(check, campo)
{
   if (check[1].checked)
   {
       campo.value = 'N.A.';
   }
}


//******************************************************************
//                              ValidateSocieta
// controlla i campi obbligatori della form per la compilazione 
// della LAR Società - Ditte
//******************************************************************

function ValidateSocieta()
{
   var index = 0;
   var errore = false;

   // controlla che il nome della società sia uguale ovunque
   if ((document.societa.ente.value != document.societa.enteClausola.value) && 
       (document.societa.clausola[0].checked))
   {
      alert ("Valore non valido nel campo 'società'!");
      return false;
   }

   // controlla che tutti i dati della form siano 
   // inseriti in quanto tutti obbligatori
   var length = document.societa.elements.length;

   // per ogni elemento della form
   while ((index < length) && !errore)
   {
      // legge il valore del campo
      var value = document.societa.elements[index].value;
      var name = document.societa.elements[index].name;

      if ((name != 'sottoscritto') && (name != 'stato') && (name != 'clausola') && (name != 'enteClausola'))
      { 
         errore = (IsEmpty(value));
      }
      index++;
   }
   if (errore)
   {
      alert ("Il form non è stato compilato correttamente!");
      return false;
   }
   else
   {
      return true;
   }
}

//******************************************************************
//                              ValidateLiberi
// controlla i campi obbligatori della form per la compilazione
// della LAR Società - Ditte
//******************************************************************

function ValidateLiberi()
{
   var index = 0;
   var errore = false;

   // controlla che tutti i dati della form siano
   // inseriti in quanto tutti obbligatori
   var length = document.liberi.elements.length;

   // per ogni elemento della form
   while ((index < length) && !errore)
   {
      // legge il valore del campo
      var value = document.liberi.elements[index].value;
      var name  = document.liberi.elements[index].name; 

      if ((name != 'numPIVA') && (name != 'albo') && (name != 'provincia') && (name != 'numalbo') && (name != 'motivazione') && (name != 'possessoIVA') && (name != 'clausola'))
      {
         errore = (IsEmpty(value));
      }
 
      index++;
   }
   
   if (!errore)
   {
      var albo = document.liberi.albo.value;
      var provincia = document.liberi.provincia.value;
      var numalbo = document.liberi.numalbo.value;
      var motivazione = document.liberi.motivazione.value;

      // controlla i dati opzionali
      if (document.liberi.possessoIVA.options[0].selected && IsEmpty(document.liberi.numPIVA.value))
      {
         alert ("Il form non è stato compilato correttamente!");
         return false;
      }

      if ( document.liberi.iscrizionealbo[1].checked && 
           ( (albo != '') || (provincia != 'N.A.') || (numalbo != 'N.A.') || IsEmpty(motivazione) ) )
      {
         alert ("Il form non è stato compilato correttamente!");
         return false;
      }
      if (document.liberi.iscrizionealbo[0].checked && ( (motivazione != '') || IsEmpty(albo) || IsEmpty(provincia) || IsEmpty(numalbo)))
      {
         alert ("Il form non è stato compilato correttamente!");
         return false;
      }
   }

   if (errore)
   {
      alert ("Il form non è stato compilato correttamente!");
      return false;
   }
   else
   {
      return true;
   }
}


//******************************************************************
//                              ValidateAssociazioni
// controlla i campi obbligatori della form per la compilazione
// della LAR Associazioni 
//******************************************************************

function ValidateAssociazioni()
{
   var index = 0;
   var errore = false;

   // controlla che il nome della società sia uguale ovunque
   if ((document.associazioni.ente.value != document.associazioni.enteClausola.value) &&
       (document.associazioni.clausola[0].checked))
   {
      alert ("Il form non è stato compilato correttamente!");
      return false;
   }
   
   // controlla che tutti i dati della form siano
   // inseriti 
   var length = document.associazioni.elements.length;
   
   // per ogni elemento della form
   while ((index < length) && !errore)
   {
      // legge il valore del campo
      var value = document.associazioni.elements[index].value;
      var name = document.associazioni.elements[index].name;

      if ((name != 'datareg') && (name != 'tribunale') && (name != 'numreg') && (name != 'numPIVA') && (name != 'PIVA') && (name != 'clausola') && (name != 'enteClausola'))
      {
         errore = (IsEmpty(value));
      }

      index++;
   }

   var datareg = document.associazioni.datareg.value;
   var tribunale = document.associazioni.tribunale.value;
   var numreg = document.associazioni.numreg.value;

   if (!errore)
   {
      if ( document.associazioni.registrata[0].checked &&
           ( ((datareg != 'N.A.') && !IsEmpty(datareg)) || ((tribunale != 'N.A.') && !IsEmpty(tribunale)) || ((numreg != 'N.A.') && !IsEmpty(numreg)) ) )
      {
         alert ("Inconsistent data!");
         return false;
      }
      var checked = document.associazioni.registrata[0].checked;
      var numPIVA = document.associazioni.numPIVA.value; 
      if (document.associazioni.registrata[0].checked && IsEmpty(document.associazioni.numPIVA.value)) 
      {
         alert ("Il form non è stato compilato correttamente!");
         return false;
      }
      if (document.associazioni.registrata[1].checked && ( IsEmpty(datareg) || IsEmpty(tribunale) || IsEmpty(numreg)))
      {
         alert ("Il form non è stato compilato correttamente!");
         return false;
      }
   }

   if (errore)
   {
      alert ("Il form non è stato compilato correttamente!");
      return false;
   }
   else
   {
      return true;
   }
}


//******************************************************************
//                              ValidateEnti
// controlla i campi obbligatori della form per la compilazione
// della LAR Enti Pubblici
//******************************************************************

function ValidateEnti()
{
   var index = 0;
   var errore = false;

   // controlla che tutti i dati della form siano
   // inseriti in quanto tutti obbligatori
   var length = document.amministrazioni.elements.length;

   var Valnomeente = document.amministrazioni.nomeente.value;
   var ValenteClausola = document.amministrazioni.enteClausola.value;

   // controlla che il nome della società sia uguale ovunque
   if ((Valnomeente != ValenteClausola) && (document.amministrazioni.clausola[0].checked))
   {
      alert ("Il campo 'Ente' non è stato compilato correttamente!");
      return false;
   }

   // per ogni elemento della form
   while ((index < length) && !errore)
   {
      // legge il valore del campo
      var value = document.amministrazioni.elements[index].value;
      var name = document.amministrazioni.elements[index].name;

      if ((name != 'numlegge') && (name != 'datalegge') && (name != 'numPIVA') && 
          (name != 'ente') && (name != 'istituito') && (name != 'PIVA') && 
          (name != 'rientra') && (name != 'clausola') && (name != 'enteClausola'))
      { 
         errore = (IsEmpty(value));
      }
      index++;
   }

   if (!errore)
   {
      var numPIVA = document.amministrazioni.numPIVA.value;
      var numlegge = document.amministrazioni.numlegge.value;
      var datalegge = document.amministrazioni.datalegge.value;

      if ( IsEmpty(numPIVA) && ( IsEmpty(numlegge) || IsEmpty(datalegge) ) )
      {
         errore = true;
      }

      if ((IsEmpty(numlegge) && !IsEmpty(datalegge)) || (!IsEmpty(numlegge) && IsEmpty(datalegge)) )
      {
         alert ("Il form non è stato compilato correttamente!");
         return false;
      }
   }
 
   if (errore)
   {
      alert ("Il form non è stato compilato correttamente!");
      return false;
   }
   else
   {
      return true;
   }
}

//******************************************************************
//                              ValidatePersone
// controlla i campi obbligatori della form per la compilazione
// della LAR Persone Fisiche 
//******************************************************************

function ValidatePersone()
{
   var index = 0;
   var errore = false;

   // controlla che tutti i dati della form siano
   // inseriti in quanto tutti obbligatori
   var length = document.persone.elements.length;

   // per ogni elemento della form
   while ((index < length) && !errore)
   {
      // legge il valore del campo
      var value = document.persone.elements[index].value;
      var name = document.persone.elements[index].name;

      if ((name != 'clausola') && (name != 'giorno') && (name != 'mese') && 
          (name != 'sesso') && (name != 'nazione') )
      { 
         errore = (IsEmpty(value));
      }
      
      index++;
   }
   if (errore)
   {
      alert ("Il form non è stato compilato correttamente!");
      return false;
   }
   else
   {
      // ---------------------------------------
      // controllo sintattico del odice fiscale
      // ---------------------------------------

      
      return true;
   }
}

function isCodFis(field) 
{ 
    var cf = field.value.toUpperCase();

    var nome = document.persone.nome.value;
    var cognome = document.persone.cognome.value;
    var gg = document.persone.giorno.value;
    var mm = document.persone.mese.value;
    var aa = document.persone.anno.value;
    var sesso = selectsesso;
    var ccomune = document.persone.codcom.value;
    var cstato = document.persone.nazione.value;

    // Return false if country field is blank.
    if (cf == "") 
    {
        return true;
    }

    // Return false if characters are not a-z, A-Z, 0-9.
    for (var i = 0; i < cf.length; i++) 
    {
        var ch = cf.substring(i, i + 1);
        if (((ch < "A" || "Z" < ch) && (ch < "0" || "9" < ch)) && ch != ' ') 
        {
            alert("\nIl campo accetta solo lettere maiuscole e cifre 0-9.\n\nPrego reinserire il Codice Fiscale del paziente.");
            field.select();
            field.focus();
            return false;
        }
    }

    pattern = /(\w{3})(\w{3})(\w\w)(\w)(\w\w)(\w{4})(\w)/;
    if ((parti = cf.match (pattern)) == null) 
    {
        alert ("Il Codice Fiscale non e' nella forma giusta");
        field.select;
        field.focus;
        return false;
    }

}


