// datesChange.js

// Functions added to datesChange.js:
function submitHotelForm() {
	if (document.frm_Hotel["numberOfRooms"] != null && document.frm_Hotel.numberOfRooms.value == 5) {
		window.location="http://www.travelnow.com/index.jsp?pageName=groups&cid=3778";
		return false;
	}
	return true;
}

function OpenCalendar(dayField, day, monthField, month, yearField, year, formName) {
	file = '/calendar.php?formName=' + formName;
	if (dayField != null)
		file += '&dayField=' + dayField + '&day=' + day;
	if (monthField != null)
		file += '&monthField=' + monthField + '&month=' + month;
	if (yearField != null)
		file += '&yearField=' + yearField + '&year=' + year;
	window.open(file, "dates", 'width=200,height=260');
}

// Original datesChange.js
// Start new script code

	// Is this year a leap year?
	function isLeapYear(yrStr) {
		var leapYear = false;
		var year = parseInt(yrStr, 10);
		// Every fourth year is a leap year
		if (year % 4 == 0) {
			leapYear = true;
			// unless it's a multiple of 100
			if (year % 100 == 0) {
				leapYear = false;
				// or it's a multiple of 400.
				if (year % 400 == 0) {
					leapYear=true;
				}
			}
		}
		return leapYear;
	}

	// Get days in a month (including leap months)
	function getDaysInMonth(mthIdx, YrStr) {
		// All the rest have 31
		var maxDays = 31
		// expect Feb. (of course).
		if (mthIdx == 1) {
			if (isLeapYear(YrStr)) {
				maxDays=29;
			} else {
				maxDays=28;
			}
		}

		// Thirty days hath...
		if (mthIdx == 3 || mthIdx == 5 || mthIdx == 8 || mthIdx == 10) {
			maxDays=30;
		}
		return maxDays;
	}

	// The function which does some magic to the date fields.
	// Return non-zero if it is the last day of the month.
	function adjustDate(mthIdx, Dt) {
		var value = 0;

		var today = new Date()
		var theYear = parseInt(today.getYear(), 10)

		if (mthIdx < today.getMonth()) {
			theYear = (parseInt(today.getYear(), 10) + 1)
		}
		if (theYear < 100) {
			theYear = "19" + theYear
		} else {
			if ((theYear - 100) < 10) {
				theYear = "0" + (theYear - 100)
			} else {
				theYear = (theYear - 100) + ""
			}
			theYear = "20" + theYear
		}


		var numDays = getDaysInMonth(mthIdx, theYear);

		if (mthIdx == 1) {
			if (Dt.options.selectedIndex + 2 < numDays) {
				return 0;
			} else {
				if (Dt.options.selectedIndex + 1 > numDays) {
					Dt.options.selectedIndex=numDays - 1;
				}
				// Check for leap year
				if ((Dt.options.selectedIndex + 1) == numDays) {
					return 1;
				} else {
					return 4;
				}
			}
		}

		if (Dt.options.selectedIndex + 2 < numDays) {
			value = 0;
		} else {
			if (Dt.options.selectedIndex + 1 > numDays) {
				Dt.options.selectedIndex--;
				value = 3;
			} else if (Dt.options.selectedIndex + 1 == numDays) {
				// Index is 31 or 30
				value = 2;
			} else {
				value = 4;
			}
		}
		return value;
	}

	// Changes departure month when arrival month is changed
	function amadChange(inM, inD, outM, outD) {
		var res = adjustDate(inM.options.selectedIndex, inD);
		if (res != 0) {
			   outD.options.selectedIndex = 0;
			   if (inM.options.selectedIndex == 11) {
					outM.options.selectedIndex = 0
			   } else if (res == 4) {
					outM.options.selectedIndex=inM.options.selectedIndex + 1;
					outD.options.selectedIndex = 0;
			   } else {
					outM.options.selectedIndex=inM.options.selectedIndex + 1;
					outD.options.selectedIndex = 1;
			   }
		} else {
			outM.options.selectedIndex = inM.options.selectedIndex;
			if (outD.options.selectedIndex <= inD.options.selectedIndex) {
				outD.options.selectedIndex = inD.options.selectedIndex + 2;
			}
		}
		return;
	}


	// departure month change
	function dmddChange(outM, outD) {
		adjustDate(outM.options.selectedIndex, outD);
		return;
	}

