var Search = new function() {

    var me = this;
    this.GeographyLevel3ID = 0;

    //level2 select
    this.Level2Select = function(iGeographyLevel2ID) {
        f.GetObject('sddGeographyLevel3ID').disabled = true;
        f.GetObject('sddDepartureAirportID').disabled = true;
        oGenerateResortDropdown.Go(iGeographyLevel2ID);
    }

    //level2 select done
    this.Level2SelectDone = function(sDropdownHTML) {
        f.ShowIf('divResort', (dd.GetValue('sddGeographyLevel2ID') > 0));
        f.SetHTML('spnResort', sDropdownHTML);
        f.GetObject('sddGeographyLevel3ID').disabled = false;
        dd.SetValue('sddGeographyLevel3ID', me.GeographyLevel3ID);
        me.GeographyLevel3ID = 0;
        oGenerateDepartureAirportDropdown.Go(dd.GetValue('sddGeographyLevel2ID'), dd.GetValue('sddDepartureAirportID'));
    }


    //departure airport select
    this.DepartureAirportSelect = function(iDepartureAirportID) {
        f.GetObject('sddGeographyLevel2ID').disabled = true;
        oGenerateRegionDropdown.Go(iDepartureAirportID, dd.GetValue('sddGeographyLevel2ID'));
    }


    //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'));
    }

    //setup
    this.Setup = function() {
        if (c.Get('searchrestore')) {
            var sSearchMode = c.Get('searchrestore').split('#')[0];
            this.SearchSelect(c.Get('searchrestore').split('#')[0]);
            if (sSearchMode != 'Flight') {
                dd.SetValue('sddGeographyLevel2ID', c.Get('searchrestore').split('#')[1]);
                me.Level2Select(c.Get('searchrestore').split('#')[1]);
                me.GeographyLevel3ID = c.Get('searchrestore').split('#')[2];
            }
            dd.SetValue('sddGeographyLevel3ID', c.Get('searchrestore').split('#')[2]);
            dd.SetValue('sddDepartureAirportID', c.Get('searchrestore').split('#')[3]);
            dd.SetValue('sddFlightOnlyDepartureAirportID', c.Get('searchrestore').split('#')[4]);
            dd.SetValue('sddArrivalAirportID', c.Get('searchrestore').split('#')[5]);
            dd.SetText('ddlRooms', c.Get('searchrestore').split('#')[6])
            c.Delete('searchrestore');
        }

        f.GetObject('sddGeographyLevel2ID').disabled = false;

        var aChildDropdowns = f.GetElementsByClassName('select', 'child');

        for (var i = 0; i < aChildDropdowns.length; i++) {
            f.AttachEvent(aChildDropdowns[i], 'change',
                function() {
                    Search.SearchShow();
                });
        }

        me.SearchShow();
    }


    //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 && sSearchMode != 'Flight') {
            dd.SetIndex('sddArrivalAirportID', 0);
        }

        var bHotel = me.SearchType() == 'FlightPlusHotel' || me.SearchType() == 'Hotel';
        var bFlight = me.SearchType() == 'Flight';
        var bFlightPlusHotel = me.SearchType() == 'FlightPlusHotel';


        f.ShowIf(new Array('divDepartureAirport', 'divDateFlexibility'), bFlightPlusHotel);
        f.ShowIf('divFlightOnlyDepartureAirport', bFlight);
        f.ShowIf('divArrivalAirport', bFlight);
        f.ShowIf('divFlightOccupancy', bFlight);
        f.ShowIf('divDestination', bFlightPlusHotel || bHotel);
        f.ShowIf(new Array('divHotelRating', 'divBoardBasis', 'divRooms',
            'divRoomOccupancy', 'divHolidayType', 'divHotelFacilities'), bHotel);
        f.ShowIf('divResort', ((bFlightPlusHotel || bHotel) && (dd.GetValue('sddGeographyLevel2ID') > 0)));

        if (bFlightPlusHotel && n.SafeInt(dd.GetValue('sddGeographyLevel2ID')) > 0) {
            me.Level2Select(dd.GetValue('sddGeographyLevel2ID'));
        }

    }


    //toggle extra rooms
    this.SearchShow = function() {

        var iRooms = me.SelectedRooms();

        f.SetHTML('tdRoom1', iRooms == 1 ? 'Guests' : 'Room 1');

        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);


    }


    //clear
    this.ClearCookie = function() {
        c.Delete('searchrestore');
    }



    //Search Validation
    this.SearchValidate = function() {

        FormHandler.CloseInfo();

        var oValidator = new Validator();
        var sWarnings;

        var bHotel = me.SearchType() == 'FlightPlusHotel' || me.SearchType() == 'Hotel';
        var bFlight = me.SearchType() == 'Flight';
        var bFlightPlusHotel = me.SearchType() == 'FlightPlusHotel';

        if (bFlightPlusHotel) {
            oValidator.AddValidation('sddDepartureAirportID', 'Departure Airport', 'NotEmpty');
        }

        if (bFlight) {
            oValidator.AddValidation('sddFlightOnlyDepartureAirportID', 'Arrival Airport', 'NotEmpty');
            oValidator.AddValidation('sddArrivalAirportID', 'Arrival Airport', 'NotEmpty');
        }

        if (bHotel || bFlightPlusHotel) {
            oValidator.AddValidation('sddGeographyLevel2ID', 'Destination', 'NotEmpty');
        }

        oValidator.AddCustomValidation(function()
        { return new Calendar(null, 'calDepartureDate').CurrentDate >= new Date() }, 'calDepartureDate_MonthYear',
            'Please make sure the Date is in the future');


        //check child ages
        if (bFlight == true) {

            //flights
            iChildren = n.SafeInt(dd.GetText('ddlFlightChildren'));
            var bValidationAdded = false;

            for (var iChild = 1; iChild <= iChildren; iChild++) {

                var sTextbox = 'txtFC_' + iChild;

                iChildAge = n.SafeInt(f.GetValue(sTextbox));
                if (iChildAge < 2 || iChildAge > 18) {
                    oValidator.AddCustomValidation(false, sTextbox,
                              'Please set child age ' + 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 sTextbox = 'txtC_' + iRoom + '_' + iChild;


                    iChildAge = n.SafeInt(f.GetValue(sTextbox));
                    if (iChildAge < 2 || iChildAge > 18) {
                        oValidator.AddCustomValidation(false, sTextbox,
                              'Please set child age ' + iChild + ' for room ' + iRoom);
                    }

                }
            }
        }

        var bSuccess = oValidator.Validate();

        if (bSuccess == true) {

            var sKeyValuePairString = f.GetContainerQueryString('divSearch');
           // f.Show('lblInfo');
            //f.Hide('lblWarning');
            //f.Hide('btnClose');
            document.body.appendChild(f.GetObject('divProgressBar'));
//            e.ModalPopup.Show('divProgressBar');

            //if ie6, hide the dropdowns!
            if (navigator.appNam == 'Microsoft Internet Explorer' && parseFloat(navigator.appVersion.split('MSIE')[1]) < 7) {
                var aSelect = document.getElementsByTagName('select');
                for (var i = 0; i < aSelect.length; i++) {
                    aSelect[i].style.visibility = 'hidden';

                }
            }

            //oGenerateSearch.Go(sKeyValuePairString);
        }
	return bSuccess;
    }



    // 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'); }
}





