// for testing, set debugMode to true to catch and alert errors in DHTML functions
// for live site, set debugMode to false to suppress JS errors
var debugMode = true;

// DETERMINE BROWSER AND PLATFORM
//
// set the browser type
// all boolean
// 	isNS
//	isIE
//	isMac
//	isIE5
// 	isIE4
//  isIE6
//	isNS4
// 	isNS6
//  isDOM : whether you can use W3C DOM (ie5/6 and NS 6)
var appName = navigator.appName;
var appVersion = navigator.appVersion;
var appCodeName = navigator.appCodeName;
var userAgent = navigator.userAgent;
var isMac = appVersion.indexOf('Macintosh');
isMac = (isMac > -1) ? true : false;
var isIE5 = appVersion.indexOf('MSIE 5');
isIE5 = (isIE5 > -1) ? true : false;
var isIE6 = appVersion.indexOf('MSIE 6');
isIE6 = (isIE6 > -1) ? true : false;
var isIE4 = appVersion.indexOf('MSIE 4');
isIE4 = (isIE4 > -1) ? true : false;
var isIE = appVersion.indexOf('MSIE');
var isIE = (isIE > -1) ? true : false;
var nsIndex = appName.indexOf('Netscape');
var isNS = (nsIndex > -1) ? true : false;
var isNS4 = appVersion.indexOf('4');
isNS4 = ((isNS4 > -1) && isNS) ? true : false;
var isNS6 = appVersion.indexOf('5');
isNS6 = ((isNS6 > -1) && isNS) ? true : false;
var isDOM = ((isNS6 || isIE5)|| isIE6) ? true : false;
var dispVal = isIE ? "block" : "table-row";

//  PAGE LOADING SCRIPT

// authors can set multiple window.onload functions without
// stepping on other onload scripts or putting anything into
// the body tag using setOnLoadScripts()

var onLoadScriptArray = new Array();

function setOnLoadScript(a) {
	onLoadScriptArray[onLoadScriptArray.length] = a;
}

function initPageScripts() {
	for (var i=0; i<onLoadScriptArray.length; i++) {
		eval(onLoadScriptArray[i]);
	}
}

window.onload = initPageScripts;



// SELECTBOX LIST OPTION MANIPULATION FUNCTIONS

// removes all options in a list
//
// listoptions:  the object reference to the list to be cleared
function clearList(listoptions){
	if (listoptions.length >0){
		for (x=listoptions.length; x >= 0; x--){
	 		listoptions.remove(x);
		}
	}
}

// adds an option to a specified list
// olist: the list object to add the option to (eg: document.formName.selectName)
// txt: the displayed text for the option
// val: the actual value of the option
// def: (true/false) is this option the default selected option (the form resets to the default selection)
// sel: (true/false) is this option the currently selected option (what is currently selected in the list)

function addoption(olist,txt,val,def,sel){
	olist.options[olist.length] = new Option(txt,val,def,sel)
}

// Creates a list of days of the month.
// lst: the list object which the options are to be created in (eg: document.formName.selectName)
// month/year: the month and year desired
// defdate: the default day you need to be selected (1 if omitted)
function createListDays(lst,month, year, defdate){
	var numDays = getDays(year, month);
	clearList(lst);
	for (x=1; x <= numDays; x++){
	 if (typeof defdate != "undefined"){
	 	if(x == defdate){
			addoption(lst,x,x,true,true);
		}
		else{
			addoption(lst,x,x,false,false);
		}
	 }
	 else{
		if(x == 1){
			addoption(lst,x,x,true,true);
		}
		else{
			addoption(lst,x,x,false,false);
		}
	 }
	}
}

//	MISC. FUNCTIONS (DATE,TIME,STATES)

// returns true if the year is a leap year
// returns false otherwise
function isLeapYear(year){
	if(year % 4 == 0){
		if(year % 100 == 0){
			if(year %400 == 0){
				return true;
			}
			else{return false}
		}
		return true;
	}
	else{
		return false;
	}
}

var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

// returns the number of days in a given month and year
function getDays(year, month){
	var days;
	if (isLeapYear(year)){
		days = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	}
	else{
	    days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	}
	return (days[month-1]);
}

