rightnow = new Date();
//write dates to dropdown
function month(i,name) {
	var thisyear = rightnow.getFullYear();
	var nextyear = thisyear + 1;
	var a = "<option value=\"";
	(i<rightnow.getMonth()) ? a +=nextyear : a +=thisyear;
	(i<9) ? a += "0"+(i+1) : a+=(i+1);
	a += "\">"+name+" 0";
	(i<rightnow.getMonth()) ? a +=(nextyear-2000) : a +=(thisyear-2000);
	a += "</option>\n";
	this.option = a;
}													
var months = new Array(new month(0,"Jan"), new month(1,"Feb"),new month(2,"Mar"),new month(3,"Apr"),new month(4,"May"), new month(5,"Jun"), new month(6,"Jul"), new month(7,"Aug"), new month(8,"Sep"), new month(9,"Oct"), new month(10,"Nov"), new month(11,"Dec"));

function setDest() {
	document.hotelOutput.destinationNameCheck.value = document.hotelOutput.destinationName.value;
}

function hotelSubmit(){
	var f1 = document.hotel
	var f2 = document.hotelOutput
	monthYear = f1.checkInDateB.options[f1.checkInDateB.selectedIndex].value;
	dayVal = f1.checkInDateA.options[f1.checkInDateA.selectedIndex].value;
	monthVal= monthYear.substr(4,2);
	yearVal = monthYear.substring(0,4);
				
	secMonthYear = f1.depDateB.options[f1.depDateB.selectedIndex].value;
	secDayVal = f1.depDateA.options[f1.depDateA.selectedIndex].value;
	secMonthVal = secMonthYear.substr(4,2);
	secYearVal = secMonthYear.substring(0,4);
	
	if (document.hotelOutput.destinationNameCheck.value == "") {
		alert("Please enter a destination");
		document.getElementById("hotelSearch").style.color="#FF3300";
		return false;			
	}
	
	var date = new Date(yearVal, monthVal-1, dayVal);
	var secdate = new Date(secYearVal, secMonthVal-1, secDayVal);
	var theDate = date.getDate();
	var theSecDate = secdate.getDate();
	var aCity = f2.destinationName.value;
	if (secdate <= date) {
		alert("Please enter valid arrival and departure dates");
		return false;			
	}
	if (aCity==0) {
		alert("Please enter a city name");
		document.getElementById("hotelSearch").style.color="#FF3300";
		return false;
	}
	if ((rightnow > date)||(rightnow > secdate)) {
		alert("Please enter valid arrival and departure dates");
		return false;			
	}
	if ((theDate == null)&&(theSecdate == null)) {
		alert("Please enter valid arrival and departure dates");
		return false;
	} 
	if ((dayVal != theDate)||(secDayVal != theSecDate)) { 
		alert("Please enter valid arrival and departure dates");
		return false;	
	} else {
		f2.checkInDate.value = monthYear + dayVal;
		f2.checkOutDate.value = secMonthYear + secDayVal;
		f2.submit();
		return false;
	}
 }
	
function init() {
	var f1 = document.hotel;
	tomorrow = new Date(rightnow.getTime()+(86400000));
	for(d=0;d<31;d++) {
		f1.checkInDateA[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
		f1.depDateA[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));
	}
	f1.checkInDateA.selectedIndex = tomorrow.getDate()-1;
	then = new Date(tomorrow.getTime()+(172800000));
	f1.depDateA.selectedIndex = then.getDate()-1;

	then = new Date(tomorrow.getTime()+(604800000))
	f1.depDateA.selectedIndex = then.getDate()-1;
	f1.depDateB.selectedIndex = (then.getMonth() - tomorrow.getMonth()) % 12;
}

var monthLengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function isLeapYear(year) {
	return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

function changeDate(objMonth, objDate) {
	maxDays = monthLengths[(rightnow.getMonth() + objMonth.selectedIndex) % 12];
	if ( (objMonth.selectedIndex == 1) && (isLeapYear(objMonth.value.substring(0, 4))) ) maxDays="29";

	var oldSelect = objDate.selectedIndex;
	if (oldSelect > maxDays-1) oldSelect = maxDays-1;
	
	objDate.length = 0;
	for (d=0;d<maxDays;d++) objDate[d] = new Option(d+1,((d<10)?"0"+(d+1):d+1));

	objDate.selectedIndex = oldSelect;
}
