function getEbyId(id) {
	if (document.getElementById)
		return document.getElementById(id)
	else
		return document.all[id];
}

function numberToText(value) {

var numToTextTbl = new Array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");

	if ((value % 1 == 0) && (value >= 0) && (value < numToTextTbl.length))
		return numToTextTbl[value]
	else
		return value.toString();
	}


function dateToStr(theDate, dateFormat, cleanText) {

/*
	author		:	David Ameeti
	description	:	creates string of specified date in specified format
	last updated:	2002.06.11 - dsa - added "shortboth" format code
					2002.06.22 - dsa - added "7" code for timezone
					2002.08.31 - dsa - modified format "day" to allow for theDate
					(passed in) to be a single value [0..6 -- i.e., value from .getDay()]

	dateFormat legal values:
	full		= "dddd, mmmm dd, yyyy @ hh:mm ap"
	std			= "ddd, mmm dd, yyyy @ hh:mm ap"
	day			= "dddd"
	date,
	longdate	= "mmmm dd, yyyy"
	shortdate	= "mm/dd/yyyy"
	intldate	= "dd-mmm-yyyy"
	time,
	longtime	= "hh:mm:ss ap"			[civilian time]
	shorttime	= "hh:mm:ss" 			[00-23 hours]
	stdtime		= "hh:mm ap"
	shortboth	= "mm/dd/yyyy hh:mm:ss"
		** example date used in below examples: 01/04/2002
	other	=	"0"	= extend next format to full form ("00" = "&nbsp;")
				"1" = day of week (ddd: "Fri"/"Friday")
				"2" = month of year (mmmm: "Jan"/"January")
				"3" = month of year (mm: "1"/"01")
				"4" = day of month (dd: "4"/"04")
				"5" = year (yyyy: "02"/"2002")
				"6" = shorttime/longtime (see above)
				"7" = timezone (i.e., "GMT +/- hh00")
				"8" = day time (i.e., "morning", "afternoon", etc.)
				"@" = "@ " but only if time present within theDate
*/
var DOW = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var MOY = new Array("January", "February", "March", "April", "May", "June", "July",
					"August", "September", "October", "November", "December");
var space = cleanText ? " " : "&nbsp;";

	if (isNaN(theDate)) return "";
	var timeStr;

// If this routine is used as a Date prototype, the default toString() format should be Date's default
	if (!dateFormat) return theDate.toString();		// if no format specified, use system default

//	if (!dateFormat) dateFormat = "std";			// if no format specified, default to "std"

	if ((theDate >= 0) && (theDate < 7) && (dateFormat == "day"))
		theDate = new Date("4/" + (theDate + 7) + "/2002");
	var dateFormatLC = dateFormat.toLowerCase();
	var fullYear = theDate.getFullYear();
	if (fullYear < 200) theDate.setFullYear(fullYear + 1900);	// fix bug in getFullDate

	if (dateFormatLC == "full")
		return dateToStr(theDate,"01, 02 4, 05@06")
	else if (dateFormatLC == "std")
		return dateToStr(theDate,"1, 2004,0005") + dateToStr(theDate,"@") + dateToStr(theDate,"stdtime")
	else if (dateFormatLC == "day")
		return dateToStr(theDate,"01")
	else if ((dateFormatLC == "date") || (dateFormatLC == "longdate"))
		return dateToStr(theDate,"02 4, 05")
	else if (dateFormatLC == "shortdate")
		return dateToStr(theDate,"03/04/05")
	else if (dateFormatLC == "intldate")
		return dateToStr(theDate,"04-2-05")
	else if ((dateFormatLC == "time") || (dateFormatLC == "longtime")) {
		if ( (theDate.getHours() == 0) && (theDate.getMinutes() == 0) && (theDate.getSeconds() == 0) ) return "";
		timeStr = (theDate.getHours() - ((theDate.getHours() > 12) ? 12 : 0) + ((theDate.getHours() == 0) ? 12:0)) + ":";
		timeStr += ((theDate.getMinutes() < 10) ? "0":"") + theDate.getMinutes();
		if (theDate.getSeconds()) timeStr += ":" + ((theDate.getSeconds() < 10) ? "0":"") + theDate.getSeconds();
		timeStr += space + ( (theDate.getHours() < 12) ? "a.m.":"p.m.");
		return timeStr;
		}
	else if (dateFormatLC == "stdtime") {
		if ( (theDate.getHours() == 0) && (theDate.getMinutes() == 0) && (theDate.getSeconds() == 0) ) return "";
		timeStr = (theDate.getHours() - ((theDate.getHours() > 12) ? 12 : 0) + ((theDate.getHours() == 0) ? 12:0)) + ":";
		timeStr += ((theDate.getMinutes() < 10) ? "0":"") + theDate.getMinutes();
		timeStr += space + ( (theDate.getHours() < 12) ? "am":"pm");
		return timeStr;
		}
	else if (dateFormatLC == "shorttime") {
		if ( (theDate.getHours() == 0) && (theDate.getMinutes() == 0) && (theDate.getSeconds() == 0) ) return "";
		timeStr = ((theDate.getHours() == 0) ? 12 : theDate.getHours()) + ":";
		timeStr += ((theDate.getMinutes() < 10) ? "0":"") + theDate.getMinutes();
		if (theDate.getSeconds()) timeStr += ":" + ((theDate.getSeconds() < 10) ? "0":"") + theDate.getSeconds();
		return timeStr;
		}
	else if (dateFormatLC == "shortboth")
		return dateToStr(theDate,"shortdate") + " " + dateToStr(theDate,"shorttime")
	else {
		var dateStr = "";
		var validFormats = "012345678@";
		var longForm = false;
//		debugOut("Formating being used: &quot;" + dateFormat + "&quot;");
		while (dateFormat.length) {
			var field = dateFormat.charAt(0);
			if (field == "#") {				// escape next character
				dateStr += dateFormat.substr(1,1);	// so next ch is literal
				dateFormat = dateFormat.substr(2,dateFormat.length);
				continue;
				}
			else if (field == "0")		// format code following is long format
				if (longForm) { dateStr += space; longForm = false; }
				else longForm = true
			else if (field == "1")	// day of week
				if (longForm) dateStr += DOW[theDate.getDay()]
				else dateStr += DOW[theDate.getDay()].substr(0,3)
			else if	(field == "2")	// month of year
				if (longForm) dateStr += MOY[theDate.getMonth()]
				else dateStr += MOY[theDate.getMonth()].substr(0,3)
			else if (field == "3")	// month #
				if (longForm) dateStr += ( ((theDate.getMonth()+1) < 10) ? "0":"") + (theDate.getMonth()+1)
				else dateStr += (theDate.getMonth()+1)
			else if (field == "4")	// day #
				if (longForm) dateStr += ((theDate.getDate() < 10) ? "0":"") + theDate.getDate()
				else dateStr += theDate.getDate()
			else if (field == "5")	// year #
				if (longForm) dateStr += theDate.getFullYear()
				else dateStr += ( ((theDate.getFullYear() % 100) < 10) ? "0":"") + (theDate.getFullYear() % 100)
			else if (field == "6")	// time
				if (longForm) dateStr += dateToStr(theDate,"longtime")
				else dateStr += dateToStr(theDate,"shorttime")
			else if (field == "7")	{ // timezone
				tzOffset = theDate.getTimezoneOffset() / 60;
//					The below code would work IF the tzOffset returned wasn't adjusted for DST (but it is).
//					Anyone know how to determine if the computer thinks it's DST?
//				var tzStrings = new Array("GMT","","","","","ET","CT","MT","PT");
//				if ( (tzOffset <= (tzStrings[tzOffset].length + 1) ) && (tzStrings[tzOffset].length > 0) )
//					dateStr += tzStrings[tzOffset]
//				else
					dateStr += "GMT" + ((tzOffset >= 0) ? "+":"-") + ((Math.abs(tzOffset) < 10) ? "0":"") + Math.abs(tzOffset) + "00";
				}
			else if (field == "8")	// day time
				if (longForm) dateStr += "Good " + dateToStr(theDate,"8")
				else dateStr += ((theDate.getHours() < 12) ? "morning" : (theDate.getHours() > 17) ? "night" : "evening"); 
			else if (field == "@")	// "@ " if time has a non-12am value
				dateStr += (dateToStr(theDate,"time") ? " @" + space : "")
			else		// Anything not used is output as a literal
				if (validFormats.indexOf(field,0) == -1) dateStr += field;
			dateFormat = dateFormat.substr(1,dateFormat.length);
			if (field != "0") longForm = false;
		}
//		debugOut("created date string: &quot;" + dateStr + "&quot;");
		return dateStr;
	}
}

