/*-----------------------------------------------------------------------------
 * Cookie object used to get and set temporary or persistant cookie values.
 * 
 * Modifications:
 * Date        By          Description
 * ----------  ----------  -------------------------------------------------
 * 11/10/2006  Garth       Initial split from general.js file.
 *---------------------------------------------------------------------------*/

// General Constructor that sets all the properties except the value of a cookie.
// Does NOT set the value because it is not clear how the constructor should behave.
// If null pass in as cookie value does that mean it should be loaded from the
// current cookie's value or does it mean that a new cookie should be created with
// a value of null...
// 
// Arguments: 
//   - name    = name of the cookie.  This is case sensitive.
//   - path    = optional element that narrows the scope of the cookie.
//   - domain  = optional element that also narrows the scope of the cookie.
//   - expires = optional value is milliseconds which represents the amount of 
//     time the cookie will be good for.  If expires is not set, the cookie will 
//     only exist until the end of the session.
//   - secure   = optional element that if exists (flag) specifies the "secure" 
//     attribute be used.
// Returns: uninitialized/unretrieved cookie object.
// Notes:
//   - A domain value of "ibis.health.utah.gov" will result in a cookie that is 
//     global for entire site.  Filtering runs left to right.
//   - A path value of "/Query" matches all Query requests, "/" matches everything 
//     for that domain
function Cookie(cookieName, cookieValue, cookiePath, cookieDomain, cookieExpires, cookieSecureFlag) 
{
	this.name    = cookieName;
	this.value   = cookieValue;
	this.path    = cookiePath;
	this.domain  = cookieDomain;
	this.expires = cookieExpires;
	this.secure  = cookieSecureFlag;
} //~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Constructor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




// General function that gets the value of a cookie.
// Arguments: cookieName = optional name of the cookie.  If not specified then 
//   the this.name property as the default.  If specified then this.name is set.
// Returns: value of the cookie baesd on the cookie name set via the constructor
//   or the cookiName passed into this method.
Cookie.prototype.getValue = function(cookieName)
{
	if(cookieName) 
		this.name = cookieName;
	else
		cookieName = this.name;

	var docCookie = document.cookie;             
	if(docCookie.length > 0) 
	{
		// try cookie name with " " prefix first.  Need to test the " " first 
		// because if cookie A is named "something" and cookie B is named "thing" 
		// then grabbing the "thing" cookie will result in the "something"'s value.
		// If NOT found in the document cookie then strip the " " prefix and test 
		// again because this cookie could be the first one which wouldn't have 
		// a " " prefix.
		cookieName = " " + cookieName + "=";	
		var begin = docCookie.indexOf(cookieName);
		if(begin == -1) 
		{
			cookieName = cookieName.substring(1, cookieName.length);
			begin = docCookie.indexOf(cookieName);
			if(begin != 0) begin = -1;  // if not the first one then it is not correct.
		}
/*
alert("Cookie.getValue - name:"+ cookieName + ":"
+"\n no blank document cookie:" + cookieName.substring(1, cookieName.length) + ":"
+"\n document cookie: "+ docCookie
+"\n begin index: "+ docCookie.indexOf(cookieName)
+"\n no blank begin index: "+ docCookie.indexOf(cookieName.substring(1, cookieName.length))
+"\n name length: " + cookieName.length
);
*/
		if(begin != -1) 
		{
			begin += cookieName.length;       
			var end = docCookie.indexOf(";", begin);
			if(end == -1) end = docCookie.length;
			this.value = unescape(docCookie.substring(begin, end));
/*
alert("Cookie.getValue - name:"+ cookieName + ":"
+"\n value:"+ this.value + ":"
+"\n document cookie: "+ docCookie
+"\n begin: "+ begin
+"\n end: "+ end
+"\n begin index: "+ docCookie.indexOf(cookieName)
+"\n name length: " + cookieName.length
);
*/
			return(this.value);
		} 
	}
	this.value = null;
	return(null);
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// Sets the actual cookie value based on the value passed in and saves the cookie.
// Arguments: value = value of the cookie (name/value pair).  If no value is specified
//   then the cookie is removed - same as setting the value to null.  
// Returns: Nothing.
Cookie.prototype.setValue = function(cookieValue) 
{
	this.value = cookieValue;
	this.set();
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// Sets the actual cookie value based on all the properties.
// Arguments: none
// Returns: Nothing.
Cookie.prototype.set = function() 
{
/*
alert("Cookie.set:" 
+ "\n  name: " + this.name
+ "\n  value: "+ this.value 
+ "\n  domain: " + (this.domain  ? "; domain="  + this.domain                : "no domain")
+ "\n  path: " + (this.path    ? "; path="    + this.path                  : "no path")
+ "\n  expires: " +(this.expires ? "; expires=" + this.expires.toGMTString() : "0")
);
*/
	document.cookie = this.name + "=" + escape(this.value) 
	  + (this.expires ? "; expires=" + this.expires.toGMTString() : "")
	  + (this.path    ? "; path="    + this.path                  : "")
	  + (this.domain  ? "; domain="  + this.domain                : "")
	  + (this.secure  ? "; secure"                                : "");

//alert("Cookie.set:" + document.cookie); 

} //~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// Sets the expires property with a date value that is Y years + D days + H hours
// + M minutes from now.  To remove/expire the cookie simply set any of these
// values to -X and set the cookie.  To set only a session cookie then do NOT
// pass in any values or pass in negative values or "0".
// Arguments: 
//   - years = optional number of years from now (default is 0).
//   - days = optional number of days from now (default is 0).
//   - hours = optional number of hourss from now (default is 0).
//   - minutes = optional number of minutes from now (default is 0).
// Returns: Nothing.
Cookie.prototype.setExpires = function(years, days, hours, minutes) 
{
	if(!minutes) minutes = 0;
	if(years) minutes += (years * 365*24*60);
	if(days)  minutes += (days  *     24*60);
	if(hours) minutes += (hours *        60);

	var date = new Date();
	date.setTime(date.getTime() + (minutes*60*1000) );
	this.expires = date;
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// Deletes the current cookie based on current properties via expiring it.  
// Note that "prototype.delete" is reserved so function is named "expire" as 
// that is what it does although it could have been named remove or clear.  
// After expiring the cookie, the expire property is then reset to null so that
// subsequent settings will result in the cookie living for the session if no
// setExpires with positive values is called.
// Arguments: none.
// Returns: Nothing.
Cookie.prototype.expire = function() 
{
	this.setExpires(-1);
	this.set();
	this.expires = null;	// clear so the old expired value is not auto used.
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/*=============================== End of File ================================*/

