function Enlightenment() {
    this.addReferralToStack = function( referralUrl, landingUrl ) {
        // trim the URLs
        referralUrl = referralUrl.substring(0,Enlightenment.MAX_URL_LENGTH);
        landingUrl = landingUrl.substring(0,Enlightenment.MAX_URL_LENGTH);
        var cookieVal = this.getDataCookie();
        if (cookieVal && cookieVal.length > 0) {
            cookieVal += Enlightenment.RECORD_SEPARATOR;
        } else {
            cookieVal = "";
        }
        var timeStamp = Math.round(new Date().getTime()/1000);
        cookieVal += timeStamp + Enlightenment.FIELD_SEPARATOR + referralUrl + Enlightenment.FIELD_SEPARATOR + landingUrl;
        this.setDataCookie(cookieVal);
        this.setTimeoutCookie("Degsy's Midnight Runners");
    };


    this.getDataCookie = function() {
        return this.getCookie(Enlightenment.DATA_COOKIE_NAME);
    };

    this.setDataCookie = function(value) {
        while (value.length > Enlightenment.DATA_COOKIE_MAX_LEN) {
            value = value.replace(Enlightenment.RECORD_TRIMMER,"");

        }
        this.setCookie(Enlightenment.DATA_COOKIE_NAME, value, Enlightenment.DATA_COOKIE_EXPIRATION, "/", Enlightenment.BASKET_DOMAIN);
    };

    this.getTimeoutCookie = function() {
        return this.getCookie(Enlightenment.TIMEOUT_COOKIE_NAME);
    };

    this.setTimeoutCookie = function(value) {
        this.setCookie(Enlightenment.TIMEOUT_COOKIE_NAME, value, Enlightenment.TIMEOUT_COOKIE_EXPIRATION, "/", Enlightenment.BASKET_DOMAIN);
    };

    this.getCookie = function ( cookieName ) {
        // first we'll split this cookie up into name/value pairs
        // note: document.cookie only returns name=value, not the other components
        var allCookies = document.cookie.split( ';' );
        var tempCookie = '';
        var tempCookieName = '';
        var cookieValue = '';
        
        for ( i = 0; i < allCookies.length; i++ )
        {
            // now we'll split apart each name=value pair
            tempCookie = allCookies[i].split( '=' );
            
            // and trim left/right whitespace while we're at it
            tempCookieName = tempCookie[0].replace(/^\s+|\s+$/g, '');
        
            // if the extracted name matches passed cookieName
            if ( tempCookieName == cookieName )
            {
                // we need to handle case where cookie has no value but exists (no = sign, that is):
                if ( tempCookie.length > 1 )
                {
                    cookieValue = unescape( tempCookie[1].replace(/^\s+|\s+$/g, '') );
                }
                // note that in cases where cookie is initialized but no value, null is returned
                return cookieValue;
			}
        }
        return null;
    };               


    this.setCookie = function ( name, value, expires, path, domain, secure ) 
    {
        document.cookie = name + "=" +escape( value ) +
        ( ( expires ) ? ";expires=" + (new Date( (new Date()).getTime() + (expires * 1000 * 60) )).toGMTString() : "" ) + 
        ( ( path ) ? ";path=" + path : "" ) + 
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
    };

    this.logReferral = function() {
        if (!this.getTimeoutCookie()) {
            var params = this.urlParams();
            this.addReferralToStack(unescape(params.r), unescape(params.l));
        } else {

				params = this.urlParams();
                var cookieVal = this.getDataCookie();
                    
                if(unescape(params[Enlightenment.CONFIG_ID]) != Enlightenment.UNDEFINED) {
               		if(cookieVal.indexOf(Enlightenment.GLOBAL_TRIPS) == -1) {
                    	if(cookieVal.indexOf(Enlightenment.LMN) != -1) {
                            cookieVal = cookieVal.replace(Enlightenment.LMN, Enlightenment.GLOBAL_TRIPS);
                    	} else {
                            cookieVal += Enlightenment.RECORD_SEPARATOR;
                            var timeStamp = Math.round(new Date().getTime()/1000);
                            cookieVal += timeStamp + Enlightenment.GLOBAL_TRIPS;
                    	}
                    	this.setDataCookie(cookieVal);
                    }
                } else {
                    if(cookieVal.indexOf(Enlightenment.LMN) == -1) {
                        if(cookieVal.indexOf(Enlightenment.GLOBAL_TRIPS) != -1) {
                        	cookieVal = cookieVal.replace(Enlightenment.GLOBAL_TRIPS, Enlightenment.LMN);
                    	} else {
                       		cookieVal += Enlightenment.RECORD_SEPARATOR;
                       		timeStamp = Math.round(new Date().getTime()/1000);
                       		cookieVal += timeStamp + Enlightenment.LMN;
                    	}
                    	this.setDataCookie(cookieVal);
                	}
            	}
            
        }
    };

    this.urlParams = function() {
       	var params = document.location.href.split('?')[1].split('&');
       	for (i = 0; i < params.length; i++) {
           nvpair = params[i].split('=');
           params[nvpair[0]] = nvpair[1];
        }
        
        return params;
    };

}

Enlightenment.DATA_COOKIE_NAME = "enlData";
Enlightenment.DATA_COOKIE_EXPIRATION = 10 * 365 * 24 * 60;
Enlightenment.DATA_COOKIE_MAX_LEN = 1000;

Enlightenment.TIMEOUT_COOKIE_NAME = "enlTimeout";
Enlightenment.TIMEOUT_COOKIE_EXPIRATION = 180;

Enlightenment.FIELD_SEPARATOR = "\t";
Enlightenment.RECORD_SEPARATOR = "\n";
Enlightenment.MAX_URL_LENGTH = 400;
Enlightenment.RECORD_TRIMMER = new RegExp( "^[^" + Enlightenment.RECORD_SEPARATOR + "]*[" + Enlightenment.RECORD_SEPARATOR + "]?");
Enlightenment.GLOBAL_TRIPS = "GlobalTrips";
Enlightenment.LMN = "LMN";
Enlightenment.UNDEFINED = "undefined";
Enlightenment.CONFIG_ID = "configId";
Enlightenment.BASKET_LOGIN_PATH = "eBasket/login/fetchLogin.do";

Enlightenment.BASKET_DOMAIN = "secureshoppingbasket.com";

var enlightenmentObject = new Enlightenment();
enlightenmentObject.logReferral();