function dateRangeToStr(theFullDate, daysBeyond, cleanText) {
var alphaMonth = true;	// Month spelled out, or numeric format
var space = cleanText ? " " : "&nbsp;";

	function monthName(theMonth) {
	var shortMonth = true;	// 3 char month name, or all characters
	var monthOfYear = new Array("January", "February", "March", "April", "May","June", "July",
							"August", "September", "October", "November", "December");

		if (alphaMonth)
			return monthOfYear[theMonth].substr(0,shortMonth ? 3 : monthOfYear[theMonth].length)
		else
			return (theMonth < 9 ? "0" : "") + (theMonth + 1);
	}

	if (!theFullDate) return "";

	var theMonth = theFullDate.getMonth();
	var theDOM = theFullDate.getDate();
	var theHour = theFullDate.getHours();
	if (theHour != 0) {
		var theSuffix = theHour >= 12 ? "pm" : "am";
		theHour = theHour == 0 ? 12 : theHour > 12 ? theHour - 12 : theHour;
		var theMinute = theFullDate.getMinutes();
		var timeStr = space + "@" + space + theHour + ":" + (theMinute < 10 ? "0" : "") + theMinute + space + theSuffix;
	} else
		timeStr = "";

	if (!daysBeyond)
		var lastDate = theFullDate
	else
		lastDate = new Date(theFullDate.getTime() + (daysBeyond * 24*60*60*1000) );
	var theYear = lastDate.getFullYear();

	var dateStr = monthName(theMonth);
	if (alphaMonth)
		dateStr += space + theDOM
	else
		dateStr += "/" + (theDOM < 10 ? "0" : "") + theDOM

	if (theFullDate != lastDate) {
		var lastMonth = lastDate.getMonth();
		if (theFullDate.getFullYear() != theYear) dateStr += (alphaMonth ? "," + space : "/") + theFullDate.getFullYear();
		if (theFullDate != lastDate) dateStr += "-";
		if (lastMonth != theMonth) dateStr += monthName(lastMonth) + (alphaMonth ? space : "/");
		var lastDOM = lastDate.getDate();
		if (theDOM != lastDOM || theMonth != lastMonth) dateStr += (!alphaMonth ? (lastDate.getDate() < 10 ? "0" : "") : "") + lastDOM;
	}
	dateStr += alphaMonth ? ("," + space + theYear) : ("/" + theYear);

	return dateStr + (theFullDate == lastDate ? timeStr : "");
}


