var ChannelType = "";
var ChannelID = "";
var ASPPersistentCookie = "";

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("&");
        return  tmp = tmp.substring( 0, (i>=0) ? i : tmp.length );
    }
    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 today = new Date();
    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=/";
    }
}