/* See Wagner p. 447.  Cookies are the mechanism for conveying information
from one html page to another, or from one browser window to another.
By default, cookies are deleted when the browser session
ends.
*/

function putCookie(cname, cval)
{

	document.cookie = cname + "=" + escape(cval);

	// escape() prevents semicolons, quotes, etc from wrecking syntax.
	// This = APPENDS, never replaces the document's cookie.
	// By default, cookie is deleted when browser is shut down.
}

/*
Date.getTime() returns milliseconds since the epoch.
Add to this one year's milliseconds 52*7*24*60*60*1000 = 31449600000.
and put it in cookie date format with cookieDate();

expires=Wednesday, 09-Nov-99 23:12:40 GMT

The cookie date format is the same as the date returned by toGMTString, with the following
exceptions:
     Dashes are added between the day, month, and year. 
     The year is a 2-digit value for cookies. 
However according to Wagner, .toGMTString() format is also accepted.
The trick is to cast the variable into the date format with
var x = new Date();
This gives it methods such as .getTime(), .setTime(), and .toGMTString.

*/

function putCookie_year(cname, cval)
{
	var d1 = new Date();
	one_year = 31449600000;
	d1.setTime(d1.getTime() + one_year);

	document.cookie = cname + "=" + escape(cval) + "; expires=" + d1.toGMTString();
}

function getCookie(cname)
{
	var result = null;
	var raw = " " + document.cookie + ";";
	var toget = " " + cname + "=";
	var s0 = raw.indexOf(toget);
	var s1;

	if (s0 != -1)
	{
		s0 += toget.length // skip cname
		s1 = raw.indexOf(";", s0);
		result = unescape(raw.substring(s0, s1));
	}
	return result;
}

function killCookie(cname)
{
	var d1 = new Date();
	one_year = 31449600000;
	d1.setTime(d1.getTime() - one_year);

	document.cookie = cname + "=ciao" + "; expires=" + d1.toGMTString();
}

function show_cookies()
{
//	alert(document.cookie);
	var raw = " " + document.cookie + ";";
	var s0 = 0;
	var i = 0;
	var m = "Cookies for this document:\n";
	while (s1 == raw.indexOf(";", s0))
	{
		if (s1 == -1)
		{
			m += "Trail: \"" + unescape(raw.substring(s0)) + "\"";
			break;
		}
		m += i++ + " ";
		m += unescape(raw.substring(s0, s1));
		s0 = s1 + 1;
		m += "\n";
	}
	alert(m);
}

function are_cookies_on()
{
//	if (confirm("Blank cookies?"))
//		document.cookie = ""; // this just ADDS nothing to the existing cookies

//	var c = getCookie("xyzzy");
//	alert("getCookie(\"xyzzy\") returns \"" + c + "\""); // null
//	alert(c == null); // true

	var c = getCookie("preferences");
//alert("getCookie(\"preferences\") returns \"" + c + "\"");

	// if cookies are on, and PE has been used before, return true.
	if (c != "" & c != null)
		return true;

	// no or blank preferences set previously -- can we set and get a cookie?
	putCookie("testcookie", "ok"); // expires at end of session
	c = getCookie("testcookie");

	if (c != "ok")
		return(false);
	else return(true);
}


