//User clicked on the Use Marriott Rewards checkbox
function chkUseMarriottRewards(objField)
{
	var objClusterCode = objField.form.clusterCode;
	var objCorporateCode = objField.form.corporateCode;
	var objGroupCode = objField.form.groupCode;
		
	if ((objField.type == "checkbox") && 
		(objField.name == "clusterCode")){		
		
		if(objClusterCode != null && 
			objClusterCode.checked == true)
		{
			objCorporateCode.value = "";
			objGroupCode.value = "";
		}

		if(objClusterCode != null &&
			objClusterCode.checked == false)
		{
			objClusterCode.value="";
		}		
	}	
}

//The FullRatesCallOut form is being loaded
function loadFullRatesCalloutForm()
{
	var objForm = document.fullRatesCalloutForm;
	var objClusterCode = objForm.clusterCode;
	var corporateCode = objForm.corporateCode.value;
	var groupCode = objForm.groupCode.value;
	
	if(objClusterCode != null &&
		objClusterCode.checked == false &&
			objClusterCode.length > 0)
	{
		objClusterCode.value="";
	}
	
	if(objClusterCode != null &&
		objClusterCode.checked == true)
	{
		corporateCode = "";
		groupCode = "";
	}	
}

function moreOptions(checkAvailabilityURL, netlinkResURL, netlinkId)
{
	objForm = document.fullRatesCalloutForm;
	var propertyCode = objForm.propertyCode.value;
	var checkInDateMonth = objForm.checkInDateMonth.value;
	var checkInDateDay = objForm.checkInDateDay.value;
	var checkInDate = objForm.checkInDate.value;
	var checkOutDateMonth = objForm.checkOutDateMonth.value;
	var checkOutDateDay = objForm.checkOutDateDay.value;
	var checkOutDate = objForm.checkOutDate.value;
	var numberOfGuests = objForm.numberOfGuests.value;
	var numberOfRooms = objForm.numberOfRooms.value;
	var marriottRewardsNumber = objForm.marriottRewardsNumber.value;
	var corporateCode = objForm.corporateCode.value;
	var groupCode = objForm.groupCode.value;
	var clusterCode;
	
	if(objForm.clusterCode != null)
	{
		 var objClusterCode = objForm.clusterCode;
		 clusterCode = objClusterCode.value;
	}
	
	//Some pages don't have this field
	//IPMI00018422
	if(objClusterCode!=null){
		var clusterCode = objClusterCode.value;
	}
	var wt_ref = objForm.WT_Ref.value;
	
	var params = "&checkInDateMonth="+checkInDateMonth+"&checkInDateDay="+checkInDateDay+"&checkInDate="+checkInDate+"&checkOutDateMonth="+checkOutDateMonth+"&checkOutDateDay="+checkOutDateDay+"&checkOutDate="+checkOutDate+"&numberOfGuests="+numberOfGuests+"&numberOfRooms="+numberOfRooms+"&marriottRewardsNumber="+marriottRewardsNumber;
	
	if(clusterCode != null && 
		objClusterCode.checked == true && 
			clusterCode.length > 0)
	{
		params = params + "&cc="+clusterCode;
		corporateCode = "";
		groupCode = "";
	}
	
	if(corporateCode.length > 0)
	{
		params = params + "&cc="+corporateCode;
	}
	
	if(groupCode.length > 0)
	{
		params = params + "&groupCode="+groupCode;
	}
	
	params = params + "&WT_Ref="+wt_ref;
	
	var url = checkAvailabilityURL+propertyCode;

	if (netlinkId == 'true')
	{
		url = netlinkResURL+propertyCode+'&app=NetLink';
	}
	//alert(url);
	document.location.href=url+params;
}
function readCookie()
{
	
	var nameEQ = "FormInfoCookie";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		{
			if (c.indexOf(nameEQ) == 0)
			{
				
								
				var cookieValue = c.substring(0,c.length);
				var values = cookieValue.split("|");

				var checkin = values[0];
				var newCheckIn = checkin.substring(checkin.indexOf("=") + 1, checkin.length)
				if(checkin != 'null' && checkin != '')
				{
					SetDate("checkInDate", newCheckIn);
				}
				else
				{
					var curDate = new Date()
					var curMonth = curDate.getMonth() + 1;
					var curDay = curDate.getDate();
					var formattedDate = curMonth + "/" + curDay + "/" + curDate.getFullYear();
					SetDate("checkInDate", formattedDate);
				}
				
				var checkout = values[1];
				if(checkout != 'null' && checkout != '')
				{
					SetDate("checkOutDate", checkout);
				}
				else
				{
					// do nothing
				}
				
				var numGuests = values[2];
				var objNumGuests = document.fullRatesCalloutForm.elements['numberOfGuests'];
				if(objNumGuests != 'null' && objNumGuests != '')
				{
					objNumGuests.selectedIndex = numGuests-1;
				}
				else
				{
					objNumGuests.selectedIndex = 1;
				}
				
				var numOfRooms = values[3];
			
				if(numOfRooms == 0) {
					numOfRooms = 1;
				}
				
				var objNumRooms = document.fullRatesCalloutForm.elements['numberOfRooms'];
				if(objNumRooms != 'null' && objNumRooms != '')
				{
					objNumRooms.selectedIndex = numOfRooms - 1;
					
					
				}
				else
				{
					objNumRooms.selectedIndex = 1;
				}
				
								
				var rewardsNumber = values[4];
				if(rewardsNumber != 'null' && rewardsNumber != '')
				{
					document.fullRatesCalloutForm.marriottRewardsNumber.value = rewardsNumber;
				}
				else
				{
					document.fullRatesCalloutForm.marriottRewardsNumber.value = '';
				}
				
				var clusterCode = values[5];
				if(clusterCode != 'null' && clusterCode != '')
				{
					document.fullRatesCalloutForm.corporateCode.value = clusterCode;
				}
				else
				{
					document.fullRatesCalloutForm.corporateCode.value = '';
				}
				
				
				var groupCode = values[6];
				if(groupCode != 'null' && groupCode != '')
				{
					document.fullRatesCalloutForm.groupCode.value = groupCode;
				}
				else
				{
					document.fullRatesCalloutForm.groupCode.value = '';
				}


			}
		}

	}
	return null;
}

