
// *************************************************************
// controllo_data.js
// funzioni javascript - Controllo Data
// *************************************************************

// -------------------------------------------------------------
// controllo data con messaggi di errore
// -------------------------------------------------------------
// Campi di input : Giorno = Dato Giorno
//                  Mese   = Dato Mese
//                  Anno   = Dato Anno
//                  Nome   = Nome Data
// -------------------------------------------------------------

function Verifica_data(giorno,mese,anno,nome)

{
         numeri = /^[0-9]+$/;

         // Controllo campo Giorno
         // ----------------------------------------------------

         msg2=nome+' incompleta (Giorno mancante)';
         if (giorno == '')
         {   alert(msg2); return false; }
         msg2=nome+' errata (Giorno non numerico)';
         if (!numeri.test(giorno))
         {   alert(msg2); return false; }
         msg2=nome+' errata (Giorno errato)';
         if (giorno < 1 || giorno > 31)
         {   alert(msg2); return false; }

         // Controllo campo Mese
         // ----------------------------------------------------

         msg2=nome+' incompleta (Mese mancante)';
         if (mese == '')
         {   alert(msg2); return false; }
         msg2=nome+' errata (Mese non numerico)';
         if (!numeri.test(mese))
         {   alert(msg2); return false; }
         msg2=nome+' errata (Mese errato)';
         if (mese < 1 || mese > 12)
         {   alert(msg2); return false; }

         // Controllo campo Anno
         // ----------------------------------------------------

         msg2=nome+' incompleta (Anno mancante)';
         if (anno == '')
         {   alert(msg2); return false; }
         msg2=nome+' errata (Anno non numerico)';
         if (!numeri.test(anno))
         {   alert(msg2); return false; }
         msg2=nome+' errata (Anno errato)';
         if (anno < 1800 || anno > 2200)
         {   alert(msg2); return false; }

         // Controllo Data
         // ----------------------------------------------------

         msg2=nome+' errata';
         if ((mese==4 || mese==6 || mese==9 ||mese==11) && (giorno>30))
         {   alert(msg2); return false; }
         if (mese==2)
         {
             if (Resto_zero(anno) == true)
             {
                if (giorno > 29)
                {   alert(msg2); return false; }
             } else {
                if  (giorno > 28)
                {   alert(msg2); return false; }
             }
         }
         return true;
}

// -------------------------------------------------------------
// controllo data senza messaggi di errore
// -------------------------------------------------------------
// Campi di input : Giorno = Dato Giorno
//                  Mese   = Dato Mese
//                  Anno   = Dato Anno
// -------------------------------------------------------------

function checkdata(giorno,mese,anno)

{
         numeri = /^[0-9]+$/;

         // Controllo campo Giorno
         // ----------------------------------------------------

         if (giorno == '')
         {   return false; }
         if (!numeri.test(giorno))
         {    return false; }
         if (giorno < 1 || giorno > 31)
         {   return false; }

         // Controllo campo Mese
         // ----------------------------------------------------

         if (mese == '')
         {   return false; }
         if (!numeri.test(mese))
         {    return false; }
         if (mese < 1 || mese > 12)
         {   return false; }

         // Controllo campo Anno
         // ----------------------------------------------------

         if (anno == '')
         {   return false; }
         if (!numeri.test(anno))
         {    return false; }
         if (anno < 1900 || anno > 2200)
         {   return false; }

         // Controllo Data
         // ----------------------------------------------------

         if ((mese==4 || mese==6 || mese==9 ||mese==11) && (giorno>30))
         {    return false; }
         if (mese==2)
         {
             if (Resto_zero(anno) == true)
             {
                if (giorno > 29)
                {   return false; }
             } else {
                if  (giorno > 28)
                {    return false; }
             }
         }
         return true;
}

// -------------------------------------------------------------
// controllo anno bisestile
// -------------------------------------------------------------
function Resto_zero(numero)

{
         if (numero % 100 == 0)
         {
             if ((numero % 400) == 0) return true;
         } else {
             if ((numero % 4) == 0) return true;
         }
         return false;
}

// -------------------------------------------------------------
// controllo campo data via onBlur
// -------------------------------------------------------------
// Campi di input : dato = Campo da controllare
//                  tipo = Giorno / Mese / Anno
//                  nome = Nome Data
// -------------------------------------------------------------
   function controllo_data(dato,tipo,nome)
   {
         numeri = /^[0-9]+$/;

         stringa=dato.value;

         msg21='ATTENZIONE - '+tipo+' '+nome+' non numerico';
         msg22='ATTENZIONE - '+tipo+' '+nome+' errato'

         if (stringa!='')
         {
             if (!numeri.test(stringa))
             {
                    alert(msg21); dato.focus(); dato.select();
             } else {
				    lunghezza = stringa.length;
					if (lunghezza == 1)
					{
						stringa = '0' + stringa;
						dato.value = stringa;
					}
                    if ((tipo == 'Giorno') && (stringa < 1 || stringa > 31))
                    {
                           alert(msg22); dato.focus(); dato.select();
                    } else {
                           if ((tipo == 'Mese') && (stringa < 1 || stringa > 12))
                           {
                                  alert(msg22); dato.focus(); dato.select();
                           } else {
                                  if ((tipo == 'Anno') && (stringa < 1900 || stringa > 2200))
                                  {
                                         alert(msg22); dato.focus(); dato.select();
                                  }
                           }
                    }
             }
         }

}
