// Presentation Block Functions

/**
 * When 'amend' or 'Add Room' links are clicked
 * it is populated with index of a room that is going to be amended or added.
 */
var currentRoomIndex = 0;
// 0 -update, 1 - add, 2- delete
var currentOperation = 0;
/**
 * When page is loaded this function displays already selected party. Strongly dependent on HTML.
 */
function displayInitialPartySelected(roomDivId, roomIndex) {
	jQuery('#' + roomDivId).show();
	
	var paxDiv = '#' + roomDivId + ' div';
	
	var divHTML = jQuery(paxDiv).html();

	// fix for IE(6,7)
	var linkIndex = divHTML.indexOf("<a");
	if (linkIndex == -1) {
		linkIndex = divHTML.indexOf("<A");
	}
	
	var linkHTML = divHTML.substring(linkIndex);

	divHTML = '<span class="sptxtstyle1">';
	if (initialPaxCombination[roomIndex].adults == "1") {
		divHTML += initialPaxCombination[roomIndex].adults + ' Adult';
	} else {
		divHTML += initialPaxCombination[roomIndex].adults + ' Adults';
	}

	if (initialPaxCombination[roomIndex].children == "1") {
		divHTML += ', ' + initialPaxCombination[roomIndex].children + ' Child';
	} else if (initialPaxCombination[roomIndex].children != "0") {
		divHTML += ', ' + initialPaxCombination[roomIndex].children + ' Children';
	}
	divHTML += '</span>';

	divHTML += '<span class="sptxtstyle2">';
	if (initialPaxCombination[roomIndex].infants == "1") {
		divHTML += initialPaxCombination[roomIndex].infants + ' Infant';
	} else if (initialPaxCombination[roomIndex].infants != "0") {
		divHTML += initialPaxCombination[roomIndex].infants + ' Infants';
	}
	divHTML += '</span>';
	
	divHTML += linkHTML;

	jQuery(paxDiv).html(divHTML);
}

/**
 * When 'amend' link is clicked this function displays pop-up
 * populating all necessary variables with actual data.
*/
function amendPartyRoom(currentRoomNumber) {
	if (!checkPageLoaded()) return;
	//jQuery('#faddroom').show();
	jQuery('#party-overlay').show();

	// Checks and displays (or not) 'Delete Room' link. Strongly dependent on HTML.
	if (currentRoomNumber == "1" && initialPaxCombination.length == 1) {
		document.getElementById('fdeleteroom').style.display = 'none';
	} else {
		document.getElementById('fdeleteroom').style.display = 'block';
	}

	currentRoomIndex = parseInt(currentRoomNumber) - 1;

	currentPaxCombination[currentRoomIndex] = initialPaxCombination[currentRoomIndex].clone();
	displayRoomPopup(currentRoomNumber, currentPaxCombination[currentRoomIndex].adults, currentPaxCombination[currentRoomIndex].children, 
			currentPaxCombination[currentRoomIndex].infants, currentPaxCombination[currentRoomIndex].maxChildAge, currentPaxCombination[currentRoomIndex].childAges);
}

/**
 * When 'Add Room' link is clicked this function displays pop-up
 * populating all necessary variables with actual data.
*/
function addRoomToParty() {
	if (!checkPageLoaded()) return;
	//jQuery('#faddroom').show();
	jQuery('#party-overlay').show();

	var currentAddingRoomNumber;
	if (jQuery('#froom2').is(":visible")) { // Checks for display:[none|block], ignores visible:[true|false]
		currentAddingRoomNumber = "3";
	} else {
		currentAddingRoomNumber = "2";
	}
	//document.getElementById('fdeleteroom').style.display = 'none'; // Do not displays 'Delete Room' link 

	currentRoomIndex = parseInt(currentAddingRoomNumber) - 1;

	currentPaxCombination[currentRoomIndex] = new PaxCombination(2, 0, 0, 12, new Array(), currentAddingRoomNumber); // By default: 2 adults, 0 children, 0 infants
	displayRoomPopup(currentAddingRoomNumber, currentPaxCombination[currentRoomIndex].adults, currentPaxCombination[currentRoomIndex].children, currentPaxCombination[currentRoomIndex].infants, currentPaxCombination[currentRoomIndex].maxChildAge, currentPaxCombination[currentRoomIndex].childAges);
}

/**
 * Displays pop-up and defines possible options for dropdowns.
 * No more used
*/
function displayRoomPopup(roomNumber, adults, children, infants, maxChildAge, childAges) {
	definePossibleOptions(adults);

	// Avoids blank dropdowns in case we got nothing from Back-End
	if (paxCombinations == 0) { // Just checks if it is empty Array
		possibleAdultsOptions[adults] = true;
		possibleChildrenOptions[children] = true;
		possibleInfantsOptions[infants] = true;
	}

	populateOptionsForSelect('fnadult3', possibleAdultsOptions, adults);
	populateOptionsForSelect('fnchild3', possibleChildrenOptions, children);
	populateOptionsForSelect('fninfant3', possibleInfantsOptions, infants);
	populateOptionsForChildAgeSelects(children, childAges, maxChildAge);

	displayChildAgeDropdowns(children);
	checkChildrenAgeSelected(children);

	var facetAddRoomPopup = document.getElementById('faddroom');
	facetAddRoomPopup.getElementsByTagName("h4")[0].innerHTML = "Room " + roomNumber + " has: ";
	facetAddRoomPopup.style.display = 'block';
}

function displayChildAgeDropdowns(children) {
	if (children != "0") {
		document.getElementById('fchildages').style.display = 'block';
		for (var i = 7; i >= 1; i--) {
			if (i <= eval(children)) {
				document.getElementById('fnchage' + i).parentNode.style.display = 'block';
			} else {
				document.getElementById('fnchage' + i).parentNode.style.display = 'none';
			}
		}
	} else {
		document.getElementById('fchildages').style.display = 'none';
	}
}

/**
 * Checks whether all child age dropdowns are selected and displays error message if not.
*/
function checkChildrenAgeSelected(children) {
	if (children == null) {
		children = currentPaxCombination[currentRoomIndex].children;
	}

	jQuery('label', 'div#fchildages').css('color', 'black');

	var isAllChildrenAgesSelected = true;
	for (var i = 1; i <= eval(children); i++) {
		var childAgeSelect = document.getElementById('fnchage' + i);
		if (childAgeSelect[childAgeSelect.selectedIndex].value == "?") {
			isAllChildrenAgesSelected = false;
			jQuery('label', 'div#fchildages').eq(i - 1).css('color', 'red');
		} else {
			jQuery('label', 'div#fchildages').eq(i - 1).css('color', 'black');
		}
	}

	var errorDiv = document.getElementById('ffaceterror');
	if (children == "0" || isAllChildrenAgesSelected == true) {
		errorDiv.style.display = 'none';
	} else {
		errorDiv.style.display = 'block';
	}

	return isAllChildrenAgesSelected;
}

