﻿var Search = new function () {

	var me = this;
	this.CurrentCookieVersion = 1;

	this.GeographyLevel3ID = 0;

	//speedup on first load.
	this.bFirstSearchSelect = true;

	//option to keep the cookie but not load it.
	this.bLoadCookie = true;

	//keeps track of the compressed search option so the flight only tab works properly.
	this.bCompressedSearch = false;

	//for toggling display of the destination autocomplete
	this.bShowAutoComplete = true;


	//setup
	this.Setup = function () {
		//grabs the compressed search option value...
		if (me.HasCompressedSearch()) {
			me.bCompressedSearch = true;
		}

		//check the autocomplete is there, and don't blunder on trying to display it if not
		if (!f.GetObject('acpGeographyLevel2ID')) me.bShowAutoComplete = false;

		//try and load the cookie
		me.EnsureCookieIsLatest();

		if (me.HasCookie() && me.bLoadCookie) {
			me.RestoreFromCookie();
		} else {
			me.SearchSelect();
			me.SetDurationOptions();

			//we don't know if the page search options have been changed in the code so we must do this too...
			//the if blocks are a speedup.
			if (dd.GetValue('sddGeographyLevel2ID') != 0 || f.GetValue('acpGeographyLevel2ID_Value') != 0) {
				me.FixSearchMismatch(me.SearchType()); // hack, fix me.

				me.GeographyLevel3ID = dd.GetValue('sddGeographyLevel3ID'); // This is important! We sometimes set this in the codebehind so we don't want it overwritten!
				me.Level2Select(dd.GetValue('sddGeographyLevel2ID'), true);
			}
			if (dd.GetValue('sddDepartureAirportID') > 0) {
				me.DepartureAirportSelect(dd.GetValue('sddDepartureAirportID'));
			}
		}

		//attach event handlers to show child age dropdowns when necessary
		var aChildDropdowns = f.GetElementsByClassName('select', 'child');

		for (var i = 0; i < aChildDropdowns.length; i++) {
			f.AttachEvent(aChildDropdowns[i], 'change',
                function () {
                	Search.SearchShow();
                });
		}

		// This is so search dropdowns restore correctly.
		// Again, the if blocks are a speedup.
		if (dd.GetValue('sddArrivalAirportID') > 0) {
			me.FlightOnlyArrivalAirportSelect(dd.GetValue('sddArrivalAirportID'));
		}
		if (dd.GetValue('sddFlightOnlyDepartureAirportID') > 0) {
			me.FlightOnlyDepartureAirportSelect(dd.GetValue('sddFlightOnlyDepartureAirportID'));
		}
	}


	// Hack: When we choose not to load a cookie, but the codebehind doesn't get run because the user has pressed browser back or forward,
	// the search mode sometimes gets overwritten by the browser while the correct tab is not displayed. So we save here and reset after
	// a timeout if the tab doesn't match the mode.
	this.FixSearchMismatch = function (sSearchType) {
		setTimeout('Search.CheckSearchTypeMatch(\'' + sSearchType + '\');', 200);
	}
	this.CheckSearchTypeMatch = function (sSearchType) {

		if (me.SearchType() != sSearchType) {
			// correct the duration dropdown.
			if (sSearchType == 'Flight') {
				dd.SetText('ddlDuration', n.SafeInt(dd.GetText('ddlDuration')) + 1);
			} else if (me.SearchType() == 'Flight') {
				dd.SetText('ddlDuration', n.SafeInt(dd.GetText('ddlDuration')) - 1);
			}
			// correct the search mode mismatch.
			f.SetValue('hidSearchMode', sSearchType);
		}
	}
	// End hack.


	//show / hide autocomplete
	this.ShowHideAutoComplete = function () {
		me.bShowAutoComplete = !me.bShowAutoComplete;

		var bHotel = me.SearchType() == 'Hotel';
		var bFlightPlusHotel = me.SearchType() == 'FlightPlusHotel';
		var bFlight = me.SearchType() == 'Flight';

		f.ShowIf('divDestination', (bFlightPlusHotel || bHotel) && !me.bShowAutoComplete);
		f.ShowIf('divDestinationAutoComplete', (bFlightPlusHotel || bHotel) && me.bShowAutoComplete);
		f.ShowIf('divResort', ((bFlightPlusHotel || bHotel) && !me.bShowAutoComplete && (dd.GetValue('sddGeographyLevel2ID') > 0)));

		f.ShowIf('divArrivalAirport', bFlight && !me.bShowAutoComplete);
		f.ShowIf('divArrivalAirportAutoComplete', bFlight && me.bShowAutoComplete);

		f.SetHTML('aShowHideAutoComplete', me.bShowAutoComplete ? Constants.SearchToolHideAutoComplete : Constants.SearchToolShowAutoComplete);
		f.SetHTML('aShowHideAirportAutoComplete', me.bShowAutoComplete ? Constants.SearchToolHideAutoComplete : Constants.SearchToolShowAutoComplete);

		if (me.bShowAutoComplete) {
			f.SetFocus('acpGeographyLevel2ID');
			f.GetObject('acpGeographyLevel2ID').select();
		}

		f.GetObject('sddDepartureAirportID').disabled = true;
		oGenerateDepartureAirportDropdown.Go(me.bShowAutoComplete ? AutoComplete.GetValue('acpGeographyLevel2ID') : f.GetValue('sddGeographyLevel2ID'),
            dd.GetValue('sddDepartureAirportID'));
	}

	//region / resort select
	this.RegionResortSelect = function (iRegionResortID) {
		f.GetObject('sddDepartureAirportID').disabled = true;

		//make sure the dropdowns are not going to interfere with the autocomplete value
		f.SetValue('sddGeographyLevel2ID', 0);
		f.SetValue('sddGeographyLevel3ID', 0);

		oGenerateDepartureAirportDropdown.Go(iRegionResortID, dd.GetValue('sddDepartureAirportID'));
	}


	//level2 select
	this.Level2Select = function (iGeographyLevel2ID, bKeepAutoCompleteValue) {
		f.GetObject('sddGeographyLevel3ID').disabled = true;
		f.GetObject('sddDepartureAirportID').disabled = true;

		//make sure the autocomplete doesn't interfere with the dropdowns
		if (f.GetObject('acpGeographyLevel2ID') && !bKeepAutoCompleteValue) AutoComplete.Clear('acpGeographyLevel2ID');

		//this should be done before the web service returns as we do not know when it may return.
		f.ShowIf('divResort', (me.SearchType() == 'FlightPlusHotel' || me.SearchType() == 'Hotel') && !me.bShowAutoComplete
            && dd.GetValue('sddGeographyLevel2ID') > 0);

		oGenerateResortDropdown.Go(iGeographyLevel2ID);
	}

	//level2 select done
	this.Level2SelectDone = function (sDropdownHTML) {
		f.SetHTML('spnResort', sDropdownHTML);

		//get back the stored geography level 3 value
		if (me.GeographyLevel3ID > 0) {
			dd.SetValue('sddGeographyLevel3ID', me.GeographyLevel3ID);
			me.GeographyLevel3ID = 0;
		}

		f.GetObject('sddGeographyLevel3ID').disabled = false;
		oGenerateDepartureAirportDropdown.Go(dd.GetValue('sddGeographyLevel2ID'), dd.GetValue('sddDepartureAirportID'));
	}


	//departure airport select
	this.DepartureAirportSelect = function (iDepartureAirportID) {
		f.GetObject('sddGeographyLevel2ID').disabled = true;
		var sSearchMode = f.GetValue('hidSearchMode');
		oGenerateRegionDropdown.Go(iDepartureAirportID, dd.GetValue('sddGeographyLevel2ID'), sSearchMode);
	}


	//flight only departure airport select
	this.FlightOnlyDepartureAirportSelect = function (iDepartureAirportID) {
		f.GetObject('sddArrivalAirportID').disabled = true;
		oGenerateArrivalAirports.Go(iDepartureAirportID, dd.GetValue('sddArrivalAirportID'));
	}


	//flight only arrival airport select
	this.FlightOnlyArrivalAirportSelect = function (iArrivalAirportID) {
		f.GetObject('sddFlightOnlyDepartureAirportID').disabled = true;
		oGenerateDepartureAirportFromArrivalAirport.Go(iArrivalAirportID, dd.GetValue('sddFlightOnlyDepartureAirportID'));
	}


	//toggle facilities
	this.ToggleFacilities = function () {

		var bAlreadyVisible = f.Visible('divFacilityHolder');
		f.ShowIf('divFacilityHolder', !bAlreadyVisible);
		f.SetClassIf('btnSearch', 'gap', !bAlreadyVisible);
		f.GetObject('aHotelFacilitiesLink').innerHTML = (!bAlreadyVisible ? 'Hide' : 'Show') + ' Hotel Facilities';
	}


	//toggle controls
	this.SearchSelect = function (sSearchMode) {

		if (sSearchMode != undefined) {
			f.SetClass('divSearchModes', sSearchMode);
			f.SetValue('hidSearchMode', sSearchMode);
		}
		f.Hide('divResort');

		if (sSearchMode != undefined) {
			this.SetDurationOptions();
		}

		var bHotel = me.SearchType() == 'Hotel';
		var bFlight = me.SearchType() == 'Flight';
		var bFlightPlusHotel = me.SearchType() == 'FlightPlusHotel';

		//fix destination dropdown dependency issues...
		//the if block is a speedup.
		if (!me.bFirstSearchSelect) {
			if (bHotel) {
				me.DepartureAirportSelect(-1);
			} else if (bFlightPlusHotel) {
				me.DepartureAirportSelect(dd.GetValue('sddDepartureAirportID'));
			}
		} else {
			me.bFirstSearchSelect = false
		}

		f.ShowIf(new Array('divDepartureAirport', 'divDateFlexibility'), bFlightPlusHotel);
		f.ShowIf('divFlightOnlyDepartureAirport', bFlight);
		f.ShowIf('divArrivalAirport', bFlight && !me.bShowAutoComplete);
		f.ShowIf('divArrivalAirportAutoComplete', bFlight && me.bShowAutoComplete);
		f.ShowIf('divFlightOccupancy', bFlight);
		f.ShowIf('divDestination', (bFlightPlusHotel || bHotel) && !me.bShowAutoComplete);
		f.ShowIf('divDestinationAutoComplete', (bFlightPlusHotel || bHotel) && me.bShowAutoComplete);
		f.ShowIf('divShowHideAirportAutoComplete', bFlight && me.bShowAutoComplete);
		f.ShowIf('divShowHideAutoComplete', (bFlightPlusHotel || bHotel) && f.GetObject('acpGeographyLevel2ID'));
		f.ShowIf(new Array('divHotelRating', 'divBoardBasis', 'divRooms',
            'divRoomOccupancy', 'divHolidayType', 'divHotelFacilities'), bFlightPlusHotel || bHotel);
		f.ShowIf('divResort', ((bFlightPlusHotel || bHotel) && !me.bShowAutoComplete && (dd.GetValue('sddGeographyLevel2ID') > 0)));

		//set the link text
		f.SetHTML('aShowHideAutoComplete', me.bShowAutoComplete ? Constants.SearchToolHideAutoComplete : Constants.SearchToolShowAutoComplete);

		//Stop the 'more search options' button showing on the flight tab. Comment out if not required.
		f.SetClassIf('divSearch', 'CompressedSearch', me.bCompressedSearch && sSearchMode != 'Flight');

		me.SearchShow();

	}

	//set options for duration dropdown
	this.SetDurationOptions = function () {
		var sDurationText = dd.GetText('ddlDuration');

		if (me.SearchType() == 'Flight') {
			dd.SetOptions('ddlDuration', '0#1#2#3#4#5#6#7#8#9#10#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#27#28#29');
		} else {
			dd.SetOptions('ddlDuration', '1#2#3#4#5#6#7#8#9#10#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#27#28#29');
			if (sDurationText == '0') {
				sDurationText = '1';
			}
		}

		dd.SetText('ddlDuration', sDurationText);
	}


	//toggle extra rooms
	this.SearchShow = function () {
		var iRooms = me.SelectedRooms();

		var sGuests = Constants.GuestsText

		f.SetHTML('tdRoom1', iRooms == 1 ? sGuests : Constants.SearchToolRoom1);

		for (var i = 1; i <= 3; i++) {

			var iChildren = me.ChildCount(i);

			f.ShowIf('trGuests_' + i, iRooms >= i);
			f.ShowIf('trAges_' + i, iRooms >= i && iChildren > 0);

			for (var j = 1; j <= 4; j++) {
				f.ShowIf('txtC_' + i + '_' + j, iChildren >= j);
			}
		}

		var iFlightChildren = dd.GetText('ddlFlightChildren');

		f.ShowIf('trChildFlightAges', iFlightChildren > 0);
		f.ShowIf('txtFC_1', iFlightChildren > 0);
		f.ShowIf('txtFC_2', iFlightChildren > 1);
		f.ShowIf('txtFC_3', iFlightChildren > 2);
		f.ShowIf('txtFC_4', iFlightChildren > 3);


	}


	//save to cookie
	this.SaveToCookie = function (sSearchMode) {
		var aCookieValues = new Array();

		aCookieValues[0] = me.CurrentCookieVersion;
		aCookieValues[1] = sSearchMode;
		aCookieValues[2] = AutoComplete.GetValue('acpGeographyLevel2ID');
		aCookieValues[3] = dd.GetValue('sddGeographyLevel2ID');
		aCookieValues[4] = dd.GetValue('sddGeographyLevel3ID');
		aCookieValues[5] = dd.GetValue('sddDepartureAirportID');
		aCookieValues[6] = dd.GetValue('sddFlightOnlyDepartureAirportID');
		aCookieValues[7] = dd.GetValue('sddArrivalAirportID');
		aCookieValues[8] = dd.GetValue('ddlRooms');
		aCookieValues[9] = dd.GetText('ddlFlightChildren');

		var aRoomValues = new Array();
		aRoomValues[0] = dd.GetText('ddl_1_Children');
		aRoomValues[1] = dd.GetText('ddl_2_Children');
		aRoomValues[2] = dd.GetText('ddl_3_Children');
		aCookieValues[10] = aRoomValues.join('|');

		aCookieValues[11] = Search.bShowAutoComplete.toString();

		c.Set('searchrestore', aCookieValues.join('#'));
	}

	//restore from cookie
	this.RestoreFromCookie = function () {
		var aCookieValues = c.Get('searchrestore').split('#');

		var sSearchMode = aCookieValues[1];

		if (f.GetObject('acpGeographyLevel2ID')) AutoComplete.SetValue('acpGeographyLevel2ID', aCookieValues[2]);
		dd.SetValue('sddGeographyLevel2ID', aCookieValues[3]);
		me.GeographyLevel3ID = aCookieValues[4]; //we have to let the level2select take care of setting the resort dropdown to this as we don't know when the webservice will return.
		me.Level2Select(aCookieValues[3], true);

		dd.SetValue('sddDepartureAirportID', aCookieValues[5]);
		dd.SetValue('sddFlightOnlyDepartureAirportID', aCookieValues[6]);
		dd.SetValue('sddArrivalAirportID', aCookieValues[7]);
		dd.SetText('ddlRooms', aCookieValues[8]);
		dd.SetText('ddlFlightChildren', aCookieValues[9]);

		var aRoomValues = aCookieValues[10].split('|');
		dd.SetText('ddl_1_Children', aRoomValues[0]);
		dd.SetText('ddl_2_Children', aRoomValues[1]);
		dd.SetText('ddl_3_Children', aRoomValues[2]);

		me.bShowAutoComplete = SafeBoolean(aCookieValues[11]) && f.GetObject('acpGeographyLevel2ID');

		//show the correct buttons and options.
		me.SearchSelect(sSearchMode);
	}


	//ensure cookie is latest
	this.EnsureCookieIsLatest = function () {

		if (me.HasCookie()) {
			var aCookieValues = c.Get('searchrestore').split('#');
			var iCookieVersion = SafeInt(aCookieValues[0]);
			if (isNaN(iCookieVersion) || iCookieVersion < me.CurrentCookieVersion) me.ClearCookie();
		}
	}

	//has cookie
	this.HasCookie = function () {
		return c.Get('searchrestore') != '';
	}

	//clear cookie
	this.ClearCookie = function () {
		c.Delete('searchrestore');
	}



	//Search Validation
	this.SearchValidate = function () {

		FormHandler.CloseInfo();

		var oValidator = new Validator();
		var sWarnings;

		var bHotel = me.SearchType() == 'Hotel';
		var bFlight = me.SearchType() == 'Flight';
		var bFlightPlusHotel = me.SearchType() == 'FlightPlusHotel';

		if (bHotel || bFlightPlusHotel) {
			if (me.bShowAutoComplete) {
				oValidator.AddValidation('acpGeographyLevel2ID', Constants.SearchToolDestination, 'NotEmpty');
			} else {
				oValidator.AddValidation('sddGeographyLevel2ID', Constants.SearchToolDestination, 'NotEmpty');
			}
		}

		if (bFlightPlusHotel) {
			oValidator.AddValidation('sddDepartureAirportID', Constants.SearchToolDepartureAirport, 'NotEmpty');
		}

		if (bFlight) {
			oValidator.AddValidation('sddFlightOnlyDepartureAirportID', Constants.SearchToolDepartureAirport, 'NotEmpty');

			if (me.bShowAutoComplete) {
				oValidator.AddValidation('acpArrivalAirportID', Constants.SearchToolDestination, 'NotEmpty');
			} else {
				oValidator.AddValidation('sddArrivalAirportID', Constants.SearchToolArrivalAirport, 'NotEmpty');
			}
		}

		oValidator.AddCustomValidation(function ()
		{ return new Calendar(null, 'calDepartureDate').CurrentDate >= new Date() }, 'calDepartureDate_MonthYear',
            Constants.SearchToolFutureDate);


		//check child ages
		if (bFlight == true) {

			//flights
			iChildren = n.SafeInt(dd.GetText('ddlFlightChildren'));
			var bValidationAdded = false;

			for (var iChild = 1; iChild <= iChildren; iChild++) {

				var sDropDown = 'txtFC_' + iChild;

				iChildAge = dd.GetIntText(sDropDown);
				if (iChildAge == 0) {
					oValidator.AddCustomValidation(false, sDropDown, s.Format(Constants.SearchToolSetChildAge, iChild));
				}
			}

		} else {

			//rooms
			var iRooms = me.SelectedRooms();

			var iChildren, iChildAge, bChildAgeErrors = false;
			for (var iRoom = 1; iRoom <= iRooms; iRoom++) {

				iChildren = n.SafeInt(dd.GetText('ddl_' + iRoom + '_Children'));
				var bValidationAdded = false;

				for (var iChild = 1; iChild <= iChildren; iChild++) {

					var sDropDown = 'txtC_' + iRoom + '_' + iChild;


					iChildAge = dd.GetIntText(sDropDown);
					if (iChildAge == 0) {

						if (iRooms == 1) {
							oValidator.AddCustomValidation(false, sDropDown, s.Format(Constants.SearchToolSetChildAge, iChild));
						} else {
							oValidator.AddCustomValidation(false, sDropDown, s.Format(Constants.SearchToolSetChildAgeRoom, iChild, iRoom));
						}
					}

				}
			}
		}

		var bSuccess = oValidator.Validate();

		if (bSuccess == true) {
			var sKeyValuePairString = f.GetContainerQueryString('divSearch');

			PopupDisplayObjects(new Array(['label', 'lblInfo', 1], ['label', 'lblWarning', 0], ['input', 'btnClose', 0]));
			ShowWaitMessage();

			//if ie6, hide the dropdowns!
			if (b.IE6()) {
				var aSelect = document.getElementsByTagName('select');
				for (var i = 0; i < aSelect.length; i++) {
					aSelect[i].style.visibility = 'hidden';

				}
			}

			oGenerateSearch.Go(sKeyValuePairString);

		}

	}

	// More Search Options
	this.ShowMoreOptions = function () {
		this.bCompressedSearch = false;
		f.RemoveClass('divSearch', 'CompressedSearch');
	}

	// Set up the click events on the top
	this.SetUpSearchTop = function () {
		f.AttachEvent('aFlightPlusHotel', 'click',
            function (oEvent) {
            	Search.ShowSearchTopTab(f.GetObjectFromEvent(oEvent));
            });
		f.AttachEvent('aFlights', 'click',
            function (oEvent) {
            	Search.ShowSearchTopTab(f.GetObjectFromEvent(oEvent));
            });
		f.AttachEvent('aHotels', 'click',
            function (oEvent) {
            	Search.ShowSearchTopTab(f.GetObjectFromEvent(oEvent));
            });

		if (f.GetValue('hidSearchMode') == "Hotel")
		{ this.ShowSearchTopTab(f.SafeObject('aHotels')) }

	}

	// Click events for Top
	this.ShowSearchTopTab = function (o) {
		var bFlightPlusHotel = true;
		var bFlights = (f.GetElementsByClassName('div', 'HotelSearch').length == 0)
		var bHotels = true;
		var sTabToShow = o.id;

		if (sTabToShow == 'aFlightPlusHotel' || (sTabToShow == 'aFlights' && !bFlightPlusHotel)) {
			f.Show('divSearchTop1');
			f.Hide('divSearchTop2');
			f.Hide('divSearchTop3');
		}
		else if ((sTabToShow == 'aFlights' && bFlightPlusHotel)
            || (sTabToShow == 'aHotels' && bFlightPlusHotel && !bFlights)) {
			f.Hide('divSearchTop1');
			f.Show('divSearchTop2');
			f.Hide('divSearchTop3');
		}
		else if (sTabToShow == 'aHotels' && bFlightPlusHotel && bFlights) {
			f.Hide('divSearchTop1');
			f.Hide('divSearchTop2');
			f.Show('divSearchTop3');
		}

	}


	// helper properties
	this.SelectedRooms = function () { return n.SafeInt(dd.GetText('ddlRooms')); }
	this.ChildCount = function (iRoom) { return n.SafeInt(dd.GetText('ddl_' + iRoom + '_Children')); }
	this.SearchType = function () { return f.GetValue('hidSearchMode'); }

	this.HasCompressedSearch = function () {
		var aElements = f.GetElementsByClassName('div', 'CompressedSearch');
		for (var i = 0; i < aElements.length; i++) {
			if (aElements[i].id = 'divSearch') return true;
		}
		return false;
	}


	//multithreaded search
	this.CheckPropertyResultsExist = function (FlightSearchID) {
		oCheckPropertyResultsExist.FlightSearchID = FlightSearchID;
		oCheckPropertyResultsExist.SearchStartTime = new Date();
		oCheckPropertyResultsExist.Go();
	}
}





