/*
Copyright (c) 2005, James Auldridge
All rights reserved.
Code licensed under the BSD License:
  http://www.jaaulde.com/license.php

Version 1.2

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
*/
//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,value;
		cookieNameStart = document.cookie.indexOf(cookieName+'=');
		if (cookieNameStart < 0) {return null;}
		valueStart = document.cookie.indexOf(cookieName+'=') + cookieName.length + 1;
		valueEnd = document.cookie.indexOf(";",valueStart);
		if (valueEnd == -1){valueEnd = document.cookie.length;}
		value = document.cookie.substring(valueStart,valueEnd );
		value = unescape(value);
		if (value == "") {return null;}
		return value;
	},
	/* 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 - 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,setCookieString;
		//If no hoursToLive argument, or it is not numeric, do not set expiration
		if (!hoursToLive || typeof hoursToLive != 'number' || parseInt(hoursToLive)=='NaN'){
			expireString = "";
		}
		else {
			timerObj = new Date();
			timerObj.setTime(timerObj.getTime()+(parseInt(hoursToLive)*60*60*1000));
			expireAt = timerObj.toGMTString();
			expireString = "; expires="+expireAt;
		}
		//If no path argument, or argument is empty string, set path to default of /
		pathString = "; path=";
		(!path || path=="") ? pathString += "/" : pathString += path;
		//If no domain argument, or argument is empty string, set domain to default of window.location.hostname
		domainString = "; domain=";
		(!domain || domain=="") ? domainString += window.location.hostname : domainString += domain;
		//If secure argument is the BOOL true, set SSL string accordingly.  Otherwise, do not set it.
		(secure === true) ? secureString = "; secure" : secureString = "";
		value = escape(value);
		setCookieString = cookieName+"="+value+expireString+pathString+domainString;
		//alert(setCookieString);
		document.cookie = setCookieString;
	},
	/* 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.length) ? path="" : path=path;
          (!domain || !domain.length) ? domain="" : domain=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(){
		jimAuld.utils.cookies.set('cT','acc');
		var runTest = jimAuld.utils.cookies.get('cT');
		if (runTest == 'acc'){
			jimAuld.utils.cookies.del('cT');
			testStatus = true;
		}
		else {
			testStatus = false;
		}
		return testStatus;
	}
};

