/************************************************************************
 *
 * Utility Cookie Class
 *
 * Creates cookie.
 * Sets and gets the value.
 * Deletes cookie
 *
 * NOTE: Add funcitonality to add more than nvp to the cookie.
 *
 * @param		document	[required] Object:	The document whish the cookie is associated with
 * @param		name			[required] String:	Name of the cookie
 * @param		years			[optional] String:	Integer: Years for the cookie to persist
 * @param		path			[optional] String:	Path attribute for the cookie
 * @param		domain		[optional] String:	Domain attribute for the cookie
 * @param		secure		[optional] Boolean:	Sets cookie's secure attribute
 *
 * Change log:
 * 09.13.2004	Brian Riley --	Created
 * 08.13.2005	Balaji Raghavan -- Added strGetCookie function
 * 06.20.2006	Balaji Raghavan	-- Added setExpiration function
 *
 ***********************************************************************/


Cookie = function(document, name, years, path, domain, secure) {
	this.document = document;
	this.name = name;
	this.expiration = (years) ? new Date((new Date()).getTime() + years*365*24*3600000) : null;
	this.path = (path) ? path : null;
	this.domain = (domain) ? domain : null;
	this.secure = (secure) ? true : false;
};

Cookie.prototype.setValue = function(value) {
	var c = this.name + '=' + escape(value);
	if(this.expiration) c += '; expires=' + this.expiration.toGMTString();
	if(this.path) c += '; path=' + this.path;
	if(this.domain) c += '; domain=' + this.domain;
	if(this.secure) c += '; secure=' + this.secure;
	this.document.cookie = c;
};

Cookie.prototype.setSessionValue = function(value){
	var c = this.name + '=' + value;
	if(this.path) c += '; path=' + this.path;
	if(this.domain) c += '; domain=' + this.domain;
	if(this.secure) c += '; secure=' + this.secure;
	this.document.cookie = c;
};

Cookie.prototype.setExpiration = function(time){
	var date = new Date();
	date.setTime(time);
	this.expiration = date;
};

Cookie.prototype.getValue = function() {
	var cookies = this.document.cookie;
	if(cookies == "") return null;
	
	var start = cookies.indexOf(this.name + '=');
	start += this.name.length + 1;
	if(start != -1) {
		var end = cookies.indexOf(';', start);
		if(end == -1) end = cookies.length;
		
		return cookies.substring(start, end);
	} else {
		return null;	
	}
};

Cookie.prototype.deleteCookie = function() {
	this.expiration = new Date((new Date()).getTime() -1);
	this.setValue(null);
};

// Retrieves the value of a cookie already existing on a user's machine.
 function strGetCookie(strCookieName) 
 {
	// return string containing value of specified cookie or null if cookie does not exist
	var dc = document.cookie;
	var prefix = strCookieName + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) 
	{
		begin = dc.indexOf(prefix);
		if (begin != 0)
		{
		return null;
		}
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	cookieTemp = new Cookie();
	return unescape(dc.substring(begin + prefix.length, end));
 }