///////////////   webservice definitions ////////////////

//generate region dropdown
var oGenerateRegionDropdown = new WebService();
oGenerateRegionDropdown.Go = function (DepartureAirportID, GeographyLevel2ID, SearchMode) {
	aParams = new Array(['DepartureAirportID', DepartureAirportID], ['GeographyLevel2ID', GeographyLevel2ID], ['SearchMode', SearchMode]);
	this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'GenerateRegionDropdown', aParams, this, false);
}

oGenerateRegionDropdown.Done = function (oXML) {
	var oReturn = this.GetTagValue(oXML, 'GenerateRegionDropdownResult');
	f.SetHTML('spnRegion', oReturn);
}

//generate resort dropdown
var oGenerateResortDropdown = new WebService();
oGenerateResortDropdown.Go = function (GeographyLevel2ID) {
	aParams = new Array(['GeographyLevel2ID', GeographyLevel2ID]);
	this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'GenerateResortDropdown', aParams, this, false);
}

oGenerateResortDropdown.Done = function (oXML) {
	var oReturn = this.GetTagValue(oXML, 'GenerateResortDropdownResult');
	Search.Level2SelectDone(oReturn);
}

//generate departure airports
var oGenerateDepartureAirportDropdown = new WebService();
oGenerateDepartureAirportDropdown.Go = function (GeographyLevel2ID, DepartureAirportID) {
	aParams = new Array(['GeographyLevel2ID', GeographyLevel2ID], ['DepartureAirportID', DepartureAirportID]);
	this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'GenerateDepartureAirportDropdown', aParams, this, false);
}