function getCookie(cookieName) {
	var cookies = document.cookie.split("; ");
	for (var i = 0; i < cookies.length; i++) {
		if (cookies[i].indexOf(cookieName) == 0)
			return cookies[i].substring(cookieName.length + 1);
	}
	return false;
}

function setCookie(cookieName, cookieValue, daysGood) {
var expiresStr, theDate;
// daysGood: number of days until cookie expires -or- actual date (in string format)
//		-- no value means 'temporary' (expires at end of session)
//
	if (daysGood) {
		if (typeof daysGood == "number") {
			theDate = new Date();
			theDate.setTime(theDate.getTime() + (daysGood * 24 * 60 * 60 * 1000) );
		} else		// should be a string or a date object
			theDate = new Date(daysGood);
		expiresStr = "; expires=" + theDate.toGMTString();
	} else
		expiresStr = "";
	document.cookie = cookieName + "=" + cookieValue + expiresStr;
}

function purgeCookie(cookieName) {
	document.cookie = cookieName + "=''; expires=" + new Date("01/01/1970").toGMTString();
}



function checkEmail(emailAddr) {

	var emailRE = new RegExp(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i);
	return emailRE.test(emailAddr);
}

function checkForm(theForm) {

/*

This routine will validate the form object passed in as the (only) argument.

It will verify that all text boxes have been filled in.
However, if the name of the field is prefixed with "qty", then the contents will
   *also* be verified to contain ONLY digits ('0'-'9', but not '-' or '.').
However, any field with "?" as the only contents will be valid, even if qty field.
However, if the name of the field is prefixed with "opt", then the contents will
   be considered optional and may be left blank (w/ no validation performed).

It will verify that all radio buttons have been set.
They will be given an alert message summarizing which rado buttons are unset.

If there is a text field named "email", the contents will be verified as
   a valid email address ("username@domain.tld"), empty not allowed.

Any text field that is considered invalid will have it's background color changed
to light red, indicating a field the user needs to correct.  They will also be
given an alert message summarizing which fields are incorrect.

Example:
	field type: text	field name: userName
	field must contain (any) text; e.g., "qwerty"

	field text: text	field name: email
	field must contain valid email address; e.g., "myname@mail.com"

	field type: text	field name: r.comments
	field may NOT be left blank

	field type: text	field name: qtyMembers
	field must contain text; digits only; e.g.,: "174" ("342.4" is invalid)
*/

function debugOut(msg) {
//	alert(msg);
}

var errorColor = "#ffbbbb";		// color when field is invalid

// debugInit(true);
debugOut("Start of form verification.");
	var nameArray = new Array();
	var theItem;

debugOut("Check to verify all radio buttons selected.");
	for (var i = 0; i < theForm.elements.length; i++) {		// check radio buttons
		theItem = theForm.elements[i];
		if (theItem.type == "radio") {				// look for radio buttons only
//			debugOut("checking radio button for question '" + theItem.name + "'");
			theItem.style.backgroundColor = "white";
			theItem.style.color = "black";			// set default color to black
			for (var j = 0; j < nameArray.length; j++)
				if (nameArray[j].name == theItem.name) break;	// name found in array
			if (j == nameArray.length) {			// scanned all entries in array
				nameArray[j] = new Object();		// add item to list of radio buttons
				nameArray[j].name = theItem.name;
				nameArray[j].checked = false;
				debugOut("Added new entry for '" + theItem.name + "' into nameArray.");
			} else									// button already in array
				debugOut("Entry for '" + nameArray[j].name + "' already present [" + j + "]; checked = '" + nameArray[j].checked + "'");
			if (theItem.checked) {
				nameArray[j].checked = true;
				nameArray[j].result = theItem.value;
			}
			debugOut("Status of radio button '" + nameArray[j].name + "' = " + nameArray[j].checked);
		}
	}

	var buttonsUnchecked = "";
	debugOut(nameArray.length + " radio buttons found.");
	for (var j = 0; j < nameArray.length; j++) {		// go thru list of found radio buttons...
//		debugOut("Checking question #" + j + " [" + nameArray[j].name + "]:  " + (nameArray[j].checked ? ("checked [" + nameArray[j].result + "]") : "unchecked") );
		if (!nameArray[j].checked) {					// ... and build error message for those unselected
			buttonsUnchecked += (buttonsUnchecked.length > 0 ? ", " : "") + "#" + (j+1);
			for (var k = 0; k < theForm.elements.length; k++)
				if (theForm.elements[k].name == nameArray[j].name)	// find all fields with that name...
					theForm.elements[k].style.color = errorColor;	// ... and change color to highlight
		}
	}

	debugOut("buttonsUnchecked = '" + buttonsUnchecked + "'");
	if (buttonsUnchecked.length > 0) {					// 
		alert("You must mark every question before submitting your information.\n\nButtons not checked: " + buttonsUnchecked);
		return false;
	}

// check (most) text field to verify fields filled in
	var boxesEmpty = "";
	var boxesInvalid = "";
	var boxNum = 0;
	for (var i = 0; i < theForm.elements.length; i++) {
		theItem = theForm.elements[i];
//		debugOut("Checking element #" + i + " of form (name/type = '" + theItem.name + "/" + theItem.type + "').");
//		if (theItem.type == "text" && theItem.type != "hidden") {				// search for text fields
		if (theItem.type == "text" || theItem.type == "hidden") {				// search for text or hidden fields
			boxNum++;
//			debugOut("Checking text box #" + boxNum);
			theItem.style.backgroundColor = "white";
			if ( (theItem.value.length == 0) && (theItem.name.indexOf("r_") == 0) ) {	// empty field?
				if ( (boxesEmpty.length == 0) && (boxesInvalid.length == 0) )
					theItem.focus();							// if first bad field, set focus on field
				boxesEmpty += (boxesEmpty.length > 0 ? ", " : "") + "#" + boxNum;
				debugOut("Text box #" + boxNum + " is empty.  Empty boxes so far are: " + boxesEmpty);
				theItem.style.backgroundColor = errorColor;		// highlight background
			} else if ( (theItem.name.indexOf("qty") == 0) && (theItem.value != '?') )	// qty field?
				for (var j = 0; j < theItem.value.length; j++) {
					var theChar = theItem.value.charAt(j);
					if ( (theChar < '0') || (theChar > '9') ) {
						debugOut("text box #" + boxNum + "contains non-numeric characters.");
						if ( (boxesEmpty.length == 0) && (boxesInvalid.length == 0) )
							theItem.focus();
						theItem.style.backgroundColor = errorColor;		// highlight background
						boxesInvalid += (boxesInvalid.length > 0 ? ", " : "") + "#" + boxNum;
						break;		// drop out of loop once we find one invalid character
					}
				}
		}
	}

// check email address (if defined *and* text) for validity
	var emailValid = true;

	if (emailValid && (typeof theForm.email != "undefined") && (theForm.email.type == "text") ) {
		emailValid = (theForm.email.value.length == 0) ? true : checkEmail(theForm.email.value);
//		theForm.email.style.backgroundColor = (emailValid ? "white" : errorColor);
		if (!emailValid) {
			theForm.email.style.backgroundColor = errorColor;
			theForm.email.focus();
		}
		debugOut("email address valid: " + emailValid);
	}

	if (emailValid && (typeof theForm.r_email != "undefined") && (theForm.r_email.type == "text") ) {
		emailValid = checkEmail(theForm.r_email.value);
//		theForm.r_email.style.backgroundColor = (emailValid ? "white" : errorColor);
		if (!emailValid) {
			theForm.r_email.style.backgroundColor = errorColor;
			theForm.r_email.focus();
		}
		debugOut("email address valid: " + emailValid);
	}
	if (!emailValid) alert("Invalid email address entered.  Please correct and retry.  Thank you.");


	if ( (boxesEmpty.length > 0) || (boxesInvalid.length > 0) ) {
//		debugOut("Warning user of incomplete text boxes.");

//		alert("Some of the entries " + (boxesEmpty.length ? "have not been filled in [entries: " + boxesEmpty + "]" : "") + ((boxesEmpty.length > 0) && (boxesInvalid.length > 0) ? " or " : "") + (boxesInvalid.length ? "have invalid quantities [entries: " + boxesInvalid + "]" : "") + ".  Please correct your information and retry your submission.  Thank you.");
		alert("Some of the entries " + (boxesEmpty.length ? "have not been filled in" : "") + ((boxesEmpty.length > 0) && (boxesInvalid.length > 0) ? " or " : "") + (boxesInvalid.length ? "have invalid quantities [entries" : "") + ".  Please correct your information and retry your submission.  Thank you.");

		return false;
	}

	return emailValid;		// if we get this far, only status not acted upon is email
}


