Validate = {	submit : function() {		if (this.all() == true) {			if (typeof (self.parent.showLoadingBeforeResults) != "undefined") {				self.parent.showLoadingBeforeResults('true');			}			return true;		} else			return false;	},	// Updates the number of selectable children based on the number of adults	party : function(selectBox) {		if (parametersManager.getValue('adults') != undefined)			return true;		var noAdults, noChildren, allowedChildren;		noAdults = jQuery('#partyComposition\\.adults').val();		noChildren = jQuery('#partyComposition\\.children').val();		noInfants = jQuery('#partyComposition\\.infants').val();		if (noChildren = undefined)			noChildren = 0;		if (noInfants = undefined)			noInfants = 0;		allowedChildren = Math.min(9 - noAdults, noAdults * 2);		if (parseInt(noAdults) + parseInt(noChildren) + parseInt(noInfants) > 9				|| noChildren > allowedChildren) {			if (arguments.length > 0) {				jQuery(selectBox).val(0);				if (selectBox.id == 'partyComposition.children') {					jQuery('div.qtip:visible').qtip('hide');				}			}			else {				jQuery('#partyComposition\\.children').val(0);				jQuery('#partyComposition\\.infants').val(0);				jQuery('div.qtip:visible').qtip('hide');			}			alert('The total size of the party cannot exceed 9 people and only a maximum of 2 children per adult is allowed.');			return false;		} else			return true;	},	childAges : function() {		var returnBool = true;		if (jQuery("#partyComposition\\.children").val() != '0'				&& jQuery("#partyComposition\\.children").val() != undefined) {			if (parametersManager.getValue('children') != undefined) {				jQuery('#childAges input').each(function(i) {					if (this.value < 2 || this.value > 12 || this.value == "") {						returnBool = false;					}				});			} else {				// For each input field within the childages qtip				jQuery('.qtip #childAges input')						.each(								function(i) {									if (this.value < 2 || this.value > 12											|| this.value == "") {										returnBool = false;									}									else {										setAgeString();										// For each qtip child age, copy it into										// the forms child ages										jQuery(												'#childAgeContent.hide #childAges input#' + this.id)												.val(this.value);									}								});			}			if (returnBool == false) {				alert('Child ages must be between 2 and 12 years of age and cannot be left blank.');				if (parametersManager.getValue('children') == undefined) {					jQuery('#partyComposition\\.children').val('0');					jQuery('#childString').html('');					jQuery('div.qtip:visible').qtip('hide');				}			}			return returnBool;		}		else			return true;	},	all : function() {		if (this.party() && this.childAges())			return true;		else			return false;	}}// Global variable for holding the last selected location box - have been trying// so hard not to use this :)var lastLocationBox = "";function addChildAges(selectId) {	if (Validate.party(document.getElementById(selectId))) {		var numberOfChildren = dwr.util.getValue(selectId)		var div = dwr.util.byId("childAges");		// First clear all current children		while (div.hasChildNodes()) {			div.removeChild(div.firstChild);		}		for ( var i = 0; i < numberOfChildren; i++) {			var label = document.createElement("label");			label.innerHTML = "Child " + (i + 1);			div.appendChild(label);			var input = document.createElement("input");			input.id = "childAges" + i;			input.name = "childAges[" + i + "]";			div.appendChild(input);		}		jQuery('div.qtip-content').html(jQuery('div#childAgeContent').html());	}}// Entry point for making location DWR call by type and creating and populating// new location select box.function performTypeSearch(holidayTypeBox) {	var selectedHolidayTypeId = dwr.util.getValue(holidayTypeBox.id);	var businessContextId = dwr.util.getValue("businessContext");	// Find the current location box id	var locationBoxId = findCurrentLocationBoxId();	var locationBox = dwr.util.byId(locationBoxId);	var selectLocationId = dwr.util.getValue(locationBoxId);	// Perform search	Searcher.getLocationsByType(selectLocationId, businessContextId,			selectedHolidayTypeId, {				callback : function(response) {					doHolidayTypes(response, locationBox);				}			});}// Callback function for holidayTypesfunction doHolidayTypes(locationBean, locationBox) {	var locations = locationBean.locations;	// Find child location box if existing	var currentLocationBoxDepth = locationBox.id			.substring(locationBox.id.length - 1);	var newDepth = parseInt(currentLocationBoxDepth) + 1;	var childLocationBoxId = "selectedLocations" + newDepth;	var childLocationBox = dwr.util.byId(childLocationBoxId);	// If currently a child select box	if (childLocationBox != null) {		// Remove old box		childLocationBox.parentNode.removeChild(childLocationBox);	}	// If we have new locations to add, create new box and add.	if (locations.length > 0) {		var select = createSelectBox(childLocationBoxId, "selectedLocations["				+ newDepth + "]",				"performLocationSearch(this, this.options[this.selectedIndex].value)");		locationBox.parentNode.appendChild(select);		dwr.util.addOptions(select.id, [ {			id : "-1",			name : "Anywhere"		} ], "id", "name");		dwr.util.addOptions(select.id, locations, "id", "name");	}	// If we have departures, update them	var departures = locationBean.departures;	if (departures.length > 0) {		dwr.util.removeAllOptions("departureSelect");		dwr.util.addOptions("departureSelect", [ {			id : "-1",			name : "Anywhere"		} ], "id", "name");		dwr.util.addOptions("departureSelect", departures, "id", "name");		sortDepartures();	}}function sortDepartures() {	if (location.href.indexOf('travelcitydirect') != -1			|| location.href.indexOf('tcd') != -1) {		var selectBox = document.getElementById('departurePoint');		var airportMap = {};		var airportArray = [];		// Save options from departurPoint dropdown		jQuery(selectBox).children('option').each(function(i) {			airportMap[this.text] = this.value; // Used to store name value				// pairs of airport location				// names to ids				airportArray[i] = this.text; // array stores names in an				// order (so we can sort)			});		jQuery(selectBox).children('option:selected').removeAttr('selected');		airportArray = airportArray.sort();		airportArray.reverse();		// Finds anywhere and puts it at the beginning		for (i in airportArray) {			if (airportArray[i] == 'Anywhere') {				var temp = airportArray.splice(i, 1);				airportArray.unshift(temp);			}			else if (airportArray[i] == 'Manchester International Apt') {				var temp = airportArray.splice(i, 1);				airportArray.unshift(temp);			}		}		jQuery(selectBox).children('option').each(function(i) {			this.value = airportMap[airportArray[i]];			this.text = airportArray[i];		});		// Either select from the session or default to the first value		if (selectedDep != -1) {			jQuery(selectBox).children('option[value=' + selectedDep + ']')					.attr('selected', 'selected');		} else {			jQuery(selectBox).children('option:first').attr('selected',					'selected');		}	}}// Entry point for making location DWR call and creaing and populating new// select box.function performLocationSearch(selectBox, locationId) {	// If the locationId == -1 then use the parent location to search with if it	// exists - unless it's the root being changed	if (locationId == "-1") {		var selectBoxId = selectBox.id;		var depth = selectBoxId.substring(selectBoxId.length - 1);		if (depth != "0") {			var parentDepth = parseInt(depth) - 1;			var parentSelectBoxId = "selectedLocations" + parentDepth;			var locationId = dwr.util.getValue(parentSelectBoxId);			var selectBox = dwr.util.byId(parentSelectBoxId);		}	}	var businessContextId = dwr.util.getValue("businessContext");	Searcher.getLocations(locationId, businessContextId, {		callback : function(response) {			doLocations(response, selectBox);		}	});}// Callback function for locationsfunction doLocations(locationBean, selectBox) {	var locations = locationBean.locations;	// Calculate new depth in the location tree and derive new select box id	var selectBoxId = selectBox.id;	var parentDepth = selectBoxId.substring(selectBoxId.length - 1);	var newDepth = parseInt(parentDepth) + 1;	var newSelectBoxId = "selectedLocations" + newDepth;	// If the select box already exists we are altering an already select	// location	// Clear all children	if (document.getElementById(newSelectBoxId) != null) {		removeLocationsBoxes(newDepth, selectBox.parentNode);	}	// Create new selectbox place it in a div and add to the parent div	// Unless there are no children	if (locations.length != 0) {		// Construct new select box		var select = createSelectBox(newSelectBoxId, "selectedLocations["				+ newDepth + "]",				"performLocationSearch(this, this.options[this.selectedIndex].value)");		selectBox.parentNode.appendChild(select);		// Add the options to the select box		dwr.util.addOptions(select.id, [ {			id : "-1",			name : "Anywhere"		} ], "id", "name");		dwr.util.addOptions(select.id, locations, "id", "name");	}	// Insert new holiday types	dwr.util.removeAllOptions("holidayTypeSelect");	var contexts = locationBean.contexts;	dwr.util.addOptions("holidayTypeSelect", [ {		id : "-1",		name : "Any type of holiday"	} ], "id", "name");	dwr.util.addOptions("holidayTypeSelect", contexts, "id", "name");	var departures = locationBean.departures;	if (departures.length > 0) {		dwr.util.removeAllOptions("departureSelect");		dwr.util.addOptions("departureSelect", [ {			id : "-1",			name : "Anywhere"		} ], "id", "name");		dwr.util.addOptions("departureSelect", departures, "id", "name");		sortDepartures();	}}function createSelectBox(id, name, onchangeText) {	var select = document.createElement("select");	select.id = id;	select.name = name	select.setAttribute("onChange", onchangeText);	return select;}// Removes all location select boxes below the the depthToRemoveFrom value from// the wrappingElement.function removeLocationsBoxes(depthToRemoveFrom, wrappingElement) {	var toRemove = [];	var childElements = wrappingElement.childNodes;	for ( var i = 0; i < childElements.length; i++) {		for ( var j = depthToRemoveFrom; j < childElements.length; j++) {			if (childElements[i].id == "selectedLocations" + j) {				toRemove.push(childElements[i].id);			}		}	}	for ( var i = 0; i < toRemove.length; i++) {		var node = document.getElementById(toRemove[i]);		node.parentNode.removeChild(node);	}}function findCurrentLocationBoxId() {	// if lastLocationBox == "" then this is first load, so use the last	// location box on the page	if (lastLocationBox == "") {		var locationsWrapper = dwr.util.byId("locations");		var childSelects = locationsWrapper.childNodes;		for ( var i = 0; i < childSelects.length; i++) {			for ( var j = 0; j < childSelects.length; j++) {				if (childSelects[i].id == "selectedLocations" + j) {					if (dwr.util.getValue(childSelects[i].id) != -1) {						lastLocationBox = childSelects[i].id;					}				}			}		}	}	return lastLocationBox;}function setCookie(name, value, daysToExpire) {	var expiryDate = new Date();	expiryDate.setDate(expiryDate.getDate() + daysToExpire);	document.cookie = name			+ "="			+ escape(value)			+ ((daysToExpire == null) ? "" : ";expires="					+ expiryDate.toGMTString());}function getCookie(name) {	if (document.cookie.length > 0) {		cookieStart = document.cookie.indexOf(name + "=");		if (cookieStart != -1) {			cookieStart = cookieStart + name.length + 1;			cookieEnd = document.cookie.indexOf(";", cookieStart);			if (cookieEnd == -1) {				cookieEnd = document.cookie.length;			}			return unescape(document.cookie.substring(cookieStart, cookieEnd));		}	}	return "";}// Initialise the datepicker for the depart and return calendarsjQuery(document)		.ready(function() {			document.getElementById('searchCriteria').reset();			// deppoint 30-apr-2010				var departurePoints = {					"Manchester International Apt" : "18493",					"Norwich Apt" : "18499",					"London Gatwick Apt" : "18490",					"London Heathrow Apt" : "18491",					"Newcastle Apt" : "18500",					"Glasgow International Apt" : "18498",					"Edinburgh Apt" : "18497",					"Bristol Apt" : "18495",					"Birmingham International Apt" : "18494",					"Belfast International Apt" : "18496",					"Cardiff Apt" : "18501"				}				jQuery('#departurePoint')						.children('option')						.each(								function(i) {									jQuery('#departurePoint')											.children('option')[i].value = departurePoints[jQuery(											'#departurePoint').children(											'option')[i].text];								});				// deppoint				sortDepartures(); // Departures sorted alph. desc. TCD only				// New calendar manager object - see calendars.js				calendarManager = new CalendarManager();				// Show hide fade function for calendar popup				jQuery.fn.fadeToggle = function(speed, easing, callback)				{					return this.animate( {						opacity : 'toggle'					}, speed, easing, callback);				};				// Limit TCD duration to 7 or 14 nights (14 only for Florida)				if (parametersManager.getValue('hotelId') == undefined)					calendarManager.limitDuration();				// If parameters have been passed in, change the date				// appropriatley (start date & duration)				if (parametersManager.getValue('startDate') != undefined						&& parametersManager.getValue('duration') != undefined) {					calendarManager.changeDuration(parametersManager							.getValue('duration'));					jQuery('#dateFrom').datepicker(							'setDate',							calendarManager.calendarInfo									.stringToDate(parametersManager											.getValue('startDate')));					jQuery('#dateFrom').find('td.ui-datepicker-current-day')							.trigger("click");				}				// Gets and sets min and max dates from parameters if it exists				if (parametersManager.getValue('minDate') != undefined						&& parametersManager.getValue('maxDate') != undefined						&& parametersManager.getValue('duration') != undefined) {					jQuery('#dateFrom').datepicker(							'option',							'minDate',							calendarManager.calendarInfo									.stringToDate(parametersManager											.getValue('minDate')));					jQuery('#dateFrom').datepicker(							'option',							'maxDate',							calendarManager.calendarInfo									.stringToDate(parametersManager											.getValue('maxDate')));					var fromDate = new Date(							calendarManager.calendarInfo									.stringToDate(parametersManager											.getValue('minDate')));					fromDate.addDays(parametersManager.getValue('duration'));					jQuery('#dateTo').datepicker('option', 'minDate', fromDate);					fromDate = new Date(							calendarManager.calendarInfo									.stringToDate(parametersManager											.getValue('maxDate')));					fromDate.addDays(parametersManager.getValue('duration'));					jQuery('#dateTo').datepicker('option', 'maxDate', fromDate);				}				// Hotfix for departure ids all being the same				jQuery("#locations select:last").trigger("change");				// Using qtip plugin to do child ages popup				jQuery('#partyComposition\\.children')						.qtip(								{									content : {										text : jQuery('div#childAgeContent')												.html(),										prerender : true									},									api : {										beforeShow : function() {											if (jQuery(													'#partyComposition\\.children')													.val() == '0') {												jQuery('#childString').html('');												return false;											}										},										beforeHide : function() {											Validate.childAges();										}									},									show : {										when : {											target : jQuery('#partyComposition\\.children'),											event : 'change'										}									},									hide : {										when : {											target : jQuery('a.okChild'),											event : 'click'										}									},									position : {										adjust : {											x : 121,											y : 0										},										corner : {											target : 'rightMiddle',											tooltip : 'leftMiddle'										}									},									style : {										tip : {											corner : 'leftMiddle',											color : '#f9f9f9;',											size : {												x : 31, // Be careful that the x												// and y values refer to												// coordinates on												// screen, not height or												// width.												y : 50											// Depending on which corner your											// tooltip is at, x and y could mean											// either height or width!											}										},										border : {											width : 0,											radius : 0,											color : '#989898'										},										width : 215,										padding : 10									}								});				Validate.party();			});// Show the ages stringfunction setAgeString() {	var ageString = "Child ages (";	for ( var i = 0; i < jQuery('#partyComposition\\.children').val(); i++) {		var cid = "#childAges" + i;		ageString += jQuery('div.qtip-content > div').children(cid).val();		if (i < +jQuery('#partyComposition\\.children').val() - 2)			ageString += ', ';		else if (i == +jQuery('#partyComposition\\.children').val() - 2)			ageString += ' and ';	}	ageString += ") <a onclick=jQuery('.qtip').qtip('show') class='clickable'>change</a>";	jQuery('#childString').html(ageString);}