oGenerateDepartureAirportDropdown.Done = function (oXML) {
	var oReturn = this.GetTagValue(oXML, 'GenerateDepartureAirportDropdownResult');
	f.SetHTML('spnDepartureAirport', oReturn);
}

//generate search results
var oGenerateSearch = new WebService();
oGenerateSearch.Go = function (sSearchString) {
	aParams = new Array(['sSearchString', sSearchString]);

	//get query string if it exists
	var aURLSplit = window.location.toString().split('?');
	var sQueryString = ''
	if (aURLSplit.length > 1) {
		sQueryString = '?' + aURLSplit[1];
	}

	this.RunWebService('/webservices/search.asmx' + sQueryString, 'http://intuitivesystems', 'GenerateSearch', aParams, this, false);
}

oGenerateSearch.Done = function (oXML) {
	var oReturn = new GenerateSearchResult(oXML);

	//save search cookie
	Search.SaveToCookie(oReturn.SearchMode);

	if (oReturn.SearchMode == 'FlightPlusHotel') {

		if (oReturn.TotalFlightCount > 0 && (oReturn.PropertyCount > 0 || f.GetValue('hidMultiThreadedSearch') == 'True')) {
			window.location = '/flightresults.aspx';
		} else {
			//show warning
			//PopupDisplayObjects(new Array(['label', 'lblInfo', 0], ['label', 'lblWarning', 1], ['input', 'btnClose', 1]));

			//close wait message and show warning
			e.ModalPopup.Close();
			FormHandler.ShowWarning(Constants.SearchNoResults);
		}

	} else if (oReturn.SearchMode == 'Flight') {

		if (oReturn.TotalFlightCount > 0) {
			window.location = '/flightresults.aspx';
		} else {
			//show warning
			//PopupDisplayObjects(new Array(['label', 'lblInfo', 0], ['label', 'lblWarning', 1], ['input', 'btnClose', 1]));

			//close wait message and show warning
			e.ModalPopup.Close();
			FormHandler.ShowWarning(Constants.SearchNoResults);
		}

	} else if (oReturn.SearchMode == 'Hotel') {

		if (oReturn.PropertyCount > 0) {
			window.location = '/propertyresults.aspx';
		} else {
			//show warning
			//PopupDisplayObjects(new Array(['label', 'lblInfo', 0], ['label', 'lblWarning', 1], ['input', 'btnClose', 1]));

			//close wait message and show warning
			e.ModalPopup.Close();
			FormHandler.ShowWarning(Constants.SearchNoResults);
		}

	} else {
		//shouldn't be here
		PopupDisplayObjects(new Array(['label', 'lblInfo', 0], ['label', 'lblWarning', 1], ['input', 'btnClose', 1]));
	}
}


