/*
	Name of library:		functions_cookies.js
	Version:				0.1
	Created on:				2005/07/06
	Created by:				Jth
	Last modified on:		xx
	Last modified by:		xx
*/

/*	cookie_write function v0.1
	It constructs the cookie string with the correct format for using the method document.cookie; it has two uses:
		a) to write a cookie
		b) to delete a cookie, using an expiration date which already expired
	Syntax: cookie_write (COOKIE_NAME, COOKIE_VALUE, EXPIRES_DATE, PATH, DOMAIN, SECURE_FLAG)
	Parameters:
		COOKIE_NAME		string, name of the cookie
		COOKIE_VALUE	string, value of the cookie
		EXPIRES_DATE	JavaScript datetime, if not specified the cookie expires when the user's session ends
		PATH			string, path from which the cookie is valid
		DOMAIN			string, domain from which the cookie is valid
		SECURE_FLAG		boolean, secure parameter of the cookie
	Examples:
		var example_expires = new Date (new Date ().getYear (), 11, 31, 23, 59, 59, 999);
		cookie_write ("my_cookie", "cookie write example", example_expires, "", "", 0)
		cookie_write ("my_cookie", "cookie write example 2", example_expires, "/subdir", "domain.com", true)
		cookie_write ("my_cookie", "cookie write example 2", example_expires, "/subdir2", "", 1)	*/
	function cookie_write (name, value, expires, path, domain, secure)
	{
		var newcookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure==true) ? "; secure" : "");
		document.cookie = newcookie;
	}
/*	end of cookie_write function	*/

/*	cookie_read function v0.1
	It parses the string returned by document.cookie to finds an specific cookie value.
	Sintax: cookie_read (COOKIE_NAME)
	Ex.: var example_prod = cookie_read ("prod");	*/
	function cookie_read (name)
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1)
		{
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		}
		else begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	}
/*	end of cookie_read function	*/

/*	cookie_exists function v0.1
	It returns a boolean value acording to the existance of a cookie.
	Sintax: cookie_exists (COOKIE_NAME)
	Ex.: if (cookie_exists ("prod")) ...	*/
	function cookie_exists (name)
	{
		if (cookie_read (name) != null) return true;
		else return false;
	}
/*	end of cookie_exists function	*/

/*	cookies_enabled function v0.1
	Returns a boolean value acording to the writing and reading cookies capabilities of the browser.
	Sintax: cookies_enabled ()
	Ex.: if (cookies_enabled ()) ...	*/
	function cookies_enabled ()
	{
		// Set a new value to test
		var value = new Date();
		// Try to write the cookie and return true of false according to the cookie reading results
		cookie_write ("test_123456", value, "", "/", "", false);
		return (cookie_exists ("test_123456")) ? ( (cookie_read ("test_123456") == value) ? (true) : (false) ) : (false);
	}
/*	end of cookies_enabled function	*/

/*	cookie_write_seconds function v0.1
	A simple function to write a cookie and set how long, in seconds, it will last.
	Ex.: <body onUnload='cookie_write_seconds ("refreshCountdown", "anyvalue", 10);'>	*/
	function cookie_write_seconds (name, value, seconds)
	{
		var expirationTime = new Date ();
		expirationTime = new Date (expirationTime.getTime () + 1000*seconds);
		cookie_write (name, value, expirationTime, "", "", 0);
	}
/*	end of cookie_write_seconds function	*/
