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.arrivalYear.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.arrivalMonth.options[inMonth].selected=true;
  theForm.arrivalDay.options[(inDay-1)].selected=true;
  theForm.arrivalYear.options[(inYear-yearOffset)].selected=true;
  theForm.departureMonth.options[outMonth].selected=true;
  theForm.departureDay.options[outDay].selected=true;
  theForm.departureYear.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.arrivalYear.options[0].value,10);
  var inMonth=parseInt(theForm.arrivalMonth.options[theForm.arrivalMonth.selectedIndex].value,10)-1;
  var inYear=parseInt(theForm.arrivalYear.options[theForm.arrivalYear.selectedIndex].value,10);
  var baseMonth=today.getMonth();
  var baseDay=today.getDate();
  var baseYear=y2k(today.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); }

  var inDay=parseInt(theForm.arrivalDay.options[theForm.arrivalDay.selectedIndex].value,10);

  if(inDay >= days[inMonth]) { inDay = days[inMonth]; }
  else { inDay = inDay%days[inMonth]; }
  theForm.arrivalDay.options[inDay-1].selected=true;

/* This is causing problems with some people
  if((inMonth < baseMonth) || (inMonth == baseMonth && inDay < baseDay))
  {
    theForm.arrivalYear.options[((baseYear-yearOffset)+1)].selected=true;
    inYear = baseYear+1;
  }
*/
  var currOutMonth = parseInt(theForm.departureMonth.options[theForm.departureMonth.selectedIndex].value,10)-1;
  var currOutDay = parseInt(theForm.departureDay.options[theForm.departureDay.selectedIndex].value,10);
  var currOutYear = parseInt(theForm.departureYear.options[theForm.departureYear.selectedIndex].value,10);

    /* if the Month is previous to the current Month, consider the user is reserving a room
       for the next year */

  if ( (inMonth < baseMonth && inYear <= baseYear) ) {
   /* || (inDay < baseDay && inYear <= baseYear && inMonth <= baseMonth)) {
      I think it is not comfortable to use this last update; if an user
      sets "yesterday" as an incoming date, it should be considered an error
      rather than that he wants to reserve a room for the next year.
   */
     theForm.arrivalYear.selectedIndex++;
     inYear = baseYear +1;
  }

  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.departureMonth.options[outMonth].selected=true;
    theForm.departureDay.options[outDay].selected=true;
    theForm.departureYear.options[(outYear-yearOffset)].selected=true;
  }
}

function checkOutDate()
{
  theForm = document.mainForm;
  var yearOffset = parseInt(theForm.arrivalYear.options[0].value,10);
  var outMonth=parseInt(theForm.departureMonth.options[theForm.departureMonth.selectedIndex].value,10)-1;
  var outYear=parseInt(theForm.departureYear.options[theForm.departureYear.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.departureDay.options[theForm.departureDay.selectedIndex].value,10);
  
  if(outDay >= days[outMonth]) { outDay = days[outMonth]; }
  else { outDay = outDay%days[outMonth]; }
  theForm.departureDay.options[outDay-1].selected=true;
}