//check property results exist for multithreaded search
var oCheckPropertyResultsExist = new WebService();
oCheckPropertyResultsExist.FlightSearchID;
oCheckPropertyResultsExist.SearchStartTime;


oCheckPropertyResultsExist.Go = function () {

	aParams = new Array();
	this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'CheckPropertyResultsExist', aParams, this, false);
}


oCheckPropertyResultsExist.Done = function (oXML) {
	var oReturn = this.GetTagValue(oXML, 'CheckPropertyResultsExistResult');
	var dTimeNow = new Date();
	var iSecondsElapsed = (dTimeNow.getTime() - oCheckPropertyResultsExist.SearchStartTime.getTime()) / 1000;


	//go to property results or check again
	if (oReturn == 'FinishedWithResults') {
		Postback('BookFlight', oCheckPropertyResultsExist.FlightSearchID);
	} else if (oReturn == 'Searching' && iSecondsElapsed < 60) {
		setTimeout('oCheckPropertyResultsExist.Go()', 2000);
		ShowWaitMessage('divHolidayOfferWaitMessage');
	} else if (oReturn == 'FinishedNoResults') {
		//no property results - display message
		e.ModalPopup.Close();
		FormHandler.ShowWarning('Your search returned no property results. Please try again using different criteria.');
	} else if (iSecondsElapsed > 60 || oReturn == 'Failed' || oReturn == '') {
		//return to homepage with warning
		window.location = '/default.aspx?splitsearchfailure'
	}
}



