var returnDateChanged = false;

function setReturnDate(parentFormID, pickupMonthYearDropDownListID, pickupDayDropDownListID, returnMonthYearDropDownListID, returnDayDropDownListID)
{

	if ( !returnDateChanged )
	{
		var today = new Date();
		
		// get the selected index of the pickupDate monthYear drop down list
		var pickupMonthYearIndex = document.forms[parentFormID].elements[pickupMonthYearDropDownListID].selectedIndex;

		// get the selected index of the pickupDate day drop down list
		var pickupDayIndex = document.forms[parentFormID].elements[pickupDayDropDownListID].selectedIndex;
		
		// get the pickup date
		//  add a month for each index in selected index past 0 (year is also incremented
		//  if necessary due to out of range handling built into javascript)
		var pickupDate = new Date();
		pickupDate.setDate(1);  // must set to 1 so setMonth will work
		pickupDate.setMonth(pickupDate.getMonth() + pickupMonthYearIndex);
		
		//  add a day for each index in selected index past today's date
		pickupDate.setDate(pickupDayIndex + 1);

		// set the return date by adding one day (8,640,000 milliseconds) 
		var returnDate = new Date(pickupDate.getTime() + 86400000);
		
		// calculate new selected index of returnDate monthYear drop down list
		var returnMonthYearNewIndex = 12*(returnDate.getFullYear() - today.getFullYear()) + returnDate.getMonth() - today.getMonth();
		
		// get currently selected index of returnDate monthYear drop down list
		var returnMonthYearCurrentIndex = document.forms[parentFormID].elements[returnMonthYearDropDownListID].selectedIndex;
		
		// set selected index of returnDate monthYear drop down list
		document.forms[parentFormID].elements[returnMonthYearDropDownListID].selectedIndex = returnMonthYearNewIndex;

		// add days to or remove days from the returnDate drop down list if needed (new month has
		//  more or fewer days than old month)
		var daysCount = document.forms[parentFormID].elements[returnDayDropDownListID].options.length;
		
		// get number of days in the returnDate month
		var requiredDays = getDaysInMonth(returnDate);
		
		if(daysCount != requiredDays)
		{
			changeDays(requiredDays, parentFormID, returnDayDropDownListID);
		}

		// set selected index of returnDate day drop down list
		var returnDayIndex = returnDate.getDate() - 1;
		document.forms[parentFormID].elements[returnDayDropDownListID].selectedIndex = returnDayIndex;
	}
}

function changeDays(requiredDays, parentFormID, dayDropDownListID)
{
	// get the drop down list
	var dayDropDownList = document.forms[parentFormID].elements[dayDropDownListID];
		
	// get number of elements in drop down list
	var elementsCount = dayDropDownList.options.length;
	
	// add days if necessary
	var newOption;
	for(var i=elementsCount+1; i <= requiredDays; i++)
	{
		//newOption = new Option(i, i);
		newOption = document.createElement("OPTION");
		newOption.text = i;
		newOption.value = i;
		dayDropDownList.options.add(newOption,i-1);
	}
	
	// remove days if necessary
	for(var i=elementsCount-1; i > requiredDays-1; i--)
	{
		dayDropDownList.remove(i);
	}
}

function getDaysInMonth(date)
{
	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	// change if leap year
	if(date.getFullYear() % 4 == 0)
	{
		if(date.getFullYear() % 100 != 0)
		{
			daysInMonth[1] = 29;
		}
		else if(date.getFullYear() % 400 == 0)
		{
			daysInMonth[1] = 29;
		}
	}
	
	return daysInMonth[date.getMonth()];
}

function popupCalendar(calendarID, parentFormID, locationTimeControlName, monthYearDropDownListID, dayDropDownListID)
{
	var selectedDate = new Date();
	
	// get drop down lists
	var monthYearDropDownList = document.forms[parentFormID].elements[monthYearDropDownListID];
	var dayDropDownList = document.forms[parentFormID].elements[dayDropDownListID];
	
	// get selected value of MonthYear dropDownList
	var monthYearString = monthYearDropDownList.options[monthYearDropDownList.selectedIndex].value;
	
	// get selected value of Day dropDownList
	var dayString = dayDropDownList.selectedIndex + 1;
	
	var dateString = monthYearString.substring(0, 2) + "/" + dayString + "/" + monthYearString.substring(3);
	
	// popup calendar
	CalendarPopup('/reservations/Calendar.aspx', 'Thrifty', '455', '230', 'no', calendarID, parentFormID, locationTimeControlName, dateString);
}

function adjustMonthDays(parentFormID, monthYearDropDownListID, dayDropDownListID)
{
	// get drop down lists
	var monthYearDropDownList = document.forms[parentFormID].elements[monthYearDropDownListID];
	var dayDropDownList = document.forms[parentFormID].elements[dayDropDownListID];
	
	// get selected value of MonthYear dropDownList
	var monthYearString = monthYearDropDownList.options[monthYearDropDownList.selectedIndex].value;
	
	// get month
	var month = monthYearString.substring(0, 2);
	
	// get year
	var year = monthYearString.substring(3);
	
	// get selected value of Day dropDownList
	var day = dayDropDownList.selectedIndex + 1;
	
	// make a date
	var date = new Date(year, month-1, 1);
	
	// get number of days in the date month
	var requiredDays = getDaysInMonth(date);

	// get number of days in day drop down list
	var daysCount = dayDropDownList.options.length;

	// if number of days is not what this month needs, change it
		if(daysCount != requiredDays)
	{
		changeDays(requiredDays, parentFormID, dayDropDownListID);
	}
	
	// adjust selected day if it is greater than the number of days in the new month
	if(day > requiredDays)
	{
		document.forms[parentFormID].elements[dayDropDownListID].selectedIndex = requiredDays-1;
	}
}

function onReturnDateChanged()
{
	returnDateChanged = true;
}