﻿/*
Copyright (c) 2005, James Auldridge
All rights reserved.
Code licensed under the BSD License:
http://www.jaaulde.com/license.php

Version 1.3

Change Log:
* 02 NOV 2005 - Version 1 written
* 20 APR 2006 - Version 1.1
o Added to jimAuld namespace
o Added method to test browser for cookie acceptance
* 09 JUL 2006 - Version 1.2
o Fixed bug in cookie expiration (changed expire to expires) Thanks to
Dustin Diaz (http://www.dustindiaz.com) for discovering it.
o Shortened object and method names to ease development and remove redundancy
* 29 AUG 2006 - Placed under BSD license
* 06 SEP 2006 - Clarified commenting in reference to the hoursToLive argument
* 24 Nov 2007 - Cleaned up some syntax, etc
*/
//Preparing namespace
var jimAuld = window.jimAuld || {};
jimAuld.utils = jimAuld.utils || {};
/*
* This library is a member of the jimAuld.utils namespace
* The libary is useful for cookie interaction with JavaScript
* There are 4 methods in the cookies library, each of which is documented separately below
*
*/
jimAuld.utils.cookies =
{
    /* METHOD: get();
    * PURPOSE: Get the value of a cookie
    * ARGUMENTS: STRING - cookieName - The name of the cookie value you wish to retrieve
    * RETURN: If cookie exists - STRING - the cookie value
    *         If cookie does not exist - null
    */
    get: function(cookieName) {
        var cookieNameStart, valueStart, valueEnd, cookieValue, returnValue;
        cookieNameStart = document.cookie.indexOf(cookieName + '=');
        if (cookieNameStart < 0) {
            returnValue = null;
        }
        else {
            valueStart = document.cookie.indexOf(cookieName + '=') + cookieName.length + 1;
            valueEnd = document.cookie.indexOf(";", valueStart);
            if (valueEnd == -1) {
                valueEnd = document.cookie.length;
            }
            cookieValue = document.cookie.substring(valueStart, valueEnd);
            returnValue = (cookieValue == '') ? null : unescape(cookieValue);
        }
        return returnValue;
    },
    /* METHOD: set();
    * PURPOSE: Write (or overwrite) a cookie (with supplemental information such as expiration, path, domain)
    * ARGUMENTS: cookieName - STRING - The name of the cookie value you wish to write
    *            value - STRING - The string you wish to write as the value of the cookie
    *            hoursToLive - INT/FLOAT - The number of hours until the cookie will expire.  If you do not provide
    *                                      this argument, or provide anything that can evaluate to false, or provide
    *                                      anything that can not be interpreted as a number, the expiration field for
    *                                      the cookie will not be set causing it to be deleted the next time the
    *                                      browser is closed (IE referes to this sort of cookie as a 'session' cookie).
    *            path - STRING - (Optional) The path for which the cookie is valid.  Defaults to "/" if not
    *                             passed, or passed empty.
    *            domain - STRING - (Optional) The domain for which the cookie is valid.  Defaults to
    *                              window.location.hostname if not passed, or passed empty.
    *            secure - BOOL - (Optional) This is used to instruct the browser to use SSL when sending the
    *                            cookie to a server.  It is almost never used, and will thus be assumed false
    *                            unless you explicitly pass true.
    * RETURN: VOID
    */
    set: function(cookieName, value, hoursToLive, path, domain, secure) {
        var expireString, timerObj, expireAt, pathString, domainString, secureString;
        //If no hoursToLive argument, or it is not numeric, do not set expiration
        if (!hoursToLive || typeof hoursToLive != 'number') {
            expireString = '';
        }
        else {
            timerObj = new Date();
            timerObj.setTime(timerObj.getTime() + (hoursToLive * 60 * 60 * 1000));
            expireAt = timerObj.toGMTString();
            expireString = '; expires=' + expireAt;
        }
        //If no path argument, or argument is empty string, set path to default of /
        path = (!path || path == '' || path == null) ? '/' : path;
        pathString = '; path=' + path;
        //If no domain argument, or argument is empty string, set domain to default of window.location.hostname
        domain = (!domain || domain == '' || domain == null) ? window.location.hostname : domain;
        domainString = '; domain=' + domain;
        //If secure argument is the BOOL true, set SSL string accordingly.  Otherwise, do not set it.
        secureString = (secure === true) ? '; secure' : '';
        //escape the value for HTTP transport to server
        value = escape(value);
        //Write the value to the document.cookie string
        document.cookie = cookieName + '=' + value + expireString + pathString + domainString;
    },
    /* METHOD: del();
    * PURPOSE: Delete a cookie
    * ARGUMENTS: STRING - cookieName - The name of the cookie value you wish to delete
    *            path - STRING - (Optional) The path for which the cookie was set.  This is necessary if
    *                            the cookie was set with a particular path--you need to delete it with the
    *                            same.  Defaults to "/" if not passed, or passed empty.
    *            domain - STRING - (Optional) The domain for which the cookie was set.  This is necessary if
    *                            the cookie was set with a particular domain--you need to delete it with the
    *                            same.  Defaults to window.location.hostname if not passed, or passed empty.
    * RETURN: VOID
    */
    del: function(cookieName, path, domain) {
        path = (!path || !path.length) ? '' : path;
        domain = (!domain || !domain.length) ? '' : domain;
        jimAuld.utils.cookies.set(cookieName, '', -8760, path, domain);
    },
    /* METHOD: test();
    * PURPOSE: Test for cookie acceptance
    * ARGUMENTS: VOID
    * RETURN: If cookies accepted: BOOL - True
    *         If cookies not accepted: BOOL - False
    */
    test: function() {
        var returnValue;
        jimAuld.utils.cookies.set('cT', 'acc');
        var runTest = jimAuld.utils.cookies.get('cT');
        if (runTest == 'acc') {
            jimAuld.utils.cookies.del('cT');
            returnValue = true;
        }
        else {
            returnValue = false;
        }
        return returnValue;
    }
};


