var Deal = new function() {

    var me = this;

    //setup
    this.Setup = function() {

        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;
            });

        }

        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() {

        var oCurrent = Deal.GetMonthYear();
        for (var i = 0; i < 4; i++) {
            oCurrent.Month--;
            if (oCurrent.Month < 1) {
                oCurrent.Month = 12;
                oCurrent.Year--;
            }
        }
        me.SetMonthYear(oCurrent);
        me.Go();
    }


    //forward
    this.Forward = function() {

        var oCurrent = Deal.GetMonthYear();
        for (var i = 0; i < 4; i++) {
            oCurrent.Month++;
            if (oCurrent.Month > 12) {
                oCurrent.Month = 1;
                oCurrent.Year++;
            }
        }
        me.SetMonthYear(oCurrent);
        me.Go();
    }


    //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.GetText('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.GetText('ddlDeal_Type');
        var iDuration = dd.GetValue('ddlDeal_Duration');
        var iStarRating = dd.GetValue('ddlDeal_StarRating');
        var iAirportID = dd.GetValue('ddlDeal_AirportID');
        var iMealBasisID = me.GetSelectedMealBasisID();


        oDeal_BuildGrid.Go(sLevel, iLevelID, dStartDate, sDealType, iDuration,
					iStarRating, iMealBasisID, iAirportID);
    }



    //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 = dd.GetValue('ddlDeal_AirportID');


        //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';

            }
        }


        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('http://www.holidayroomsdirect.com/_engine1/providers/lcb_support.asp', '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('http://www.holidayroomsdirect.com/_engine1/providers/lcb_support.asp', '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) {
        window.location = 'http://www.lowcostholidays.com/PropertyResults.aspx?ref=HRD&ss=true&ppid=' + oReturn.PropertyID + (oReturn.ExactMatch!='true' ? '&df=true' : '');
    } else {
        f.Hide('lblInfo');
        f.Show('lblWarning');
        f.Show('btnClose');
    }

}

function Deal_SearchResult(PropertyID, BestTotal, ExactMatch, Cheaper) {
    this.PropertyID = PropertyID;
    this.BestTotal = BestTotal;
    this.ExactMatch = ExactMatch;
    this.Cheaper = Cheaper;
}
