
// c'tor
function Cookie (_forDocument, _cookieName, _lifeInMinutes, _cookiePath, _cookieDomain, _isToBeSecure)
	{
	this.$document = _forDocument;
	this.$name = _cookieName;
	this.$expiration = (_lifeInMinutes) ? new Date ((new Date ()).getTime() + _lifeInMinutes * 60000) : null;
	this.$path = _cookiePath;
	this.$domain = _cookieDomain;
	this.$secure = _isToBeSecure;
	}

function _Cookie_getAsString ()
	{
	var allCookies = this.$document.cookie;
	var start = allCookies.indexOf(this.$name + '=');
	if (start == -1)
		return false;   // cookie not defined for this page
	start += this.$name.length + 1;  // skip name and equals sign
	var end = allCookies.indexOf(';', start);
	if (end == -1)
		end = allCookies.length;
	var values = allCookies.substring(start, end);
	return unescape(values);
	}

function _Cookie_getVerbatim ()
	{
	var allCookies = this.$document.cookie;
	var start = allCookies.indexOf(this.$name + '=');
	if (start == -1)
		return false;   // cookie not defined for this page
	start += this.$name.length + 1;  // skip name and equals sign
	var end = allCookies.indexOf(';', start);
	if (end == -1)
		end = allCookies.length;
	var values = allCookies.substring(start, end);
	return unescape(values);
	}

function _Cookie_store ()
	{
	// assemble the properties
	var values = new StringBuilder ();
	for (property in this)
		{
		if (property.charAt(0) != "$" && typeof this[property] != "function")
			if (this[property] != null)
				{
				var val = new String (this[property]);
				//if (val.replace)
				//	val = val.repace(/\x20+/g, " ");
				if (property == "featureStr" || val.length > 40)
					values.Append( (values != "" ? "&" : "") + property + "=" + val );
				else
					values.Append( (values != "" ? "&" : "") + property + "=" + escape(val) );
				}
		}
	// assemble the complete cookie string
	var cookieData = new StringBuilder ();
	cookieData.Append(this.$name + "=" + values.toString());
	cookieData.Append(this.$expiration ? "; expires=" + this.$expiration.toGMTString() : "");
	cookieData.Append(this.$path ? "; path=" + this.$path : "");
	cookieData.Append(this.$domain ? "; domain=" + this.$domain : "");
	cookieData.Append(this.$secure ? "; secure" : "");
	// store the cookie by setting the Document.cookie property
	this.$document.cookie = cookieData.toString();
	//if (top.rdb) top.rdb(cookieData.toString());
	if (top.rdb) top.rdb("_Cookie_store COOKIES: Byte-length of document cookies is now " + this.$document.cookie.length);
	}

function _Cookie_retrieve ()
	{
	// retrieve list of all cookies for the document
	var allCookies = this.$document.cookie;
	if (allCookies == "")
		return false;
	// extract the named cookie
	var start = allCookies.indexOf(this.$name + '=');
	if (start == -1)
		return false;   // cookie not defined for this page
	start += this.$name.length + 1;  // skip name and equals sign
	var end = allCookies.indexOf(';', start);
	if (end == -1)
		end = allCookies.length;
	var values = allCookies.substring(start, end);
	// parse the values
	var a = values.split('&');  // break it into array of name/value pairs
	for (var i = 0; i < a.length; i++)  // break each pair into an array
		 a[i] = a[i].split('=');
	// set the properties
	for (var i = 0; i < a.length; i++)
		 this[a[i][0]] = unescape(a[i][1]);
	//if (top.rdb) top.rdb("_Cookie_retrieve COOKIES: Byte-length of document cookies is now " + this.$document.cookie.length);
	return true;
	}

function _Cookie_remove ()
	{
	var cookieData = new StringBuilder ();
	cookieData.Append(this.$name + '=');
	cookieData.Append(this.$path ? '; path=' + this.$path : "");
	cookieData.Append(this.$domain ? '; domain=' + this.$domain : "");
	cookieData.Append('; expires=Fri, 02-Jan-1970 00:00:00 GMT');
	this.$document.cookie = cookieData.toString();
	//if (top.rdb) top.rdb("_Cookie_remove COOKIES: Byte-length of document cookies is now " + this.$document.cookie.length);
	}

function _Cookie_clear ()
	{
	for (property in this)
		if (property.substring(0,1) != "$" && typeof(this[property]) != "function")
			this[property] = null;
	//if (top.rdb) top.rdb("_Cookie_clear COOKIES: Byte-length of document cookies is now " + this.$document.cookie.length);
	}

// itterate a callback function on each property
function _Cookie_forCursor (callback)
	{
	for (property in this)
		if (property.substring(0,1) != "$" && typeof(this[property]) != "function")
			callback(this, property, this[property]);
	}

// Create a dummy Cookie object to prototype the methods
new Cookie ();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.retrieve = _Cookie_retrieve;
Cookie.prototype.remove = _Cookie_remove;
Cookie.prototype.getAsString = _Cookie_getAsString;
Cookie.prototype.getVerbatim = _Cookie_getVerbatim;
Cookie.prototype.asString = _Cookie_getAsString;
Cookie.prototype.toString = _Cookie_getAsString;
Cookie.prototype.clear = _Cookie_clear;
Cookie.prototype.forCursor = _Cookie_forCursor;

