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.inyear.options[0].value,10);
  // getDate
  var tomorrow = new Date(new Date().valueOf() + (24*60*60*1000));
  var inmonth=tomorrow.getMonth();
  var inday=tomorrow.getDate();
  var inyear=y2k(tomorrow.getYear());

  if(isLeapYear(inyear)) { 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); }

  outmonth=inmonth;
  outday = inday%days[inmonth];
  outyear=inyear;
  if(outday == 0) { outmonth = (inmonth + 1) % 12; }
  if(outday == 0 && inmonth == 11) { outyear++; }

  // Now set the select boxes to the appropriate dates:
  theForm.inmonth.options[inmonth].selected=true;
  theForm.inday.options[(inday-1)].selected=true;
  theForm.inyear.options[(inyear-yearOffset)].selected=true;
  theForm.outmonth.options[outmonth].selected=true;
  theForm.outday.options[outday].selected=true;
  theForm.outyear.options[(outyear-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.inyear.options[0].value,10);
  var inmonth=parseInt(theForm.inmonth.options[theForm.inmonth.selectedIndex].value,10)-1;
  var inyear=parseInt(theForm.inyear.options[theForm.inyear.selectedIndex].value,10);
  var baseMonth=today.getMonth();
  var baseDay=today.getDate();
  var baseYear=y2k(today.getYear());

  if(inmonth < baseMonth && inyear <= baseYear)
  {
    inyear++;
    theForm.inyear.options[inyear - yearOffset].selected = true;
  }
  
  if(isLeapYear(inyear)) { 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 inday=parseInt(theForm.inday.options[theForm.inday.selectedIndex].value,10);
  
  if(inday >= days[inmonth]) { inday = days[inmonth]; }
  else { inday = inday%days[inmonth]; }
  theForm.inday.options[inday-1].selected=true;
  

/* This is causing problems with some people
  if((inmonth < baseMonth) || (inmonth == baseMonth && inday < baseDay))
  {
    theForm.inyear.options[((baseYear-yearOffset)+1)].selected=true;
    inyear = baseYear+1;
  }
*/
  var curroutmonth = parseInt(theForm.outmonth.options[theForm.outmonth.selectedIndex].value,10)-1;
  var curroutday = parseInt(theForm.outday.options[theForm.outday.selectedIndex].value,10);
  var curroutyear = parseInt(theForm.outyear.options[theForm.outyear.selectedIndex].value,10);
  
  if((curroutyear < inyear) || (curroutyear == inyear && curroutmonth < inmonth) ||
    (curroutyear == inyear && curroutmonth == inmonth && curroutday <= inday))
  {
    outmonth=inmonth;
    outday = inday%days[inmonth];
    outyear=inyear;
    if(outday == 0) { outmonth = (inmonth + 1) % 12; }
    if(outday == 0 && inmonth == 11) { outyear++; }
  
    theForm.outmonth.options[outmonth].selected=true;
    theForm.outday.options[outday].selected=true;
    theForm.outyear.options[(outyear-yearOffset)].selected=true;
  }
}

function checkOutDate()
{
  theForm = document.mainForm;
  var yearOffset = parseInt(theForm.inyear.options[0].value,10);
  var outmonth=parseInt(theForm.outmonth.options[theForm.outmonth.selectedIndex].value,10)-1;
  var outyear=parseInt(theForm.outyear.options[theForm.outyear.selectedIndex].value,10);
  
  if(isLeapYear(outyear)) { 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 outday=parseInt(theForm.outday.options[theForm.outday.selectedIndex].value,10);
  
  if(outday >= days[outmonth]) { outday = days[outmonth]; }
  else { outday = outday%days[outmonth]; }
  theForm.outday.options[outday-1].selected=true;
}
