	/*
	 * Cookie-handling functions. (Last revision: 111206)
	 * Some algorithms from <http://www.quirksmode.org/js/cookies.html>.
	 *
	 * string findcookie(name);
	 * void writecookie(name, key, value);
	 * string readcookie(name, key);
	 *
	 * findcookie()
	 *   Treat as private function, private to this library; do not directly call this function!
	 *   Returns a string representing the value stored in the cookie named "name".
	 * writecookie()
	 *   Write or update "key=value" pair in the stored value of the cookie named "name".  "value"
	 *   should not contain commas or semicolons, but it's possible they do not pose a threat due to
	 *   built-in encoding.  This remains to be tested.
	 * readcookie()
	 *   Returns a string representing the value of the "key=value" pair with the specified "key"
	 *   stored in the cookie named "name".  Note the difference in behavior between this function
	 *   and the corresponding function in the cookies.php library.
	 *
	 */
	
	function findcookie(name) { //search document.cookie; return value of specified cookie or null if not found
		var cookies = document.cookie.split(";");
		for (var i = 0; i < cookies.length; i ++) {
			var curcookie = cookies[i];
			if (curcookie.indexOf(name + "=") == 0) {
				return curcookie.substring(name.length + 1, curcookie.length);
				}
			}
		return null;
		}
	
	function writecookie(name, key, value) { //write "key=value" into value string of cookie named "name"
		var newcookieval = "";
		var date = new Date();
		date.setTime(date.getTime() + (90*24*60*60*1000));
		var curcookie = findcookie(name);
		if (curcookie == null) { //cookie does not exist; set it
			newcookieval = key + "=" + value;
			}
		else { //cookie exists; find and replace key or append "key=value" if not found
//decodeURIComponent on IE requires IE5.5 or later!
			curcookie = decodeURIComponent(curcookie);
			var flagexists = false;
			var items = curcookie.split(",");
			for (var i = 0; i < items.length; i ++) {
				var curitem = items[i];
				if (curitem.indexOf(key + "=") == 0) { //"key=oldvalue" found
					flagexists = true;
					newcookieval += (key + "=" + value + ",");
					}
				else {
					newcookieval += (curitem + ",");
					}
				}
			if (flagexists == false) { //"key=oldvalue" was not found, so append "key=value"
				newcookieval += (key + "=" + value);
				}
			else { //there's a comma at the end of newcookieval that doesn't need to be there
				newcookieval = newcookieval.substring(0, newcookieval.length - 1);
				}
			}
//encodeURIComponent on IE requires IE5.5 or later!
		document.cookie = "userprefs=" + encodeURIComponent(newcookieval) + "; expires=" + date.toGMTString() + "; path=/";
		}
	
	function readcookie(name, key) { //note difference from readcookie() in our PHP library!
			//search cookie named "name" for key; return associated value or null if not found
		var curcookie = findcookie(name);
		if (curcookie == null) { //the cookie doesn't even exist
			return null;
			}
//decodeURIComponent on IE requires IE5.5 or later!
		curcookie = decodeURIComponent(curcookie);
		//search the cookie value for the specified key
		var items = curcookie.split(",");
		for (var i = 0; i < items.length; i ++) {
			var curitem = items[i];
			if (curitem.indexOf(key + "=") == 0) {
				return curitem.substring(key.length + 1, curitem.length);
				}
			}
		return null;
		}
