//Modified 2004-07-22: minor changes to code formatting and function names, and addition of the cookiesEnabled() function.

/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/browser_detection.php
Version 1.0.0
Last Update: 30 May 2004

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
*/

//returns the value of a cookie, if it exists
function getCookie(name)
{
	var nameStart	= document.cookie.indexOf(name + "=" );
	var nameEnd		= nameStart + name.length + 1;
	if (nameStart == -1) return null;
	if (!nameStart && name != document.cookie.substring(0, name.length)) return null;

	var valueEnd = document.cookie.indexOf(';', nameEnd);
	if (valueEnd == -1) valueEnd = document.cookie.length;
	return unescape(document.cookie.substring(nameEnd, valueEnd));
}

//set the value of a cookie
function setCookie(name, value, expires, path, domain, secure)
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());

	if (expires)
	{
		expires = expires * 1000 * 60 * 60 * 24;
		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" : "");
	return true;
}

// this deletes the specified cookie
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		return true;
	}
	return false;
}

//returns true if cookies have been enabled, false otherwise
function cookiesEnabled()
{
	var name = 'cookies_enabled';
	var value = 'Some functionality of this website requires cookies to be accepted.';
	setCookie(name, value, '', '/');
	//be tidy and unset it again once we're finished
	if (getCookie(name)) { deleteCookie(name, '/'); return true; }
	else return false;
}
