/*
* name = Required parameter. String value that does not include any of the following 
*				characters: semi-colon, comma, white space, and the "@" character. Non-string 
*				values will be converted to a string.
* duration = Integer value that defines how many hours the cookie should persist for. If 
*						this parameter isn't specified, a session cookie is created.
* path = String value specifying the path that the cookie is in scope for. If this 
*				parameter isn't specified, the cookie is only available for the directory that 
*				it was created in, and any of its subdirectories. The most common value for this 
*				parameter is "/", which specifies the root of the 
*				website. Giving the cookie root scope makes it available globally throughout 
*				your website.
* domain = String value. This parameter is only necessary 
*					for sites that run multiple sub domains.
* secure = Boolean value. If this parameter is "true", the cookie 
*					created will only be transmitted under Secure Sockets Layer (SSL).
*/

function Cookie(name, hours, path, domain, secure) 
{
	this.$document = document;  // Javascript "document" object
	this.$name     = name;

	this.$expiration = hours ? new Date((new Date()).getTime() + hours * 3600000) : null;

	this.$path   = path   ? path    : null;
	this.$domain = domain ? domaine : null;
	this.$secure = secure ? secure  : null;

	// To see the value of cookie
	this.Debug = function Debug() 
	{
		this.$document.writeln("Debug Cookie Name: " + this.$name + "<br><br>");
		
		for (var prop in this) 
		{
			if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
				continue;
			this.$document.writeln(unescape(prop + " = " + this[prop] + "<br>"));
		}
	}

	// Delete the cookie
	this.Delete = function Delete() 
	{
		// Set the minimum date to make this expired
		this.$document.cookie = this.$name + "=" + ";expires=Fri, 02-Jan-1970 00:00:00 GMT;";
	}

	// Read data and set properties
	this.Read = function Read() 
	{
		var arr;
		var arrValues;
		var start, end, i;
		var allCookies = unescape(this.$document.cookie);
		var buffer = "";

		if (allCookies == "" || allCookies.indexOf(this.$name) == -1)
			return false;

		start = allCookies.indexOf(this.$name) + (this.$name.length + 1);
		end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;

		arr = allCookies.substring(start, end).split("&");

		if (arr != "")
		{
			for (i = 0; i < arr.length; ++i) 
			{
				arrValues = arr[i].split(":");
				
				buffer = "this." + arrValues[0] + " = ";
				
				if (isNaN(arrValues[1]) && arrValues[1].indexOf("%2C") == -1)
					buffer += "'";
				
				buffer += (arrValues[1].indexOf("%2C") != -1 ? 
					"new Array(" + unescape(arrValues[1]) + ")" :
					unescape(arrValues[1]));
					
				if (isNaN(arrValues[1]) && arrValues[1].indexOf("%2C") == -1)
					buffer += "';";
				else
					buffer += ";";
					
				eval(buffer);
			}
		}

		return true;
	}

	// Save a new cookie if does not exist
	this.Save = function Save() 
	{
		var strCookie = "";

		// ****************************************************
		// Get cookie's properties to store in a string
		// ****************************************************
		for (var prop in this) 
		{
			// ignore properties with names that beg
			// in with '$' and also methods
			if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
				continue;
			if (strCookie != "") strCookie += '&';
			strCookie += prop + ':' + this[prop];
		}
		// ****************************************************

		// ****************************************************
		// Save data in the cookie
		// ****************************************************
		strCookie = this.$name + '=' + escape(strCookie);
		if (this.$expiration) strCookie += ';expires=' + this.$expiration.toGMTString();
		if (this.$path)       strCookie += ';path='    + this.$path;
		if (this.$domain)     strCookie += ';domain='  + this.$domain;
		if (this.$secure)     strCookie += ';secure='  + this.$secure;
		// ****************************************************

		// ****************************************************
		// Save cookie
		// ****************************************************
		this.$document.cookie = strCookie;
		// ****************************************************
	}
}