﻿function unpackCookie()
{
    var output = { increment: function(key){
                            if(this[key] > 0)
                            {
                                currVal = parseInt(this[key]);
                                this[key] = currVal + 1;
                            }
                            else
                            {
                                this[key] = 1;
                            }
                               
                            repackCookie(this);
                          }
                 };
                 
    var cookieValue = document.cookie.match('ecart=([^;]*)');
    
    if(cookieValue == null || cookieValue == "")
        return output;             
    
    var instances = cookieValue[1].split('&');

    for(i in instances) {
        if (typeof (instances[i]) != 'string')
            continue;
            
        splitVals = instances[i].split("=");
        key = splitVals[0];
        val = splitVals[1];

        output[key] = val;
    }
    
    return output;
}

function repackCookie(unpackedCookie)
{
    var cookieValue = "";
    
    for(i in unpackedCookie)
    {
        var instance = unpackedCookie[i];
        // Don't save the helper functions
        if(typeof(instance) == 'function')
            continue;
        
        if(cookieValue != "")
            cookieValue += "&";
        
        cookieValue += i +"="+ instance;
    }
    
    var expiryDate = new Date(GetExpiryTime()).toGMTString();
    var path = "/";
    var domain = "lmigroup.com";
    cookieString = "ecart="+ cookieValue +"; expiry="+ expiryDate +";path="+ path +";domain="+ domain +";";
    document.cookie = cookieString;
}
       
function addToCart(id)
{
    cookie = unpackCookie();
    cookie.increment(id);
    var redirectUrl = GetArticleCategoryRedirect();
    document.location = redirectUrl;
}