//***************************************************
// *** Date Validation Script ***********************
//***************************************************
// *** assembled by Heaven's Martini ****************
// *** borrowed ideas from all over (www.irt.org ****
//***************************************************

// ********************************************************
// *** movebox takes the parameters of the active field ***
// *** the next field you want to auto forward to and the**
// *** length of the activefield when auto forward ********
function movebox(activefield,nextfield,maxlen)
{
  var activefieldlen = activefield.value.length;
  
   if (activefieldlen == maxlen)
    nextfield.focus();
}
// **** End if Move Box ***************************

// ************************************************
//  *** Upper Range integer function **************

function upRange(mynum,upr)
{
  var un = mynum.value;
  if (un > upr)
   return false;
  else  
   return true;
}

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

// *************************************************


function checkDay(dobj,monthobj)
{
   var mobj = monthobj.value;

  if (isNaN(dobj.value))
    {
     alert("Inserire il numero del giorno correttamente\.nTu hai invece inserito: " + mobj.value);
     mobj.value = "0";
    }

   if (mobj.length < 1)
     monthobj.focus();
   else
    {
      // ** if first day num is > 3, add a 0 to it **
     if (dobj.value.length == 1)
      if (dobj.value > 3)
       dobj.value = '0' + dobj.value;
      
     if (dobj.value.length == 2)
      {
       if (mobj == 1 || mobj == 3 || mobj == 5 || mobj == 7 || mobj == 8 || mobj == 10 || mobj == 12)
        var daymax = 31;
       else if (mobj == 2)
        var daymax = 29
       else
        var daymax = 30

       if (!upRange(dobj,daymax))
        {
         alert("Il giorno del mese non puo' essere maggiore di " + daymax);
         dobj.value = daymax;
         dobj.focus();
        }
  
      } // ** if length of field is 2, then do cool things **
    } // ** Month has correct month.
}
//*****************************************************
//***************** END OF DAY ************************

//*****************************************************
//***************** Start of MONTH ************************
//*****************************************************
function checkMonth(mobj)
{
  if (isNaN(mobj.value))
    {
     alert("Inserire il numero del mese correttamente.\nTu hai invece inserito: " + mobj.value);
     mobj.value = "0";
    }

   {
    // ** if first num is > 1, add a 0 to it **
     if (mobj.value.length == 1)
       if (mobj.value > 1)
        mobj.value = '0' + mobj.value;

     if (mobj.value.length == 2)
      {
       if (!upRange(mobj,12))
        {
         alert("Il mese non puo' essere maggiore di 12");
         mobj.value = 12;
         mobj.focus();
        }
  
      } // ** if length of field is 2, then do cool things **
    } // *** numbers only ***
}
//*****************************************************
//***************** END OF Month ************************

//*****************************************************
//***************** Start of YEAR ************************
//*****************************************************

function checkYear(yobj,mobj,dobj)
{
  if (isNaN(yobj.value))
    {
     alert("L'anno non è inserito correttamente.\nTu hai inserito: " + mobj.value);
     mobj.value = "0";
    }
  else
   {
    if (yobj.value.length == 2)
     {
       // ****** Quick Year ******
      if(yobj.value-0 < 10)
       yobj.value = "20" + yobj.value;
      if (yobj.value > 50 && yobj.value-0 < 100)
       yobj.value = "19" + yobj.value;
     } // ** if 2 numbers in validation **  

    if (yobj.value.length == 4)
     {
       // ******* Ridiculous Year Validation ******
      if (yobj.value > 2100)
       {
        alert("L'anno da te inserito non e'\n realisticamente corretto. \nTu hai inserito: " + yobj.value);
       }
      if (yobj.value < 1900)
       {
        alert("L'anno da te inserito non e'\n realisticamente corretto. \nTu hai inserito: " + yobj.value);
        yobj.value = "19";
       }
    
      if (!upRange(mobj,12))
       {
        alert("Il mese non puo' essere maggiore di 12");
        mobj.value = 12;
        mobj.focus();
       }

      if (!LeapYear(yobj.value) && (mobj.value == 2) && (dobj.value == 29))
       {
        dobj.value = 28;  
       }
      if (!upRange(dobj,31))
       {
        alert("Il giorno non puo' essere maggiore di 31");
        dobj.value = 31;
        dobj.focus();
       }
     } // *** if 4 digits in Year ***  
  } // *** numbers only ***
}

function setSel(thelist,theval)
{      
    for ( var i = 0; i < thelist.options.length; ++i )
       {
        if (thelist.options[i].value == theval)
         {
          thelist.options[i].selected = true;
          break;
         }
       }
  }
  
function LeapYear(year)
{
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function clearcombo(lt)
{
   for (var i=lt.options.length-1; i>=0; i--)
     {
       lt.options[i] = null;
     }
  lt.selectedIndex = -1;
}

function getDays(monval,dayobj)
{
  // *** clear the days list ***  
  clearcombo(dayobj);
  
  var largestDay = 31;
  
  if (monval == 2)
    largestDay = 29;
    
  if (monval == 4 || monval == 6 || monval == 9 || monval == 11)
    largestDay = 30;

  for (i=0;i<largestDay;i++)
    {
      dayobj.options[i] = new Option(i+1,i+1,true,false);  
    }
   dayobj.length = (i);
}

function checkYear(yobj,mobj,dobj)
{
  if (isNaN(yobj.value))
    {
     alert("L'anno non è inserito correttamente.\nTu hai inserito: " + yobj.value);
     yobj.focus();    
    }
  else
   {
    if (yobj.value.length == 2)
     {
      if(yobj.value-0 < 10)
       yobj.value = "20" + yobj.value;
      if (yobj.value-0 > 50 && yobj.value-0 < 100)
       yobj.value = "19" + yobj.value;
     } // ** if 2 numbers in validation **  
    if (yobj.value.length == 4)
     {
      if (yobj.value > 2100)
       {
        alert("L'anno da te inserito non e' \nrealisticamente corretto. \nTu hai inserito: " + yobj.value);
        yobj.focus();
       }
      if (yobj.value < 1900)
       {
        alert("L'anno da te inserito non e' \nrealisticamente corretto. \nTu hai inserito: " + yobj.value);
        yobj.focus();
       }
      if (!LeapYear(yobj.value) && (mobj.value == 2) && (dobj.value == 29))
       {
        dobj.focus();
        getDays(28,dobj);
        dobj.options[27].selected = true;    
       }
     } // *** if 4 digits in Year ***  
  } // *** numbers only ***
}
