//
	theForm = document.form;
	
	
Array.prototype.indexOf = function(value){
  var i = 0;
  while(i < this.length){
    if(this[i] == value) return i;
    i++;
  }
  return -1;
}

// ===================================================================
// date selector
// ===================================================================

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;
  }

}


// date box

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

function setDates()
{
  // This will populate the date dropdowns with today and tomarrow's values.
  theForm = document.form;
  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;
}
// 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.form;
  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;


/* This is causing problems with some people
  if((check_in_m < baseMonth) || (check_in_m == baseMonth && check_in_d < baseDay))
  {
    theForm.check_in_y.options[((baseYear-yearOffset)+1)].selected=true;
    check_in_y = baseYear+1;
  }
*/
  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.form;
  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;
}


// checkForm()

function checkForm(){
	var now_time = new Date();
	var now_date = formatDate(now_time,"yyyyMMdd");
	var cfd_errors='';
	var check_in_date = document.forms[0].check_in_y.value+document.forms[0].check_in_m.value+document.forms[0].check_in_d.value;
	var check_out_date = document.forms[0].check_out_y.value+document.forms[0].check_out_m.value+document.forms[0].check_out_d.value;

	if(now_date > check_in_date){
		cfd_errors = cfd_errors+'- Check-in date you have entered is past/ too far in advance to search.\n';
	}
	if(check_in_date > check_out_date || check_in_date == check_out_date){
		cfd_errors = cfd_errors+'- Check-out date cannot be earlier than Check-in date.\n';
	}
	
	if(cfd_errors){alert(cfd_errors+'\nPlease modify your search.');}
	document.cfd_returnValue = (cfd_errors == '');
}