var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
var NSX = (navigator.appName == "Netscape");
var IE4 = (document.all) ? true : false;

var defaultNumberOfNights = 2;
var form1 = document._ctl0;

var arrival = new Date();
arrival.setDate(arrival.getDate()+1);
var tm = arrival.getTime();
var departure = new Date(tm);
departure.setDate(departure.getDate()+defaultNumberOfNights);

var SetFirst = true
var firstPossibleArrival = new Date();

var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var mon_of_year = new Array('Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');

firstPossibleArrival.setDate(firstPossibleArrival.getDate()+1);

function addOption(theOptions, theText, theValue) {
	if (NSX) {
		var newOpt  = new Option(theText, theValue);
		var selLength = theOptions.length;
		theOptions.options[selLength] = newOpt;
		if (NS4) history.go(0);
	} else if (IE4) {
		var newOpt = document.createElement("OPTION");
		newOpt.text=theText;
		newOpt.value=theValue;
		theOptions.add(newOpt);
	}
}

function removeOption(theOptions) {
	if(theOptions.length==0) return false;
	var selLength = theOptions.length-1;
	if (NSX) {
		theOptions.options[selLength]=null;
		if (NS4) history.go(0);
	} else if (IE4) {
		theOptions.remove(selLength);;
	}
	return theOptions.length>0;
}

function numberOfNights() {
	var diff = new Date();
	diff.setTime(departure.getTime() - arrival.getTime());
	var timeDiff = diff.getTime();
	var nights = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
	if(nights<=0) return defaultNumberOfNights;
	return nights;
}

function clearControls(controlType) {
	switch (controlType) {
	case "in":
		while(removeOption(form1.inMonth2));
		while(removeOption(form1.inDay2));
	break;
	case "out":
		while(removeOption(form1.outMonth2));
		while(removeOption(form1.outDay2));
	break;
	}
}

function loadControls(controlType,FirstTime) {
	clearControls(controlType);
	var idx;
	var daysInMonth;
	var nowCYMD = firstPossibleArrival.getFullYear()*10000 + firstPossibleArrival.getMonth()*100 + firstPossibleArrival.getDate();
	
	switch (controlType) {

	 case "in":
		var calendar = new Date();
		calendar.setDate(1);
		addOption(form1.inMonth2,"Month Year","");
		for(idx=0;idx<14;idx++) {
			addOption(form1.inMonth2, mon_of_year[calendar.getMonth()]  + ' ' + calendar.getFullYear(), calendar.getFullYear()*100 + calendar.getMonth());
			if(arrival.getFullYear()==calendar.getFullYear()) {
				if(arrival.getMonth()==calendar.getMonth()) {
					form1.inMonth2.selectedIndex = idx+1;
				}
			}
			calendar.setMonth(calendar.getMonth() + 1);
		}
		calendar.setDate(1);
		calendar.setFullYear(arrival.getFullYear(),arrival.getMonth()+1,calendar.getDate()-1);

		daysInMonth = calendar.getDate();
		calendar.setDate(1);
		
		addOption(form1.inDay2,"Day", "");
		for(idx=0;idx<daysInMonth;idx++) {
			var calendarCYMD = calendar.getFullYear()*10000 + calendar.getMonth()*100 + calendar.getDate();
	
			if(calendarCYMD>=nowCYMD) {
				addOption(form1.inDay2, calendar.getDate(), calendar.getDate());
				if(arrival.getDate()==calendar.getDate()) {
					form1.inDay2.selectedIndex = form1.inDay2.length-1;
				}
			}
			calendar.setDate(calendar.getDate()+1);

		}
		
		if (FirstTime != undefined) {
		form1.inDay2.selectedIndex = 0;
		form1.inMonth2.selectedIndex = 0;

		}

		break;
				
	 case "out":
		calendar = new Date();
		calendar.setDate(1);
		addOption(form1.outMonth2,"Month Year","");
		for(idx=0;idx<15;idx++) {
			addOption(form1.outMonth2, mon_of_year[calendar.getMonth()]  + ' ' + calendar.getFullYear(), calendar.getFullYear()*100 + calendar.getMonth());
			if(departure.getFullYear()==calendar.getFullYear()) {
				if(departure.getMonth()==calendar.getMonth()) {
					form1.outMonth2.selectedIndex = idx+1;
				}
			}
			calendar.setMonth(calendar.getMonth()+1);
		}
		calendar.setDate(1);
		calendar.setFullYear(departure.getFullYear(),departure.getMonth()+1,calendar.getDate()-1);
		daysInMonth = calendar.getDate();
		calendar.setDate(1);
		addOption(form1.outDay2,"Day", "");
		for(idx=0;idx<daysInMonth;idx++) {
			var calendarCYMD = calendar.getFullYear()*10000 + calendar.getMonth()*100 + calendar.getDate();
			if(calendarCYMD>=nowCYMD) {
				addOption(form1.outDay2, calendar.getDate(), calendar.getDate());
				if(departure.getDate()==idx+1) {
					form1.outDay2.selectedIndex = form1.outDay2.length-1;
				}
			}
			calendar.setDate(calendar.getDate()+1);
		}
		if (FirstTime != undefined) {
		form1.outDay2.selectedIndex = 0;
		form1.outMonth2.selectedIndex = 0;
		
		}

		break;
	}
	if (SetFirst) {
	SetFirst = false;
	}
	return true;
}