var statesLong = new Array("Alabama","Alaska","Arizona","Arkansas","California",
"Colorado","Connecticut","Delaware","Florida","Georgia"," Hawaii","Idaho","Illinois",
"Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland"," Massachusetts",
"Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon",
"Pennsylvania","Rhode Island","South Carolina"," South Dakota","Tennessee","Texas","Utah","Vermont",
"Virginia","Washington","West Virginia","Wisconsin","Wyoming");

var statesShort = new Array("AL","AK","AZ","AR","CA","CO","CN","DE","FL","GA","HI","ID","IL","IN","IO",
"KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH",
"OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY");

// print out an options list of all of the states
// Len is required, accepted values are l for long, s for short, and m for mixed which will
// print  the long name for what is shown but the short name for the value passed
// if state is defined (must be defined by the states abbreviation IN CAPS) that state will be set as the default selected state
function stateOptions(len,state){
	var disp;
	var val;
	var def = -1;
	switch(len){
		case "l":
			disp=statesLong;
			val=statesLong;
			break;
		case "s":
			disp = statesShort;
			val = statesShort;
			break;
		case "m":
			disp = statesLong;
			val = statesShort;
			break;
		default:
			alert("invalid argument in stateOptions()");
			return;
	}
	if(typeof state != "undefined"){
		for (x=0; x<50; x++){
			if(state == statesShort[x]){
				def = x;
			}
		}
	}			
	for (x=0; x<50; x++){
		if (def == x){
			document.write('<option value=\"'+val[x]+'\" defaultselected selected>'+disp[x]+'</option>\n');
		}
		else{
			document.write('<option value=\"'+val[x]+'\">'+disp[x]+'</option>\n');
		}
	}
}

// returns true/false if string str contains only alphabetic (a-z,A-Z) or numeric (0-9) characters
function isAlphaNumeric(str){
	var lowAlpha = "abcdefghijklmnopqrstuvwxyz";
	var upAlpha = lowAlpha.toUpperCase();
	var numeric = "0123456789";
	for(x=0;x<str.length;x++){
		if(lowAlpha.indexOf(str.charAt(x)) == -1){
			if(upAlpha.indexOf(str.charAt(x)) == -1){
				if(numeric.indexOf(str.charAt(x)) == -1){
					return false;
				}
			}
		}
	}
	return true;
}

// returns true/false if string str contains only numeric (0-9) characters
function isNumeric(str){

	var numeric = "0123456789";
	for(x=0;x<str.length;x++){
		if(numeric.indexOf(str.charAt(x)) == -1){
			return false;
		}
	}
	return true;
}

// returns true/false if string str contains only alphabetic (a-z,A-Z) characters
function isAlpha(str){
 	var lowAlpha = "abcdefghijklmnopqrstuvwxyz";
	var upAlpha = lowAlpha.toUpperCase();
	for(x=0;x<str.length;x++){
		if(lowAlpha.indexOf(str.charAt(x)) == -1){
			if(upAlpha.indexOf(str.charAt(x)) == -1){
				return false;
			}
		}
	}
	return true;
}

// IMAGE MANIPULATION AND LOADING FUNCTIONS

// changes an image within a document base to a new url
function swap(name, source){
	try{
		document.images[name].src = source;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: swap \n Location: tools.js");
		}
	}
}

// preloads images for rollovers etc
// required variables
// Name: must be an array of the images to be loaded
//
// optional variables
// width, height: may either be single numbers or an array that matches the needs for the images in Name.  
// root: path to the images directory, defaults to ""
function loadImage(name, width, height, root){
	imagesToLoad = new Array(name.length);
	var w;
	var h;
	var r = "";
	if (typeof root != "undefined"){
		r = root;
	}
	
	for (var i = 0; i < imagesToLoad.length ; i++){
		if (typeof width != "undefined"){
			if (typeof width =="object"){
				w = width[i];
			}
			else{ w = width;}
		}
		else {w = "n";}
		if (typeof height != "undefined"){
			if (typeof height == "object"){
				h = height[i];
			}
			else {h = height};
		}
		else {h = "n"}
		if ((w != "n") && (h != "n")){
			imagesToLoad[i] = new Image(w,h);
		}
		else {imagesToLoad[i] = new Image();}
		imagesToLoad[i].src = r + name[i];
	}
}