///////////////   webservice definitions ////////////////

//generate region dropdown
var oGenerateRegionDropdown = new WebService();
oGenerateRegionDropdown.Go = function(DepartureAirportID, GeographyLevel2ID) {
    aParams = new Array(['DepartureAirportID', DepartureAirportID], ['GeographyLevel2ID', GeographyLevel2ID]);
    this.RunWebService('http://www.holidayroomsdirect.com/_engine1/providers/lcb_searchform.asp', '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('http://www.holidayroomsdirect.com/_engine1/providers/lcb_searchform.asp', '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('http://www.holidayroomsdirect.com/_engine1/providers/lcb_searchform.asp', '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]);
    this.RunWebService('http://www.holidayroomsdirect.com/_engine1/providers/lcb_searchform.asp', 'http://intuitivesystems', 'GenerateSearch', aParams, this, false);
}

oGenerateSearch.Done = function(oXML) {
    var oReturn = new GenerateSearchResult(this.GetTagValue(oXML, 'SearchMode'), this.GetTagValue(oXML, 'PropertyCount'),
        this.GetTagValue(oXML, 'FlightCount'), this.GetTagValue(oXML, 'TotalFlightCount'));

    //store destination and departure
    c.Set('searchrestore', oReturn.SearchMode + '#' + dd.GetValue('sddGeographyLevel2ID')
        + '#' + dd.GetValue('sddGeographyLevel3ID') + '#' + dd.GetValue('sddDepartureAirportID')
        + '#' + dd.GetValue('sddFlightOnlyDepartureAirportID') + '#' + dd.GetValue('sddArrivalAirportID')
        + '#' + dd.GetValue('ddlRooms'));

    if ((oReturn.SearchMode == 'FlightPlusHotel') || (oReturn.SearchMode == 'Flight')) {

        if (oReturn.TotalFlightCount > 0) {
            window.location = 'http://www.lowcostholidays.com/flight.aspx';
        } else {
            //show warning
            f.Hide('lblInfo');
            f.Show('lblWarning');
            f.Show('btnClose');
        }

    } else if (oReturn.SearchMode == 'Hotel') {

        if (oReturn.PropertyCount > 0) {
            window.location = 'http://www.lowcostholidays.com/propertyresults.aspx';
        } else {
            //show warning
            f.Hide('lblInfo');
            f.Show('lblWarning');
            f.Show('btnClose');
        }

    } else {
        //shouldn't be here
        f.Hide('lblInfo');
        f.Show('lblWarning');
        f.Show('btnClose');
    }
}



function GenerateSearchResult(SearchMode, PropertyCount, FlightCount, TotalFlightCount) {
    this.SearchMode = SearchMode;
    this.PropertyCount = PropertyCount;
    this.FlightCount = FlightCount;
    this.TotalFlightCount = TotalFlightCount;
}



var oGenerateArrivalAirports = new WebService();
oGenerateArrivalAirports.Go = function(DepartureAirportID, ArrivalAirportID) {
    aParams = new Array(['DepartureAirportID', DepartureAirportID], ['ArrivalAirportID', ArrivalAirportID]);
    this.RunWebService('http://www.holidayroomsdirect.com/_engine1/providers/lcb_searchform.asp', '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('http://www.holidayroomsdirect.com/_engine1/providers/lcb_searchform.asp', 'http://intuitivesystems', 'GenerateDepartureAirportFromArrivalAirport', aParams, this, false);
}

oGenerateDepartureAirportFromArrivalAirport.Done = function(oXML) {
    var oReturn = this.GetTagValue(oXML, 'GenerateDepartureAirportFromArrivalAirportResult');
    f.SetHTML('spnFlightOnlyDepartureAirport', oReturn);
}

