
// Cookies instance
var Cookies =
{
    versionCookie : 'mtversion',
    version : '2.5.0.1',


    init : function()
        {
            var today = new Date();
            var expiry = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
            var cookies = this.loadCookies();
            if (cookies[this.versionCookie] != this.version)
                this.clear();
            this.set(this.versionCookie, this.version, expiry);
        },

    get : function(iName)
        {
            var cookies = Cookies.loadCookies();        
            return cookies[iName];
        },

    set : function(name, value, date, path, domain, isSecure)
        {
            if (name == null)
                return false;

            if (typeof(value) == 'string')
                value = escape(value);

            var cookieStr = name+'='+value;

            if (date != null)
                cookieStr += ';expires=' + date.toGMTString();
            if (path == null)
                path = '/';

            cookieStr += ';path='+path;
            if (domain == null)
                domain = window.location.host.match(/\.[^\.]*\.[^\.]*$/);

            cookieStr += ';domain='+domain;
            if (isSecure != null)
                cookieStr = ';secure='+isSecure;

            document.cookie = cookieStr;

            return true; 
        },

    kill : function(name, path, domain)
        {
            if (name == null)
                return false;
            return this.set(name, null, new Date(0), path, domain);
        },

    loadCookies : function()
        {
            var cookies = new Object();

            try
            {
                var cs = document.cookie.split(';');

                for (var i = 0; i < cs.length; i++)
                {
                    var pair = cs[i].split('=');
                    if (pair.length != 2)
                        continue;
                    var cookieName = pair[0].replace(/^\s+|\s+$/g,'');
                    var cookieValue = unescape(pair[1].replace(/^\s+|\s+$/g,''));
                    cookies[cookieName] = cookieValue;
                }
            }
            catch(e)
            { }

            return cookies;
        },

    clear : function (path, domain)
        {
            var cookies = this.loadCookies();
            for (var name in cookies)
                this.kill(name, path, domain);          
        }
        
}

Cookies.init();

