/**
 * This file contains the definition for a javascript Cookie object.  The Cookie
 * object allows you to set and retrieve cookie attributes.
 *
 * Usage:
 * To use a Cookie, you must first instantiate an instance of the Cookie for a
 * named cookie:
 *   var cookie = new Cookie(document, <cookieName>, <hoursToExpiration>);
 * where <cookieName> is the name of the cookie you are instantiating the object
 * for, and <hoursToExpiration> is the number of hours before the cookie expires.
 * <hoursToExpiration> is optional.  If not provided, the cookie expires with the
 * session.
 *
 * To retrieve current Cookie settings, call the Cookies' load method:
 *   cookie.load();
 *
 * To set attributes for the cookie simply assign a value to the attribute, 
 * for example:
 *    cookie.username = "some_user";
 *    cookie.age = "11";
 *
 * To get the value of a cookie attribute simply reference:
 *    cookie.<attributeName>
 * For Example:
 *    var age = cookie.age;
 *
 * To save cookie attributes after they have been set or changed, call the 
 * Cookies' store method:
 *    cookie.store();
 * 
 * @author Scott Dyke
 * @version 1.0
 * @since 6/18/03
 */


		//  this sets up the store and load methods
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;


/**
 * Constructor for cookie.
 *
 * @param document			The javascript document the cookie belongs to.
 * @param name				The name of the cookie.
 * @param hoursToExpiration	(optional) The number of hours until the cookie expires.
 *							If not provided, cookie expires with session.
 */
function Cookie(
	document,
	name,
	hoursToExpiration)
{

	this.$document = document;
	this.$name = name;
	if (hoursToExpiration) {
		this.$expires = new Date((new Date()).getTime() + hoursToExpiration * 3600000);
	} else {
		this.$expires = null;
	}
}


/**
 * Saves the cookie attributes to the associated document.
 */
function _Cookie_store()
{
	var cookieVal = "";
	
	for (var prop in this) {
		if ((prop.charAt(0) != '$') && (typeof this[prop] != 'function')) {
			if (cookieVal != "") {
				cookieVal += "&";
			}
			cookieVal += prop + "=" + escape(this[prop]);
		}
	}

	var cookie = this.$name + '=' + cookieVal;
	
	if (this.$expires) {
		cookie += "; expires=" + this.$expires.toGMTString();
	}

	this.$document.cookie = cookie;
}


/**
 * Saves the cookie attributes to the associated document.
 *
 * @return	True if the cookie was successfully loaded, false if not.
 */
function _Cookie_load()
{
	var allCookies = this.$document.cookie;
	
	if (allCookies == "") {
		return false;
	}

	var cookieName = this.$name + '=';
	var start = allCookies.indexOf(cookieName);
			//  if no cookie with given name, exit
	if (start == -1) {
		return false;
	}
			//  skip name and "="
	start += this.$name.length + 1;

			//  find the end of the named cookie
	var end = allCookies.indexOf(';', start);

	if (end == -1) {
		end = allCookies.length;
	}

			//  get the value for the cookie
	var cookieVal = allCookies.substring(start, end);

			//  split the cookie string into an array of name/value pairs
	var keysAry = cookieVal.split('&');
	
			//  split each name/value pair into an array
	for (var k = 0; k < keysAry.length; k++) {
		keysAry[k] = keysAry[k].split('=');
	}

	for (var k = 0; k < keysAry.length; k++) {
		this[keysAry[k][0]] = unescape(keysAry[k][1]);
	}
	
	return true;
}


/**
 * Displays the cookies of the current document.  Used for debugging.
 */
function showCookies()
{
	var str = "";
	
	var allCookies = document.cookie;
	
	alert(allCookies);
}


