
//function to set the subcategory selection given the chosen category, then redirect
//back to itself with appended querystrings.
function setSubCatSelection(objForm){
	if (objForm.escapeCats.selectedIndex > 0){
    //set hidden variables for subcategoryid and clustercode
    iSubCatIDIndex = objForm.escapeSubCats.selectedIndex;
    objForm.catID.value = objForm.escapeCats.options[objForm.escapeCats.selectedIndex].value
    objForm.subCatID.value = objForm.escapeSubCats.options[iSubCatIDIndex].value;
   // objForm.clusterCode.value = subcatArray[3][iCatIDIndex][iSubCatIDIndex];
    location.href = "/specials/escapes/escapeSearch.mi?" +"CatID=" + objForm.catID.value + 
    	"&SubCatID=" + objForm.subCatID.value +"&cid=" + objForm.fromDate.value +
    	"&cod=" + objForm.toDate.value
	}
}

// **************************** ValidateES(objForm) **************************************

function ValidateES(objForm)
{
	var invalid = false;
	arErrors = new Array();

	if (objForm.escapeSubCats && objForm.escapeSubCats.type.toLowerCase() != 'hidden') {
		var iSubCatIDIndex = objForm.escapeSubCats.selectedIndex;
		objForm.SubCatID.value = objForm.escapeSubCats.options[iSubCatIDIndex].value;
		if (subcatArray && subcatArray[3] && subcatArray[3][iCatIDIndex])
			objForm.ClusterCode.value = subcatArray[3][iCatIDIndex][iSubCatIDIndex];

		if (objForm.escapeCats.options[objForm.escapeCats.options.selectedIndex].value == '') {
			arErrors[arErrors.length] = 31;
			invalid = true;
		}
		if (objForm.escapeSubCats.options[objForm.escapeSubCats.options.selectedIndex].value == '') {
			arErrors[arErrors.length] = 33;
			invalid = true;
		}
	}
	if (objForm.Country.options[objForm.Country.options.selectedIndex].value == '') {
		arErrors[arErrors.length] = 32;
		invalid = true;
	}
	var isValid = DefaultValidator(objForm, invalid);

	if (!isValid) {
		for (var i = 0; i < arErrors.length; i++) {
			if (arErrors[i] == "1") {
				arErrors[i] = "";
			}
		}
		ShowError();
		return false;
	} else
		return true;
}


//********************************** SetCountry(state, country, objForm)**********************************
function SetCountry(objForm)
{
	var state = 'stateProvince';
	var country = 'country';
	
	if (objForm.elements[state].selectedIndex != 0) {
		for (var i = 0; i < objForm.elements[country].length; i++)  {
			if (objForm.elements[country].options[i].value == 'US')
				objForm.elements[country].options[i].selected = true;
		}
	}
}

//********************************** SetState(state, country, objForm) **********************************	
function SetState(objForm)
{
	var state = 'stateProvince';
	var country = 'country';
	if (objForm.elements[country].options[objForm.elements[country].selectedIndex].value != 'US')
		objForm.elements[state].options[0].selected = true;
}

//********************************** SetRecsPerPage(objForm) ********************************** 

function SetRecsPerPage(objForm)

{

	switch (objForm.elements['recordsDisplayedPerPage'].options[objForm.elements['recordsDisplayedPerPage'].selectedIndex].value)

	{

		case "10":

		objForm.recordsPerPage.value = "10";

		objForm.resultsMap.value="N"; 

		break;


		case "10MAP":

		objForm.recordsPerPage.value = "10";

		objForm.resultsMap.value="Y";

		break;


		case "all":

		objForm.recordsPerPage.value = "100";

		objForm.resultsMap.value="N";

		break;


		default:

		objForm.recordsPerPage.value = "10";

		objForm.resultsMap.value="N"; 

	}

}

//********************************** ValidateOfferDates(offerStartDate, offerEndDate, rewardsOffers, subCat) ********************************** 


function ValidateOfferDates(offerStartDate, offerEndDate, rewardsOffers, subCat)
{	

	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);

		var dateFormatPattern = document.forms['OffersDetailSearchActionForm'].elements['dateFormatPattern'].value.toLowerCase();
		var fromDate = document.forms['OffersDetailSearchActionForm'].elements['fromDate'].value ;

		fromDate = dateInMMDDYYFormat(fromDate,dateFormatPattern) ;
		if (!IsDateEqualOrAfter(offerStartDate, fromDate) ||
	 		!IsDateEqualOrAfter(fromDate, offerEndDate)){
			alert("Check In Date is not valid for this offer.  Please verify your selection criteria.");
			return false;
		}

		var toDate = document.forms['OffersDetailSearchActionForm'].elements['toDate'].value ;
		toDate = dateInMMDDYYFormat(toDate,dateFormatPattern) ;

		if (!IsDateEqualOrAfter(toDate, offerEndDateForChkout)){
			alert("Check Out Date is not valid for this offer.  Please verify your selection criteria.");
			return false;
		}
 	}
	return true;
}

