
// availability.js

function validateForm() {
	return ValidateStayDates();
}


function setHDate(strField, objMonthField, objDayField) {
	
	var strReservationDate;
	
	arrDate = objMonthField.options[objMonthField.selectedIndex].value.split("/");
	strReservationDate = arrDate[0]+"/"+objDayField.options[objDayField.selectedIndex].value+"/"+arrDate[1];
	document.forms['quickRateSearchActionForm'].elements[strField].value = strReservationDate;
	return strReservationDate;
}


function SetFormDate(strField) {
	var arrDate;
	var strReservationDate;
	var objMonthField = document.forms['quickRateSearchActionForm'].elements[strField+'Month'];
	var objDayField = document.forms['quickRateSearchActionForm'].elements[strField+'Day'];
	var dtmFieldDate;
	var intAdvanceDays = document.forms['quickRateSearchActionForm'].elements['numberOfNights'].value;

	if ((objMonthField.options[objMonthField.selectedIndex].value != '') && (objDayField.options[objDayField.selectedIndex].value != '')) {

		strReservationDate = setHDate(strField, document.forms['quickRateSearchActionForm'].elements[strField+'Month'], document.forms['quickRateSearchActionForm'].elements[strField+'Day']);
		if (strField == 'checkInDate') {
			dtmFieldDate = new Date(strReservationDate);
			dtmFieldDate.setTime(dtmFieldDate.getTime() + (86400000*intAdvanceDays));
			if (dtmFieldDate.getYear() < 1900) {
				strReservationDate = (dtmFieldDate.getMonth()+1) + '/' + dtmFieldDate.getDate() + '/' + (1900+dtmFieldDate.getYear());
			} else {
				strReservationDate = (dtmFieldDate.getMonth()+1) + '/' + dtmFieldDate.getDate() + '/' + (dtmFieldDate.getYear());
			}
			
			SetDate('checkOutDate', strReservationDate);
		}
		
	}
	
	if ((objMonthField.options[objMonthField.selectedIndex].value == '') && (objDayField.options[objDayField.selectedIndex].value == '')) {
		document.forms['quickRateSearchActionForm'].elements[strField].value = '';
		if (document.forms['quickRateSearchActionForm'].elements['checkOutDate'].value != '') {
			SetDate('checkOutDate', '');
		}
		if (document.forms['quickRateSearchActionForm'].elements['checkInDate'].value != '') {
			SetDate('checkInDate', '');
		}
	}
}



function SetDate(strField, strDate) {
	
	var dtmDate;
	var objMonthField = document.forms['quickRateSearchActionForm'].elements[strField+'Month'];
	var objDayField = document.forms['quickRateSearchActionForm'].elements[strField+'Day'];
	var strMonthValue;
	var x = 0;
	var blnDone = false;
	
	if (strDate != '') {
		dtmDate = new Date(strDate);
		strDayValue = dtmDate.getDate();
		if (dtmDate.getYear() < 1900) {
			strMonthValue = (dtmDate.getMonth()+1) + '/' + (1900+dtmDate.getYear());
		} else {
			strMonthValue = (dtmDate.getMonth()+1) + '/' + (dtmDate.getYear());
		}
	} else {
		strDayValue = '';
		strMonthValue  = '';
	}
	
	do {
		if (objMonthField.options[x].value == strMonthValue) {
			objMonthField.selectedIndex = x;
			blnDone = true;
		}
	} while (!(blnDone) && (++x < objMonthField.length));
	
	x = 0;
	blnDone = false;
	do {
		if (objDayField.options[x].value == strDayValue) {
			objDayField.selectedIndex = x;
			blnDone = true;
		}
	} while (!(blnDone) && (++x < objDayField.length));
	SetFormDate(strField);
}

	

