﻿/// <reference path="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" />
//Tim C: This script is adapated from here http://www.java2s.com/Code/JavaScript/Development/Cookieinstallanddeleteremove.htm 

function Cookie(document, name, hours, path, domain, secure){
    /// <summary>This creates a cookie object that you can add custom properties to on the fly to have stored</summary>
    /// <param name="document">The document object</param>
    /// <param name="name">The name of the cookie</param>
    /// <param name="hours">How many hours to keep the cookie alive, omit this to make it a session cookie.</param>
    /// <param name="path">The path of the cookie, if omitted will default to /</param>
    /// <param name="domain">The domain for the cookie</param>
    /// <param name="secure">Whether or not the cookie should be secure</param>
    this.$document = document;
    if(!name) {
        throw 'Cookie name can not be null';
        return null;
    }
    this.$name = name;
    
    //force null to avoid undefined
    this.$expiration = hours ? new Date((new Date()).getTime() + hours*3600000) : null;
    this.$path = path ? path : '/';
    this.$domain = domain ? domain : null;
    this.$secure = secure ? secure : null;
}

Cookie.prototype.store = function () {
    /// <summary>Will store the cookie for you into the document object after you modfiy it</summary>
    var cookieval = "";
    //loop this obejects props to build the cookie
    for(var prop in this) {
        // Ignore properties with names that begin with '$' and also methods.
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval.length == 0) cookieval += '&';
        
        cookieval += prop + ':' + escape(this[prop]);
    }

    var cookie = this.$name + '=' + cookieval;
    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';

    // Now store the cookie by setting the magic Document.cookie property.
    // alert('storing cookie=' + cookie);
    this.$document.cookie = cookie;
    return this;
}

// This function is the load() method of the Cookie object.
Cookie.prototype.load = function() { 
    /// <summary>Loads the cookie from the memory if a session cookie, the cookie collection otherwise</summary>
    var allcookies = this.$document.cookie;
    if (allcookies == "") return null;

    // Now extract just the named cookie from that list.
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return null;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    var a = cookieval.split('&');    // Break it into array of name/value pairs.
    var b = [];    
    var j = 0;

    for(var i=0; i < a.length; i++){  // Break each pair into an array.
        if(a[i].trim().length > 0){
            b[j++] = a[i].split(':');
        }
    }
    
    for(var i = 0; i < b.length; i++) {
        this[b[i][0]] = unescape(b[i][1]);
    }

    return this;
}

Cookie.prototype.exists = function() {
    var cookieIndex = this.$document.cookie.indexOf(this.$name + '=');
    return (cookieIndex != -1);
}


// This function is the remove() method of the Cookie object.
Cookie.prototype.remove = function() {
    /// <summary>Removes the cookie</summary>
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
    return true;
}

String.prototype.trim = function(){
    /// <summary>Will remove pre and post white space from a string</summary>
    return this.replace(/^\s+|\s+$/g, ''); //strip white space off 
}


