// ************************************************************************************************
// *** Utility class to read and write cookies
// ************************************************************************************************

// This object manages cookies
function CookieManager(cookieName, expirationDate, domain) {
	// ************************************************************************************************
	// *** Interface
	// ************************************************************************************************	

	// Fields
	// ******
	this.cookieName = cookieName;
	this.expirationDate = expirationDate;
	this.domain = domain;
	
	// Methods
	// *******
	this.getValue = cm_getValue;
	this.setValue = cm_setValue;
	this.eraseValue = cm_eraseValue;
	
	// ************************************************************************************************
	// *** Methods
	// ************************************************************************************************	
	
	// Return a cookie value (null if key is not found in the cookie)
	function cm_getValue(key) {
		var i;
		var result;

		result = document.cookie.match(key + '=([^;&]*)'); // value will be in the position 1 (first group)

		return (result != null) ? result[1] : null;
	}
	
	// Set a cookie value (create the key/value pair if it is not found in the cookie)
	function cm_setValue(key, val) {
		var result = '';
		var expiration = this.expirationDate == null ? '' : 'expires=' + this.expirationDate.toGMTString() + ';';

		// Read all cookie string (the cookie value will be at array postition 1)
		result = document.cookie.match(this.cookieName + '=([^;]*)');
		if(result == null)
			result = "";
		else
			result = result[1];			

		// if key already exists in the cookie, replace its value
		if(result.indexOf(key) > -1)
			result = result.replace(new RegExp(key + '=[^&]*'), key + '=' + val);
		else // If key does not exist int the cookie, put it
			result += ((result != '') ? '&' : '') + key + '=' + val;
			
		// write the cookie
		document.cookie = this.cookieName + '=' + result + ';domain=' + this.domain + ';path=/;' + expiration;
	}
	function cm_eraseValue(key, val)
	{
		var result = '';
		var expiration = this.expirationDate == null ? '' : 'expires=' + this.expirationDate.toGMTString() + ';';

		// Read all cookie string (the cookie value will be at array postition 1)
		result = document.cookie.match(this.cookieName + '=([^;]*)');
		if(result == null)
			result = "";
		else
			result = result[1];			

		// if key already exists in the cookie, replace its value
		if(result.indexOf(key) > -1)
			result = result.replace(new RegExp(key + '=[^&]*'), key + '=' + val);
		else // If key does not exist int the cookie, put it
			result += ((result != '') ? '&' : '') + key + '=' + val;
			
		// write the cookie
		document.cookie = this.cookieName + '=' + result + ';domain=' + this.domain + ';path=/;' + expiration;
	}
}