function readCookieJS()
{
	
	var nameEQ = "FormInfoCookie";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		{
			if (c.indexOf(nameEQ) == 0)
			{
				
								
				var cookieValue = c.substring(0,c.length);
				var values = cookieValue.split("|");

				var checkin = values[0];
				var newCheckIn = checkin.substring(checkin.indexOf("=") + 1, checkin.length)
				if(checkin != 'null' && checkin != '')
				{
					SetDate("checkInDate", newCheckIn);
				}
				else
				{
					var curDate = new Date()
					var curMonth = curDate.getMonth() + 1;
					var curDay = curDate.getDate();
					var formattedDate = curMonth + "/" + curDay + "/" + curDate.getFullYear();
					SetDate("checkInDate", formattedDate);
				}
				
				var checkout = values[1];
				if(checkout != 'null' && checkout != '')
				{
					SetDate("checkOutDate", checkout);
				}
				else
				{
					// do nothing
				}
				
				var numGuests = values[2];
				var objNumGuests = document.fullRatesCalloutForm.elements['numberOfGuests'];
				if(objNumGuests != 'null' && objNumGuests != '')
				{
					objNumGuests.selectedIndex = numGuests-1;
				}
				else
				{
					objNumGuests.selectedIndex = 1;
				}
				
				var numOfRooms = values[3];
			
				if(numOfRooms == 0) {
					numOfRooms = 1;
				}
				
				var objNumRooms = document.fullRatesCalloutForm.elements['numberOfRooms'];
				if(objNumRooms != 'null' && objNumRooms != '')
				{
					objNumRooms.selectedIndex = numOfRooms - 1;
					
					
				}
				else
				{
					objNumRooms.selectedIndex = 1;
				}
				
								
				var rewardsNumber = values[4];
				if(rewardsNumber != 'null' && rewardsNumber != '')
				{
					document.fullRatesCalloutForm.marriottRewardsNumber.value = rewardsNumber;
				}
				else
				{
					document.fullRatesCalloutForm.marriottRewardsNumber.value = '';
				}
	
			}
		}

	}
	return null;
}
function validateForm() {
	objForm = document.fullRatesCalloutForm;
	var checkInDate = objForm.checkInDate.value;
	var checkOutDate = objForm.checkOutDate.value;
	if (checkInDate != '' && checkOutDate != '')
	{
		return ValidateStayDates();
	}
	else
	{
		//javascript:moreOptions('/reservation/availability.mi?propertyCode=','/netlink/reservation/availability.mi?propertyCode=','');
		moreOptions('/reservation/availability.mi?propertyCode=','/netlink/reservation/availability.mi?propertyCode=','');
		return false;
	}
}


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.fullRatesCalloutForm.elements[strField].value = strReservationDate;
	return strReservationDate;
}


function SetFormDate(strField) {
	var arrDate;
	var strReservationDate;
	var objMonthField = document.fullRatesCalloutForm.elements[strField+'Month'];
	var objDayField = document.fullRatesCalloutForm.elements[strField+'Day'];
	var dtmFieldDate;
	var intAdvanceDays = document.fullRatesCalloutForm.elements['numberOfNights'].value;

	if ((objMonthField.options[objMonthField.selectedIndex].value != '') && (objDayField.options[objDayField.selectedIndex].value != '')) {

		strReservationDate = setHDate(strField, document.fullRatesCalloutForm.elements[strField+'Month'], document.fullRatesCalloutForm.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.fullRatesCalloutForm.elements[strField].value = '';
		if (document.fullRatesCalloutForm.elements['checkOutDate'].value != '') {
			SetDate('checkOutDate', '');
		}
		if (document.fullRatesCalloutForm.elements['checkInDate'].value != '') {
			SetDate('checkInDate', '');
		}
	}
}



function SetDate(strField, strDate) {

	var dtmDate;
	var objMonthField = document.fullRatesCalloutForm.elements[strField+'Month'];
	var objDayField = document.fullRatesCalloutForm.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 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());
}

