﻿// JavaScript Document
document.xCookie = new Object();
document.xCookie.load = function(d) {
    this.$document = d || document;

    var cookies = this.$document.cookie;
    var cookieRegEx = /\w+=[^;]+/g;
    var matchs = cookies.match(cookieRegEx);

    for (var i = 0; matchs && i < matchs.length; i++) {
        var arr = matchs[i].split('=');
        this[arr[0]] = new document.xCookie.cookie(this.$document, arr[0], arr[1],5000);
    }
}

document.xCookie.save = function() {
    for (var prop in this)
        if ((prop.charAt(0) != '$') && ((typeof this[prop]) != 'function') && this[prop].write) {
        this[prop].write();

    }
}

document.xCookie.clear = function() {
    for (var prop in this)
        if (this[prop].remove) this[prop].remove();
}

document.xCookie.$ = function(name) {
    if (this[name])
        return this[name];
    else {
        this[name] = new document.xCookie.cookie(this.$document, name);
        return this[name];
    }
}

document.xCookie.cookie = function(document, name, val, timeout, path, domain, secure) {
    this.$document = document;
    this.$name = name;
    if (val)
        this.$val = val;
    else
        this.$val = '';
    if (timeout)
        this.$expiration = new Date((new Date()).getTime() + timeout * 60000);
    else
        this.$expiration = null;
    if (path)
        this.$path = path;
    else
        this.$path = null;
    if (domain)
        this.$domain = domain;
    else
        this.$domain = null;
    if (secure)
        this.$secure = true;
    else
        this.$secure = false;
    this.read();
}

document.xCookie.cookie.prototype.read = function() {
    var valRegEx = /\w+:[^&]+/g;
    var matchs = this.$val.match(valRegEx);

    for (var i = 0; matchs && i < matchs.length; i++) {
        var arr = matchs[i].split(':');
        this[arr[0]] = unescape(arr[1]);
    }
}

document.xCookie.cookie.prototype.write = function() {
    var val = this.toString();
    var cookie = this.$name + '=' + val;
    if (this.$expiration) cookie += ";expires=" + this.$expiration.toGMTString();
    if (this.$path) cookie += ";path=" + this.$path;
    if (this.$domain) cookie += ";domain=" + this.$domain;
    if (this.$secure) cookie += ";secure";
    this.$document.cookie = cookie;
}

document.xCookie.cookie.prototype.remove = function() {
    var val = '';
    for (var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
            continue;
        this[prop] = null;
    }
    this.$expiration = new Date(1970, 1, 2, 0, 0, 0);
}

document.xCookie.cookie.prototype.toString = function() {
    var val = '';
    for (var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
            continue;
        if (val != '') val += '%';
        val = prop + ':' + escape(this[prop]);
    }
    return val;
}

xCookie = document.xCookie;