//**********************************************************************
// file: 	cookies.js
// author:	Claude M. Landry
// Date:	Jan 2008
//
// Inspired from many sources including:
//		http://www.netspade.com/media/cookies.js
//		http://www.echoecho.com/jscookies02.htm
//		
//**********************************************************************


//**********************************************************************
// Sets a Cookie with the given name and value.
//
// name       Name of the cookie
// value      Value of the cookie
// [expires]  Expiration date of the cookie suing a Date() object
//              (default: end of current session)
// [path]     Path where the cookie is valid (default: path of calling document)
// [domain]   Domain where the cookie is valid
//              (default: domain of calling document)
// [secure]   Boolean value indicating if the cookie transmission requires a
//              secure transmission
//**********************************************************************
function setCookie(name, value, expires, path, domain, secure)
{
	// Note the date is converted to Greenwich Mean time using the "toGMTstring()" function
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

//**********************************************************************
// Gets the value of the specified cookie.
//
// name  Name of the desired cookie.
//
// Returns a string containing value of specified cookie,
//   or null if cookie does not exist.
//**********************************************************************
function getCookie(name) 
{
	// First we check to see if there is a cookie stored.
	// Otherwise the length of document.cookie would be zero.
	if (document.cookie.length > 0) {
		// Second we check to see if the cookie's name is stored in the
		// "document.cookie" object for the page.

		// Since more than one cookie can be set on a
		// single page it is possible that our cookie
		// is not present, even though the "document.cookie" object
		// is not just an empty text.
		// If our cookie name is not present the value -1 is stored
		// in the variable called "begin".
		begin = document.cookie.indexOf(name+"=");
		if (begin != -1) {
			// Our cookie was set.
			// The value stored in the cookie is returned from the function.

			begin += name.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		}
	}
	// Our cookie was not set.
	// The value "null" is returned from the function.
	return null;
}

//**********************************************************************
// Deletes the specified cookie.
//
// name      name of the cookie
// [path]    path of the cookie (must be same as path used to create cookie)
// [domain]  domain of the cookie (must be same as domain used to create cookie)
//**********************************************************************
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//**********************************************************************
// Dumps all the cookies
//
//**********************************************************************
function dumpCookies()
{
	list = new Array();
	list = document.cookie.split("; ");
	var x;
	var name;
	var valut;
	for( x in list ) {
		name = list[x].split("=")[0];
		value = list[x].split("=")[1];
		document.write("cookie["+name+"]="+value+"<br \>");
	}
}