// Party Facet Business Logic Block Functions

PaxCombination = function (adults, children, infants, maxChildAge, childAges, roomIndex) {
	this.adults = adults;
	this.children = children;
	this.infants = infants;
	this.maxChildAge = maxChildAge;
	this.childAges = childAges;
	this.roomIndex = roomIndex;
};

PaxCombination.prototype = {
	adults : 2,
	children : 0,
	infants : 0,
	maxChildAge : null,
	childAges : null,
        roomIndex : null,

	/**
	 * Returns string in form of X_Y_Z_A_B_C_...
	 * where X - number of adults,
	 *       Y - number of children,
	 *       Z - number of infants,
	 *       A - max child age,
	 *       B, C, ... - children ages if children are present 
	*/
	trailString : function() {
		var minPossibleChildAge = 999;
		for (var i in paxCombinations) {
			if (paxCombinations[i].adults == this.adults && paxCombinations[i].children == this.children && paxCombinations[i].infants == this.infants) {
				var maxChosenChildAge = 0;
				for (var j in this.childAges) {
					if (parseInt(this.childAges[j]) > parseInt(maxChosenChildAge)) {
						maxChosenChildAge = this.childAges[j];
					}
				}
				if (this.maxChildAge == null || ((parseInt(paxCombinations[i].maxChildAge) >= parseInt(maxChosenChildAge)) && (parseInt(paxCombinations[i].maxChildAge) < parseInt(minPossibleChildAge)))) {
					minPossibleChildAge = paxCombinations[i].maxChildAge;
					this.maxChildAge = paxCombinations[i].maxChildAge;
				}
			}
		}
		var resultTrailString = facetId + '%3A' + this.adults + '_' + this.children + '_' + this.infants  + '_' + this.maxChildAge;
		for (var i in this.childAges) {
			resultTrailString += '_' + this.childAges[i];
		}
		if(this.roomIndex) {
			resultTrailString += '_' + this.roomIndex;
		}
		return resultTrailString;
	},

	clone : function() {
		return new PaxCombination(this.adults, this.children, this.infants, this.maxChildAge, this.childAges.slice(0), this.roomIndex);
	}
};

var paxCombinations = new Array(); // All available PAX Combinations received from Back-End
var initialPaxCombination = [new PaxCombination(2, 0, 0, 12, new Array(), 1)]; // By default: 1 room, 2 adults, 0 children, 0 infants
var currentPaxCombination = [new PaxCombination(2, 0, 0, 12, new Array(), 1)]; // By default: 1 room, 2 adults, 0 children, 0 infants
var possibleMaxChildAge = 0;
var possibleAdultsOptions = new Object();
var possibleChildrenOptions = new Object();
var possibleInfantsOptions = new Object();
//var childAgesHtml = new Array();

/**
 * Parses initial PAX Combinations received from Back-End into PaxCombination objects.
 * Parses and displays previously selected combination(s).
 * Populate all necessary variables with actual data.
*/
function parseInitialPaxValues() {
	for (var i in initialPaxValues) {
		var values = (initialPaxValues[i].substring(initialPaxValues[i].indexOf(":") + 1)).split("_");
		paxCombinations[i] = new PaxCombination(values[0], values[1], values[2], values[3]);
	}
	var activeTrailValueToProcess = ajaxFacetPanelEnabled ? jQuery('#activeTrailValueHidden').val() : activeTrailValue;
	var initialTrailValues = (activeTrailValueToProcess.substring(activeTrailValueToProcess.indexOf(facetId))).split(":");
	var index = 0;
	for (var i = 0; i < initialTrailValues.length; i++) {
		if (initialTrailValues[i] != null && initialTrailValues[i].indexOf("_") != -1 && initialTrailValues[i - 1] == facetId) {
			var partyValues = initialTrailValues[i].split("_");
			var childAgeArray = new Array();
			for (var j = 4; j < partyValues.length && j < partyValues[1]; j++) {
				childAgeArray.push(partyValues[j]);
			}
			var roomIndexPosition = Number(partyValues[1]) + 4;
			if(roomIndexPosition < partyValues.length) {
				roomIndex = partyValues[roomIndexPosition];	
			} else {
				roomIndex = i;
			}
			initialPaxCombination[index] = new PaxCombination(partyValues[0], partyValues[1], partyValues[2], partyValues[3], childAgeArray, roomIndex);
			currentPaxCombination[index] = initialPaxCombination[index].clone();
			/**commented out as a part of multi room functionality **/
			//displayInitialPartySelected('froom' + (index + 1), index);
			index++;
		} else {
			/**commented out as a part of multi room functionality **/
			//displayInitialPartySelected('froom1', 0);
		}
	}
	if (index == 3) {
		jQuery('#faddroomlink').hide();
	}
}

/**
 * Searches through possible paxCombinations and dynamically defines
 * possible values for adults, children, infants and max child age.
 *
 * CORE method !!! Think twice or better thrice before changing anything here.
 *
*/
function definePossibleOptions(adults, children, infants, maxChildAge) {
	possibleMaxChildAge = 0;
	possibleAdultsOptions = new Object();
	possibleChildrenOptions = new Object();
	possibleInfantsOptions = new Object();

	for (var i in paxCombinations) {
		possibleAdultsOptions[paxCombinations[i].adults] = true;

		if (adults != null && paxCombinations[i].adults != adults) {
			continue;
		}
		if (children != null && paxCombinations[i].children != children) {
			continue;
		}
		if (infants != null && paxCombinations[i].infants != infants) {
			continue;
		}

		possibleChildrenOptions[paxCombinations[i].children] = true;
		possibleInfantsOptions[paxCombinations[i].infants] = true;

		if (paxCombinations[i].maxChildAge > possibleMaxChildAge) {
			possibleMaxChildAge = paxCombinations[i].maxChildAge;
		}
	}
}

/**
 * Populates dropdown (adults\children\infants) with given values.
 * Not in use.
*/
function populateOptionsForSelect(selectId, possibleOptions, currentOption) {
	var hasCurrentOption = false;
	var select = jQuery('#' + selectId);
	jQuery('option', select).remove();
	for (var i in possibleOptions) {
		if (possibleOptions[i] == true) {
			select.append(jQuery('<option></option>').val(i).html(i));
			if (currentOption == i) {
				select.val(i);
				hasCurrentOption = true;
			}
		}
	}
	select.show();
	return hasCurrentOption;
}

