﻿
function Cookie(iName, iValue, iExpires, iDomain, iPath, iSecure)
{
    var This = this;
    
    // Data members
    This.Name = iName;
    This.Value = null;
    This.Domain = null;
    This.Path = null;
    This.Expires = null;
    This.Secure = null;
    
    // Methods
    This.Set = function(iValue, iExpires, iDomain, iPath, iSecure)
    {
        This.Value = iValue;
        This.Domain = (iDomain) ? iDomain : window.location.host;
        This.Path = (iPath) ? iPath : '/';
        This.Expires = (iExpires) ? iExpires : null;
        This.Secure = iSecure;
        
        This.Update();
    }    
    
    This.GetValue = function()
    { return This.Value; }
    
    This.SetValue = function(iValue)
    {
        This.Value = iValue;
        This.Update();
    }
    
    This.SetExpires = function(iExpires)
    {
        This.Expires = iExpires;
        This.Update();
    }
    
    This.Destroy = function()
    { This.SetExpires((new Date()).addDays(-1)); }

    // TODO: fix domain
    This.Update = function()
    {
        var cookie = ((This.Value != null) ? This.Name + '=' + escape(This.Value) : This.Name) +
            ((This.Expires) ? '; expires=' + This.Expires.toGMTString() : '') +
            //((This.Domain) ? '; domain=' + This.Domain : '') +
            ((This.Path) ? '; path=' + This.Path : '') +
            ((This.Secure) ? '; secure' : '');
        document.cookie = cookie;
    }
    
    // Initialize
    {
        This.Value = iValue;
        This.Expires = iExpires;
        This.Domain = iDomain;
        This.Path = iPath;        
        This.Secure = iSecure;
    }    
}

function CookieManager()
{
    var This = this;
    
    // Data members
    This.Cookies = {};
    
    // Methods
    This.Get = function(iName)
    { return This.Cookies[iName]; }

    This.Set = function(iName, iValue, iExpires, iDomain, iPath, iSecure)
    {
        var cookie = This.Get(iName);

        if (cookie)
            cookie.Set(iValue, iExpires, iDomain, iPath, iSecure);
        else
        {
            cookie = new Cookie(iName, iValue, iExpires, iDomain, iPath, iSecure);
            This.Cookies[iName] = cookie;
            cookie.Update();
        }

        return cookie;
    }
    
    This.Unset = function(iName)
    {
        var cookie = This.Get(iName);
        if (cookie)
            cookie.Destroy();
    }

    This.Load = function()
    {
        var cookies = document.cookie.split(';');

        for (var i = 0; i < cookies.length; i++)
        {
            var pair = cookies[i].split('=');
            var name = pair[0].trim();
            var value;

            if (pair.length > 1)
                value = unescape(pair[1]);
            else
                value = null;

            This.Cookies[name] = new Cookie(name, value);
        }

        Debug.Trace('[Cookies] ' + document.cookie);
    }
    
    // Initialize
    {
        This.Load();
    }
}

var Cookies = new CookieManager();
