/* 	common.js 
	A container for global, common functions not limited to a particular subject area
	Current index of functions:
		cookiesEnabled() 	- returns true or false to test to see if cookies are enabled in the browser
		parseCookies() 		- returns an associative array of cookies indexed by name
		nullCookie(cookie) 	- given a cookie, nulls the value (sets it to blank) 
		setCookie(name, value)	- given name and value pair, set a cookie
		twocharMonth(month)	- given a numeric month, return a two character month (with a leading 0 if necessary)
		twocharDay(day)		- given a numberic day, return a two character day

	External Files
		None
*/

function cookiesEnabled() {
/* Set a cookie to see if they are enabled */
	document.cookie = "cookiesenabled=yes; path=/";
	parseCookies();
	if (cookies["cookiesenabled"] == "yes") {
		return true;
	} else {
		return false;
	}
}

function parseCookies() {
/* returns an associative array of cookies indexed as cookies["name"] = "value" */
	var name, value, beginning, middle, end;

	for (name in cookies) {				//get rid of old stuff
		cookies = new Object();
		break;
	}
	beginning = 0;					//start at the beginning
	while (beginning < document.cookie.length) {
		middle = document.cookie.indexOf('=', beginning);	//find next =
		end = document.cookie.indexOf(';', beginning);		//find next ;
		if (end == -1) 				//if no ; then its the last cookie
			end = document.cookie.length;
		if ((middle > end) || middle == -1) {
			//if cookie has no value
			name = document.cookie.substring(beginning, end);
			value = '';
		} else {
			//extract the value
			name = document.cookie.substring(beginning, middle);
			value = document.cookie.substring(middle+1, end);
		}
		cookies[name] = unescape(value);	//add to associative array
		beginning = end +2;			//step over space to start next cookie
	}
}

function nullCookie(cookieName)  {
/* Set the value for the cookie to null */
	document.cookie = cookieName+'=; path=/';
//	document.cookie = cookieName+"=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT"	//deletes the valued cookie
//	document.cookie = cookieName+"; expires=Thu, 01-Jan-1970 00:00:01 GMT"		//deletes the unvalued cookie
	return;
}

function setCookie(name, value) {
/* Used to pass state information across windows and page changes
   At this time, only used for transferring anchor position hash: #Other for the iframe in PresMsg page
   Session cookie only, no expiration date set, cookie deleted after browser session closed
*/
	if (cookiesEnabled()) {
		document.cookie = name+value+"; path=/";
	}
}

function twocharMonth(nummonth) {
/* Given a numeric month return a 2 char string with a leading "0" if from 1-9 */
	var strmonth = new String();

	if ((nummonth) && (typeof nummonth == "number")) {
		if (nummonth < 10) {
			strmonth = '0'+nummonth.toString();
		} else {
			strmonth = nummonth.toString();
		}
	} else {
		alert("Invalid Month passed: "+nummonth+" is a "+(typeof nummonth)+" - \(twocharMonth\)");
		return false;
	}
	return (strmonth);
}

function twocharDay(numday) {
/* Given a numeric day return a 2 char string with a leading "0" if from 1-9 */
	var strday = new String();

	if ((numday) && (typeof numday == "number")) {
		if (numday < 10) {
			strday = '0'+numday.toString();
		} else {
			strday = numday.toString();
		}
	} else {
		alert("Invalid Day passed: "+numday+" is a "+(typeof numday)+" - \(twocharDay\)");
		return false;
	}
	return (strday);
}