function GenerateSearchResult(oXML) {
	this.SearchMode = oWebService.GetTagValue(oXML, 'SearchMode');
	this.PropertyCount = oWebService.GetTagValue(oXML, 'PropertyCount');
	this.FlightCount = oWebService.GetTagValue(oXML, 'FlightCount');
	this.TotalFlightCount = oWebService.GetTagValue(oXML, 'TotalFlightCount');
}



var oGenerateArrivalAirports = new WebService();
oGenerateArrivalAirports.Go = function (DepartureAirportID, ArrivalAirportID) {
	aParams = new Array(['DepartureAirportID', DepartureAirportID], ['ArrivalAirportID', ArrivalAirportID]);
	this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'GenerateArrivalAirports', aParams, this, false);
}

oGenerateArrivalAirports.Done = function (oXML) {
	var oReturn = this.GetTagValue(oXML, 'GenerateArrivalAirportsResult');
	f.SetHTML('spnArrivalAirport', oReturn);
}



var oGenerateDepartureAirportFromArrivalAirport = new WebService();
oGenerateDepartureAirportFromArrivalAirport.Go = function (ArrivalAirportID, DepartureAirportID) {
	aParams = new Array(['ArrivalAirportID', ArrivalAirportID], ['DepartureAirportID', DepartureAirportID]);
	this.RunWebService('/webservices/search.asmx', 'http://intuitivesystems', 'GenerateDepartureAirportFromArrivalAirport', aParams, this, false);
}

oGenerateDepartureAirportFromArrivalAirport.Done = function (oXML) {
	var oReturn = this.GetTagValue(oXML, 'GenerateDepartureAirportFromArrivalAirportResult');
	f.SetHTML('spnFlightOnlyDepartureAirport', oReturn);
}