function refreshWindow(controlType,FirstTime) {
		loadControls("in",FirstTime);
		loadControls("out",FirstTime);
	return true;
}

function d_change_Month() {
   if (form1.outMonth2.value != "") {
   return d_change();
   } else {
   return refreshWindow("all",1);
   }
}


function d_change_Day() {
   if (form1.outDay2.value != "") {
   return d_change();
   } else {
   return refreshWindow("all",1);
   }
}

function d_change() {
	var tm = arrival.getTime();
	var nArrival = new Date(tm);
	if (form1.inMonth2.value != "") {
	nArrival.setFullYear(form1.inMonth2.value.substring(0,4),form1.inMonth2.value.substring(4,6));
	} else {
	nArrival.setFullYear(form1.inMonth2.options[1].value.substring(0,4),form1.inMonth2.options[1].value.substring(4,6));
	}
	if (form1.inDay2.value == "") {
	nArrival.setDate(form1.inDay2.options[1].value);
	} else {
	nArrival.setDate(form1.inDay2.value);
	}
	var tm = departure.getTime();
	var nDeparture = new Date(tm);
	if (form1.outMonth2.value != "") {
	nDeparture.setFullYear(form1.outMonth2.value.substring(0,4),form1.outMonth2.value.substring(4,6));
	} else {
	nDeparture.setFullYear(form1.outMonth2.options[1].value.substring(0,4),form1.outMonth2.value.substring(4,6));
	}
	if (form1.outDay2.value != "") {
	nDeparture.setDate(form1.outDay2.value);
	} else {
	nDeparture.setDate(form1.outDay2.options[1].value);
	}

	if ((nDeparture.getTime() - nArrival.getTime()) <= 0) {
		var tm = arrival.getTime();
		departure.setTime(tm);
		departure.setDate(departure.getDate()+defaultNumberOfNights);
	}
	else {
		departure.setFullYear(form1.outMonth2.value.substring(0,4),form1.outMonth2.value.substring(4,6));
		departure.setDate(form1.outDay2.value);
		}

	return refreshWindow("all");
}

function a_change_Month() {
   if (form1.inMonth2.value != "") {
   return a_change();
   } else {
   return refreshWindow("all",1);
   }
}
function a_change_Day() {
   if (form1.inDay2.value != "") {
   return a_change();
   } else {
   return refreshWindow("all",1);
   }
}

function a_change() {
	

	var tm = arrival.getTime();
	var nArrival = new Date(tm);

	if (form1.inMonth2.value != "") {
	nArrival.setFullYear(form1.inMonth2.value.substring(0,4),form1.inMonth2.value.substring(4,6));
	} else {
	nArrival.setFullYear(form1.inMonth2.options[1].value.substring(0,4),form1.inMonth2.options[1].value.substring(4,6));
	}

	if (form1.inDay2.value == "") {
	nArrival.setDate(form1.inDay2.options[1].value);
	} else {
	nArrival.setDate(form1.inDay2.value);
	}
	var tm = departure.getTime();
	var nDeparture = new Date(tm);
	nDeparture.setFullYear(form1.outMonth2.value.substring(0,4),form1.outMonth2.value.substring(4,6));
	nDeparture.setDate(form1.outDay2.value);
	if (form1.inMonth2.value != "") {
	arrival.setFullYear(form1.inMonth2.value.substring(0,4),form1.inMonth2.value.substring(4,6));
	} else {
	arrival.setFullYear(form1.inMonth2.options[1].value.substring(0,4),form1.inMonth2.options[1].value.substring(4,6));
	}

	if (form1.inDay2.value == "") {
	arrival.setDate(form1.inDay2.options[1].value);
	} else {
	arrival.setDate(form1.inDay2.value);
	}

	if (numberOfNights()<=defaultNumberOfNights) {
		var tm = arrival.getTime();
		departure.setTime(tm);
		departure.setDate(departure.getDate()+defaultNumberOfNights);
	} 
	
	return refreshWindow("all");

}