function rot13 (a) {
// The rot13 of the given string.  The &#nn;, &lt;, &gt;, and &amp; entities are also expanded
     
	if (!rot13map) rot13map = rot13init();
	var s = "";
	var i;
	var entity = "";
	for (i = 0; i < a.length; i++) {
		var b = a.charAt(i);
		if (entity) {
			entity += b;
 			if (b == ';') {
				if (entity == "&lt;") s += "<";
				else if (entity == "&gt;") s += ">";
				else if (entity == "&amp;") s += "&";
				else {
					var matches = entity.match(rot13numericEntityRE);
 					if (matches && matches[0])
						s+= String.fromCharCode(matches[0]);
				}
 				entity = "";
			}
		} else if (b == '&')
			entity = "&";
		else
			s += (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' ? rot13map[b] : b);
	}
 
	return s;
}

var rot13map = null;
var rot13numericEntityRE = /&#(.*);/;
 
function rot13init() {
	var map = new Array();
	var s = "abcdefghijklmnopqrstuvwxyz";
	var i;
	for (i = 0; i < s.length; i++)
		map[s.charAt(i)] = s.charAt((i + 13) % 26);
	for (i = 0; i < s.length; i++)
		map[s.charAt(i).toUpperCase()] = s.charAt((i + 13) % 26).toUpperCase();

	return map;
}
