// .v....1....v....2....v....3....v....4....v....5....v....6....v....7....v....8
//	JavaScript routines to handle Cookies
//	(c) CSC Computer Sciences Ltd 1999
//
//	Amendment Record
//		29-Nov-1999	J S Cole	CSC GIS (Brough, UK)	1.00.00
//			First Version
//		dd-Mmm-yyyy	Your Name	Your Organisation	Version
//			Description of change
//
//	Notes
//		As cookies are saved one per variable and there is a very low 
//		limit of entries (20 per server), this set of routines enable 
//		you to combine sets of variables into a single variable. The 
//		name of the envelope variable is defined in the constructor. 
//		Names of sub-variables are defined as properties of the object 
//		created to hold the details. 
//
//	Usage
//	1)	Create a Cookie object
//		Syntax
//			var <cookie varname> = Cookie(<name>);
//		Where
//			<name>	Name of the envelope variable that the values 
//				are to be read from / written to
//		Example
//			var mycookie = new Cookie('Project1');
//
//	2)	Methods
//		save(<days>, <path>, <domain>, <secure>)
//			Save values in the Cookie file
//			<days>	is an optional no of days that the cookie is 
//				to be retained for. If omitted then the 
//				existing expiration date will not be altered, 
//				if this is a new cookie, it will be deleted 
//				when the browser is exitted.
//			<path>	is an optional Cookie path attribute
//			<domain>
//				is an optional Cookie domain attribute
//			<secure>
//				is an optional Cookie secure attribute (must 
//				evaluate to true or false)
//		remove(<path>, <domain>)
//			Delete the Cookie from the Cookie file
//			<path>	is an optional Cookie path attribute
//			<domain>
//				is an optional Cookie domain attribute
//
//	3)	Properties
//		There are no predefined properties but variables within the 
//		Cookie can be referred to as properties of the object
//
//	Known Problems
//		None
//
//	========================================================================

//	Functions used by the object

//	------------------------------------------------------------------------

//	Object constructor
function	Cookie(name)
{	// Save arguments
//	alert ("entered function Cookie, name is " + name);
	if	(name)	// Not pseudo object for Navigator 3
	{
		this.internal = new Object();	// Internal values only
		this.internal.document = document;	// Back pointer
		this.internal.name = name;
		// Get values for this envelope name
		var	allValues = '; ' + document.cookie + '; ' + name + '=;';	// Force value to be found
		allValues = allValues.substring(allValues.indexOf('; ' + name + '=') + name.length + 3);
		allValues = allValues.substring(0, allValues.indexOf(';')).split('&');

		// Split into sub-variables and values
		for	(var i = 0; i < allValues.length; i++)
		{

			var	nameValue = allValues[i].split('!');
			if	(nameValue[0] && nameValue[0] != '')
				this[nameValue[0]] = 
					(allValues[i].indexOf('!') == allValues[i].length - 1)
					? ''	// No text after separator
					: unescape(nameValue[1]);
		}	// for
	}	// if
}	// Cookie

//	------------------------------------------------------------------------

//	Save values in specified cookie
function	Cookie_save(days, path, domain, secure)
{	// Package all sub variables. Use '&' as subvar delimiter, use ':' as 
	// name / value delimiter
	alert ("entered Cookie_save, arguments are " + days + ", " + path + ", " + 
		   domain + ", " + secure);
	var	saveString = "";
	for	(var prop in this)
		if	(typeof this[prop] != "function" && 
			typeof this[prop] != "object" && 
			prop != "")

			saveString += '&' + prop + '!' + escape(this[prop]);

	// Add in optional components
	if	(days)	// true if non-null string or non-zero number
	{
		var	d = new Date();
		d = new Date(d.getTime() + days * 86400000);
		saveString += '; expires=' + d.toGMTString();
	}	// if

	this.internal.document.cookie = this.internal.name + '=' + saveString + 
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
}	// end Cookie_save

//	------------------------------------------------------------------------

//	Delete specified cookie
function	Cookie_remove(path, domain)
{
	this.internal.document.cookie = this.internal.name + 
		'=; expires=Fri 02-Jan-1970 00:00:00 GMT' + 
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '');
}	// Cookie_remove

//	========================================================================

//	Prototype definitions

//	------------------------------------------------------------------------

new	Cookie();	// Dummy object (required for Navigator 3 to create a prototype object)

//	------------------------------------------------------------------------

//	Functions
Cookie.prototype.save = Cookie_save;		// Write cookie values
Cookie.prototype.remove = Cookie_remove;	// Delete a cookie

//	========================================================================
