﻿var portfolioCookieName = "DAC689F083B9431CB2E34E009141314F";

function getPortfolioCookie()
{
    if (document.cookie.length > 0)
    {
        var start = document.cookie.indexOf(portfolioCookieName + "=");
        if (start > -1)
        {
            start = start + portfolioCookieName.length + 1; 
            var end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return decodeURIComponent(document.cookie.substring(start, end));
        }
    }
    return "";
}

function setPortfolioCookie(newValue)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 365);
    document.cookie = portfolioCookieName + "=" + encodeURIComponent(newValue) + ";expires=" + exdate.toGMTString() + ";path=/";
}

function togglePortfolio(id)
{
    var cookieValue = getPortfolioCookie();
    var found = false;
    var newCookieValue = "";
    if (cookieValue != null)
    {
        var values = cookieValue.split("|");
        if (values != null)
        {
            for (var i = 0; i < values.length; i++)
            {
                if (values[i] == id)
                {
                    found = true;
                }
                else
                {
                    if (newCookieValue.length > 0) newCookieValue = newCookieValue.concat("|");
                    newCookieValue = newCookieValue.concat(values[i]);
                }
            }
        }
    }
    if (!found)
    {
        if (newCookieValue.length > 0) newCookieValue = newCookieValue.concat("|");
        newCookieValue = newCookieValue.concat(id);
    }
    setPortfolioCookie(newCookieValue);
}

function isInPortfolio(id) {
    var cookieValue = getPortfolioCookie();
    var found = false;
    var newCookieValue = "";
    if (cookieValue != null) {
        var values = cookieValue.split("|");
        if (values != null) {
            for (var i = 0; i < values.length; i++) {
                if (values[i] == id) return true;
            }
        }
    }
    return false;   
}

function getPortfolioCount() {
    var cookieValue = getPortfolioCookie();
    var c = 0;
    if (cookieValue != null)
    {
        var values = cookieValue.split("|");       
        for (var i = 0; i < values.length; i++)
        {
            if (values[i] != null && values[i].length > 0)
            {
                c++;
            }
        }
    }
    return c;
}

