/*************************************************************************
* Preferences will be stored and accessed through a structure.  This
* will thus try to keep an OO interface to the Preferences.
*************************************************************************/
function Preferences()
{

    var self = this;

    this.version = 36; /*Will store the version of the prefeernce object
						IMPORTANT: Must update version if new properties are
						added*/
    this.minSingleClickStake = 30;

    /* The default preferences values are set BOTH here and in Preferences.cs in GBE.Web.ServiceLayer.
    However, The way the code currently works, the values in Preferences.cs are the ones that are used, so
    the values here are somewhat irrelevant. Nevertheless, we should probably keep them the same
    here as in Preferences.cs for the moment (though this is not possible for defaults read from Web.config).
	
    If for any reason the real default values are required, take a look on DefaultPreferenceString and it's using*/
    this.PandL = true;
    this.LeftNavWidth = 197;
    this.LeftNavMinimized = false;
    this.DisplayBalance = false;
    this.MarketByVolume = 0;   /* Ignore this value - default value is actually read from Web.config (in Preferences.cs) - see comment above & Mantis 15172*/
    this.BetReviewPage = true;
    this.ShowFractionalOdds = false;
    this.DisplayAverageOdds = false;
    this.ViewFunds = true; 	/*DO NOT USE - Use DisplayBalance instead*/
    this.AsyncPlaceBet = false;
    this.DisplayOverRound = true;
    this.DisplayEachwayHelp = true;
    this.NotificationHeight = 100;
    this.Language = "en";
    this.DisplayNotifications = true;
    this.BackRepriceOption = 2;
    this.LayRepriceOption = 1;
    /*single click betting user values stored.*/
    this.OneClickStake1 = this.minSingleClickStake;
    this.OneClickStake2 = this.minSingleClickStake * 2;
    this.OneClickStake3 = this.minSingleClickStake * 3;
    this.OneClickStake4 = this.minSingleClickStake * 4;
    this.OneClickStake5 = this.minSingleClickStake * 5;
    /*display expriy time -- used along with config setting*/
    this.DisplayExpiryTimePrefs = true;
    /*where the user gets forwarded to on bet confirmation. (betslip or current bets)*/
    this.ConfirmationRedirect = true;
    this.DisplayOneClickAlertBox = true;
    /*odds system preference -> 0 = decimal, 1 = fractional, 2 = american. (default = decimal)*/
    this.OddsSystem = top.getTopWindow().defaultOddsSystem;
    this.IncludeSettledSelections = false;
    this.MaximumLiability = 0;
    this.ExpressBetOn = false;
    this.TimeZoneOffSet = 1;
    this.TimeZoneDateRange1 = 0;
    this.TimeZoneDateRange2 = 0;
    this.DisplayClothColors = true;
    this.DisplayNewLeftNav = true;
    this.DisplayProformaPnL = true;
    this.NumberBackPrices = 3;
    this.NumberLayPrices = 3;
    this.IsMultiplesSite = false; /*derived from LastViewedTab*/
    this.IsIBrokerSite = false; /*derived from LastViewedTab*/
    this.IsPokerSite = false; /*derived from LastViewedTab*/
    this.DisplaySingleClickBetting = true;
    this.PlaceBetFromWinMarket = true;
    this.MultiplesStakeSettings = "2,5,10,20,50~75,100,150,200,250";
    this.DisplayBackToWin = false;
    this.DisplayLayToLose = true;
    this.IsExpressSite = false; /*derived from LastViewedTab*/
    this.ExpressStakeSettings = "2,5,10,20,50~75,100,150,200,250";
    this.DisplayLayOdds = true;
    this.LastViewedTab = top.getTopWindow().defaultMainTab;
    this.LoadFromDatabase = false;
    this.LastTimeUsed = 0;
    this.DisplayMarketNavOnEventCard = true;
    this.MultiplesInExchangeView = false;
    this.CancelOnInRunning = true;
    this.CookieState = false;
    this.NetOfCommissions = true;
    this.DisplayAverageOddsPopUp = false;
    this.SeenSplashScreenVersion = 0;
    this.MinimizeLiveMarketsPanel = false;
    this.MiniGamesCarouselOpened = true;
    this.DisplayMiniGames = true;

    this.DefaultPreferenceString = '';


    /*************************************************************************
    * For storing our user preferences, we need to maintain an array, and
    serialize/deserialize the array to a cookie.

    These are the actions that we must support:
    - SavePreferences     - will save the preferences structure to a cookie
    - RestorePreferences  - will restore the preferences from a cookie
    - ResetPreferences    - will reset the preferences structure to default
    values.  These must then be saved.
    *************************************************************************/

    /*************************************************************************
    *PUBLIC METHODS
    *************************************************************************/
    this.SavePreferences = _savePrefs;
    this.RestorePreferences = _restorePrefs;
    this.SetOddsSystemValues = _setOddsSystemValues;
    this.CreateCookie = _createCookie;
    this.SetLastViewedTab = _setLastViewedTab;
    this.GetPrefsString = _getPrefsString;
    this.EraseCookie = _eraseCookie;
    this.ReadCookie = _readCookie;
    this.TimeReceivedFromServer = _timeReceivedFromServer;
    /*this.SetExpressInterface = _setExpressInterface;	*/

    /*************************************************************************
    *PRIVATE METHODS
    *************************************************************************/

    function _setLastViewedTab(value, isValueNotSaved)
    {
        this.LastViewedTab = value;

        this.IsExpressSite = false;
        this.IsMultiplesSite = false;
        this.IsPokerSite = false;
        this.IsIBrokerSite = false;

        if (value == 'exchange')
        {

        }
        else if (value == 'multiples')
        {
            this.IsMultiplesSite = true;
        }
        else if (value == 'ibroker_tab')
        {
            this.IsIBrokerSite = true;
        }
        else if (value == 'express')
        {
            this.IsExpressSite = true;
        }
        else if (value == 'poker')
        {
            this.IsPokerSite = true;
        }


        if (!(isValueNotSaved == true))
        {
            this.SavePreferences(false);
        }

    }

    function _getPrefsString()
    {
        var prefs = "";

        prefs += self.version;
        prefs += "|";
        prefs += (self.PandL == false) ? "0" : "1";
        prefs += "|";
        prefs += self.LeftNavWidth;
        prefs += "|";
        prefs += (self.LeftNavMinimized == false ? "0" : "1");
        prefs += "|";
        prefs += (self.DisplayBalance == false ? "0" : "1");
        prefs += "|";
        prefs += self.MarketByVolume;
        prefs += "|";
        prefs += (self.BetReviewPage == false ? "0" : "1");
        prefs += "|";
        prefs += (self.ShowFractionalOdds == false ? "0" : "1");
        prefs += "|";
        prefs += (self.DisplayAverageOdds == false ? "0" : "1");
        prefs += "|";
        prefs += (self.ViewFunds == false ? "0" : "1");
        prefs += "|";
        prefs += (self.AsyncPlaceBet == false ? "0" : "1");
        prefs += "|";
        prefs += (self.DisplayOverRound == false ? "0" : "1");
        prefs += "|";
        prefs += self.NotificationHeight;
        prefs += "|";
        prefs += self.Language;
        prefs += "|";
        prefs += self.DisplayNotifications == false ? "0" : "1";
        prefs += "|";
        prefs += self.BackRepriceOption;
        prefs += "|";
        prefs += self.LayRepriceOption;
        prefs += "|";
        prefs += self.OneClickStake1;
        prefs += "|";
        prefs += self.OneClickStake2;
        prefs += "|";
        prefs += self.OneClickStake3;
        prefs += "|";
        prefs += self.OneClickStake4;
        prefs += "|";
        prefs += self.OneClickStake5;
        prefs += "|";
        prefs += self.DisplayExpiryTimePrefs == false ? "0" : "1";
        prefs += "|";
        prefs += self.ConfirmationRedirect == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayOneClickAlertBox == false ? "0" : "1";
        prefs += "|";
        prefs += checkOddsSystem(self.OddsSystem);
        prefs += "|";
        prefs += self.IncludeSettledSelections == false ? "0" : "1";
        prefs += "|";
        prefs += self.MaximumLiability;
        prefs += "|";
        prefs += self.ExpressBetOn == false ? "0" : "1";
        prefs += "|";
        prefs += top.getTopWindow().timeZoneOffset;
        prefs += "|";
        prefs += top.getTopWindow().timeZoneDateRange1;
        prefs += "|";
        prefs += top.getTopWindow().timeZoneDateRange2;
        prefs += "|";
        prefs += self.DisplayClothColors == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayNewLeftNav == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayProformaPnL == false ? "0" : "1";
        prefs += "|";
        prefs += self.NumberBackPrices;
        prefs += "|";
        prefs += self.NumberLayPrices;
        prefs += "|";
        prefs += self.IsMultiplesSite == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplaySingleClickBetting == false ? "0" : "1";
        prefs += "|";
        prefs += self.PlaceBetFromWinMarket == false ? "0" : "1";
        prefs += "|";
        prefs += self.MultiplesStakeSettings;
        prefs += "|";
        prefs += self.DisplayBackToWin == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayLayToLose == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayEachwayHelp == false ? "0" : "1";
        prefs += "|";
        prefs += self.IsExpressSite == false ? "0" : "1";
        prefs += "|";
        prefs += self.ExpressStakeSettings;
        prefs += "|";
        prefs += self.DisplayLayOdds == false ? "0" : "1";
        prefs += "|";
        prefs += self.LastViewedTab;
        prefs += "|";
        prefs += self.LoadFromDatabase == false ? "0" : "1";
        prefs += "|";
        prefs += self.LastTimeUsed;
        prefs += "|";
        prefs += self.DisplayMarketNavOnEventCard == false ? "0" : "1";
        prefs += "|";
        prefs += self.MultiplesInExchangeView == false ? "0" : "1";
        prefs += "|";
        prefs += self.CancelOnInRunning == false ? "0" : "1";
        prefs += "|";
        prefs += self.CookieState == false ? "0" : "1";
        prefs += "|";
        prefs += self.NetOfCommissions == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayAverageOddsPopUp == false ? "0" : "1";
        prefs += "|";
        prefs += self.SeenSplashScreenVersion;
        prefs += "|";
        prefs += self.MinimizeLiveMarketsPanel == false ? "0" : "1";
        prefs += "|";
        prefs += self.MiniGamesCarouselOpened == false ? "0" : "1";
        prefs += "|";
        prefs += self.DisplayMiniGames == false ? "0" : "1";

        return prefs;
    }
    /*Need to create a string from all the preferences and save the string*/
    function _savePrefs(isSavedToBroker, functionCalledAfterSave)
    {
        // Get time from server asynchronously, to update lastTimeUsed field when response received
        LoadData("top.prefs.TimeReceivedFromServer", GBE_Web_UI_Util_Root + "Common/CurrentTime.aspx?currentPreferences=" + top.getTopWindow().GetPreferencesURLPostfix(), null);

        // Update preferences cookie
        var prefs = _getPrefsString();
        _createCookie('Prefs-' + SITE_ID + "-" + top.getTopWindow().GetPreferencesURLPostfix(), prefs, 999);

        // Save prefs to broker database
        if (top.getTopWindow().SavePrefsToBroker)
            top.getTopWindow().SavePrefsToBroker(prefs, isSavedToBroker, functionCalledAfterSave);
    }

    function _timeReceivedFromServer(oXml, oXsl)
    {
        // Update lastTimeUsed with time returned by server
        if (!isNaN(oXml) && oXml != "")
            self.LastTimeUsed = oXml;

        // Update preferences cookie
        var prefs = _getPrefsString();
        _createCookie('Prefs-' + SITE_ID + "-" + top.getTopWindow().GetPreferencesURLPostfix(), prefs, 999);
    }

    /**************************************
    Method:	Ensure that the cookie oddsSystem is supported in WebConfig otherwise return default;
    Author:	KL
    /**************************************/
    function checkOddsSystem(oddsSystem)
    {
        var oddsSystemToReturn = oddsSystem;
        if (top.getTopWindow()
            && top.getTopWindow().defaultOddsSystemValues
            && top.getTopWindow().defaultOddsSystemValues.indexOf(oddsSystem) < 0)
            oddsSystemToReturn = top.getTopWindow().defaultOddsSystem;
        return oddsSystemToReturn;
    }
    function _setOddsSystemValues()
    {
        if (this.version >= 14)
        {
            switch (this.OddsSystem)
            {
                case "Decimal":
                    top.getTopWindow().global.oddsSystem.oddsFormat = top.getTopWindow().oddsSystemTypes.DECIMAL;
                    top.getTopWindow().global.oddsSystem.isDecimalOdds = true;
                    top.getTopWindow().global.oddsSystem.isFractionalOdds = false;
                    top.getTopWindow().global.oddsSystem.isAmericanOdds = false;
                    break;
                case "Fractional":
                    top.getTopWindow().global.oddsSystem.oddsFormat = top.getTopWindow().oddsSystemTypes.FRACTIONAL;
                    top.getTopWindow().global.oddsSystem.isDecimalOdds = false;
                    top.getTopWindow().global.oddsSystem.isFractionalOdds = true;
                    top.getTopWindow().global.oddsSystem.isAmericanOdds = false;
                    break;
                case "American":
                    top.getTopWindow().global.oddsSystem.oddsFormat = top.getTopWindow().oddsSystemTypes.AMERICAN;
                    top.getTopWindow().global.oddsSystem.isDecimalOdds = false;
                    top.getTopWindow().global.oddsSystem.isFractionalOdds = false;
                    top.getTopWindow().global.oddsSystem.isAmericanOdds = true;
                    break;
            }
        }
        else
        {
            top.getTopWindow().global.oddsSystem.oddsFormat = top.getTopWindow().oddsSystemTypes.DECIMAL;
            top.getTopWindow().global.oddsSystem.isDecimalOdds = true;
            top.getTopWindow().global.oddsSystem.isFractionalOdds = false;
            top.getTopWindow().global.oddsSystem.isAmericanOdds = false;
        }
    }
    /*Need to grab values from the cookie, and restore values to this object*/
    function _restorePrefs(stringPreferencesTakenFrom)
    {
        var prefs = '';

        if (typeof (stringPreferencesTakenFrom) != "undefined" && stringPreferencesTakenFrom != null && stringPreferencesTakenFrom != '')
        {
            prefs = stringPreferencesTakenFrom;
        }
        else
        {
            var cookieName = 'Prefs-' + SITE_ID + "-" + top.getTopWindow().GetPreferencesURLPostfix();
            /*Get the prefs from the stored cookie*/
            prefs = _readCookie(cookieName);
        }

        if (prefs != null)
        {

            /*Now, split, and we'll parse into our properties*/
            var prefsArray = new Array();
            prefsArray = prefs.split("|");

            var version = prefsArray[0] * 1;  /*The version number of the saved prefs will always be the first entry; (mulitply by 1 converts the string to a number)*/

            /*Process version 1 cookie properties*/
            if (version >= 1)
            {
                this.PandL = prefsArray[1] == "1" ? true : false;
            }

            /*Process version 2 cookie properties*/
            if (version >= 2)
            {
                this.LeftNavWidth = prefsArray[2] * 1;  /*This will convert string to number.*/
            }
            if (version >= 3)
            {
                this.LeftNavMinimized = prefsArray[3] == "1" ? true : false;
                this.DisplayBalance = prefsArray[4] == "1" ? true : false;
                this.MarketByVolume = prefsArray[5] * 1;
                this.BetReviewPage = prefsArray[6] == "1" ? true : false;
                this.ShowFractionalOdds = prefsArray[7] == "1" ? true : false;
                this.DisplayAverageOdds = prefsArray[8] == "1" ? true : false;
                this.ViewFunds = prefsArray[9] == "1" ? true : false;
            }
            if (version >= 4)
            {
                this.AsyncPlaceBet = prefsArray[10] == "1" ? true : false;
            }
            if (version >= 5)
            {
                this.DisplayOverRound = prefsArray[11] == "1" ? true : false;
            }
            if (version >= 6)
            {
                this.NotificationHeight = prefsArray[12] * 1;
            }
            if (version >= 7)
            {
                this.Language = prefsArray[13];
            }
            if (version >= 8)
            {
                this.DisplayNotifications = prefsArray[14] == "1" ? true : false;
            }
            if (version >= 9)
            {
                this.BackRepriceOption = prefsArray[15] * 1;
                this.LayRepriceOption = prefsArray[16] * 1;
            }
            if (version >= 10)
            {
                this.OneClickStake1 = prefsArray[17] * 1;
                this.OneClickStake2 = prefsArray[18] * 1;
                this.OneClickStake3 = prefsArray[19] * 1;
                this.OneClickStake4 = prefsArray[20] * 1;
                this.OneClickStake5 = prefsArray[21] * 1;
            }
            if (version >= 11)
            {
                this.DisplayExpiryTimePrefs = prefsArray[22] == "1" ? true : false;
            }
            if (version >= 12)
            {
                this.ConfirmationRedirect = prefsArray[23] == "1" ? true : false;
            }
            if (version >= 13)
            {
                this.DisplayOneClickAlertBox = prefsArray[24] == "1" ? true : false;
            }
            if (version >= 14)
            {
                this.OddsSystem = checkOddsSystem(prefsArray[25]);
            }
            if (version >= 15)
            {
                this.IncludeSettledSelections = prefsArray[26] == "1" ? true : false;
            }
            if (version >= 16)
            {
                this.MaximumLiability = prefsArray[27];
            }
            if (version >= 17)
            {
                this.ExpressBetOn = prefsArray[28] == "1" ? true : false;
                top.getTopWindow().global.expressBetOn = this.ExpressBetOn;
            }
            if (version >= 18)
            {
                this.TimeZoneOffSet = prefsArray[29];
            }
            if (version >= 19)
            {
                this.TimeZoneDateRange1 = prefsArray[30];
                this.TimeZoneDateRange2 = prefsArray[31];
            }
            if (version >= 20)
            {
                this.DisplayClothColors = prefsArray[32] == "1" ? true : false;
            }
            if (version >= 21)
            {
                this.DisplayNewLeftNav = prefsArray[33] == "1" ? true : false;
            }
            if (version >= 22)
            {
                this.DisplayProformaPnL = prefsArray[34] == "1" ? true : false;
                this.NumberBackPrices = prefsArray[35];
                this.NumberLayPrices = prefsArray[36];
            }
            if (version >= 23)
            {
                /*this.IsMultiplesSite = prefsArray[37] == "1" ? true : false;	this is derived property from LastViewedTab*/
                this.DisplaySingleClickBetting = prefsArray[38] == "1" ? true : false;
                this.PlaceBetFromWinMarket = prefsArray[39] == "1" ? true : false;
                this.MultiplesStakeSettings = prefsArray[40];
            }
            if (version >= 24)
            {
                this.DisplayBackToWin = prefsArray[41] == "1" ? true : false;
                this.DisplayLayToLose = prefsArray[42] == "1" ? true : false;
                this.DisplayEachwayHelp = prefsArray[43] == "1" ? true : false;
            }
            if (version >= 25)
            {
                /*this.IsExpressSite = prefsArray[44] == "1" ? true : false;	this is derived property from LastViewedTab*/
                this.ExpressStakeSettings = prefsArray[45];
                this.DisplayLayOdds = prefsArray[46] == "1" ? true : false;
            }
            if (version >= 26)
            {
                this.SetLastViewedTab(prefsArray[47], true);
                this.LoadFromDatabase = prefsArray[48] == "1" ? true : false;
            }

            if (version >= 27)
            {
                this.LastTimeUsed = prefsArray[49];
            }

            if (version >= 28)
            {
                this.DisplayMarketNavOnEventCard = prefsArray[50] == "1" ? true : false;
            }

            if (version >= 29)
            {
                this.MultiplesInExchangeView = prefsArray[51] == "1" ? true : false;
            }

            if (version >= 30)
            {
                this.CancelOnInRunning = prefsArray[52] == "1" ? true : false;
            }

            if (version >= 31)
            {
                this.CookieState = prefsArray[53] == "1" ? true : false;
            }

            if (version >= 32)
            {
                this.NetOfCommissions = prefsArray[54] == "1" ? true : false;
            }

            if (version >= 33)
            {
                this.DisplayAverageOddsPopUp = prefsArray[55] == "1" ? true : false;
            }
            if (version >= 34)
            {
                this.SeenSplashScreenVersion = prefsArray[56];
            }
            if (version >= 35)
            {
                this.MinimizeLiveMarketsPanel = prefsArray[57] == "1" ? true : false;
            }
            if (version >= 36)
            {
                this.MiniGamesCarouselOpened = prefsArray[58] == "1" ? true : false;
                this.DisplayMiniGames = prefsArray[59] == "1" ? true : false;
            }
        }
    }


    /*******************************************************************
    * C.R.U.D for managing cookies.  These methods are private to the 
    * Preferences object.
    * code used from http://www.quirksmode.org/index.html
    *******************************************************************/
    function _createCookie(name, value, days)
    {
        var expires = '';
        if (days == -1000)
        {
            expires = "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
        else if (days)
        {
            var date = new Date();
            if (days > 0)
            {
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            }
            else
            {
                /*minutes*/
                var minutes = days * (-1);
                date.setTime(date.getTime() + (minutes * 60 * 1000));
            }
            expires = "; expires=" + date.toGMTString();
        }

        document.cookie = name + "=" + value + expires + "; domain=." + TOPLEVELDOMAIN + "; path=/";
    }

    function _readCookie(name)
    {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++)
        {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function _eraseCookie(name)
    {
        _createCookie(name, "", -1000);
    }
}

