﻿// ***********************************************************************************************************
// Name:Control Objects
// Type:Library
// Author: Cliff Gower
//************************************************************************************************************

var Framework = Framework || {};
Framework.Cookies = Framework.Cookies || {};


Framework.Cookies.CookieSecurity = {
    SECURE: true,
    INSECURE: false
}


Framework.Cookies.WriteCookie = function (cookie) {
    document.cookie = cookie.Name + "=" + escape(cookie.Value) +
    ( (cookie.Expiration) ? ";expires=" + cookie.Expiration : "" ) +
    ( (cookie.Path) ? ";path=" + cookie.Path : "") +
    ( (cookie.Domain) ? ";domain=" + cookie.Domain : "" ) +
    ( (cookie.Secure) ? ";secure" : "" );
};

Framework.Cookies.ReadCookie = function (name) {
    var cookieArray = document.cookie.split(';');
    for (var index = 0; index < cookieArray.length; index++) {
        var cookieNameValuePair = cookieArray[index].split('=');
        if (cookieNameValuePair[0] == name) {
            return new Framework.Cookies.Cookie(name, unescape(cookieNameValuePair[1]));
        }
    }
    return null;
};

Framework.Cookies.EraseCookie = function (cookie) {
    cookie.Value = "";
    cookie.Expiration = "Thu, 01-Jan-1970 00:00:01 GMT";

    Framework.Cookies.WriteCookie(cookie);
};



Framework.Cookies.Cookie = function (name, value, expiration, domain, path, secure) {
    this.Name = null;
    this.Value = null;
    this.Expiration = null;
    this.Domain = null;
    this.Path = null;
    this.Secure = null;


    if (name != null && name != undefined) { this.Name = name };
    if (value != null && value != undefined) { this.Value = value };
    if (expiration != null && expiration != undefined) { };
    if (domain != null && domain != undefined) { this.Domain = domain };
    if (path != null && path != undefined) { this.Path = path };
};

Framework.Cookies.Cookie.prototype.AddCrumb = function (name, value) {
    if (this.Value.length > 0) { this.Value = this.Value + "|" }
    this.Value = this.Value + name + "=" + value;
}

Framework.Cookies.Cookie.prototype.GetCrumb = function (name) {
    var crumbArray = this.Value.split('|');
    for (var index = 0; index < crumbArray.length; index++) {
        var crumbNameValuePair = crumbArray[index].split('=');
        if (crumbNameValuePair[0] == name) {
            return new Framework.Cookies.Crumb(name, crumbNameValuePair[1]);
        }
    }

    return null;
}



Framework.Cookies.Crumb = function (name, value) {
    this.Name = null;
    this.Value = null;

    if (name != null && name != undefined) { this.Name = name };
    if (value != null && value != undefined) { this.Value = value };
}
