// Set a cookie (expires is in minutes).
function set_cookie(name, value, expires, path, domain, secure)
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());

	if (expires)
	{
		// Expires in minutes.
		expires = expires * 1000 * 60;
	}

	var expires_date = new Date(today.getTime() + (expires));

	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "") + 
		((path) ? ";path=" + path : "") + 
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
}

// Retrieves the value of a cookie if it exists, returns false otherwise.
function get_cookie(name)
{
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;

	if ((!start) && (name != document.cookie.substring(0, name.length)))
	{
		// No such cookie.
		return null;
	}

	// No such cookie.
	if (-1 == start) { return null; }

	var end = document.cookie.indexOf(";", len);
	if (-1 == end) { end = document.cookie.length; }

	// Return cookie value.
	return unescape( document.cookie.substring(len, end ));
}

// Removes a cookie from browser.
// Note: This does not always remove the cookie on all browsers. Some
// browsers such as IE and Opera requires user to restart before the
// cookie entry is totally removed.
function delete_cookie(name, path, domain)
{
	// If there is such cookie.
	if (get_cookie(name))
	{
		document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
