﻿var Deal = new function() {

    var me = this;

    //setup
    this.Setup = function() {

        if (dd.ListCount('ddlDeal_Type') == 1) {
            f.Hide('ddlDeal_Type');
        }

        //this will check if the prices need refreshing on page load
        f.AttachEvent(window, 'load', function() {
            if (f.GetValue('hidRefreshDealPricesOnPageLoad') == 'true') Deal.Go();
        });


        var aDropdowns = f.GetObjectsByIDPrefix('ddlDeal_', 'select');
        for (var i = 0; i < aDropdowns.length; i++) {
            f.AttachEvent(aDropdowns[i], 'change', function() {
                Deal.Go();
            });
        }

        var aMealBasis = f.GetObjectsByIDPrefix('aDealMB_', 'a');
        for (var i = 0; i < aMealBasis.length; i++) {

            f.AttachEvent(aMealBasis[i], 'click', function(oEvent) {
                Deal.MealBasisSelect(f.GetObjectFromEvent(oEvent));
                oEvent.returnValue = false;
                oEvent.cancelBubble = true;
                oEvent.cancel = true;
                if (oEvent.preventDefault) oEvent.preventDefault();
            });

        }

        me.SetPriceLinkEvents();

    }



    //set price link events
    this.SetPriceLinkEvents = function() {
        var aLinks = f.GetObjectsByIDPrefix('p_', 'a');
        for (var i = 0; i < aLinks.length; i++) {
            f.AttachEvent(aLinks[i], 'click', function(oEvent) {
                Deal.Search(f.GetObjectFromEvent(oEvent));
            });

        }
    }


    //back
    this.Back = function(iCurrentPanel) {
        f.Hide('divDealGridPricesPanel_' + iCurrentPanel);
        f.Show('divDealGridPricesPanel_' + (iCurrentPanel - 1));
    }
    //forward
    this.Forward = function(iCurrentPanel) {
        f.Hide('divDealGridPricesPanel_' + iCurrentPanel);
        f.Show('divDealGridPricesPanel_' + (iCurrentPanel + 1));
    }


    //mealbasis select
    this.MealBasisSelect = function(oLink) {
        var iMealBasisID = oLink.id.split('_')[1];
        var iCurrentlySelectedMealBasisID = me.GetSelectedMealBasisID();
        var iTargetMealBasisID = (iMealBasisID != iCurrentlySelectedMealBasisID) ? iMealBasisID : 0;

        var aMealBasis = f.GetObjectsByIDPrefix('aDealMB_', 'a');
        for (var i = 0; i < aMealBasis.length; i++) {
            f.SetClassIf(aMealBasis[i], 'selected', aMealBasis[i].id == 'aDealMB_' + iTargetMealBasisID);
        }
        me.Go();
    }


    //go
    this.Go = function() {

        f.ShowIf('ddlDeal_AirportID', dd.GetValue('ddlDeal_Type') == 'Flight + Hotel');

        var sLevel = f.GetValue('hidDeal_Level');
        var iLevelID = f.GetValue('hidDeal_LevelID');
        var dStartDate = me.GetMonthYear().Year + '-' + (me.GetMonthYear().Month.length == 1 ? '0' : '') + me.GetMonthYear().Month + '-01';
        var sDealType = dd.GetValue('ddlDeal_Type');
        var iDuration = dd.GetValue('ddlDeal_Duration');
        var iStarRating = dd.GetValue('ddlDeal_StarRating');
        var iMealBasisID = me.GetSelectedMealBasisID();
        var iAirportID = 0;
        if (f.GetObject('ddlDeal_AirportID') != null) {
            iAirportID = dd.GetValue('ddlDeal_AirportID');
        }

        if (document.getElementById('pDealFooter')) {
            f.SetHTML('pDealFooter', Constants.DealGridRetrievingPrices);
            f.SetClass('pDealFooter', 'working');
        }

        oDeal_BuildGrid.Go(sLevel, iLevelID, dStartDate, sDealType, iDuration,
					iStarRating, iMealBasisID, iAirportID);

        //set the hidden control so that the prices will get refreshed if browser back / forward are used	
        f.SetValue('hidRefreshDealPricesOnPageLoad', 'true')
    }



    //Search
    this.Search = function(oLink) {

        var aBits = oLink.id.split('_');
        var sDealType = dd.GetText('ddlDeal_Type');
        var sShortLevel = aBits[3];
        var sLevel = sShortLevel == 'G' ? 'GeographyGrouping' : 'GeographyLevel' + sShortLevel;
        var iLevelID = aBits[4];
        var iMonth = aBits[2];
        var iYear = aBits[1];
        var iDuration = dd.GetValue('ddlDeal_Duration');
        var iStarRating = dd.GetValue('ddlDeal_StarRating');
        var iMealBasisID = me.GetSelectedMealBasisID();
        var iAirportID = 0;
        if (f.GetObject('ddlDeal_AirportID') != null) {
            iAirportID = dd.GetValue('ddlDeal_AirportID');
        }

        PopupDisplayObjects(new Array(['label', 'lblInfo', 1], ['label', 'lblWarning', 0], ['input', 'btnClose', 0]));
        ShowWaitMessage()

        //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';

            }
        }


        oDeal_Search.Go(sDealType, sLevel, iLevelID, iMonth, iYear, iDuration, iStarRating, iMealBasisID, iAirportID)

    }


    //get and set monthyear
    this.GetMonthYear = function() {
        return { Month: f.GetValue('hidDeal_StartMonth'), Year: f.GetValue('hidDeal_StartYear') }
    }
    this.SetMonthYear = function(oMonthYear) {
        if (oMonthYear.Month < 10) { oMonthYear.Month = '0' + oMonthYear.Month; }
        f.SetValue('hidDeal_StartMonth', oMonthYear.Month);
        f.SetValue('hidDeal_StartYear', oMonthYear.Year);

    }

    this.GetSelectedMealBasisID = function() {

        var iMealBasisID = 0;
        var aMealBasis = f.GetElementsByClassName('a', 'selected', 'divDeal_MealBasis');
        if (aMealBasis.length > 0) {
            iMealBasisID = aMealBasis[0].id.split('_')[1];
        }
        return iMealBasisID;

    }


}


