
// Get the value of a querystring variable or
// returns the default value if none is found
function getQueryStringVariable(variableName, defaultValue)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    
    // convert the input variableName to lowercase only once
    variableName = variableName.toLowerCase();
    
    for (var i=0;i<vars.length;i++)
    {
        var pair = vars[i].split("=");
        if (pair[0].toLowerCase() == variableName)
        {
            return pair[1];
        }
    }
    //if no match is found, return default value
    return defaultValue;
}

// Get the value of a cookie variable or
// returns the default value if cookie is not found
function getSessionCookieValue(cookieName, defaultValue) 
{
    var cookieValue = readSessionCookie(cookieName);

    if (cookieValue == null)
        cookieValue = defaultValue;

    return cookieValue;
}

// REF: Nice javascript cookie example code at http://www.quirksmode.org/js/cookies.html   
             
// creates a session cookie value.  A session cookie expires when the browser is closed.
function createSessionCookie(name,value) 
{
    // Are we on a domain? If so, then set the proper cookie domain. ( www.realage.com should be .realage.com)
    // If it is not a domain, then just use the host name. ( qaweb )
    var cookieDomain = window.location.hostname;  
    
    // Regex looks for two dots in the host name (www.realage.com = success, qaweb = fail)  
    var matchString = cookieDomain.match(/\..+\..+/); 
	
    if(matchString) cookieDomain = matchString;

    document.cookie = name + "=" + value + "; path=/" + ";domain=" + cookieDomain;
}

// reads a session cookie value.  A session cookie expires when the browser is closed.
function readSessionCookie(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;
}

// get the persistent value from either the cookie or the querystring
// here's the logic: first look for it in the cookie.  If the cookie does not exist 
// then look for it in the querystring and set the cookie for next time.  
// This will prevent looking through the querystring every time.
function getPersistentValue(cookieName, variableName, defaultValue)
{
    var persistentValue = readSessionCookie(cookieName);

    if (persistentValue == null)
    {
        persistentValue = getQueryStringVariable(variableName, defaultValue);
        createSessionCookie(cookieName, persistentValue);
    }

    return persistentValue;
}

// get the persistent value from either the querystring or the cookie
// here's the logic: first look for it in the querystring.  If the querystring value
// exists set or reset the cookie value.  If the querystring value does not exist 
// then look for it in the cookie.  
function getPersistentQuerystringValue(cookieName, querystringName, defaultValue) {

    var persistentValue = getQueryStringVariable(querystringName, defaultValue);

    if (persistentValue != "") {
        // querystring value was found in the querystring.  Add it to the cookie for next time
        // if the cookie already exists it's value will be updated
        createSessionCookie(cookieName, persistentValue);
    }
    else {
        // querystring value was not found in the querystring, look for it in the cookie.
        persistentValue = getSessionCookieValue(cookieName, defaultValue);
    }

    return persistentValue;
}
    
// Use this function on sites that do not live under the realage.com domain
// The main difference is that the ExclusiveSponsor cookie can be set from the querystring and reset
// This function gets the Sales CBR value passed to DoubleClick
// Here's the logic: first look for a Sales CBR value in the cookieCBR cookie.
// If the cookie does not exist then look for it in the querystring and set the cookie for next time.
// if a Sales CBR value is not found then look for the exclusiveSponsor value in the querystring.
// if it's found then set or update the cookieExclusiveSponsor Cookie for next time.
// But always check the querystring first incase there is a new value.
function getRemoteHostedDoubleClickCBR() 
{
    // get the CBR value from either the cookie or querystring.  If it's in the querystring put it in the cookie for next time.
    var cbr_val = getPersistentValue("cookieCBR", "cbr", "").toLowerCase();

    if (!isSalesCBR(cbr_val)) 
    {
        // Sales CBR value was not found so look for an Exclusive Sponsor query string parameter or cookie.
        getPersistentQuerystringValue("cookieExclusiveSponsor", "exsp", "");
    }

    return cbr_val;
}

// Use this function on sites that live under the realage.com domain
// This function gets the Sales CBR value to be passed to DoubleClick
// Here's the logic: first look for a Sales CBR value in the cookieCBR cookie.  
// If the cookie does not exist then look for it in the querystring and set the cookie for next time.
// If a Sales CBR value is not found then check in the cookieExclusiveSponsor Cookie.  
function getDoubleClickCBR() {
    
    // get the CBR value from either the cookie or querystring.  If it's in the querystring put it in the cookie for next time.
var cbr_val = getPersistentValue("cookieCBR", "cbr", "").toLowerCase();

    if (!isSalesCBR(cbr_val)) 
    {
        // Sales CBR value not found so look for an Exclusive Sponsor value in the cookie.
        cbr_val = getSessionCookieValue("cookieExclusiveSponsor", "");
    }
        
    return cbr_val;
}

// determine if the CBR tag is a Sales CBR tag
function isSalesCBR(cbrTag) {
    var returnValue = false

    if (cbrTag != null && cbrTag != "") {
        // make uppercase and convert html encoded characters
        cbrTag = unescape(cbrTag.toUpperCase());
        
        if (cbrTag == "HG_EXFG") {
            // Special case
            returnValue = true;
        }
        else {               
            var splitCBRTag = cbrTag.split("_");

            if (splitCBRTag.length > 1) {
                // look at the last substring for the unique sales itentifier
                switch (splitCBRTag[splitCBRTag.length-1]) {
                    case "LW":
                    case "HG":
                    case "CN":
                    case "BA":
                        returnValue = true;
                }
            }
        }
    }
    return returnValue;
}