/**
 * Populates child age dropdowns with given values.
*/
function populateOptionsForChildAgeSelects(children, currentOptions, maxChildAge) {
	if (maxChildAge == null) {
		maxChildAge = possibleMaxChildAge;
	}
	for (var i = 1; i <= eval(children); i++) {
		var childAgeSelect = jQuery('#fnchage' + i);
		jQuery('option', childAgeSelect).remove();
		childAgeSelect.append(jQuery('<option></option>').val("?").html("?"));
		for (var j = 2; j <= maxChildAge; j++) {
			childAgeSelect.append(jQuery('<option></option>').val(j).html(j));
		}
		if (currentOptions != null && currentOptions[i - 1] != null) {
			childAgeSelect.val(currentOptions[i - 1]);
		} else {
			childAgeSelect.val("?");
		}
	}
}

/**
 * Dynamically redefines possible values for children\infants with selected adults.
*/
function changeSelectedAdults(adults) {
	currentPaxCombination[currentRoomIndex].adults = adults;

	// Checks whether previously selected children\infants suit newly selected adults
	definePossibleOptions(adults);
	if (possibleChildrenOptions[currentPaxCombination[currentRoomIndex].children] == true) {
		definePossibleOptions(adults, currentPaxCombination[currentRoomIndex].children);
		if (possibleInfantsOptions[currentPaxCombination[currentRoomIndex].infants] != true) {
			currentPaxCombination[currentRoomIndex].children = "0"; // If not sets children\infants to '0'
			currentPaxCombination[currentRoomIndex].infants = "0";
			currentPaxCombination[currentRoomIndex].childAges = new Array();
		}
	} else {
		currentPaxCombination[currentRoomIndex].children = "0"; // If not sets children\infants to '0'
		currentPaxCombination[currentRoomIndex].infants = "0";
		currentPaxCombination[currentRoomIndex].childAges = new Array();
	}

	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, currentPaxCombination[currentRoomIndex].children, currentPaxCombination[currentRoomIndex].infants);
	currentPaxCombination[currentRoomIndex].maxChildAge = possibleMaxChildAge;

	// Defines and populates children\infants\child age dropdowns with possible values for selected adults
	if(currentPaxCombination[currentRoomIndex].children != 0) {
		changeSelectedChildren(currentPaxCombination[currentRoomIndex].children);
	} else {
		definePossibleOptions(adults);
		populateOptionsForSelect('fnchild3', possibleChildrenOptions, currentPaxCombination[currentRoomIndex].children);
		populateOptionsForSelect('fninfant3', possibleInfantsOptions, currentPaxCombination[currentRoomIndex].infants);
		//populateOptionsForChildAgeSelects(currentPaxCombination[currentRoomIndex].children, currentPaxCombination[currentRoomIndex].childAges, currentPaxCombination[currentRoomIndex].maxChildAge);
		displayChildAgeDropdowns(currentPaxCombination[currentRoomIndex].children);
	}
}

/**
 * Dynamically redefines possible values for infants with selected adults\children.
*/
function changeSelectedChildren(children) {
	currentPaxCombination[currentRoomIndex].children = children;

	// Checks whether previously selected adults\infants suit newly selected children
	//alert(currentPaxCombination[currentRoomIndex].adults +"::::::::::::::::::"+ children);
	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, children);
	if (possibleInfantsOptions[currentPaxCombination[currentRoomIndex].infants] != true) {
		currentPaxCombination[currentRoomIndex].infants = "0"; // If not sets infants to '0'
	}

	// Adjusts child age values according to newly selected children
	if (children == "0") {
		currentPaxCombination[currentRoomIndex].childAges = new Array();
	} else {
		//alert(currentPaxCombination[currentRoomIndex].childAges.length);
		//alert(eval(children));
		if (currentPaxCombination[currentRoomIndex].childAges.length > eval(children)) {
			var size = currentPaxCombination[currentRoomIndex].childAges.length;
			for (var i = 0; i < (size - eval(children)); i++) {
				currentPaxCombination[currentRoomIndex].childAges.pop();
			}
		}
	}

	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, children, currentPaxCombination[currentRoomIndex].infants);
	currentPaxCombination[currentRoomIndex].maxChildAge = possibleMaxChildAge;

	// Defines and populates infants\child age dropdowns with possible values for selected adults\children
	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, children);
	populateOptionsForSelect('fninfant3', possibleInfantsOptions, currentPaxCombination[currentRoomIndex].infants);
	//populateOptionsForChildAgeSelects(children, currentPaxCombination[currentRoomIndex].childAges, currentPaxCombination[currentRoomIndex].maxChildAge);
	displayChildAgeDropdowns(children);
	document.getElementById('ffaceterror').style.display = 'none';
}

/**
 * Dynamically redefines possible values for children with selected adults\infants.
*/
function changeSelectedInfants(infants) {
	currentPaxCombination[currentRoomIndex].infants = infants;

	// Checks whether previously selected adults\children suit newly selected infants
	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, null, infants);
	if (possibleChildrenOptions[currentPaxCombination[currentRoomIndex].children] != true) {
		currentPaxCombination[currentRoomIndex].children = "0"; // If not sets children to '0' 
		currentPaxCombination[currentRoomIndex].childAges = new Array();
	}

	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, currentPaxCombination[currentRoomIndex].children, infants);
	currentPaxCombination[currentRoomIndex].maxChildAge = possibleMaxChildAge;

	// Defines and populates children\child age dropdowns with possible values for selected adults\infants
	definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, null, infants);

	populateOptionsForSelect('fnchild3', possibleChildrenOptions, currentPaxCombination[currentRoomIndex].children);
	populateOptionsForChildAgeSelects(currentPaxCombination[currentRoomIndex].children, currentPaxCombination[currentRoomIndex].childAges, currentPaxCombination[currentRoomIndex].maxChildAge);
	displayChildAgeDropdowns(currentPaxCombination[currentRoomIndex].children);
}

function changeSelectedChildAge(childAgeSelect, childAgeSelectedValue) {
	var childAgeIndex = parseInt(childAgeSelect.id.substring(childAgeSelect.id.length - 1));
	currentPaxCombination[currentRoomIndex].childAges[childAgeIndex - 1] = childAgeSelectedValue;
}

// JQuery functions 

