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

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

  ckom=ckim;
  ckod = ckid%days[ckim];
  ckoy=ckiy;
  if(ckod == 0) { ckom = (ckim + 1) % 12; }
  if(ckod == 0 && ckim == 11) { ckoy++; }

  // Now set the select boxes to the appropriate dates:
  theForm.ckim.options[ckim].selected=true;
  theForm.ckid.options[(ckid-1)].selected=true;
  theForm.ckiy.options[(ckiy-yearOffset)].selected=true;
  theForm.ckom.options[ckom].selected=true;
  theForm.ckod.options[ckod].selected=true;
  theForm.ckoy.options[(ckoy-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.ckiy.options[0].value,10);
  var ckim=parseInt(theForm.ckim.options[theForm.ckim.selectedIndex].value,10)-1;
  var ckiy=parseInt(theForm.ckiy.options[theForm.ckiy.selectedIndex].value,10);
  var baseMonth=today.getMonth();
  var baseDay=today.getDate();
  var baseYear=y2k(today.getYear());
  
  if(isLeapYear(ckiy)) { 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 ckid=parseInt(theForm.ckid.options[theForm.ckid.selectedIndex].value,10);
  
  if(ckid >= days[ckim]) { ckid = days[ckim]; }
  else { ckid = ckid%days[ckim]; }
  theForm.ckid.options[ckid-1].selected=true;


/* This is causing problems with some people
  if((ckim < baseMonth) || (ckim == baseMonth && ckid < baseDay))
  {
    theForm.ckiy.options[((baseYear-yearOffset)+1)].selected=true;
    ckiy = baseYear+1;
  }
*/
  var currckom = parseInt(theForm.ckom.options[theForm.ckom.selectedIndex].value,10)-1;
  var currckod = parseInt(theForm.ckod.options[theForm.ckod.selectedIndex].value,10);
  var currckoy = parseInt(theForm.ckoy.options[theForm.ckoy.selectedIndex].value,10);
  
  if((currckoy < ckiy) || (currckoy == ckiy && currckom < ckim) ||
    (currckoy == ckiy && currckom == ckim && currckod <= ckid))
  {
    ckom = ckim;
    ckod = ckid%days[ckim];
    ckoy = ckiy;
    if(ckod == 0) { ckom = (ckim + 1) % 12; }
    if(ckod == 0 && ckim == 11) { ckoy++; }
  
    theForm.ckom.options[ckom].selected=true;
    theForm.ckod.options[ckod].selected=true;
    theForm.ckoy.options[(ckoy-yearOffset)].selected=true;
  }
}

function checkOutDate()
{
  theForm = document.form;
  var yearOffset = parseInt(theForm.ckiy.options[0].value,10);
  var ckom=parseInt(theForm.ckom.options[theForm.ckom.selectedIndex].value,10)-1;
  var ckoy=parseInt(theForm.ckoy.options[theForm.ckoy.selectedIndex].value,10);
  
  if(isLeapYear(ckoy)) { 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 ckod=parseInt(theForm.ckod.options[theForm.ckod.selectedIndex].value,10);
  
  if(ckod >= days[ckom]) { ckod = days[ckom]; }
  else { ckod = ckod%days[ckom]; }
  theForm.ckod.options[ckod-1].selected=true;
}


// checkForm()

function checkForm(){
	var now_time = new Date();
	var now_date = formatDate(now_time,"yyyyMMdd");
	var cfd_errors='';
	var ckidate = document.forms[0].ckiy.value+document.forms[0].ckim.value+document.forms[0].ckid.value;
	var ckodate = document.forms[0].ckoy.value+document.forms[0].ckom.value+document.forms[0].ckod.value;

	if(now_date > ckidate){
		cfd_errors = cfd_errors+'- Check-in date you have entered is past/ too far in advance to search.\n';
	}
	if(ckidate > ckodate || ckidate == ckodate){
		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 == '');
}