﻿if (typeof IgluSearch == 'undefined') {
    throw 'IgluSearch not found';
}

IgluSearch.Control =
{
    /*Name of this Search Control*/
    name: 'Iglu Cruise',

    /*The HTML we will be rendering out*/
    html: '<div id="igluSearchControl" style="width: 185px;">' +
                '<div id="iglu_cruise_destination">' +
                    '<span id="iglu_cruise_spnDestination">Destination: </span>' +
                    '<select id="iglu_cruise_cboDestination" ></select>' +
                '</div>' +
                '<div id="iglu_cruise_cruiseline">' +
                    '<span id="iglu_cruise_spnCruiseLine">Cruise Line: </span>' +
                    '<select onchange="IgluSearch.Control.createShips(this.value);" id="iglu_cruise_cboCruiseLine"></select>' +
                '</div>' +
                '<div id="iglu_cruise_cruiseship">' +
                    '<span id="iglu_cruise_spnCruiseShip">Cruise Ship:</span>' +
                    '<select id="iglu_cruise_cboCruiseShip"></select>' +
                '</div>' +
                '<div id="iglu_cruise_cruisetype">' +
                    '<span id="iglu_cruise_spnCruiseType">Cruise Type:</span>' +
                    '<select onchange="IgluSearch.Control.createDepStations(this.value);" id="iglu_cruise_cboCruiseType"></select>' +
                '</div>' +
                '<div id="iglu_cruise_departuredate">' +
                    '<span id="iglu_cruise_spnDeparture">Departure:</span>' +
                    '<select id="iglu_cruise_cboDepartureDay"></select>' +
                    '<select onchange="IgluSearch.Control.createDepDates(this.value);" id="iglu_cruise_cboDepartureMonth"></select>' +
                '</div>' +
                '<div id="iglu_cruise_flex">' +
                    '<span id="iglu_cruise_spnFlex">Flexibility:</span>' +
                    '<select id="iglu_cruise_cboFlex">' +
                        '<option Value="None">None</option>' +
                        '<option Value="3">+/- 3 days</option>' +
                        '<option Value="7">+/- 1 week</option>' +
                        '<option Value="14">+/- 2 weeks</option>' +
                        '<option Value="30">+/- 1 month</option>' +
                    '</select>' +
                '</div>' +
                '<div id="iglu_cruise_duration">' +
                    '<span id="iglu_cruise_spnDuration">Duration:</span>' +
                    '<select id="iglu_cruise_cboDuration">' +
                        '<option Value="ALL">Any Duration</option>' +
                        '<option Value="0,5">up to 5 nights</option>' +
                        '<option Value="6,7">6 to 7 nights</option>' +
                        '<option Value="8,12">8 to 12 nights</option>' +
                        '<option Value="13,16">13 to 16 nights</option>' +
                        '<option Value="17,21">17 to 21 nights</option>' +
                        '<option Value="22,9999">22 nights and above</option>' +
                    '</select>' +
                '</div>' +
                '<div id="iglu_cruise_depstation">' +
                    '<span id="iglu_cruise_spnDepStation">Departure Station:</span>' +
                    '<select id="iglu_cruise_cboDepStation"></select>' +
                '</div>' +
                '<div id="buttonContainer">' +
                    '<button id="btnSearch" onclick="IgluSearch.Search();" type="button">Search</button>' +
                '</div>' +
            '</div>',

    /*These are the options that are definable by the client browser*/
    options:
    {
        showDestination: true,
        showCruiseLine: true,
        showShip: true,
        showCruiseType: true,
        showDepDate: true,
        showFlex: true,
        showDuration: true,
        showDepStation: true
    },

    /*Populate Destination Combo Box*/
    createDestination: function () {

        //Check to make sure we have the countries...
        if (typeof IgluSearch.Data.Destination == 'undefined')
            throw 'Could not find Destinations';

        //Get access to the Country Dropdown List
        var cboDestination = IgluSearch.get('iglu_cruise_cboDestination');
        cboDestination.disabled = true;

        //Loop through all the Countries
        for (var i = 0; i < IgluSearch.Data.Destination.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.Destination[i][0]); //Value
            option.innerHTML = IgluSearch.Data.Destination[i][1]; //Text

            //Append the child
            cboDestination.appendChild(option);
        }

        //Re-Enable the Combo Box
        cboDestination.disabled = false;

    },

    /*Populate Destination Combo Box*/
    createCruiseLine: function () {

        //Check to make sure we have the countries...
        if (typeof IgluSearch.Data.CruiseLine == 'undefined')
            throw 'Could not find Destinations';

        //Get access to the Country Dropdown List
        var cboCruiseLine = IgluSearch.get('iglu_cruise_cboCruiseLine');
        cboCruiseLine.disabled = true;

        //Loop through all the Countries
        for (var i = 0; i < IgluSearch.Data.CruiseLine.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.CruiseLine[i][0]); //Value
            option.innerHTML = IgluSearch.Data.CruiseLine[i][1]; //Text

            //Append the child
            cboCruiseLine.appendChild(option);
        }

        //Re-Enable the Combo Box
        cboCruiseLine.disabled = false;

    },


    /*The Function for the Resort Combo Box*/
    createShips: function (cruiselinecode) {
        //Deal with the cruiselinecode, we may need to restrict the Resorts dependant on the selected country
        var shipsToUse;

        if (typeof cruiselinecode != 'undefined' && cruiselinecode != 'ALL') {
            shipsToUse = new Array();
            var totalShips = 0;

            //Loop through all the Resorts, getting the ones that match
            for (var i = 0; i < IgluSearch.Data.CruiseShip.length; i++)
                if (IgluSearch.Data.CruiseShip[i][1] == cruiselinecode ||
                    IgluSearch.Data.CruiseShip[i][1] == 'ALL') {
                    shipsToUse[totalShips] = IgluSearch.Data.CruiseShip[i];
                    totalShips++;
                }
        }
        else
            shipsToUse = IgluSearch.Data.CruiseShip;

        //Get access to the Resort Dropdown, and clear it of it's items
        var cboCruiseShip = IgluSearch.get('iglu_cruise_cboCruiseShip');
        IgluSearch.clearSelect(cboCruiseShip);

        //Disable the control, i.e. Grey it out
        cboCruiseShip.disabled = true;

        //populate ship combo box with filtered ones
        for (var i = 0; i < shipsToUse.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', shipsToUse[i][0]); //Value
            option.innerHTML = shipsToUse[i][2]; //Text

            cboCruiseShip.appendChild(option);
        }

        cboCruiseShip.disabled = false;
    },

    /*Populate CruiseType Combo Box*/
    createCruiseType: function () {

        //Check to make sure we have the countries...
        if (typeof IgluSearch.Data.CruiseType == 'undefined')
            throw 'Could not find Cruise Types';

        //Get access to the Country Dropdown List
        var cboCruiseType = IgluSearch.get('iglu_cruise_cboCruiseType');
        cboCruiseType.disabled = true;

        //Loop through all the Countries
        for (var i = 0; i < IgluSearch.Data.CruiseType.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.CruiseType[i][0]); //Value
            option.innerHTML = IgluSearch.Data.CruiseType[i][1]; //Text

            //Append the child
            cboCruiseType.appendChild(option);
        }

        //Re-Enable the Combo Box
        cboCruiseType.disabled = false;

    },

    createFlex: function () {
    },

    createDuration: function () {
    },

    /*Populate Departure stations Combo Box*/
    createDepStations: function (cruisetypecode) {

        var cruisetype;
        if (cruisetypecode == null || cruisetypecode == "ALL") cruisetype = "ALL";
        else if (cruisetypecode == "CRO") cruisetype = "Cruise";
        else cruisetype = "Fly";

        var stationsToUse;
        if (cruisetype != 'ALL') {
            stationsToUse = new Array();
            var totalStations = 0;

            //Loop through all the Resorts, getting the ones that match
            for (var i = 0; i < IgluSearch.Data.DepartureStation.length; i++) {
                if (IgluSearch.Data.DepartureStation[i][1] == cruisetype ||
                    IgluSearch.Data.DepartureStation[i][1] == 'ALL') {
                    stationsToUse[totalStations] = IgluSearch.Data.DepartureStation[i];
                    totalStations++;
                }
            }
        }
        else {
            stationsToUse = IgluSearch.Data.DepartureStation;
        }


        //Get access to the Resort Dropdown, and clear it of it's items
        var cboDepStation = IgluSearch.get('iglu_cruise_cboDepStation');
        IgluSearch.clearSelect(cboDepStation);

        //Disable the control, i.e. Grey it out
        cboDepStation.disabled = true;

        var CROadded = false;
        var FLYadded = false;
        var optGroupCruise = document.createElement('optgroup');
        var optGroupFly = document.createElement('optgroup');
        //populate ship combo box with filtered ones
        for (var i = 0; i < stationsToUse.length; i++) {

            var option = document.createElement('option');

            if (stationsToUse[i][1] == 'Cruise') {
                optGroupCruise.label = 'Cruise From:';

                option.setAttribute('value', stationsToUse[i][0]); //Value
                option.innerHTML = stationsToUse[i][2]; //Text
                optGroupCruise.appendChild(option);
                CROadded = true;
            }
            else if (stationsToUse[i][1] == 'Fly') {
                optGroupFly.label = 'Fly From:';

                option.setAttribute('value', stationsToUse[i][0]); //Value
                option.innerHTML = stationsToUse[i][2]; //Text
                optGroupFly.appendChild(option);
                FLYadded = true;
            }

        }

        var option = document.createElement('option');
        option.setAttribute('value', stationsToUse[0][0]);
        option.innerHTML = stationsToUse[0][2];
        cboDepStation.appendChild(option);

        cboDepStation.appendChild(optGroupCruise);
        if (CROadded) cboDepStation.appendChild(optGroupCruise);
        if (FLYadded) cboDepStation.appendChild(optGroupFly);

        cboDepStation.disabled = false;
    },

    /*Populate departure dates Combo Box*/
    createDepDates: function (monthYear) {
        if (typeof IgluSearch.Data.DepartureMonth == 'undefined')
            throw 'Could not find Departure Month';

        var daysInMonth = 31;
        if (monthYear != null) {
            daysInMonth = IgluSearch.getDaysInMonth(monthYear.split('/')[0]);
        }

        //Get access to the cboDepartureDay List
        var cboDepartureDay = IgluSearch.get('iglu_cruise_cboDepartureDay');
        IgluSearch.clearSelect(cboDepartureDay);
        cboDepartureDay.disabled = true;

        var optanyday = document.createElement('option');
        optanyday.setAttribute('value', 'ALL'); //Value
        optanyday.innerHTML = 'Any Day'; //Text
        cboDepartureDay.appendChild(optanyday);

        for (var i = 1; i <= daysInMonth; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', i); //Value
            option.innerHTML = i; //Text

            cboDepartureDay.appendChild(option);
        }
        cboDepartureDay.disabled = false;


        //Get access to the cboDepartureDay List
        var cboDepartureMonth = IgluSearch.get('iglu_cruise_cboDepartureMonth');
        cboDepartureMonth.disabled = true;
        for (var i = 0; i < IgluSearch.Data.DepartureMonth.length; i++) {
            var option = document.createElement('option');
            option.setAttribute('value', IgluSearch.Data.DepartureMonth[i][0]); //Value
            option.innerHTML = IgluSearch.Data.DepartureMonth[i][1]; //Text

            //Append the child
            cboDepartureMonth.appendChild(option);
        }
        cboDepartureMonth.disabled = false;
    },


    BuildSearchUrl: function (cboId, ignoreVal, queryString) {
        var cbo = IgluSearch.get(cboId);

        if (cbo.value != ignoreVal)
            return '&' + queryString + '=' + cbo.value;
        else
            return '';
    },

    /*Build up the URL for the Search*/
    Search: function () {
        var completeUrl = IgluSearch.Control.LinkThroughUrl;

        completeUrl += this.BuildSearchUrl('iglu_cruise_cboDestination', 'ALL', 'destination');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboCruiseLine', 'ALL', 'supplier');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboCruiseShip', 'ALL', 'ship');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboCruiseType', 'ALL', 'cruisetype');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboDepartureDay', 'ALL', 'depday');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboDepartureMonth', 'ALL', 'depmonthyear');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboFlex', 'None', 'flex');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboDuration', 'ALL', 'duration');
        completeUrl += this.BuildSearchUrl('iglu_cruise_cboDepStation', 'ALL', 'depstation');

        //Return the Complete Url
        return completeUrl;
    }
};