$(function() {
	var windowSize = getDimensions();
	
	// Overlay
	jQuery('<div></div>')
				.attr('id', 'party-overlay')
				.css({
					display: 'none',
					backgroundColor: '#000000',
					opacity: 0.3,
					height: windowSize[0],
					width: windowSize[1],
					position: 'fixed',
					left: 0,
					top: 0,
					zIndex: 10
				})
				.appendTo('body');

	// Update Results Button Click checks whether all child age selected and submits the search if so
	jQuery('#fbuttonlink').live('click', function(e) {
		var childrenSelected = checkChildrenAgeSelected();
		if (childrenSelected == true) {
			composeLocationHref();
			jQuery('#faddroom').hide();
			
			var fp = position(jQuery('#froom' + (currentRoomIndex + 1))[0]);
			if (!fp || fp.y == 0) {
				fp = position(jQuery('#froom' + (currentRoomIndex))[0]);
			}
			var waitElem = jQuery('div.facetwaitprogress');
			var fpsearch = jQuery("div.fpsearch");

			waitElem.css("display", "block").css("top", fp.y - 45)
				.css("left", position(fpsearch[0]).x 
					+ parseInt(fpsearch.css('width'))
					+ parseInt(fpsearch.css('paddingLeft'))
					+ parseInt(fpsearch.css('paddingRight'))
					+ parseInt(fpsearch.css('borderLeftWidth'))
					+ parseInt(fpsearch.css('borderRightWidth')) 
					+ 10 
				);
			
			triggerSearch();
			
		}
		return false;
	});

	// Closes Pop-Up and reverts changes in variables to initial state
	jQuery('#fpartyclosebutton').live('click', function(e) {
		jQuery('#faddroom').hide();
		jQuery('#party-overlay').hide();
		if (initialPaxCombination[currentRoomIndex] != null) {
			currentPaxCombination[currentRoomIndex] = initialPaxCombination[currentRoomIndex].clone();
		} else if (currentPaxCombination[currentRoomIndex] != null) {
			currentPaxCombination.splice(currentRoomIndex, 1);
		}
		return false;
	});

	/**
	 * Composes href for a search substituting actual values
	*/
	function composeLocationHref() {
		
		var resultTrailString = "";
		for (var i in currentPaxCombination) {
			if (i > 0) {
				resultTrailString += '%3A';
			}
			resultTrailString += currentPaxCombination[i].trailString();
		}
		
		var prevActiveTrailValue = "";
		if(ajaxFacetPanelEnabled) {
			var activeTrailValueToProcess = jQuery('#activeTrailValueHidden').val();
			var prevActiveTrailValue = jQuery('#encodedActiveTrailValueHidden').val();
			if (activeTrailValueToProcess.indexOf(facetId) != -1) {
				var re = new RegExp("(:?" + facetId + ":\\d(_\\d+)+)+", "g");
				prevActiveTrailValue = activeTrailValueToProcess.replace(re, "");
				if (prevActiveTrailValue.charAt(0) == ":") {
					prevActiveTrailValue = prevActiveTrailValue.substring(1);
				}
				prevActiveTrailValue = prevActiveTrailValue.replace(/:/g, '%3A');
			}
			if(prevActiveTrailValue != "") {
				prevActiveTrailValue += '%3A';
			}
		}
		
		locationHref = locationHrefMock.replace("currentPaxCombination.trailString()", prevActiveTrailValue + resultTrailString).replace("nonActiveTrailValue", jQuery("#nonActiveTrailValueHidden").val());;
	};
});

	function buildLocationHref() {
		var activeTrailValue = jQuery('#encodedActiveTrailValueHidden').val();
		var resultTrailString = "";
		if (activeTrailValue == "" ) {
			currentOperation = 1;
			for (var i in currentPaxCombination) {
				if (i > 0) {
					resultTrailString += '%3A';
				}
				resultTrailString += currentPaxCombination[i].trailString();
			}
		} else {
			for (var i in currentPaxCombination) {
				if (i > 0) {
					resultTrailString += '%3A';
				}
				resultTrailString += currentPaxCombination[i].trailString();
			}
			if(activeTrailValue.indexOf(facetId) != -1){
				currentOperation = 0;
			} else {
				currentOperation = 1;
			}
		}
		switch(currentOperation) {
			case 1: //add
				if (activeTrailValue.lastIndexOf("%3A") < (activeTrailValue.length - "%3A".length) ) {
					activeTrailValue += "%3A";
				}
				activeTrailValue += resultTrailString;
				break;
			case 2: //delete
				activeTrailValue = updateTrail(activeTrailValue, resultTrailString);
				break;
			default: // update
				activeTrailValue = updateTrail(activeTrailValue, resultTrailString);
		}
		if (activeTrailValue.lastIndexOf("%3A") == (activeTrailValue.length - "%3A".length) ) {
			activeTrailValue = activeTrailValue.substring(0, activeTrailValue.length - "%3A".length);
		}
		locationHref = locationHrefMock.replace("currentPaxCombination.trailString()", activeTrailValue).replace("nonActiveTrailValue", jQuery("#nonActiveTrailValueHidden").val());
		//alert("locationHref::::"+locationHref);
	}

	function updateTrail(trailValues, resultTrail) {
		var delim = facetId + "%3A";
		var del = currentOperation == 2 ? true : false;
		var mTrail = "";
		var arrFacet = trailValues.split(delim);
		var prefix="";
		if(trailValues.indexOf(facetId)==0){
			prefix="";
		}else{
			prefix = trailValues.substring(0,trailValues.indexOf(facetId));
		}
		var suffix =  trailValues.substring(trailValues.lastIndexOf(facetId));
		var arrFacet = suffix.split("%3A");
		suffix="";
		for(i=2; i<arrFacet.length; i++){
			if(i<(arrFacet.length -1)){
				suffix+= arrFacet[i]+"%3A";
			}else{
				suffix+= arrFacet[i];
			}
		}
		resultTrail = prefix+ resultTrail+"%3A" +suffix;
		return resultTrail;
	}
	
	/**
	 * Composes href for a search substituting actual values
	*/
	function updateHrefForPaxMix(currentPaxCombination, seletedRoomCount) {
		var resultTrailString = "";
		for (var i in currentPaxCombination) {
			if (i > 0) {
				resultTrailString += '%3A';
			}
			resultTrailString += currentPaxCombination[i].trailString();
		}
		var prevActiveTrailValue = "";
		if(ajaxFacetPanelEnabled) {
			var activeTrailValueToProcess = jQuery('#activeTrailValueHidden').val();
			var prevActiveTrailValue = jQuery('#encodedActiveTrailValueHidden').val();
			if (activeTrailValueToProcess.indexOf(facetId) != -1) {
				var re = new RegExp("(:?" + facetId + ":\\d(_\\d+)+)+", "g");
				prevActiveTrailValue = activeTrailValueToProcess.replace(re, "");
				if (prevActiveTrailValue.charAt(0) == ":") {
					prevActiveTrailValue = prevActiveTrailValue.substring(1);
				}
				prevActiveTrailValue = prevActiveTrailValue.replace(/:/g, '%3A');
			}
			if(prevActiveTrailValue != "") {
				prevActiveTrailValue += '%3A';
			}
		}
		
		locationHref = locationHrefMock.replace("currentPaxCombination.trailString()", prevActiveTrailValue + resultTrailString).replace("nonActiveTrailValue", jQuery("#nonActiveTrailValueHidden").val());;
		//alert(locationHref);
	}
	
	/**
	 * When 'numAdults'changed , update the pax mix accordingly.
	**/
	function modifyAdultsPerRoom(currentRoomNumber, adultCount) {
		if (currentRoomNumber == "1" && initialPaxCombination.length == 1) {
			if(document.getElementById('fdeleteroom')!=null) document.getElementById('fdeleteroom').style.display = 'none';
		} else {
			if(document.getElementById('fdeleteroom')!=null){document.getElementById('fdeleteroom').style.display = 'block';}
		}
		var previousRoomNumber = jQuery('#numRoomSelected').val();
		var totalRoomCount;
		if(previousRoomNumber> currentRoomNumber){
			totalRoomCount = previousRoomNumber;
		}else{
			totalRoomCount = currentRoomNumber;
			}
		/**Re Intitalize the rooms******/
		var childAgeSelected = updateRoomDetails(totalRoomCount);
		/***************************************************************/
		validateChildAges();
		
		if(childAgeSelected){
		currentOperation=0;
		buildLocationHref();
		var fp = position(jQuery('#adultRm'+currentRoomNumber));
		if (!fp || fp.y == 0) {
			fp = position(jQuery('#froom' + (currentRoomIndex))[0]);
		}
		var waitElem = jQuery('div.facetwaitprogress');
		var fpsearch = jQuery("div.fpsearch");

		waitElem.css("display", "block").css("top", fp.y - 45)
			.css("left", position(fpsearch[0]).x 
				+ parseInt(fpsearch.css('width'))
				+ parseInt(fpsearch.css('paddingLeft'))
				+ parseInt(fpsearch.css('paddingRight'))
				+ parseInt(fpsearch.css('borderLeftWidth'))
				+ parseInt(fpsearch.css('borderRightWidth')) 
				+ 10 
			);	
			
			triggerSearch();
	}
	}

	/**
	 * Dynamically redefines possible values for infants with selected adults\children.
	**/
	function updateSelectedChildren(currentRoomNumber, childCount) {
		currentRoomIndex = parseInt(currentRoomNumber) - 1;
		//alert(currentRoomIndex);
		currentPaxCombination[currentRoomIndex].children = childCount;
		definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, childCount);
		if(eval(childCount)>0){
			jQuery('#childrenRmAge'+currentRoomNumber).css("display", "block");			
		}else{
			jQuery('#childrenRmAge'+currentRoomNumber).css("display", "none");
			jQuery('#ageOnReturn'+currentRoomNumber).css("display", "none");
		}
		// Adjusts child age values according to newly selected children
		if (childCount == "0") {
			currentPaxCombination[currentRoomIndex].childAges = new Array();
		} else {
			if (currentPaxCombination[currentRoomIndex].childAges.length > eval(childCount)) {
				var size = currentPaxCombination[currentRoomIndex].childAges.length;
				for (var i = 0; i < (size - eval(childCount)); i++) {
					currentPaxCombination[currentRoomIndex].childAges.pop();
				}
			}
		}

		definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, childCount, currentPaxCombination[currentRoomIndex].infants);
		currentPaxCombination[currentRoomIndex].maxChildAge = possibleMaxChildAge;
		definePossibleOptions(currentPaxCombination[currentRoomIndex].adults, childCount);
		populateOptionsForSelect('fninfant3', possibleInfantsOptions, currentPaxCombination[currentRoomIndex].infants);
		displayChildAgeDropdownsPerRoom(childCount, currentRoomNumber);
	}
	/**
		Display ChildDropDownValue based on user Selection 
	**/
	function displayChildAgeDropdownsPerRoom(children,currentRoomNumber ) {

		var previousChildSelectVal = jQuery('#currentChildCount'+currentRoomNumber).val();
		if(previousChildSelectVal< children){
			var innerHtml=jQuery('#childrenRmAge'+currentRoomNumber).html();
			if(innerHtml.length>0 && innerHtml.indexOf("<span id=\"ageOnReturn"+currentRoomNumber+"\"") >0){
				innerHtml = innerHtml.substring(0,innerHtml.indexOf("<span id=\"ageOnReturn"+currentRoomNumber+"\""));
			}
			for (var i = 1; i <= eval(children)-eval(previousChildSelectVal); i++) {
				innerHtml += "<div class=\"childAge\"><label for=\"child2AgeRm1\">Child "+eval(i+eval(previousChildSelectVal)) + " age* </label>";
				innerHtml += "<select onchange=\"populateChildAges(this.value,"+eval(i+eval(previousChildSelectVal))+","+currentRoomNumber+");\"  id=\"child_" +eval(i+eval(previousChildSelectVal))+"_"+currentRoomNumber+"\">"
				for(var j=0; j<=17; j++){
					if(j==0){innerHtml += "<option selected=\"selected\" value=\"?\">?</option>";}
					else if(j==1){innerHtml += "<option value="+j+"><"+2+"</option>";}
					else{
						innerHtml += "<option value="+j+">"+j+"</option>";
					}
				}
				innerHtml += "</select></div>";
			}
			jQuery('#ageOnReturn'+currentRoomNumber).css("display","block");
			jQuery('#childrenRmAge'+currentRoomNumber).html(innerHtml);

			jQuery('#currentChildCount'+currentRoomNumber).val(children);

			var previousRoomNumber = jQuery('#numRoomSelected').val();
			var totalRoomCount;
			if(previousRoomNumber> currentRoomNumber){
				totalRoomCount = previousRoomNumber;
			}else{
				totalRoomCount = currentRoomNumber;
				}
			var childAgeSelected = updateRoomDetails(totalRoomCount);
			/******************************/
			
		}//if the number of children selected by user is less than the previous selection.
		else if (previousChildSelectVal> children){
			/**Re Intitalize the rooms******/
			var previousRoomNumber = jQuery('#numRoomSelected').val();
			var totalRoomCount;
			if(previousRoomNumber> currentRoomNumber){
				totalRoomCount = previousRoomNumber;
			}else{
				totalRoomCount = currentRoomNumber;
				}
			var childAgeSelected = updateRoomDetails(totalRoomCount);
			/******************************/
			
			var innerHtml=jQuery('#childrenRmAge'+currentRoomNumber).html();
			if(navigator.appName == "Microsoft Internet Explorer")
			{
				innerHtml = innerHtml.substring(0,innerHtml.indexOf("<LABEL for=child2AgeRm1>Child "+eval(eval(children)+1)+" age* </LABEL>"));
			}else{
			innerHtml = innerHtml.substring(0,innerHtml.indexOf("<label for=\"child2AgeRm1\">Child "+eval(eval(children)+1)+" age* </label>"));
			}
			jQuery('#childrenRmAge'+currentRoomNumber).html(innerHtml);
			
			jQuery('#currentChildCount'+currentRoomNumber).val(children);
			//look for all the child agebox drop down if any one of these contains ? stop the search and display the error message
			//childrenRm1 childrenRm2 childrenRm3
			var ischildAgeValid=true;
			ischildAgeValid = validateChildAges();
			if(!ischildAgeValid){
				return ischildAgeValid;
			}
			//alert(previousChildSelectVal+":::::" +children +"  childAgeSelected" + childAgeSelected);
			if(childAgeSelected){
				//rm1ChildAgeError
				jQuery("#rm"+currentRoomNumber+"ChildAgeError").css('display', 'none');
				//update the searchqueryURL;
				currentOperation=0;
				buildLocationHref();
				var fp = position(jQuery('#childrenRm'+currentRoomIndex));
				if (!fp || fp.y == 0) {
					fp = position(jQuery('#froom' + (eval(currentRoomNumber)-1))[0]);
				}
				var waitElem = jQuery('div.facetwaitprogress');
				var fpsearch = jQuery("div.fpsearch");

				waitElem.css("display", "block").css("top", fp.y - 45)
					.css("left", position(fpsearch[0]).x 
						+ parseInt(fpsearch.css('width'))
						+ parseInt(fpsearch.css('paddingLeft'))
						+ parseInt(fpsearch.css('paddingRight'))
						+ parseInt(fpsearch.css('borderLeftWidth'))
						+ parseInt(fpsearch.css('borderRightWidth')) 
						+ 10 
					);
				//updateHrefForPaxMix(currentPaxCombination,eval(currentRoomNumber)-1);
				triggerSearch();
							jQuery('#ageOnReturn'+currentRoomNumber).css("display", "block");
			}else{
				jQuery("#rm"+currentRoomNumber+"ChildAgeError").css('display','block');
			}
		}
	}

	function populateChildAges(childAgeSelectedValue, childAgeIndex, currentRoomNumber) {
		//for loop for capturing the number of children and infant and accordingly update the paxmix childrenRm1, 
		currentRoomIndex = parseInt(currentRoomNumber) - 1;
		//alert(currentRoomIndex);
		/**Re Intitalize the rooms******/
		var previousRoomNumber = jQuery('#numRoomSelected').val();
		var totalRoomCount;
		if(previousRoomNumber> currentRoomNumber){
			totalRoomCount = previousRoomNumber;
		}else{
			totalRoomCount = currentRoomNumber;
			}
		var childAgeSelected = updateRoomDetails(totalRoomCount);
		/******************************/
		var ischildAgeValid=true;
		ischildAgeValid = validateChildAges();
		if(!ischildAgeValid){
			return ischildAgeValid;
		}
		
		if(childAgeSelected){
			//rm1ChildAgeError
			jQuery("#rm"+currentRoomNumber+"ChildAgeError").css('display', 'none');
			
			var fp = position(jQuery('#childrenRm'+currentRoomNumber));
			if (!fp || fp.y == 0) {
				fp = position(jQuery('#froom' + (currentRoomIndex))[0]);
			}
			var waitElem = jQuery('div.facetwaitprogress');
			var fpsearch = jQuery("div.fpsearch");

			waitElem.css("display", "block").css("top", fp.y - 45)
			.css("left", position(fpsearch[0]).x 
				+ parseInt(fpsearch.css('width'))
				+ parseInt(fpsearch.css('paddingLeft'))
				+ parseInt(fpsearch.css('paddingRight'))
				+ parseInt(fpsearch.css('borderLeftWidth'))
				+ parseInt(fpsearch.css('borderRightWidth')) 
				+ 10 
			);
			//update the searchqueryURL;
			currentOperation=0;
			buildLocationHref();
			triggerSearch();
		}else{
			jQuery("#rm"+currentRoomNumber+"ChildAgeError").css('display','block');
		}
	}

	/**
	 * Composes href for a search substituting actual values
	*/
	function composeLocationHref() {
		var resultTrailString = "";
		for (var i in currentPaxCombination) {
			if (i > 0) {
				resultTrailString += '%3A';
			}
			resultTrailString += currentPaxCombination[i].trailString();
		}
		
		var prevActiveTrailValue = "";
		if(ajaxFacetPanelEnabled) {
			var activeTrailValueToProcess = jQuery('#activeTrailValueHidden').val();
			var prevActiveTrailValue = jQuery('#encodedActiveTrailValueHidden').val();
			if (activeTrailValueToProcess.indexOf(facetId) != -1) {
				var re = new RegExp("(:?" + facetId + ":\\d(_\\d+)+)+", "g");
				prevActiveTrailValue = activeTrailValueToProcess.replace(re, "");
				if (prevActiveTrailValue.charAt(0) == ":") {
					prevActiveTrailValue = prevActiveTrailValue.substring(1);
				}
				prevActiveTrailValue = prevActiveTrailValue.replace(/:/g, '%3A');
			}
			if(prevActiveTrailValue != "") {
				prevActiveTrailValue += '%3A';
			}
		}
		locationHref = locationHrefMock.replace("currentPaxCombination.trailString()", prevActiveTrailValue + resultTrailString).replace("nonActiveTrailValue", jQuery("#nonActiveTrailValueHidden").val());;
		//alert(locationHref);
	}
		
	/**
	 * When 'Add Another Room' link is clicked this function adds another room with default values 2,0,0 and trigers the search
	 * populating all necessary variables with actual data.
	*/
	function addAnotherRoom() {
		var previousRoomCount = jQuery('#numRoomSelected').val();
		//alert(previousRoomCount);
		var currentAddingRoomNumber= eval(previousRoomCount)+1;
		jQuery('#numRoomSelected').val(currentAddingRoomNumber);
		jQuery('#froom'+currentAddingRoomNumber).css("display", "block");
		if(currentAddingRoomNumber==1){
			document.getElementById('fdeleteroom').style.display = 'none'; // Do not displays 'Delete Room' link 
		}else{
			jQuery('#fdeleteroom').html("Remove room "+(eval(currentRoomIndex)+1));
			displayAddRemoveLink("block", "block", currentAddingRoomNumber)
		}
		
		/**Re Intitalize the rooms******/
		for(var roomIndex=0; roomIndex< currentAddingRoomNumber; roomIndex++){
			//alert(roomIndex);
			if( roomIndex>0 && roomIndex == (eval(currentAddingRoomNumber)-1)){
				currentRoomIndex =roomIndex;
				currentPaxCombination[currentRoomIndex] = new PaxCombination(2, 0, 0, 17, new Array(), currentAddingRoomNumber); 
				// By default: 2 adults, 0 children, 0 infants
				currentOperation =1;
		}else {
				updatePaxMix(roomIndex);			
		}
		}
		/******************************/
		
		var ischildAgeValid=true;
		ischildAgeValid = validateChildAges();
		jQuery('#fdeleteroom').html("Remove room "+currentAddingRoomNumber);
		if(!ischildAgeValid){
			return ischildAgeValid;
		}
		
		buildLocationHref();
		var fp = position(jQuery('#faddroom'));
		if (!fp || fp.y == 0) {
			fp = position(jQuery('#froom' + (currentRoomIndex))[0]);
		}
		var waitElem = jQuery('div.facetwaitprogress');
		var fpsearch = jQuery("div.fpsearch");

		waitElem.css("display", "block").css("top", fp.y - 45)
		.css("left", position(fpsearch[0]).x 
			+ parseInt(fpsearch.css('width'))
			+ parseInt(fpsearch.css('paddingLeft'))
			+ parseInt(fpsearch.css('paddingRight'))
			+ parseInt(fpsearch.css('borderLeftWidth'))
			+ parseInt(fpsearch.css('borderRightWidth')) 
			+ 10 
		);
		triggerSearch();
	}

	/**
	*Remove the current room and trigger the search and udate the trail.
	**/
	function removeCurrentRoom(){
		currentRoomIndex = jQuery('#numRoomSelected').val();
		//alert(currentRoomIndex);
		jQuery('#froom'+currentRoomIndex).css("display", "none");
		currentRoomIndex = eval(currentRoomIndex) -1 ;
		currentOperation =2;
		for(i=1 ; i<=3; i++){
			var numChildrenRoom = jQuery('#childrenRm'+i).val();
			for(j=1; j <=eval(numChildrenRoom); j++){
				//if the value of any dropdown in '?' dont allow the user to search child_1_2 ie child_count_roomnumber
				var childAge = document.getElementById('child_'+j+'_'+i).value;
				if( childAge == "?" && i!= eval(currentRoomIndex+1)){
					jQuery('#rm'+i+'ChildAgeError').css("display", "block");
					 document.getElementById("facetDBPaxCombination").focus();
					//rest for all other set the didplay to none
					for(rmCount=1 ; rmCount<=3; rmCount++){
						if(rmCount!=i){
							jQuery('#rm'+rmCount+'ChildAgeError').css("display", "none");
						}
					}
					if(eval(currentRoomIndex)>1){
						jQuery("#fdeleteroom").html("Remove room "+currentRoomIndex);
						if(eval(currentRoomIndex)<3){
							jQuery('#faddroom').css("display", "block");
						}else{
							jQuery('#faddroom').css("display", "none");
						}
					}else{
						jQuery('#fdeleteroom').css("display", "none");
						jQuery('#faddroom').css("display", "block");
					}
					currentPaxCombination.splice(currentRoomIndex,1);
					jQuery('#numRoomSelected').val(eval(currentRoomIndex));
					jQuery("#childrenRmAge"+(eval(currentRoomIndex)+1)).html("");
					return;
				}
			}
		}
		document.getElementById('fdeleteroom').value="Remove room "+currentRoomIndex;
		currentPaxCombination.splice(currentRoomIndex,1);

		var childAgeSelected = updateRoomDetails(currentRoomIndex);		
		
		buildLocationHref();
		
		
		var fp = position(jQuery('#fdeleteroom'));
		if (!fp || fp.y == 0) {
			fp = position(jQuery('#froom' + (currentRoomIndex))[0]);
		}
		var waitElem = jQuery('div.facetwaitprogress');
		var fpsearch = jQuery("div.fpsearch");

		waitElem.css("display", "block").css("top", fp.y - 45)
		.css("left", position(fpsearch[0]).x 
			+ parseInt(fpsearch.css('width'))
			+ parseInt(fpsearch.css('paddingLeft'))
			+ parseInt(fpsearch.css('paddingRight'))
			+ parseInt(fpsearch.css('borderLeftWidth'))
			+ parseInt(fpsearch.css('borderRightWidth')) 
			+ 10 
		);
		
		triggerSearch();
	}
		
	function validatePaxMix(event){
		//return incase of error
		//alert("i am here ");
		for(i=1 ; i<=3; i++){
			var numChildrenRoom = jQuery('#childrenRm'+i).val();
			for(j=1; j <=eval(numChildrenRoom); j++){
				//if the value of any dropdown in '?' dont allow the user to search child_1_2 ie child_count_roomnumber
				var childAge = document.getElementById('child_'+j+'_'+i).value;
				if( childAge == "?"){
					jQuery('#rm'+i+'ChildAgeError').css("display", "block");
					 document.getElementById("facetDBPaxCombination").focus();
					//rest for all other set the didplay to none
					for(rmCount=1 ; rmCount<=3; rmCount++){
						if(rmCount!=i){
							jQuery('#rm'+rmCount+'ChildAgeError').css("display", "none");
						}
					}
					stopEvent(event);
					return false;
				}
			}
		}
		//if no error then it will trigger the showWaitEvent.
		showSearchWaitPage(event);
	}
	
	function stopEvent(e) {
		if(!e) var e = window.event;
		
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		e.cancelBubble = true;
		e.returnValue = false;

		//e.stopPropagation works only in Firefox.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
		return false;
	}
	
	function clearPreviousSelection(locationHref,event){
		//alert(locationHref);
		
		var fp = position(jQuery('#clearPrevSearch'));
		var waitElem = jQuery('div.facetwaitprogress');
		var fpsearch = jQuery("div.fpsearch");

		waitElem.css("display", "block").css("top", fp.y - 45)
		.css("left", position(fpsearch[0]).x 
			+ parseInt(fpsearch.css('width'))
			+ parseInt(fpsearch.css('paddingLeft'))
			+ parseInt(fpsearch.css('paddingRight'))
			+ parseInt(fpsearch.css('borderLeftWidth'))
			+ parseInt(fpsearch.css('borderRightWidth')) 
			+ 10 
		);
		
		if (ajaxFacetPanelEnabled) {
					jQuery('#party-overlay').hide();
					loadAjaxFacetPanel(locationHref  + '&expand=' + getFacetsToExpand());
		} else {
			//alert(currentRoomIndex);
			if(currentRoomIndex==2)	currentPaxCombination.splice(currentRoomIndex,2);
			if(currentRoomIndex==1) currentPaxCombination.splice(currentRoomIndex,1);
			var pageRefreshEnabled = jQuery('#pageRefreshEnabled').val();
			//alert(pageRefreshEnabled);
			if(pageRefreshEnabled == "false"){
				stopEvent(event);
				var ajaxUrl = locationHref;
				jQuery.ajax({
				url: ajaxUrl,
				success: function(data){
					var refinedData=data.substring(data.indexOf("<div id=\"facetpanel_phase2\">"),data.indexOf("<div id=\"EndSearchPanel\"></div>"));
					jQuery('#facetpanel_phase2').empty().html(refinedData);
					jQuery('#party-overlay').hide();
				}
			});
			}else{
				location.href = locationHref;
			}
		}
	}	
	
	function displayAddRemoveLink(addRoomStyle, removeRoomStyle, roomCount){
		//alert(addRoomStyle+"::::"+removeRoomStyle+"::::::"+roomCount);
		//alert(removeRoomText);
		var rmRoom= "<a class=\"removeRoom\" href=\"#\" id='fdeleteroom' onclick=\"removeCurrentRoom()\" style=\"display:${deleteRoomDisplay}\">"+ removeRoomText +" "+roomCount+"</a>";
		var addRoom = "<a class=\"addRoom\" href=\"#\" id='faddroom' onclick=\"addAnotherRoom()\" style=\"display:${addRoomDisplay}\">" + addRoomText + "</a>";
		if(removeRoomStyle=="block" && addRoomStyle=="block"){
			jQuery('#addRemoveRoom').html(rmRoom + addRoom);
		}else if(removeRoomStyle=="none" && addRoomStyle=="block"){
			jQuery('#addRemoveRoom').html(addRoom);
		}else if(removeRoomStyle=="block" && addRoomStyle=="none"){
			jQuery('#addRemoveRoom').html(rmRoom);
		}else if(removeRoomStyle=="none" && addRoomStyle=="none"){
			jQuery('#addRemoveRoom').html("");
		}
	}
	
	function updateRoomDetails(currentRoomIndex){
		/**Re Intitalize the rooms******/
		//alert(eval(currentRoomIndex)-1);
		var childAgeSelected = true;
		for(var roomIndex=0; roomIndex< currentRoomIndex; roomIndex++){
			childAgeSelected = updatePaxMix(roomIndex);
		}
		return childAgeSelected;
		/******************************/
	}
	
	function updatePaxMix(roomIndex){
		var childAgeSelected =true;
		var childAgeindex = 1+ eval(roomIndex);
		var adults = jQuery('#adultRm'+childAgeindex).val();
		totalCount = jQuery('#childrenRm'+childAgeindex).val();
		var roomInfantCount=0;
		var roomChildCount=0;
		var tempChildAges = new Array();
		var childAgeCount=0;
		for(var childCount=1;childCount<=totalCount; childCount++ ){
			//child_1_1
			var childAge = jQuery("#child_" +childCount+"_"+childAgeindex).val();
			//alert(childAge);
			if(childAge == "?"){
				childAgeSelected = false;
			}else if(eval(childAge) >= 2){
				tempChildAges[childAgeCount] = childAge;
				childAgeCount+=1;
				roomChildCount+=1;
			}else if(eval(childAge) < 2){
				roomInfantCount+=1;
			}
		}
		currentPaxCombination[roomIndex].adults = adults;
		currentPaxCombination[roomIndex].childAges = tempChildAges;
		//alert(currentPaxCombination[currentRoomIndex].childAges);
		currentPaxCombination[roomIndex].infants=roomInfantCount;
		currentPaxCombination[roomIndex].children=roomChildCount;
		//alert(currentPaxCombination[roomIndex].adults+ ":::"+currentPaxCombination[roomIndex].infants+ ":::"+currentPaxCombination[roomIndex].children+":::::"+currentPaxCombination[roomIndex].childAges);
		return childAgeSelected;
	}
	
	function validateChildAges(){
		var isValid = true;
		for(i=1 ; i<=3; i++){
			var childAgeErrorBox = jQuery('#rm'+i+'ChildAgeError');
			var infantCountErrorBox = jQuery('#rm'+i+'InfantCountError');
			childAgeErrorBox.hide();
			infantCountErrorBox.hide();
			var adults = Number(jQuery('#adultRm'+i).val());
			var infants = 0;
			var numChildrenRoom = Number(jQuery('#childrenRm'+i).val());
			for(j=1; j<=numChildrenRoom; j++){
				//if the value of any dropdown in '?' dont allow the user to search child_1_2 ie child_count_roomnumber
				if(document.getElementById('child_'+j+'_'+i)!=null){
					var childAge = document.getElementById('child_'+j+'_'+i).value;
					if( childAge == "?"){
						childAgeErrorBox.show().focus();
						isValid = false;
					} else if (childAge == "1") {
						++infants;
							}
						}
					}
			if (adults < infants) {
				isValid = false;
				infantCountErrorBox.show().focus();
				}
			}
		return isValid;
		}
	function triggerSearch(){
		if (ajaxFacetPanelEnabled) {
				jQuery('#party-overlay').hide();
				loadAjaxFacetPanel(locationHref  + '&expand=' + getFacetsToExpand());
		} else {
			var pageRefreshEnabled = jQuery('#pageRefreshEnabled').val();
			//alert(pageRefreshEnabled);
			/** the below mentioned condition is for loading/updating the facets with or without refreshing the page 
				fully configurable on page level. **/
			if(pageRefreshEnabled == "false"){
				var ajaxUrl = locationHref  + '&expand=' + getFacetsToExpand();
				jQuery.ajax({
				url: ajaxUrl,
				success: function(data){
					var refinedData=data.substring(data.indexOf("<div id=\"facetpanel_phase2\">"),data.indexOf("<div id=\"EndSearchPanel\"></div>"));
					jQuery('#facetpanel_phase2').empty().html(refinedData);
					jQuery('#party-overlay').hide();
				}
			});
			}else{
				location.href = locationHref  + '&expand=' + getFacetsToExpand();
			}
		}
	}
