		var cfgAirportsMatrixMaxBits = 31;
		var cfgMaxChildren = 7;
		var cfgMaxRooms = 8;
		var required_fields = new Array('airportcombinedfromid','airportgrouptoid','airporttoid');
		
		
		
		// avoid risk of corrupting UTF-8 in PHP file
		var laquo = '\u00ab';
		var raquo = '\u00bb';
		
		function create_xml_http_request()
		{
			if (window.XMLHttpRequest)
			{
				// If IE7, Mozilla, Safari, etc: Use native object
				return new XMLHttpRequest()
		
			}
			else if (window.ActiveXObject)
			{
				// Microsoft recommended fallback for old IEs (5 & 6)
				// http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspx
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			return false;
		}
		
		function srhfrm_filter_both_ways(parent_value, group_value, all_child_options, filter, child_dropdown, first_option, append)
		{
			var group = 1;
			var textPrefix='';
			var textSuffix='';
			var idPrefix='';
			var intOldVal=child_dropdown.value;
			var option_container = child_dropdown;
			var optgroup;
			
			// Need to detect if group or airport is selected.
			if (!append)
			{
				//child_dropdown.options.length = keep_first;
				child_dropdown.innerHTML = '';
				if (first_option)
				{
					child_dropdown[child_dropdown.length] = new Option
					(
						first_option,
						-1
					);
				}
				
			}


			// Add UK group
			new_option = new Option
			(
				'',//textPrefix + 'UK' + textSuffix,
				idPrefix + '48',
				false,
				('UK' == intOldVal)
			);
			
			// To cope with IE's poor optgroup support
			new_option.appendChild(document.createTextNode(textPrefix + 'UK' + textSuffix));
			option_container.appendChild(new_option);

			
			// look up group in arrDestAirportGroups array
//			for (var j = 0; j < all_child_options.length; j++)
//			{
//				if(all_child_options[j][0] == group_value)
//				{
//					group_index = j;
//					break;
//				}
//			}
//			
//			var bit = group_index;
//
//			// loop through departure airports			
//			for(var j = 0; j < arrDepAirports.length; j++)
//			{
//				// find filter
//				for (var i = 0; i < filter.length; i++)
//				{
//					if(arrDepAirports[j][0] == filter[i][0])
//					{
//						// Bitwise AND (don't add another &)
//						if ((1 << bit) & filter[i][group])
//						{
//							new_option = new Option
//							(
//								'',//textPrefix + arrDepAirports[j][1] + textSuffix,
//								idPrefix + arrDepAirports[j][0],
//								false,
//								(arrDepAirports[j][0] == intOldVal)
//							);
//									
//							// To cope with IE's poor optgroup support
//							new_option.appendChild(document.createTextNode(textPrefix + arrDepAirports[j][1] + textSuffix));
//							option_container.appendChild(new_option);
//						}
//					}
//				}
//			}



//			if (child_dropdown.length <= 1)
//			{
//				// Check through all the possible options
//				for (var j = 0; j < all_child_options.length; j++)
//				{
//					if (all_child_options[j][2] != current_group_id)
//					{
//						option_container = child_dropdown;
//						current_group_id = 0;
//					}
//				
//					// Bitwise AND (don't add another &)
//					if (all_child_options[j][3] && all_child_options[j][3] != '')
//					{
//						// Add the previous optgroup to the dropdown before creating a new one
//						if (optgroup && optgroup.childNodes.length > 0)
//						{
//							child_dropdown.appendChild(optgroup);
//						}
//						current_group = all_child_options[j][3];
//						current_group_id = all_child_options[j][2];
//						optgroup = document.createElement('optgroup');
//						optgroup.setAttribute('label', all_child_options[j][3]);
//						option_container = optgroup;
//						
//					}
//					else
//					{
//						if ((!group_value) || (all_child_options[j][2] == group_value))
//						{
//							new_option = new Option
//							(
//								'',//textPrefix + all_child_options[j][1] + textSuffix,
//								idPrefix + all_child_options[j][0],
//								false,
//								(all_child_options[j][0] == intOldVal)
//							);
//							// To cope with IE's poor optgroup support
//							new_option.appendChild(document.createTextNode(textPrefix + all_child_options[j][1] + textSuffix));
//							option_container.appendChild(new_option);
//						}
//					}
//				}
//			}
//			
			child_dropdown.disabled = (child_dropdown.length <= 1) ? 'disabled' : false;
		}
		
		/**
		 * @param int parent_value The value selected in the parent dropdown
		 * @param int group_value
		 * @param array all_child_options The list of potential options for the child dropdown
		 * @param array filter Bit-based array containing information about the mappings from one airport to another
		 * @param HTML_select_element child_dropdown The dropdown to put the new options into
		 * @param int first_option The display string of the first option in the dropdown (e.g. Any) which will have a value of -1
		 * @param boolean append If true, add additional options without deleting any existing options (keep_first will be ignored)
		 */
		function srhfrm_filter_options(parent_value, group_value, all_child_options, filter, child_dropdown, first_option, append)
		{
			// var append boolean if true, additional options will be added to the same dropdown.
			var matchGroup = false;
			var textPrefix='';
			var textSuffix='';
			var idPrefix='';
			var intOldVal=child_dropdown.value;
			var current_group = '';
			var current_group_id = 0;
			var option_container = child_dropdown;
			var optgroup;
			var group_found = false;
			var group_index;
			var goup = 1;
			
			/* parent_value  - airport value */
			/* group_value - null */
			
			/* all_child_options - aiport group options */
			/* filter - bit map */
			
			/* child_dropdown - select element */
			/* first_option - please select string */
			/* append - false */
					
			
			// Need to detect if group or airport is selected.
			if (!append)
			{
				//child_dropdown.options.length = keep_first;
				child_dropdown.innerHTML = '';
				
				if (first_option == laquo+' Any '+raquo)
				{
					child_dropdown[child_dropdown.length] = new Option
					(
						first_option,
						'Any'
					);
				}
				else if (first_option)
				{
					child_dropdown[child_dropdown.length] = new Option
					(
						first_option,
						-1
					);
				}
			}
			
			if(filter.length == 0)
			{
				group_found = true;
			}
			
			for (var i = 0; i < filter.length; i++)
			{
				// Check through the filter array to find the values to filter on.
				if (filter[i][0] == parent_value)
				{
					var strTest = '';
					var bit = 0;
					var group = 1;
					group_found = true;
					// Check through all the possible options
					for (var j = 0; j < all_child_options.length; j++)
					{
						if (all_child_options[j][2] != current_group_id)
						{
							option_container = child_dropdown;
							current_group_id = 0;
						}

						if (all_child_options[j].length == 4 && all_child_options[j][3] != '')
						{
							// Add the previous optgroup to the dropdown before creating a new one
							if (optgroup && optgroup.childNodes.length > 0)
							{
								child_dropdown.appendChild(optgroup);
							}
							current_group = all_child_options[j][3];
							current_group_id = all_child_options[j][2];
							optgroup = document.createElement('optgroup');
							optgroup.setAttribute('label', all_child_options[j][3]);
							option_container = optgroup;
							
						}
						// Bitwise AND (don't add another &)
						else if ((1 << bit) & filter[i][group])
						{
							if ((!group_value) || (all_child_options[j][2] == group_value))
							{
								new_option = new Option
								(
									'',//textPrefix + all_child_options[j][1] + textSuffix,
									idPrefix + all_child_options[j][0],
									false,
									(all_child_options[j][0] == intOldVal)
								);
								// To cope with IE's poor optgroup support
								new_option.appendChild(document.createTextNode(textPrefix + all_child_options[j][1] + textSuffix));
								option_container.appendChild(new_option);
//								This is for combined destination airports
//								if ( mixAirports )
//								{
//									// append airports underneath this group
//									alert('Run filter airports again');
//									//filterAirports(child_dropdown, 'dep_airports_combined_groups', 0, all_child_options[j][0], resortid_dropdownid);
//								}
							}
						}
						
						bit++;
						if (bit == cfgAirportsMatrixMaxBits)
						{
							bit = 0;
							group++;
						}
					}
					
					// Add the last remaining optgroup to the dropdown
					if (optgroup && optgroup.childNodes.length > 0)
					{
						child_dropdown.appendChild(optgroup);
					}
				}
			}
			// ****************************************************************
			// ***** departure outside the UK *********************************
			// ****************************************************************
			if(!group_found && !group_value)
			{
				// Add UK group
				new_option = new Option
				(
					'',//textPrefix + 'UK' + textSuffix,
					idPrefix + '48',
					false,
					('UK' == intOldVal)
					);
			
				// To cope with IE's poor optgroup support
				new_option.appendChild(document.createTextNode(textPrefix + 'UK' + textSuffix));
				option_container.appendChild(new_option);
			}			
			
			else if(group_value == 48)
			{
				for (var j = 0; j < arrDestAirportGroups.length; j++)
				{
					if(arrDestAirportGroups[j][0] == 99)
					{
						group_index = j;
						break;
					}
				}

				var bit = group_index;
				
				// loop through departure airports	

				for(var j = 0; j < arrDepAirports.length; j++)
				{
					// find filter
					for (var i = 0; i < arrDepAirportsDestGroups.length; i++)
					{
						if(arrDepAirports[j][0] == arrDepAirportsDestGroups[i][0])
						{
							
							// Bitwise AND (don't add another &)
							if ((1 << bit) & arrDepAirportsDestGroups[i][1])
							{
								
								new_option = new Option
								(
									'',//textPrefix + arrDepAirports[j][1] + textSuffix,
									idPrefix + arrDepAirports[j][0],
									false,
									(arrDepAirports[j][0] == intOldVal)
								);
										
								// To cope with IE's poor optgroup support
								new_option.appendChild(document.createTextNode(textPrefix + arrDepAirports[j][1] + textSuffix));
								option_container.appendChild(new_option);
							}
						}
					}
				}
			}
			if (child_dropdown.length <= 1)
			{
				// Check through all the possible options
				for (var j = 0; j < all_child_options.length; j++)
				{
					if (all_child_options[j][2] != current_group_id)
					{
						option_container = child_dropdown;
						current_group_id = 0;
					}
				
					// Bitwise AND (don't add another &)
					if (all_child_options[j][3] && all_child_options[j][3] != '')
					{
						// Add the previous optgroup to the dropdown before creating a new one
						if (optgroup && optgroup.childNodes.length > 0)
						{
							child_dropdown.appendChild(optgroup);
						}
						current_group = all_child_options[j][3];
						current_group_id = all_child_options[j][2];
						optgroup = document.createElement('optgroup');
						optgroup.setAttribute('label', all_child_options[j][3]);
						option_container = optgroup;
						
					}
					else
					{
						if ((!group_value) || (all_child_options[j][2] == group_value))
						{
							new_option = new Option
							(
								'',//textPrefix + all_child_options[j][1] + textSuffix,
								idPrefix + all_child_options[j][0],
								false,
								(all_child_options[j][0] == intOldVal)
							);
							// To cope with IE's poor optgroup support
							new_option.appendChild(document.createTextNode(textPrefix + all_child_options[j][1] + textSuffix));
							option_container.appendChild(new_option);
						}
					}
				}
			}
			
			child_dropdown.disabled = (child_dropdown.length <= 1) ? 'disabled' : false;
		}
		
		function srhfrm_update_destination_airport_groups(srhfrm)
		{
			if(srhfrm['resort_dd[1]'])
			{
				srhfrm_update_resort_dd(srhfrm, 1);
			}
			else
			{
				srhfrm_fill_dropdown('srhfrm_destination_airport', new Array({'value': '', 'display': laquo+' Please Select '+raquo}), 0);
				srhfrm_fill_dropdown('srhfrm_resort', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
				srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
				departure_airport = -1;
				departure_airport_group = -1;	
				onewayrow = document.getElementById('onewaydepartures');
				
				if ( srhfrm.onewayairportfromid && onewayrow.style.display != 'none')
				{
					departure_airport_combined = srhfrm.onewayairportfromid.value;
					
					if ( departure_airport_combined.substring(0, 1) == "|" )
					{
						departure_airport = departure_airport_combined.substring(1);
						departure_airport_group = -1;
					}
				}			
				else if ( srhfrm.airportcombinedfromid )
				{
					departure_airport_combined = srhfrm.airportcombinedfromid.value;
					if ( departure_airport_combined.substring(0, 1) == "|" )
					{
						departure_airport = departure_airport_combined.substring(1);
						departure_airport_group = -1;
					}
					else
					{
						departure_airport = -1;
						departure_airport_group = departure_airport_combined;
					}
				}
				else
				{
					departure_airport = srhfrm.airportfromid.value;
					departure_airport_group = srhfrm.airportgroupfromid.value;
				}
				
				if (departure_airport == -1 && departure_airport_group == -1)
				{		
					srhfrm_fill_dropdown('srhfrm_destination_airport_group', new Array({'value': '', 'display': laquo+' Please Select '+raquo}), 0);
				}
				else if(departure_airport != -1)
				{
					if (srhfrm.airportgrouptoid)
					{
						srhfrm_filter_options(
							departure_airport,
							null,
							arrDestAirportGroups,
							arrDepAirportsDestGroups,
							srhfrm.airportgrouptoid, 
							laquo+' Please Select '+raquo,
							false
						);
						
						// If there is only one option, choose it
						if (srhfrm.airportgrouptoid.length == 2 && (srhfrm.airportgrouptoid.options[0].value == -1 || srhfrm.airportgrouptoid.options[0].value == ''))
						{
							srhfrm.airportgrouptoid.selectedIndex = 1;
						}
	
						// Repopulate if the option is in the list.
						else if (srhfrm.repop_srhfrm_destination_airport_group && srhfrm.repop_srhfrm_destination_airport_group.value) 
						{
							srhfrm.airportgrouptoid.value = srhfrm.repop_srhfrm_destination_airport_group.value;
						}
						if (srhfrm.airportgrouptoid.value && srhfrm.airportgrouptoid.value != -1)
						{
							srhfrm_update_destination_airports(srhfrm);
						}
					}
					//else if (document.getElementById('srhfrm_destination_airport_combined'))
					//{
					//	alert('Update combined destination airport dropdown');
					//}
				}
				else // have departure_airport_group ID
				{
					if (srhfrm.airportgrouptoid)
					{
						srhfrm_filter_options(
							departure_airport_group,
							null,
							arrDestAirportGroups,
							arrDepGroupsDestGroups,
							srhfrm.airportgrouptoid, 
							laquo+' Please Select '+raquo,
							false
						);
						
						// Repopulate if the option is in the list.
						if (srhfrm.repop_srhfrm_destination_airport_group && srhfrm.repop_srhfrm_destination_airport_group.value) 
						{
							srhfrm.airportgrouptoid.value = srhfrm.repop_srhfrm_destination_airport_group.value;
						}
						if (srhfrm.airportgrouptoid.value && srhfrm.airportgrouptoid.value != -1)
						{
							srhfrm_update_destination_airports(srhfrm);
						}
					}
	//				else if (document.getElementById('srhfrm_destination_airport_combined'))
	//				{
	//					alert('Update combined destination airport dropdown');
	//				}
				}
			}
			srhfrm_append_subsites();
			srhfrm_highlight_required(srhfrm);
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_append_subsites()
		{
			if (document.forms && document.forms.srhfrm && document.forms.srhfrm.airportgrouptoid)
			{
				// //// If we include the subsites in the destination drop down - or other stuff declared in optionSubsites array
				if(typeof(optionSubsites)!= 'undefined' && optionSubsites.length > 0)
				{	
					var subsites_optgroup = document.createElement('optgroup');
					subsites_optgroup.setAttribute('label', optionSubsitesTitle);
								
					// Add extra options
					for (var i = 0; i < optionSubsites.length; i++)
					{
						new_option = new Option
						(
							'',
							optionSubsitesValues[i]
						);
						// To cope with IE's poor optgroup support
						new_option.appendChild(document.createTextNode(optionSubsites[i]));
						subsites_optgroup.appendChild(new_option);
					}
					
					document.getElementById('srhfrm_destination_airport_group').appendChild(subsites_optgroup);
				}
			}
		}
		
		function srhfrm_update_destination_airports(srhfrm)
		{	
			// //// Change tab - onClick event
			if((typeof(optionSubsites)!= 'undefined') && (document.getElementById('srhfrm_destination_airport_group').value.substr(0,8) == 'onchange'))
			{
				type = document.getElementById('srhfrm_destination_airport_group').value.substr(9);
				changeTab(type);
			}
			
			srhfrm_fill_dropdown('srhfrm_resort', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
			srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
			if (document.getElementById('srhfrm_departure_airport_combined'))
			{
				departure_airport = document.getElementById('srhfrm_departure_airport_combined').value;
				if(departure_airport.substring(0, 1) == "|")
				{
					departure_airport = departure_airport.substring(1);
				}
			}
			else
			{
				departure_airport = -1;
			}
			
			if(srhfrm.airportgrouptoid.value == 'Any')
			{
				srhfrm_fill_dropdown('srhfrm_destination_airport', new Array({'value': 'Any', 'display': laquo+' Any '+raquo}), 0);
			}
			
			else if (srhfrm.airporttoid && srhfrm.airportgrouptoid.value)
			{
				srhfrm_filter_options(
					departure_airport,
					srhfrm.airportgrouptoid.value,
					arrDestAirports,
					arrDepAirportsDestAirports,
					srhfrm.airporttoid, 
					laquo+' Please Select '+raquo,
					false
				);
					
				// If there is only one option, choose it
				if (srhfrm.airporttoid.length == 2 && (srhfrm.airporttoid.options[0].value == -1 || srhfrm.airporttoid.options[0].value == ''))
				{
					srhfrm.airporttoid.selectedIndex = 1;
				}
				// Repopulate if the option is in the list.
				else if (srhfrm.repop_srhfrm_destination_airport && srhfrm.repop_srhfrm_destination_airport.value) 
				{
					srhfrm.airporttoid.value = srhfrm.repop_srhfrm_destination_airport.value;
				}
				else
				{
					// Make sure we at least select something
					srhfrm.airporttoid.selectedIndex = 0;
				}
			}
			else if (document.getElementById('srhfrm_destination_airport_combined'))
			{
				alert('Update combined destination airport dropdown');
			}
			srhfrm_update_resorts(srhfrm);
			srhfrm_highlight_required(srhfrm);
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_update_departure_airports(srhfrm)
		{
			var textPrefix='';
			var textSuffix='';
			
			if ( srhfrm.airportfromid )
			{
				srhfrm.airportfromid.length = 1;
				for (var i = 0; i < arrDepAirports.length; i++)
				{
					if (arrDepAirports[i][2] == srhfrm.airportgroupfromid.value)
			{
						srhfrm.airportfromid.appendChild(new Option
						(
							textPrefix + arrDepAirports[i][1] + textSuffix,
							arrDepAirports[i][0],
							false
						));
					}
				}
				srhfrm.airportfromid.disabled = srhfrm.airportfromid.length > 1 ? false : true;
			}
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_update_departure_airports(srhfrm)
		{
			var textPrefix='';
			var textSuffix='';
			
			if ( srhfrm.airportfromid )
			{
				srhfrm.airportfromid.length = 1;
				for (var i = 0; i < arrDepAirports.length; i++)
				{
					if (arrDepAirports[i][2] == srhfrm.airportgroupfromid.value)
			{
						new_option = new Option
						(
							'',
							arrDepAirports[i][0],
							false
						);
						// To cope with IE's poor optgroup support
						new_option.appendChild(document.createTextNode(textPrefix + arrDepAirports[i][1] + textSuffix));
						srhfrm.airportfromid.appendChild(new_option);
					}
				}
				srhfrm.airportfromid.disabled = srhfrm.airportfromid.length > 1 ? false : true;
			}
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_update_resort_dd(srhfrm, level_num, old_values, old_accomm)
		{
			var resort_dd_element = srhfrm['resort_dd['+level_num+']'];
			
			// Clear the accommodation dd, but only if the resort dropdowns are visible
			if (!document.getElementById('srhfrm_resort_dd_'+level_num))
			{
				if (document.getElementById('srhfrm_resort_dd_'+(level_num-1)))
				{
					srhfrm_update_accomm(srhfrm, old_accomm);
				}
				return false;
			}
			
			if (!old_values)
			{
				old_values = new Array();
				for (var i = level_num; srhfrm['resort_dd['+i+']']; i++)
				{
					old_values.push(srhfrm['resort_dd['+i+']'].value);
				}
			}
			if (!old_accomm && srhfrm.property_id)
			{
				old_accomm = srhfrm.property_id.value;
			}
			
			// Variable number of dots to show what is doing the updating
			var loading_text = 'Loading.........';
			loading_text = loading_text.substr(0, 7 + level_num);
			for (var i = level_num; srhfrm['resort_dd['+i+']']; i++)
			{
				srhfrm_fill_dropdown('srhfrm_resort_dd_'+i, new Array({'value': '', 'display': loading_text}), 0);
			}
			if (srhfrm['resort_dd['+level_num+']'])
			{
				srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': loading_text}), 0);
			}
			if (level_num > 1)
			{
				var parent_level_num = level_num - 1;
				var parent_resort_dd_id = srhfrm['resort_dd['+parent_level_num+']'].value;
				if (parent_resort_dd_id.substr(0, 2) == -1 || srhfrm['resort_dd['+parent_level_num+']'].disabled)
				{
					srhfrm_fill_dropdown('srhfrm_resort_dd_'+level_num, new Array({'value': '', 'display': laquo+' Please select '+raquo}), 0);
					// Fill any lower level dropdowns with 'any' and update the accommodation
					srhfrm_update_resort_dd(srhfrm, level_num + 1);
					return;
				}
			}
			
			var resort_dd_set_id = srhfrm['resort_dd_set_id'].value;
			
			if (srhfrm['airportfromid'])
			{
				var dep_airport_id = srhfrm['airportfromid'].value;
			}
			else if (srhfrm['airportcombinedfromid'])
			{
				if (srhfrm['airportcombinedfromid'].value == -1)
				{
					var dep_airport_id = -1;
				}

				else
				{
					var dep_airport_id = srhfrm['airportcombinedfromid'].value.substr(1, srhfrm['airportcombinedfromid'].length);
				}
			}
			else
			{
				var dep_airport_id = -1;
			}
			
//			if (document.getElementById('srhfrm_destination_airport') 
//				&& document.getElementById('srhfrm_destination_airport').value 
//				&& document.getElementById('srhfrm_destination_airport').value != -1)
//			{
//				var airport_to_id = document.getElementById('srhfrm_destination_airport').value;
//			}
//			else if (document.getElementById('srhfrm_destination_airport_combined') 
//					 && document.getElementById('srhfrm_destination_airport_combined').value
//					 && document.getElementById('srhfrm_destination_airport_combined').value != -1)
//			{
//				var airport_to_id = document.getElementById('srhfrm_destination_airport_combined').value;
//			}
//			else
//			{
//				// No destination airport - Don't get resorts 
//				srhfrm_fill_dropdown('srhfrm_resort', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
//				return true;
//			}
//			srhfrm_highlight_required(srhfrm);
//			srhfrm_save_values(srhfrm);
			
			var xmlhttprequest = create_xml_http_request();
			xmlhttprequest.onreadystatechange = function()
			{
				if(xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200)
				{
					var doc = xmlhttprequest.responseXML;
					var root = doc.documentElement;
					var nds;
					resort_dd_element.options.length = 0;
							
					if(root.hasChildNodes()) 
					{
						nds=root.childNodes;
						var len = nds.length;
						for (var i = 0; i < len; i++)
						{
							if(nds[i].nodeName == 'ResortDD')
							{
								resort_dd_element.options[resort_dd_element.options.length] = new Option(nds[i].getAttribute("name"), nds[i].getAttribute("value") + ',' + nds[i].getAttribute("is_searchable"));
							}
						}
					}
					if (resort_dd_element.length > 1)
					{
						resort_dd_element.disabled = false;
						// If there is only one option, choose it
						if (resort_dd_element.length == 2 && (resort_dd_element.options[0].value == -1 || resort_dd_element.options[0].value == ''))
						{
							resort_dd_element.selectedIndex = 1;
						}
						// Repopulate if the option is in the list.
						else if (old_values[0])
						{
							srhfrm['resort_dd['+level_num+']'].value = old_values[0];
						}
						else
						{
							// Make sure we at least select something
							resort_dd_element.selectedIndex = 0;
						}
						if (resort_dd_element.selectedIndex == -1)
						{
							// Make sure we at least select something
							resort_dd_element.selectedIndex = 0;
						}
					}
					else
					{
						resort_dd_element.disabled = true;
						srhfrm_fill_dropdown('srhfrm_resort_dd_'+level_num, new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
						resort_dd_element.selectedIndex = 0;
					}
					srhfrm_update_resort_dd(srhfrm, level_num + 1, old_values.slice(1), old_accomm);
				}
			}
			xml_url = '/fetch_opts.php?mode=resort_dd&resort_dd_set_id='+resort_dd_set_id+'&level_num='+level_num+'&dep_airport_id='+dep_airport_id;
			if (parent_resort_dd_id)
			{
				xml_url += '&parent_resort_dd_id='+parent_resort_dd_id;
			}
			xmlhttprequest.open('GET', xml_url, true);
			xmlhttprequest.send(null);
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_update_resorts(srhfrm)
		{
			srhfrm_fill_dropdown('srhfrm_resort', new Array({'value': '', 'display': 'Loading...'}), 0);
			
			if (document.getElementById('srhfrm_destination_airport') 
				&& document.getElementById('srhfrm_destination_airport').value 
				&& document.getElementById('srhfrm_destination_airport').value != -1)
			{
				var airport_to_id = document.getElementById('srhfrm_destination_airport').value;
			}
			else if (
				   document.getElementById('srhfrm_destination_airport_combined') 
				&& document.getElementById('srhfrm_destination_airport_combined').value
				&& document.getElementById('srhfrm_destination_airport_combined').value != -1
				&& document.getElementById('srhfrm_destination_airport_combined').value != -1
				&& document.getElementById('srhfrm_destination_airport_combined').value.substring(0,1) == '|'
			)
			{
				// The value here will be of the form "|airport_id"
				var airport_to_id = document.getElementById('srhfrm_destination_airport_combined').value.substring(1);
			}
			else
			{
				// No destination airport - Don't get resorts 
				srhfrm_fill_dropdown('srhfrm_resort', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
				return true;
			}
			srhfrm_highlight_required(srhfrm);
			srhfrm_save_values(srhfrm);
			
			var xmlhttprequest = create_xml_http_request();
			xmlhttprequest.onreadystatechange = function()
			{
				if(xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200)
				{
					var doc = xmlhttprequest.responseXML;
					var root = doc.documentElement;
					var resorts = document.getElementById('srhfrm_resort');
					if ( ! resorts )
					{
						return false;
					}
					resorts.options.length = 0;
					var nds;		
							
					if(root.hasChildNodes()) 
					{
						nds=root.childNodes;
						var len = nds.length;
						for (var i = 0; i < len; i++)
						{
							if(nds[i].nodeName == 'resort')
							{
								resorts.options[resorts.options.length] = new Option(nds[i].getAttribute("name"), nds[i].getAttribute("value"));
							}
						}
					}
					
					
					if (resorts.length > 1)
					{
						resorts.disabled = false;
						// If there is only one option, choose it
						if (resorts.length == 2 && (resorts.options[0].value == -1 || resorts.options[0].value == ''))
						{
							resorts.selectedIndex = 1;
						}
						// Repopulate if the option is in the list.
						else if (srhfrm.resortid && srhfrm && srhfrm.repop_srhfrm_resort && srhfrm.repop_srhfrm_resort.value) 
						{
							srhfrm.resortid.value = srhfrm.repop_srhfrm_resort.value;
						}
					}
					else
					{
						resorts.disabled = true;
						srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
					}
					srhfrm_update_accomm(srhfrm);
				}
			}
			xmlhttprequest.open('GET', '/fetch_opts.php?mode=resorts&airport_to_id='+airport_to_id, true);
			xmlhttprequest.send(null);
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_update_accomm(srhfrm, old_accomm)
		{
			var select_element = srhfrm.property_id;
			srhfrm_save_values(srhfrm);
			if (!select_element) { return true; }
			if (!old_accomm)
			{
				old_accomm = select_element.value;
			}
			srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': 'Loading...'}), 0);
			var xmlhttprequest = create_xml_http_request();
				
			// Resort dd set based
			if (srhfrm['resort_dd[1]'] || srhfrm['resort_dd[2]'])
			{
				for (var i = 5; i > 0; i--)
				{
					if (srhfrm['resort_dd[' + i + ']'] 
						&& srhfrm['resort_dd[' + i + ']'].selectedIndex
						&& srhfrm['resort_dd[' + i + ']'].selectedIndex != -1 // Aparently IE thinks -1 is a natural number
						&& srhfrm['resort_dd[' + i + ']'][srhfrm['resort_dd[' + i + ']'].selectedIndex]
						&& srhfrm['resort_dd[' + i + ']'][srhfrm['resort_dd[' + i + ']'].selectedIndex].value 
						&& srhfrm['resort_dd[' + i + ']'][srhfrm['resort_dd[' + i + ']'].selectedIndex].value != -1
						&& srhfrm['resort_dd[' + i + ']'][srhfrm['resort_dd[' + i + ']'].selectedIndex].value != "-1,null"
					)
					{
						var param = 'resort_dd_id='+srhfrm['resort_dd[' + i + ']'][srhfrm['resort_dd[' + i + ']'].selectedIndex].value;
						break;
					}
				}
				if (!param)
				{
					srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
					return true;
				}
			}
			// Airport group set based
			else if (document.getElementById('srhfrm_resort') 
				&& document.getElementById('srhfrm_resort').value 
				&& document.getElementById('srhfrm_resort').value != -1)
			{
				var param = 'region_id=' + document.getElementById('srhfrm_resort').value;
			}
			else if (document.getElementById('srhfrm_destination_airport') 
				&& document.getElementById('srhfrm_destination_airport').value 
				&& document.getElementById('srhfrm_destination_airport').value != -1)
			{
				var param = 'airport_to_id=' + document.getElementById('srhfrm_destination_airport').value;
			}
			else if (document.getElementById('srhfrm_destination_airport_combined') 
					 && document.getElementById('srhfrm_destination_airport_combined').value
					 && document.getElementById('srhfrm_destination_airport_combined').value != -1)
			{
				var param = 'airport_to_id=' + document.getElementById('srhfrm_destination_airport_combined').value;
			}
			else
			{
				// No destination - Don't get accomm 
				srhfrm_fill_dropdown('srhfrm_accomm', new Array({'value': '', 'display': laquo+' Any '+raquo}), 0);
				return true;
			}
			
			xmlhttprequest.onreadystatechange = function()
			{
				if(xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200)
				{
					var doc = xmlhttprequest.responseXML;
					var root = doc.documentElement;
					var accomm = document.getElementById('srhfrm_accomm');
					if ( ! accomm )
					{
						return false;
					}
					accomm.options.length = 0;
					var nds;		
							
					if(root.hasChildNodes()) 
					{
						nds=root.childNodes;
						var len = nds.length;
						for (var i = 0; i < len; i++)
						{
							
							if(nds[i].nodeName == 'AccommItem')
							{
								accomm.options[accomm.options.length] = new Option(nds[i].getAttribute("name"), nds[i].getAttribute("value"));
							}
						}
					}
		
					accomm.disabled = false;
					// Repopulate if the option is in the list.
					if (old_accomm)
					{
						accomm.value = old_accomm;
					}
					else if (srhfrm.repop_srhfrm_accomm && srhfrm.repop_srhfrm_accomm.value) 
					{
						accomm.value = srhfrm.repop_srhfrm_accomm.value;
					}
				}
			}
			xml_url = '/fetch_opts.php?mode=accomm&'+param;
			if (srhfrm.rating && srhfrm.rating.value)
			{
				xml_url += '&min_rating='+srhfrm.rating.value;
			}
			xmlhttprequest.open('GET', xml_url, true);
			xmlhttprequest.send(null);
			// srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_fill_dropdown(dropdown_id, options, keep_first)
		{
			var select_element = document.getElementById(dropdown_id);
			
			if (!select_element) { return true; }
			
			// Clear out elements (if keepFirst is 1, the first item, probably "Any", will be kept)
			select_element.length = keep_first
			
			for (var i = 0; i < options.length; i++)
			{
				select_element.options[i] = new Option(options[i]['display'], options[i]['value']);
			}
			if (options.length <= 1)
			{
				select_element.disabled = true;
			}
			else
			{
				select_element.disabled = false;
			}
			
			select_element.selectedIndex = 0; // Make sure we have a sensible item selected (IE defaults to -1!)
		}
		
		function srhfrm_update_children(srhfrm)
		{
			var total_children = 0; 
			if (document.getElementById('srhfrm_num_rooms'))
			{
				var selected_rooms = document.getElementById('srhfrm_num_rooms').value;
			}
			else
			{
				var selected_rooms = 1;
			}
	
			if (document.getElementById('srhfrm_children'))
			{
				total_children = parseInt(document.getElementById('srhfrm_children').value);
			}
			else
			{
				for (var j = 1; j <= selected_rooms; j++)
				{
					total_children = total_children + parseInt(document.getElementById('srhfrm_children_' + j).value);
				}
			}
			
			for (var i = 1; i <= cfgMaxChildren; i++)
			{
				if (document.getElementById('srhfrm_child_' + i + '_row'))
				{
					if (i <= total_children)
					{
						// Set display to inherit value from parent because IE needs block and Moz needs table-row
						document.getElementById('srhfrm_child_' + i + '_row').style.display = '';
					}
					else if (document.getElementById('srhfrm_child_' + i))
					{
						document.getElementById('srhfrm_child_' + i + '_row').style.display = 'none';
						document.getElementById('srhfrm_child_' + i).value = '';
					}
				}
			}
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_update_rooms_rows(srhfrm)
		{
			for (var i = 1; i <= cfgMaxRooms; i++)
			{
				if (document.getElementById('srhfrm_room_' + i + '_row'))
				{
					if (srhfrm.numrooms && 
						i <= srhfrm.numrooms.value)
					{
						// Set display to inherit value from parent because IE needs block and Moz needs table-row
						document.getElementById('srhfrm_room_' + i + '_row').style.display = '';
					}
					else if (document.getElementById('srhfrm_room_' + i + '_row'))
					{
						document.getElementById('srhfrm_room_' + i + '_row').style.display = 'none';
					}
				}
			}
			srhfrm_update_children(srhfrm);
			srhfrm_save_values(srhfrm);
		}
		
		function srhfrm_highlight_required(srhfrm)
		{
			for (var i = 0; i < required_fields.length; i++)
			{
				if ( eval('srhfrm.' + required_fields[i]) )
				{
					if (eval('srhfrm.' + required_fields[i]).value && eval('srhfrm.' + required_fields[i]).value != -1)
					{
						eval('srhfrm.' + required_fields[i]).className = 'srhfrm_dropdown';
					}
					else
					{
						eval('srhfrm.' + required_fields[i]).className = 'srhfrm_dropdown srhfrm_required';
					}
				}
			}
		}
		
		function srhfrm_save_rating_value(srhfrm)
		{
			if(document.getElementById('srhfrm_rating').value 
			   && document.getElementById('srhfrm_rating').value != -1)
			   {
				  srhfrm_save_values(srhfrm);
			   }
		}
		
		function srhfrm_save_board_value(srhfrm)
		{
			if(document.getElementById('srhfrm_board_basis').value 
			   && document.getElementById('srhfrm_board_basis').value != -1)
			   {
				   srhfrm_save_values(srhfrm)
			   }
		}
		
		function srhfrm_save_property_value(srhfrm)
		{
			if(document.getElementById('srhfrm_accomm').value 
			   && document.getElementById('srhfrm_accomm').value != -1)
			   {
				   srhfrm_save_values(srhfrm)
			   }
		}
		
		// Store form values in a cookie for when user clicks 'back'
		function srhfrm_save_values(objForm)
		{
			/* General Params */
			var cookieString = '';
			
			if(objForm.departure_monthyear && objForm.departure_day)
			{
				cookieString += 'general_params,departure_date,' 
					+ objForm.departure_monthyear.value + '-' + objForm.departure_day.value + '|';
			}
			else if(objForm.departure)
			{
				cookieString += 'general_params,departure_date,' + objForm.departure.value + '|';
			}
			
			var resort_dd_ids = '';
			for (var i = 1; objForm['resort_dd['+i+']']; i++)
			{
				if (resort_dd_ids) { resort_dd_ids += ','; }
				resort_dd_ids += objForm['resort_dd['+i+']'].value.substr(0, objForm['resort_dd['+i+']'].value.indexOf(','));
			}
			if (resort_dd_ids) { cookieString += 'general_params,resort_dd_ids,' + resort_dd_ids + '|'; }
			
			if(objForm.duration) 		{ cookieString += 'general_params,duration,' + objForm.duration.value + '|'; }
			
			/* Flight Section */
			var airportfromgroup, airportfrom;
			if ( objForm.airportcombinedfromid )
			{
				var fromairports = objForm.airportcombinedfromid.value.split('|');
				if( fromairports.length == 1 )
				{
					airportfromgroup = fromairports[0];
					airportfrom = undefined;
				}
				else
				{
					airportfromgroup = undefined;
					airportfrom = fromairports[1];
				}
			}
			else
			{
				if(objForm.airportgroupfromid) 	{ airportfromgroup = objForm.airportgroupfromid.value; }
				if(objForm.airportfromid) 	{ airportfrom = objForm.airportfromid.value; }
			}
			if(airportfromgroup) 		{ cookieString += 'flight,airport_group_from_id,' + airportfromgroup + '|'; }
			if(airportfrom)			{ cookieString += 'flight,airport_from_id,' + airportfrom + '|'; }
			
			var airporttogroup, airportto;
			if ( objForm.airportcombinedtoid )
			{
				var toairports = objForm.airportcombinedtoid.value.split('|');
				if( toairports.length == 1 )
				{
					airporttogroup = toairports[0];
					airportto = undefined;
				}
				else
				{
					airporttogroup = undefined;
					airportto = toairports[1];
				}
			}
			else
			{
				if(objForm.airportgrouptoid) 	{ airporttogroup = objForm.airportgrouptoid.value; }
				if(objForm.airporttoid) 	{ airportto = objForm.airporttoid.value; }
			}
			if(airporttogroup) 		{ cookieString += 'flight,airport_group_to_id,' + airporttogroup + '|'; }
			if(airportto) 			{ cookieString += 'flight,airport_to_id,' + airportto + '|'; }
			
			/* Accommodation Section */
			if(objForm.resorttoid) 		{ cookieString += 'accommodation,resort_id,' + objForm.resorttoid.value + '|'; }
			if(objForm.boardbasis) 		{ cookieString += 'accommodation,board_code,' + objForm.boardbasis.value + '|'; }
			if(objForm.rating) 		{ cookieString += 'accommodation,rating,' + objForm.rating.value + '|'; }
			
			/* Occupancy Section */
			if(objForm.numrooms) 		{ cookieString += 'occupancy,num_rooms,' + objForm.numrooms.value + '|'; }
			if(objForm.adults) 		{ cookieString += 'occupancy,adults,' + objForm.adults.value + '|'; }
			if(objForm.infants)		{ cookieString += 'occupancy,infants,' + objForm.infants.value + '|'; }
			if(objForm.children)
			{
				if (objForm.children.value == 0)
				{
					cookieString += 'occupancy,childages,undefined|';
				}
				else
				{
					for (i = 1; objForm['child'+i]; i++)
					{
						if (i <= objForm.children.value)
						{
							if(i == 1)
							{
								childages = objForm['child'+i].value;
							}
							else
							{
								childages += ","+objForm['child'+i].value;
							}
						}
					}
					if (childages)
					{
						cookieString += 'occupancy,childages,' + childages + '|';
					}
				}
			}
			
			document.cookie = 'SearchFormValues=' + encodeURIComponent(cookieString) + '; path=/';
			return true;
		}
		
		function srhfrm_update_return(srhfrm)
		{
			srhfrm_save_values(srhfrm);

			if (document.getElementById("srhfrm_duration") == null 
				|| document.getElementById("srhfrm_departure_date") == null
				|| document.getElementById("srhfrm_return_text") == null)
			{
				
				return;
			}
			
			var duration = document.getElementById("srhfrm_duration").value.split("-");
			var deps = document.getElementById("srhfrm_departure_date").value.split("-");
			var return_text = document.getElementById("srhfrm_return_text");
						
			// Add extra 12 hours so we are always looking midday - avoids problems with Daylight Saving changes.
			var departure = new Date(deps[0], deps[1] - 1, deps[2], 12);
			var checkout = new Date(departure.getTime() + (duration[0] * 1000 * 60 * 60 * 24));
								
			var datestring = checkout.toDateString();
			
			// Not sure why, but this regex doesn't seem to have any effect, so using split() instead.
			 datestring = datestring.replace(/^(\w{3})\s(\w{3})\s(\d{1,2})(.*)/, "$1, $3 $2$4");

			return_text.innerHTML = datestring;
			 
			//var datearray = datestring.split(' ');
			//return_text.innerHTML = datearray[0] + ', ' + datearray[2] + ' ' + datearray[1] + ' ' + datearray[3];
		}
		
		function srhfrm_validate(srhfrm)
		{
			var errors = new Array();
						
			for (var i = 0; i < required_fields.length; i++)
			{
				if (srhfrm[required_fields[i]] && (!srhfrm[required_fields[i]].value || srhfrm[required_fields[i]].value == -1))
				{
					// Would be better not to hard code the field labels here.
					if (required_fields[i] == 'airportgroupfromid')
					{
						errors.push('Departure airport group');
					}
					else if (required_fields[i] == 'airportcombinedfromid' || required_fields[i] == 'airportfromid')
					{
						errors.push('Departure airport');
					}
					else if (required_fields[i] == 'airportgrouptoid')
					{
						errors.push('Destination airport group');
					}
					else if (required_fields[i] == 'airportcombinedtoid' || required_fields[i] == 'airporttoid')
					{
						errors.push('Destination airport');
					}
					else 
					{
						errors.push(required_fields[i]);
					}
				}
			}
			
			if (srhfrm['resort_dd[1]'])
			{
				var selected_dropdown_is_searchable = false;
				var lowest_not_selected;
				for (var i = 1; i < 10; i++)
				{
					if (srhfrm['resort_dd['+i+']'])
					{
						// Negative substring indexes don't work in IE, so:
						if (srhfrm['resort_dd['+i+']'].value.substr(
							srhfrm['resort_dd['+i+']'].value.length - 4
						) == 'true')
						{
							selected_dropdown_is_searchable = true;
						}
						
						else if (
							!selected_dropdown_is_searchable
							&& !lowest_not_selected
							&& srhfrm['resort_dd['+i+']'].value
							&& srhfrm['resort_dd['+i+']'].value.substr(0, 2) == '-1'
						)
						{
							lowest_not_selected = i;
							errors.push(srhfrm['dd_'+i+'_label'].value);
						}
					}
					else
					{
						break;
					}
				}
						}
			
			// Check that numbers have been entered as ages for all children specified.
			if (srhfrm['children'] && srhfrm['children'].value > 0)
			{
				for (var i = 1; i <= srhfrm['children'].value; i++)
				{
					
					if (srhfrm['child' + i])
					{
						if (isNaN(parseInt(srhfrm['child' + i].value, 10)))
						{
							if (srhfrm['children'].value == 1)
							{
								errors.push('Child age');
							}
							else
							{
								errors.push('Child ' + i + ' age');
							}
						}
						else if (parseInt(srhfrm['child' + i].value, 10) < 2)
						{
							if (srhfrm['children'].value == 1)
							{
								errors.push('Child is under minimum age.');
							}
							else
							{
								errors.push('Child ' + i + ' is under minimum age.');
							}
						}
						else if (parseInt(srhfrm['child' + i].value, 10) >= 18)
						{
							if (srhfrm['children'].value == 1)
							{
								errors.push('Child is over maximum age.');
							}
							else
							{
								errors.push('Child ' + i + ' is over maximum age.');
							}
						}
					}
				}
			}
			
			if (errors.length > 0)
			{
				error_message = 'Please enter the following information:\n';
				for (var j = 0; j < errors.length; j++)
				{
					error_message += '\n' + errors[j];
				}
				alert(error_message);
				return false;
			}
			else
			{
				return true;
			}
		}
		
		function duration_display(flight_type, srhfrm)
		{
			duration_row = document.getElementById('srhfrm_duration_row');
			duration = document.getElementById('srhfrm_duration');
			one_way_departures = document.getElementById('onewaydepartures');
			normal_departures = document.getElementById('normaldepartures');
			one_way_select_field = document.getElementById('srhfrm_one_way_departure_airport');
			
			normal_departures_field = document.getElementById('srhfrm_departure_airport');
			
			var str_one_way_dep = one_way_select_field.value;
			
			if(flight_type.value == 'one_way')
			{
				duration_row.style.display = 'none';
				duration.value = "-1";
				one_way_departures.style.display = '';
				normal_departures.style.display = 'none';
				one_way_select_field.style.display = '';
				// Set value to that of normal departures field
				one_way_select_field.value = "|" + normal_departures_field.value;
			}
			else
			{
				duration_row.style.display = '';
				duration.value = "7-7";
				one_way_departures.style.display = 'none';
				normal_departures.style.display = '';
				one_way_select_field.style.display = 'none';
				
				normal_departures_field.value = str_one_way_dep.replace('|', '');
			}
			
			// update destination fields
			srhfrm_update_destination_airport_groups(srhfrm);
		}

		function srhfrm_save_cookie(srhfrm)
		{
			var cookie_values = new Array();
			for (var i = 0; i < srhfrm.elements.length; i++)
			{
				cookie_values.push(srhfrm[i].name + '=' + srhfrm[i].value);
			}
			var cookie_name = 'srhfrm_' + srhfrm['search_id'];
			document.cookie=cookie_name + "=" +escape(cookie_values.join('|'));
		}