﻿

function CookieSource(doc) {
    this._document = doc;
}

CookieSource.prototype.read = function(name) {
    if (this._cookies == null) {
        this._cookies = this._loadCookies();
    }
    return this._cookies[name];
}

CookieSource.prototype._simpleRead = function(name) {
    var ca = this._document.cookie.split(';');
    var nameEQ = name + "=";
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

CookieSource.prototype.create = function(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    this._document.cookie = name + "=" + value + expires + "; path=/";
    this._cookies = null;
}

CookieSource.prototype._loadCookies = function() {
    var cr = []; if (this._document.cookie != '') {
        var ck = this._document.cookie.split('; ');
        for (var i = ck.length - 1; i >= 0; i--) {
            var cv = ck[i].split('=');
            cr[cv[0]] = cv[1];
        }
    }
    return cr;
}

CookieSource.prototype._cookies = null;
CookieSource.prototype._document = null;

