var ChannelType = "";
var ChannelID = "";
var ASPPersistentCookie = "";

var referrerAddress = document.referrer;
if (referrerAddress != "")
{
    var tpid = GetCookieValue("tpid", 1);
    var eapid = GetCookieValue("iEAPID", 0)
    
    // only applies to FR, DE and AT POSs
    //if ((tpid == 20) || (tpid == 6) || ((tpid == 6) && (eapid == 10122)))
    if (tpid == 20)
    {
        // flag for remove SEOCID or not 
        // (need to be cleared if referrer is Goggle or Yahoo)
        var clearSeocid = false;
        // flag for clear cookie or not 
        // (need to be cleared if referrer is Goggle or Yahoo normal search /not sponsored link/)
        var clearCookie = false;
        
        // check if the user is coming from Google
        if (referrerAddress.toLowerCase().indexOf("www.google.") > -1)
        {
            clearSeocid = true;
            // if it is a sponsored link contains the SEMCID URL query attribute
            clearCookie = (referrerAddress.toLowerCase().indexOf("semcid") == -1);
        }
        // check if the user coming from Yahoo normal search
        else if ((referrerAddress.toLowerCase().indexOf("search.yahoo.com") > -1) || (referrerAddress.toLowerCase().indexOf("wrs.yahoo.com") > -1))
        {
            clearSeocid = true;
            clearCookie = true;
        }
        // check if the user coming from Yahoo sponsored link
        else if (referrerAddress.toLowerCase().indexOf("rc12.overture.com") > -1)
        {
            clearSeocid = true;
            clearCookie = false;
        }
        
        if (clearCookie)
        {
            // clear the cookie value
            SetASPPersistentCookie("v.1,0||||||||||||");
        }
        
        if (clearSeocid)
        {
            var urlAndQueryAttributes = window.location.toString().split("?");
            if (urlAndQueryAttributes.length > 1)
            {
                var url = urlAndQueryAttributes[0];
                var queryAttributes = urlAndQueryAttributes[1].split("&");
                for (var index = 0; index < queryAttributes.length; index++)
                {
                    if (queryAttributes[index].toLowerCase().indexOf("seocid") == -1)
                    {
                        url += (index == 0) ? "?" : "&";
                        url += queryAttributes[index];
                    }
                }
                window.location = url;
            }
        }
    }
}

ChannelType = GetChannelType();
if (ChannelType >"")
{
    ChannelID = GetChannelID(ChannelType+"CID");
    if ((ChannelID > ""))
    {
        ASPPersistentCookie = GetASPPersistentCookie();
        
        if (!ASPPersistentCookie>"")
        {
            SetASPPersistentCookie("v.1,0||||||||||||");
            ASPPersistentCookie = GetASPPersistentCookie();
        }
        
        var expires = new Date(new Date().setTime(new Date().getTime()+30*24*60*60*1000));  //Add 30 Days
        var expYear = expires.getFullYear().toString();
        var expMonth = (expires.getMonth()+1).toString();
        if (expMonth.length<2) 
            expMonth = "0" + expMonth;
        var expDay = expires.getDate().toString();
        if (expDay.length<2) 
            expDay = "0" + expDay;
        var ChannelExpiry = expYear + expMonth + expDay
        var ASPPValues = ASPPersistentCookie.split("|");
        ASPPValues[1] = ChannelID;
        ASPPValues[10] = ChannelType;
        ASPPValues[11] = ChannelExpiry;
        
        ASPPersistentCookie = "";
        
        for (i=0; i< ASPPValues.length; i++)
        {
            ASPPersistentCookie += ASPPValues[i] + "|";
        }
        ASPPersistentCookie = ASPPersistentCookie.substring(0,ASPPersistentCookie.length-1);
        SetASPPersistentCookie(ASPPersistentCookie);
        
    }  
}

function GetChannelType()   
{
    var tmp = location.search.substring(1).toUpperCase();
    var i   = tmp.indexOf("CID=");

    if ( i >= 3 )
    {
        var ct = tmp.substr(i-3,3);
        if (ct != "AFF" && ct != "OLA" && ct != "EML" && ct != "MDP" && ct != "SEM" & ct != "OFF" & ct != "MSN")
        {
            if (tmp.indexOf("CID=") != tmp.lastIndexOf("CID="))
            {
                return (tmp.substr(tmp.lastIndexOf("CID=")-3,3));
            }
            else 
            { 
                return (""); 
            }
        }
        else
        {
            return (ct)
        }
    }
    return("");
}

function GetChannelID(name)   
{
    var tmp = ( location.search.substring(1) );
    var i   = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");

    if ( i >= 0 )
    {
        tmp = tmp.substring( name.length+i+1 );
        i = tmp.indexOf("&");
        tmp = tmp.substring( 0, (i>=0) ? i : tmp.length );
        return  escape(tmp);
    }
    return("");
}

function GetASPPersistentCookie() {
    var start = document.cookie.indexOf("aspp=");
    var len = start+5;
    if ((!start) && ("aspp" != document.cookie.substring(0,4))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return document.cookie.substring(len,end);
}

function SetASPPersistentCookie(value) {
    var expires = new Date(new Date().setTime(new Date().getTime()+5*365*24*60*60*1000)); // 5 years
    document.cookie = escape("aspp") + "=" + value + "; expires=" + expires.toGMTString() + "; path=/";
    
    var domain = location.href.match( /:\/\/(www\.)?([^\/:]+)/ ); 
    if (domain > "")
    {
        domain = domain[2]?domain[2]:''; 
        document.cookie = escape("aspp") + "=" + value + "; expires=" + expires.toGMTString() + "; domain=." + domain + "; path=/";
    }
}

// this function is used to retrieve the 'tpid' or the 'iEAPID' cookie value
function GetCookieValue(cookieName, index)
{
    cookieName = cookieName + "=";
    var start = document.cookie.indexOf(cookieName);
    var len = start + cookieName.length;
    if ((!start) && (cookieName != document.cookie.substring(0,cookieName.length-1))) return "";
    if (start == -1) return "";
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    
    var versionedValue = document.cookie.substring(len,end);
    var valueArray = versionedValue.split(",");
    
    if(valueArray[index] != null)
        return valueArray[index];
    else   
        return null;
}
