var today = new Date(new Date().valueOf());

function setDates()
{
  // This will populate the date dropdowns with today and tomarrow's values.
  theForm = document.mainForm;
  var yearOffset = parseInt(theForm.anoin.options[0].value,10);
  // getDate
  var tomorrow = new Date(new Date().valueOf() + (24*60*60*1000));
  var mesin=tomorrow.getMonth();
  var diain=tomorrow.getDate();
  var anoin=y2k(tomorrow.getYear());

  if(isLeapYear(anoin)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }

  mesout=mesin;
  diaout = diain%days[mesin];
  anoout=anoin;
  if(diaout == 0) { mesout = (mesin + 1) % 12; }
  if(diaout == 0 && mesin == 11) { anoout++; }

  // Now set the select boxes to the appropriate dates:
  theForm.mesin.options[mesin].selected=true;
  theForm.diain.options[(diain-1)].selected=true;
  theForm.anoin.options[(anoin-yearOffset)].selected=true;
  theForm.mesout.options[mesout].selected=true;
  theForm.diaout.options[diaout].selected=true;
  theForm.anoout.options[(anoout-yearOffset)].selected=true;
}
// quadYear
function y2k(number){return (number < 1000) ? number + 1900 : number;}

function isLeapYear(yr)
{
  if (((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0)) { return true; }
  else { return false; } 
}

function changeDates()
{
  theForm = document.mainForm;
  var yearOffset = parseInt(theForm.anoin.options[0].value,10);
  var mesin=parseInt(theForm.mesin.options[theForm.mesin.selectedIndex].value,10)-1;
  var anoin=parseInt(theForm.anoin.options[theForm.anoin.selectedIndex].value,10);
  var baseMonth=today.getMonth();
  var baseDay=today.getDate();
  var baseYear=y2k(today.getYear());

//  if(mesin < baseMonth && anoin <= baseYear) // original
  if(mesin < baseMonth-1 && anoin <= baseYear) // LV
  {
    anoin++;
    theForm.anoin.options[anoin - yearOffset].selected = true;
  }
  
  if(isLeapYear(anoin)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }

  var diain=parseInt(theForm.diain.options[theForm.diain.selectedIndex].value,10);
  
  if(diain >= days[mesin]) { diain = days[mesin]; }
  else { diain = diain%days[mesin]; }
  theForm.diain.options[diain-1].selected=true;
  

/* This is causing problems with some people
  if((mesin < baseMonth) || (mesin == baseMonth && diain < baseDay))
  {
    theForm.anoin.options[((baseYear-yearOffset)+1)].selected=true;
    anoin = baseYear+1;
  }
*/
  var currmesout = parseInt(theForm.mesout.options[theForm.mesout.selectedIndex].value,10)-1;
  var currdiaout = parseInt(theForm.diaout.options[theForm.diaout.selectedIndex].value,10);
  var curranoout = parseInt(theForm.anoout.options[theForm.anoout.selectedIndex].value,10);
  
  if((curranoout < anoin) || (curranoout == anoin && currmesout < mesin) ||
    (curranoout == anoin && currmesout == mesin && currdiaout <= diain))
  {
    mesout=mesin;
    diaout = diain%days[mesin];
    anoout=anoin;
    if(diaout == 0) { mesout = (mesin + 1) % 12; }
    if(diaout == 0 && mesin == 11) { anoout++; }
  
    theForm.mesout.options[mesout].selected=true;
    theForm.diaout.options[diaout].selected=true;
    theForm.anoout.options[(anoout-yearOffset)].selected=true;
  }
}

function checkOutDate()
{
  theForm = document.mainForm;
  var yearOffset = parseInt(theForm.anoin.options[0].value,10);
  var mesout=parseInt(theForm.mesout.options[theForm.mesout.selectedIndex].value,10)-1;
  var anoout=parseInt(theForm.anoout.options[theForm.anoout.selectedIndex].value,10);
  
  if(isLeapYear(anoout)) { var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31); }
  else { var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31); }

  var diaout=parseInt(theForm.diaout.options[theForm.diaout.selectedIndex].value,10);
  
  if(diaout >= days[mesout]) { diaout = days[mesout]; }
  else { diaout = diaout%days[mesout]; }
  theForm.diaout.options[diaout-1].selected=true;
}