function ViewCalendar(strField, strCheckInDate, strCheckOutDate) {
	page = "http://d6marriott.marriott.com/reservations/calendar.asp?Field=" + escape(strField)
	+ "&checkInDate="+escape(strCheckInDate)
	+ "&checkOutDate="+escape(strCheckOutDate);
	
	windowprops = "height=200,width=175,location=no,"
	+ "scrollbars=no,menubars=yes,toolbars=yes,resizable=yes,status=yes";

	window.open(page, "Calendar", windowprops);
}


// function returns true if secondDate is at least one day after the first
// dates must be passed in mm/dd/yyyy format
function IsDateAfter(firstDate, secondDate){
	
	var dtmDate1 = new Date(firstDate);
	var dtmDate2 = new Date(secondDate);
	
	return (dtmDate1.getTime() < dtmDate2.getTime());
}


// returns true if year is leap year
function isLeapYear(year) 
{
	if ((year%400==0) || ((year%4==0) && (year%100!=0)))
	{
		return true;
	}
	else 
	{
		return false;
	}
}

// returns true if date is valid false otherwise. 
// checks if user selected correct lastday of month from dropdown 
function IsValidDateString(dateString)
{
	var arrDate = dateString.split("/");
	
	switch (arrDate[0])
	{
		case '1' :
		case '01' :
			return (arrDate[1] <= 31);
			break;
		case '2' :
		case '02' :		
			if (isLeapYear(arrDate[2]))
			{ 
				return (arrDate[1] <= 29);
				break;
			}
			else
			{
				return (arrDate[1] <= 28);
				break;
			}
		case '3' :
		case '03' :
			return (arrDate[1] <= 31);
			break;
		case '4' :
		case '04' :
			return (arrDate[1] <= 30);
			break;
		case '5' :
		case '05' :
			return (arrDate[1] <= 31);
			break;
		case '6' :
		case '06' :
			return (arrDate[1] <= 30);
			break;
		case '7' :
		case '07' :
			return (arrDate[1] <= 31);
			break;
		case '8' :
		case '08' :
			return (arrDate[1] <= 31);
			break;
		case '9' :
		case '09' :
			return (arrDate[1] <= 30);
			break;
		case '10' :
			return (arrDate[1] <= 31);
			break;
		case '11' :
			return (arrDate[1] <= 30);
			break;
		case '12' :
			return (arrDate[1] <= 31);
			break;
		default :
			return false;
			break;
	} 
}


function ValidateStayDates(){
	
	if (!IsValidDateString(document.forms['quickRateSearchActionForm'].elements['checkInDate'].value)){
		alert("Check In Date is not valid.");
		return false;
	}
	
	if (!IsValidDateString(document.forms['quickRateSearchActionForm'].elements['checkOutDate'].value)){
		alert("Check Out Date is not valid.");
		return false;
	}
	
	if (!IsDateAfter(document.forms['quickRateSearchActionForm'].elements['checkInDate'].value, document.forms['quickRateSearchActionForm'].elements['checkOutDate'].value)){
		alert("Check Out Date must be a date after the Check In Date.  Please verify your selection criteria.");
		return false;
	}
	
	var curDate = new Date()
	var curMonth = curDate.getMonth() + 1;
	var curDay = curDate.getDate() - 1;
	
	var formattedDate = curMonth + "/" + curDay + "/" + curDate.getFullYear()	

	if (!IsDateAfter(formattedDate, document.forms['quickRateSearchActionForm'].elements['checkInDate'].value)) {
		alert("Check In Date occurs in the past.");
		return false;
	}
	
	var kDate = new Date();
	// add 357 days to current day and substarct current week day from it.
	kDate.setTime(curDate.getTime() + (357 * 86400000) - ((curDate.getDay()) * 86400000));	
		
	var ciDate = new Date(document.forms['quickRateSearchActionForm'].elements['checkInDate'].value);
	var coDate = new Date(document.forms['quickRateSearchActionForm'].elements['checkOutDate'].value);
		
	if (ciDate.getTime() >=  kDate.getTime() ||coDate.getTime() > kDate.getTime() ) {
		alert("Dates occur beyond inventory period.");
		return false;
	}
	return true;
}