//********************************** IsDateEqualOrAfter(firstDate, secondDate)********************************** 

function IsDateEqualOrAfter(firstDate, secondDate)
{

	var dtmDate1 = new Date(firstDate);
	var dtmDate2 = new Date(secondDate);

	return (dtmDate1.getTime() <= dtmDate2.getTime());
}


//********************************** checkDatesAndClusterCode(Objform) for AAA and Senior Discount ***************************** 
function checkDatesAndClusterCode(Objform)
{

	if (Objform.fromDate && Objform.toDate && Objform.clusterCode) {
		if((Objform.fromDate.value==null || 
		    Objform.fromDate.value== "") &&
		   (Objform.toDate.value==null ||
		    Objform.toDate.value=="") &&
		   (Objform.clusterCode.value!=""))
		{
			var choice = confirm("You have requested a special rate without specifying dates. We can store this information for your convenience, but cannot check rates and availability unless dates are specified.\n" +
								 "Click 'OK' to continue without dates, or 'Cancel' to go back and change your search options.");
			return choice;
		}
	}
}

/*
 * Function used when clicking on Find from the left nav of configurable promotion pages
 */
function configPromoSearch(objForm,siteId)
{
	var submit = checkDatesAndClusterCode(objForm) ;
	if(submit == null){
		submit = true ;
	}

	if(submit == true){
		//Submit the form for sites which supports the search
		if (siteId == '' || siteId == 'US' || siteId == 'UK' || siteId == 'AU' || siteId == 'FR' || siteId == 'DE' || siteId == 'CN' || siteId == 'LACA') {
			objForm.submit() ;
		}else{
			//Send to m.com seach page on global sites which do not support search
			sendUSSearchCommand(objForm);
		}
	}
}

/*
 * Submits the search to m.com on global sites which do not support search
 */
function sendUSSearchCommand(objForm) {

	var dateFormatPattern = $(':hidden[name=dateFormatPattern]').val().toLowerCase();
	var fromDate = objForm.fromDate.value;
	if(fromDate != null && fromDate != '' && isDateValid(fromDate,dateFormatPattern)){
		fromDate = dateInMMDDYYFormat(fromDate,dateFormatPattern) ;
	}
	var toDate = objForm.toDate.value;
	if(toDate != null && toDate != '' && isDateValid(toDate,dateFormatPattern)){
		toDate = dateInMMDDYYFormat(toDate,dateFormatPattern) ;
	}

	var city = objForm.city.value;
	var stateProvince = objForm.stateProvince.value;
	var country = objForm.country.value;	
	var numberOfGuests = objForm.guestCount.value;
	var numberOfRooms = objForm.roomCount.value;
	var numberOfNights = objForm.numberOfNights.value;
	var marriottRewardsNumber = objForm.marriottRewardsNumber.value;
	var marriottBrands = objForm.marriottBrands.value;
	
	//Hidden fields on the page
	var promotionType = objForm.promotionType.value;
	var searchType = objForm.searchType.value;
	var distanceUnitMeasure = objForm.distanceUnitMeasure.value;
	var searchRadius = objForm.searchRadius.value;
	var brandPickListType = objForm.brandPickListType.value;
	var showMarriottBrandPickList = objForm.showMarriottBrandPickList.value;
	var clusterCode = objForm.clusterCode.value;

	var params = "?city="+city+"&stateProvince="+stateProvince+"&country="+country;
	params = params + "&fromDate="+fromDate+"&toDate="+toDate+"&numberOfGuests="+numberOfGuests+"&numberOfRooms="+numberOfRooms+"&numberOfNights="+numberOfNights;;
	params = params + "&clusterCode="+clusterCode+"&promotionType="+promotionType+"&searchType="+searchType+"&distanceUnitMeasure="+distanceUnitMeasure+"&searchRadius="+searchRadius;
	params = params + "&brandPickListType="+brandPickListType+"&showMarriottBrandPickList="+showMarriottBrandPickList;
	params = params + "&marriottRewardsNumber="+marriottRewardsNumber+"&marriottBrands="+marriottBrands;

	var url = "/promotionProcess.mi" ;
	javascript:sendto('US','default_domain',url+params,'3');
}