//  General DHTML functions.  All require setobj()
//

// sets the object string to be used
function setobj(obj){
	var theobject; 
	if (typeof obj == "string"){
		if (obj == "document"){
			theobject = document;
		}
		else{
			theobject = document.getElementById(obj);
		}
	}
	else{
		theobject = obj;
	}
	return theobject;
}

//sets the cursor for an object
function setCursor(name, cstyle){
	var obj = setobj(name);
	try{
		obj.style.cursor = cstyle;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setCursor \n Location: tools.js");
		}
	}
}

// GENERAL STYLE ELEMENTS

// sets the stylesheet text
// equivelent to style="stylesheet:declarations;" in a tag
// styleText: stylesheet:declarations; stylesheet:declarations;
function setStyleText(name, styleText){
	var obj = setobj(name);
	try{
		obj.style.cssText = styleText;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setStyleText \n Location: tools.js");
		}
	}
}

function getStyleText(name){
	var obj = setobj(name);
	var csTxt;
	try{
		csTxt = obj.style.cssText;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: getStyleText \n Location: tools.js");
		}
	}
	return csTxt;
}

// sets the current Stylesheet class
function setClass(name,clsName){
	var obj = setobj(name);
	try{
		obj.className = clsName;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setClass \n Location: tools.js");
		}
	}
}

// returns the current Stylesheet class
function getSClass(name){
	var obj = setobj(name);
	var clsNm;
	try{
		clsNm = obj.className;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: getSClass \n Location: tools.js");
		}
	}
	return clsNm;
}

// CONTENT MANIPULATION

// redefine the content of an element
function setInnerHTML(name,content){
	var obj = setobj(name);
	try{
		obj.innerHTML = content;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setInnerHTML \n Location: tools.js");
		}
	}
}

function getInnerHTML(name){
	var obj = setobj(name);
	var iHtm;
	try{
		iHtm = obj.innerHTML;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: getInnerHTML \n Location: tools.js");
		}
	}
	return iHtm;
}
//  sets the color of an element (not the background color)
// values are in hex: #rrggbb
// or rgb color: rgb(0-255,0-255,0-255)
function setColor(name,value){
	var obj = setobj(name);
	try{
		obj.style.color = value;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setColor \n Location: tools.js");
		}
	}
}


// sets the display type for an item (none = hidden)
function setDisplay(name,dtype){
	var obj = setobj(name);	
	try{
		obj.style.display = dtype;	
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setDisplay \n Location: tools.js");
		}
	}
}

function getDisplay(name){
	var obj = setobj(name);
	var disp;
	try{
		disp = obj.style.display;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: getDisplay \n Location: tools.js");
		}
	}
	return disp
}
// sets the background color for the layer
function setBGColor(name,color){
	var obj = setobj(name);
	try{
		obj.style.backgroundColor = color;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setBGColor \n Location: tools.js");
		}
	}
}
// sets the background image of a layer
function setBGImage(name,loc){
	var obj = setobj(name);
	try{
		obj.style.backgroundImage = 'url('+loc+')';
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setBGImage \n Location: tools.js");
		}
	}
}

// gets the width of a non-size-specified item
function getWidth(name){
	var obj = setobj(name);
	var wid;
	try{
		wid = obj.offsetWidth;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: getWidth \n Location: tools.js");
		}
	}
	return wid
}

// gets the height of a non-size-specified item
function getHeight(name){
	obj = setobj(name);
	var hgt;
	try{
		hgt = obj.offsetHeight;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: getHeight \n Location: tools.js");
		}
	}
	return hgt
}

// sets the width of a layer
function setWidth(name, x){
	var obj = setobj(name);
	try{
		obj.style.width = x;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setWidth \n Location: tools.js");
		}
	}
}

// sets the height of a layer
function setHeight(name, x){
	var obj = setobj(name);
	try{
		obj.style.height = x;
	}
	catch(e){
		if(debugMode == true){
			alert("Error: "+e+"\n Name: "+name+"\n Function: setHeight \n Location: tools.js");
		}
	}
}