// cookie_functions.js

// call this script using the following include:
/* ********************* snip below this line *********************
<SCRIPT TYPE="text/javascript"
	SRC="http://integrativemedicine.arizona.edu/snippets/cookie_functions.js" 
	CHARSET="ISO-8859-1">
	<!-- version 1.1b April 05, 2000
		// defines function getCookie(name)
		// defines function getCookieNames()
		// defines function setCookie(name, value, expires, path, domain, secure)
		// defines function delCookie (name,path,domain)
		// defines function cookies_work()
	// -->
</SCRIPT>
********************* snip above this line ********************* */


// source: http://builder.cnet.com/Programming/Kahn/111997/cookieFncs.html
// optionally use the Cookie Cutter at: http://builder.cnet.com/Programming/Kahn/111997/toolcc.html
// some cookie references:
	// http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm#1003254
	// http://www.cookiecentral.com/
	// http://www.devguru.com/index.asp?page=/Technologies/ecmascript/quickref/doc_cookie.html
// some enhancements made by Shawn Seley

// from Netscapes JavaScript Cookies information:
// Cookies have these limitations:
	// 300 total cookies in the cookie file. 
	// 4 Kbytes per cookie, for the sum of both the cookie's name and value. 
	// 20 cookies per server or domain (completely specified hosts and domains are treated a
		// separate entities and have a 20-cookie limitation for each, not combined). 


function getCookie(name){
	// Heinle's function for retrieving a cookie.
	// returns the value of the specific cookie element you are getting
	var cname = name + "=";
	var dc = document.cookie;
	if (dc.length > 0) {
		begin = dc.indexOf(cname);
		if (begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1){
				end = dc.length;
			}
			return unescape(dc.substring(begin, end));
		}
	}
	return null;
}


function getCookieNames(){
	// written by Shawn Seley
	// returns an array containing all cookie names available to this document
	var cookie_names = new Array;
	var split_cookie = "";
	var all_cookies = new String(document.cookie);
	var whole_cookies = all_cookies.split("; ");
	for(i=0; i<whole_cookies.length; i++) {
		split_cookie = whole_cookies[i].split("=");
		cookie_names[i] = unescape(split_cookie[0]);
	}
	return cookie_names;
}


function setCookie(name, value, expires, path, domain, secure) {
	// An adaptation of Dorcht's function for setting a cookie.
	// sets a specified cookie name/value pair with other parameters
	// automatically escapes illegal characters in "name" and "value"
	// expires defaults to the current Navigator session
	// "/" is the most general "path", defaults to the specifying page only
	// "domain" default's to current site's domain
	// "secure" is for SSL, defaults to false
	// only name and value must be specified, don't even define rest to default
	// if need to define parameters after a null parameter, then use "null" (no quotes)
	document.cookie = escape(name) + "=" + escape(value) + 
	// syntax explaination:  ((evaluate) ? (if true do this) : (if false do this)
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
	((path == null) ? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}


function delCookie (name,path,domain) {
	// modified by Shawn Seley on April 05, 2000
		// changed the expiration date to "now" instead of a very old and possibly
		// non Y2K compliant static date
	// An adaptation of Dorcht's function for deleting a cookie.
	// deletes a specified cookie
	// "/" is the most general "path", defaults to the specifying page only
	// "domain" defaults to current site's domain
	// only "name" must be specified, don't even define rest to default
	// if need to define parameters after a null parameter, then use "null" (no quotes)
	now = new Date();
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path == null) ? "" : "; path=" + path) +
		((domain == null) ? "" : "; domain=" + domain) +
		"; " + now.toGMTString();
	}
}

function cookies_work() {
	// written by Shawn Seley
	// modified (fixed) on Tuesday, August 29, 2000 10:37:32 AM
	// returns true if browser is actively accepting and returning cookies
	
	// delete the test cookie to make sure isn't already there
	delCookie ("cookie_test","/");
	
	// set the test cookie
	setCookie('cookie_test', 'cookies are working!', null, '/');

	//get the test cookie
	cookie_result = getCookie('cookie_test');
	
	// delete the test cookie again to clean up (only 20 allowed per site)
	delCookie ("cookie_test","/");
	
	// return true if cookie_result was returned properly, else return false
	if (cookie_result == "cookies are working!") {
		return true;
	} else {
		return false;
	}
}