/*
Universal Cookie Cutter
Version 2.1b
Created to streamline cross-brand A/B testing
New version modularizes variables

instantiate new object:

MyCookieCutter = new universalCookieCutter([CookieVarName],      // name of your cookie variable
[CookieUsedName],     // name of cookie used (usually 'mktUniversalPersist')
[CookieValueBase],    // name of base cookie value so AB tests can be distinguished (ONOctFSEG)
[ForcedModeBaseName], // distinct base string for forced mode, (oldnavyabmode), etc
[SegmentBreakdown]    // segment breakdowns separated by "-" (80-10-10) highest to lowest values
[eVar you are using]  // right now it is either s_eVar24 or s.eVar36 - enter either "24" or "36"
[Piperlime-only segment] // Piperlime often doesn't segment, scan for this variable and overwrite it. 
);
A sample instantiation:

ONBRABCookieCutter = new universalCookieCutter("ONBRAB","mktUniversalPersist","ONBRDecXSEG","onbrabmode","70-10-10-5-5","24","0625FSSegE");
										   
*/



var universalCookieCutter_CS = Class.create();





universalCookieCutter_CS.prototype = {



    initialize: function(CookieVarName, CookieUsedName, CookieValueBase, ForcedModeBaseName, SegmentBreakdown, theEvar, plExcept) {



        //Keeping it neat by having two discreet variables that check for and gather cookie information

        this.EvarToUse = theEvar != "" ? theEvar : false;

        this.CookieVariableName = CookieVarName;

        this.CookieUsed = CookieUsedName;

        this.CookieValueBase = CookieValueBase;

        this.SegBreakdown = SegmentBreakdown.split("-");

        this.DoesTheCookieExist = gidLib.getCookieVar(this.CookieUsed, this.CookieVariableName);

        this.RetrievedCookieValue = gidLib.getCookieVar(this.CookieUsed, this.CookieVariableName);

        this.QuickSegment = '';

        this.piperlimeException = plExcept;

        this.userNeedsSync = false;

        this.ForcedModeBase = ForcedModeBaseName;

        this.ModeArrayIndex = this.SegBreakdown.length; //segment breakdown determines how many segments too!

        //alert("ModeArrayIndex: " + this.ModeArrayIndex);

        this.modeArray = new Array();

        this.modeArray[0] = "a";

        this.modeArray[1] = "b";

        this.modeArray[2] = "c";

        this.modeArray[3] = "d";

        this.modeArray[4] = "e";

        this.modeArray[5] = "f";

        this.modeArray[6] = "g";

        //alert(this.piperlimeException);





        if (location.href.indexOf(this.ForcedModeBase) >= 1) {

            this.forcedMode();

        } else {

            this.cookieScanner();

        }

    },





    forcedMode: function() {



        for (i = 0; i < this.ModeArrayIndex; i++) {



            if (location.href.indexOf(this.ForcedModeBase + "=" + this.modeArray[i]) >= 1) {

                //alert('you are in segment ' + this.modeArray[i].toUpperCase() + ' / ' + this.SegBreakdown[i] + '% of the population');

                this.UserSegment = this.CookieValueBase + this.modeArray[i].toUpperCase();

                this.QuickSegment = this.modeArray[i].toUpperCase();

            }



        }



        gidLib.setCookieVar(this.CookieUsed, this.CookieVariableName, this.UserSegment);



        if (this.EvarToUse) {

            this.setABTestVariable(this.UserSegment);

        }



        userNeedsSync = true;

        //alert("Forced: Cookie set.");



    },



    cookieScanner: function() {

        //If the cookie isn't available, bake one!

        if (this.DoesTheCookieExist) {

            if (this.EvarToUse) {

                this.setABTestVariable(this.RetrievedCookieValue);

            }

            for (i = 0; i < this.ModeArrayIndex; i++) {

                if (this.RetrievedCookieValue == (this.CookieValueBase + this.modeArray[i].toUpperCase())) {

                    this.QuickSegment = this.modeArray[i].toUpperCase();

                    break;

                }

            }



        } else {

            this.tagManager();

        }



    },



    tagManager: function() {



        //Breaking out the segmentation into specified percentages



        this.Segmentation = Math.floor((Math.random() * 100) + 1); // chooses 1 to 100

        //alert("Seg: " + this.Segmentation);	

        this.FloorThreshold = 0;

        this.TopThreshold = 0;



        for (i = 0; i < this.ModeArrayIndex; i++) {



            this.TopThreshold = this.FloorThreshold + parseInt(this.SegBreakdown[i]);



            if ((this.Segmentation > this.FloorThreshold) && (this.Segmentation <= this.TopThreshold)) {

                // put me in this segment

                this.UserSegment = this.CookieValueBase + this.modeArray[i].toUpperCase();

                this.QuickSegment = this.modeArray[i].toUpperCase();

                break;

            } else {

                // add top to floor and try next segment

                this.FloorThreshold = this.TopThreshold;

            }



        }



        gidLib.setCookieVar(this.CookieUsed, this.CookieVariableName, this.UserSegment);



        if (this.EvarToUse) {

            this.setABTestVariable(this.UserSegment);

        }



        //userNeedsSync = true;

        //alert("Tag Manager: Cookie set.");



    },



    setABTestVariable: function(setValue) {
        thisEvar = 'eVar' + this.EvarToUse;
        ABTestVariables[thisEvar] = setValue;
    },

    getABTestVariable: function() {
        thisEvar = 'eVar' + this.EvarToUse;
        return ABTestVariables[thisEvar];
    }

};