// JavaScript Document

// ===================================================================
// date selector
// ===================================================================
var today = new Date(new Date().valueOf());

function setCurrentDate() {
  // changes the date selector menus to the current date
  var currentDate = new Date();

  document.form.year.selectedIndex = 0;
  document.form.month.selectedIndex = currentDate.getMonth();

  setDays();  
  document.form.day.selectedIndex = currentDate.getDate() - 1;
}

function setDays() {

  var y = document.form.year.options[document.form.year.selectedIndex].value;
  var m = document.form.month.selectedIndex;
  var d;

  // find number of days in current month
  if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {
    days = 30;
  }
  else if (m == 1) {
    // check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
    if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
      days = 29
    else
      days = 28
  }
  else {
    days = 31;
  }


  // if (days in new month > current days) then we must add the extra days
  if (days > document.form.day.length) {
    for (i = document.form.day.length; i < days; i++) {
      document.form.day.length = days;
      document.form.day.options[i].text = i + 1;
      document.form.day.options[i].value = i + 1;
    }
  }

  
  // if (days in new month < current days) then we must delete the extra days
  if (days < document.form.day.length) {
    document.form.day.length = days;
    if (document.form.day.selectedIndex == -1) 
      document.form.day.selectedIndex = days - 1;
  }

}


// JavaScript Document
function getPageObject(sObjID) {
	if (document.all)
		return document.all[sObjID]
	else
		return document.getElementById(sObjID);

}

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

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

  check_out_m=check_in_m;
  check_out_d = check_in_d%days[check_in_m];
  check_out_y=check_in_y;
  if(check_out_d == 0) { check_out_m = (check_in_m + 1) % 12; }
  if(check_out_d == 0 && check_in_m == 11) { check_out_y++; }

  // Now set the select boxes to the appropriate dates:
  theForm.check_in_m.options[check_in_m].selected=true;
  theForm.check_in_d.options[(check_in_d-1)].selected=true;
  theForm.check_in_y.options[(check_in_y-yearOffset)].selected=true;
  theForm.check_out_m.options[check_out_m].selected=true;
  theForm.check_out_d.options[check_out_d].selected=true;
  theForm.check_out_y.options[(check_out_y-yearOffset)].selected=true;
}

function restoreDates(y1,m1,d1,y2,m2,d2)
{
 
  var checkin= new Date(y1,m1,d1);
  var checkout= new Date(y2,m2,d2);
  
  var checkin_m=checkin.getMonth(); 
  var checkin_d=checkin.getDate(); 
  var checkin_y=y2k(checkin.getYear()); 

  var checkout_m=checkout.getMonth();
  var checkout_d=checkout.getDate();
  var checkout_y=y2k(checkout.getYear());

  var td = new Date();
  var yearOffset = td.getFullYear();

  var check_in_m  = getPageObject("check_in_m");
  var check_in_d  = getPageObject("check_in_d");
  var check_in_y  = getPageObject("check_in_y");
  var check_out_m  = getPageObject("check_out_m");
  var check_out_d  = getPageObject("check_out_d");
  var check_out_y  = getPageObject("check_out_y");

  check_in_m.options[(checkin_m)].selected=true;
  check_in_d.options[(checkin_d-1)].selected=true;
  check_in_y.options[(checkin_y-yearOffset)].selected=true;
  
  check_out_m.options[(checkout_m)].selected=true;
  check_out_d.options[(checkout_d-1)].selected=true;
  check_out_y.options[(checkout_y-yearOffset)].selected=true;
  
/*
  theForm = document.fn;

   getPageObject('check_in_m').options[(checkin_m)].selected=true;
   getPageObject('check_in_d').options[(checkin_d-1)].selected=true;
   getPageObject('check_in_y').options[(checkin_y-yearOffset)].selected=true;
   
  
//  theForm.check_in_m.options[(checkin_m)].selected=true;
// theForm.check_in_d.options[(checkin_d-1)].selected=true;
//  theForm.check_in_y.options[(checkin_y-yearOffset)].selected=true;
  
   getPageObject('check_out_m').options[(checkin_m)].selected=true;
   getPageObject('check_out_d').options[(checkin_d-1)].selected=true;
   getPageObject('check_out_y').options[(checkin_y-yearOffset)].selected=true;
   
//  theForm.check_out_m.options[(checkout_m)].selected=true;
//  theForm.check_out_d.options[(checkout_d-1)].selected=true;
//  theForm.check_out_y.options[(checkout_y-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.fn;
  var yearOffset = parseInt(theForm.check_in_y.options[0].value,10);
  
  var check_in_m=parseInt(theForm.check_in_m.options[theForm.check_in_m.selectedIndex].value,10)-1;
  var check_in_y=parseInt(theForm.check_in_y.options[theForm.check_in_y.selectedIndex].value,10);
  
  var baseMonth=today.getMonth();
  var baseDay=today.getDate();
  var baseYear=y2k(today.getYear());
  
  if(isLeapYear(check_in_y)) { 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 check_in_d=parseInt(theForm.check_in_d.options[theForm.check_in_d.selectedIndex].value,10);
  
  if(check_in_d >= days[check_in_m]) { check_in_d = days[check_in_m]; }
  else { check_in_d = check_in_d%days[check_in_m]; }
  theForm.check_in_d.options[check_in_d-1].selected=true;

  var currcheck_out_m = parseInt(theForm.check_out_m.options[theForm.check_out_m.selectedIndex].value,10)-1;
  var currcheck_out_d = parseInt(theForm.check_out_d.options[theForm.check_out_d.selectedIndex].value,10);
  var currcheck_out_y = parseInt(theForm.check_out_y.options[theForm.check_out_y.selectedIndex].value,10);
  
  if((currcheck_out_y < check_in_y) || (currcheck_out_y == check_in_y && currcheck_out_m < check_in_m) ||
    (currcheck_out_y == check_in_y && currcheck_out_m == check_in_m && currcheck_out_d <= check_in_d))
  {
    check_out_m=check_in_m;
    check_out_d = check_in_d%days[check_in_m];
    check_out_y=check_in_y;
    if(check_out_d == 0) { check_out_m = (check_in_m + 1) % 12; }
    if(check_out_d == 0 && check_in_m == 11) { check_out_y++; }
  
    theForm.check_out_m.options[check_out_m].selected=true;
    theForm.check_out_d.options[check_out_d].selected=true;
    theForm.check_out_y.options[(check_out_y-yearOffset)].selected=true;
  }
  
}

function checkOutDate()
{
  theForm = document.fn;
  var yearOffset = parseInt(theForm.check_in_y.options[0].value,10);
  var check_out_m=parseInt(theForm.check_out_m.options[theForm.check_out_m.selectedIndex].value,10)-1;
  var check_out_y=parseInt(theForm.check_out_y.options[theForm.check_out_y.selectedIndex].value,10);
  
  if(isLeapYear(check_out_y)) { 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 check_out_d=parseInt(theForm.check_out_d.options[theForm.check_out_d.selectedIndex].value,10);
  
  if(check_out_d >= days[check_out_m]) { check_out_d = days[check_out_m]; }
  else { check_out_d = check_out_d%days[check_out_m]; }
  theForm.check_out_d.options[check_out_d-1].selected=true;
}