/*Set the Pre Render event for this Control, so we can show/hide what we need to.*/
IgluSearch.PreRender = function () {

    /*Hide the Country if we need to...*/
    if (!IgluSearch.Control.options.showDestination)
        IgluSearch.get('iglu_cruise_destination').style.display = 'none';
    else
        IgluSearch.Control.createDestination();

    if (!IgluSearch.Control.options.showCruiseLine)
        IgluSearch.get('iglu_cruise_cruiseline').style.display = 'none';
    else
        IgluSearch.Control.createCruiseLine();

    if (!IgluSearch.Control.options.showShip)
        IgluSearch.get('iglu_cruise_cruiseship').style.display = 'none';
    else
        IgluSearch.Control.createShips();

    /*Hide the Cruise Type if we need to...*/
    if (!IgluSearch.Control.options.showCruiseType)
        IgluSearch.get('iglu_cruise_cruisetype').style.display = 'none';
    else
        IgluSearch.Control.createCruiseType();

    /*Hide the Date if we need to...*/
    if (!IgluSearch.Control.options.showDepDate) 
        IgluSearch.get('iglu_cruise_departuredate').style.display = 'none';
    else
        IgluSearch.Control.createDepDates();
    
    /*Hide the Flex if we need to...*/
    if (!IgluSearch.Control.options.showFlex)
        IgluSearch.get('iglu_cruise_flex').style.display = 'none';
    else
        IgluSearch.Control.createFlex();

    /*Hide the PropertyType if we need to...*/
    if (!IgluSearch.Control.options.showDuration)
        IgluSearch.get('iglu_cruise_duration').style.display = 'none';
    else
        IgluSearch.Control.createDuration();

    /*Hide the PropertyType if we need to...*/
    if (!IgluSearch.Control.options.showDepStation)
        IgluSearch.get('iglu_cruise_depstation').style.display = 'none';
    else
        IgluSearch.Control.createDepStations();
}