var oDeal_BuildGrid = new WebService();
oDeal_BuildGrid.Go = function(Level, LevelID, StartDate, DealType, Duration, StarRating, MealBasisID, AirportID) {
    aParams = new Array(['Level', Level], ['LevelID', LevelID], ['StartDate', StartDate], ['DealType', DealType],
    		['Duration', Duration], ['StarRating', StarRating], ['MealBasisID', MealBasisID], ['AirportID', AirportID]);
    this.RunWebService('/webservices/support.asmx', 'http://intuitivesystems', 'Deal_BuildGrid', aParams, this, false);
}

oDeal_BuildGrid.Done = function(oXML) {
    var oReturn = this.GetTagValue(oXML, 'Deal_BuildGridResult');
    f.SetHTML('divGridHold', oReturn);
    Deal.SetPriceLinkEvents();
}


var oDeal_Search = new WebService();
oDeal_Search.Go = function(DealType, ParentType, ParentID, Month, Year, Duration, StarRating, MealBasisID, AirportID) {
aParams = new Array(['DealType', DealType], ['ParentType', ParentType], ['ParentID', ParentID], ['Month', Month], ['Year', Year], ['Duration', Duration], ['StarRating', StarRating], ['MealBasisID', MealBasisID], ['AirportID', AirportID]);
    this.RunWebService('/webservices/support.asmx', 'http://intuitivesystems', 'Deal_Search', aParams, this, false);
}

oDeal_Search.Done = function(oXML) {

    var oReturn = new Deal_SearchResult(this.GetTagValue(oXML, 'PropertyID'), this.GetTagValue(oXML, 'BestTotal'), this.GetTagValue(oXML, 'ExactMatch'), this.GetTagValue(oXML, 'Cheaper'));

    if (oReturn.PropertyID > -1) {
        c.Delete('changepartysizerestore');
        window.location = '/PropertyResults.aspx?g=true&ss=false&ppid=' + oReturn.PropertyID + (oReturn.ExactMatch != 'true' && oReturn.Cheaper != 'true' ? '&df=true' : '');
    } else {
        PopupDisplayObjects(new Array(['label', 'lblInfo', 0], ['label', 'lblWarning', 1], ['input', 'btnClose', 1]));
    }

}

function Deal_SearchResult(PropertyID, BestTotal, ExactMatch, Cheaper) {
    this.PropertyID = PropertyID;
    this.BestTotal = BestTotal;
    this.ExactMatch = ExactMatch;
    this.Cheaper = Cheaper;
}