// function returns true if secondDate is equal or greater than the first
// dates must be passed in mm/dd/yyyy format
function IsDateEqualOrAfter(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 ValidateOfferDates(offerStartDate, offerEndDate, rewardsOffers, subCat)
{	
	//alert("comes in the function ValidateOfferDates " + offerStartDate + " end " + offerEndDate);
	
	if (offerStartDate != '' && offerEndDate != '') {
		var startYear = offerStartDate.substring(0, offerStartDate.indexOf('-'));
		var startMonthDay = offerStartDate.substring(offerStartDate.indexOf('-')+1);
		var startMonth = startMonthDay.substring(0, startMonthDay.indexOf('-'));
		var startDay = startMonthDay.substring(startMonthDay.indexOf('-')+1);
	 	offerStartDate = startMonth+"/"+startDay+"/"+startYear;

	 	var endYear = offerEndDate.substring(0, offerEndDate.indexOf('-'));
		var endMonthDay = offerEndDate.substring(offerEndDate.indexOf('-')+1);
		var endMonth = endMonthDay.substring(0, endMonthDay.indexOf('-'));
		var endDay = endMonthDay.substring(endMonthDay.indexOf('-')+1);
	 	offerEndDate = endMonth+"/"+endDay+"/"+endYear;	 
	 	
	 	var offerEndDateForChkout = new Date(offerEndDate); 
		offerEndDateForChkout.setDate(offerEndDateForChkout.getDate() + 1);

	 	if (!IsDateEqualOrAfter(offerStartDate, document.fullRatesCalloutForm.elements['checkInDate'].value) ||
	 		!IsDateEqualOrAfter(document.fullRatesCalloutForm.elements['checkInDate'].value, offerEndDate)){
			alert("Check In Date is not valid for this offer.  Please verify your selection criteria.");
			return false;
		}

		if (!IsDateEqualOrAfter(document.fullRatesCalloutForm.elements['checkOutDate'].value, offerEndDateForChkout)){
			alert("Check Out Date is not valid for this offer.  Please verify your selection criteria.");
			return false;
		}
 	}
	return ValidateRewards(rewardsOffers, subCat);
}

function ValidateRewards(rewardsOffers, subCat)
{
	var marriottRewardsNumber = document.fullRatesCalloutForm.marriottRewardsNumber.value;
	if (subCat != '' && rewardsOffers.indexOf(subCat) != -1) {
		if (marriottRewardsNumber == '') {
			alert("A Marriott Rewards Number is required for this offer.\n Please enter a Rewards Number.");
			document.fullRatesCalloutForm.marriottRewardsNumber.focus();
			return false;
		}
	}
	return true;
}

function corpCodeChanged()
{
	objForm = document.fullRatesCalloutForm;
	var corporateCode = objForm.corporateCode.value;
	if (corporateCode != '')
	{
		objForm.groupCode.value = '';
	}

	//Clear out the MR checkbox if the user has entered a corp code
	if(corporateCode.length>0)
	{
		// if statement added to resolve defect 21149
		if(objForm.clusterCode != null)
		{
			objForm.clusterCode.checked=false;
		}
	}
}

function groupCodeChanged()
{
	objForm = document.fullRatesCalloutForm;
	var groupCode = objForm.groupCode.value;
	if (groupCode != '')
	{
		objForm.corporateCode.value = '';
	}
}


function initFormDates()
{
	var curDate = new Date()
	var curMonth = curDate.getMonth() + 1;
	var curDay = curDate.getDate();
	var formattedDate = curMonth + "/" + curDay + "/" + curDate.getFullYear();
	SetDate("checkInDate", formattedDate);
}

function viewPopupCalendar(objField, checkOutFieldName)
{
		var fieldName = objField.name;
		var objForm = objField.form;
		var formName = objForm.name;
		var fieldValue  = objForm.elements[fieldName].value;

		page = "http://marriott.com/common/viewCalendar.mi?"
		+ "fieldName=" + fieldName
		+ "&checkOutFieldName=" + checkOutFieldName
		+ "&currentDate=" + fieldValue
		+ "&formName=" + formName;


		windowprops = "height=200,width=175,location=no,"
		+ "scrollbars=no,menubars=no,toolbars=no,resizable=yes"
		+ ",left=" + (500)
		+ ",top=" + (325);

		window.open(page, "Calendar", windowprops);
}

function openAgency()
{
	window.open("/property/property/locatorPopUp.jsp","AgencyLocator", 'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=yes,width=500,height=500');
}



