/*utilities.js*/
//easier than typing document.getElement so much
//can be made custom to handle multiple browsers
function get(element) {
	if(document.getElementById) {
		if(document.getElementById(element)) {
			return document.getElementById(element);
		} else {
			alert("get() called with invalid elementId");
			return false;
		}
	} else {
		alert("browser doesn't support \"get\" method.");
	}
}

//function clearMatchValue
//method for clearing out a value from an element if the value matches
//what's passed in
//depends on:  element being present, message value passed with call
function clearMatchValue(elementId, message) {
	if(message != "" && get(elementId) != false) {
		if(get(elementId).value == message) {
			get(elementId).value = "";
		}
	}
}


//function for showing elements
function show(elementId) {
	if(get(elementId)) {
		get(elementId).style.display = "block";
	} else {
		alert("show() called with invalid elementId: " + elementId);
	}
}

//function for hiding elements
function hide(elementId) {
	if(get(elementId)) {
		get(elementId).style.display = "none";
	} else {
		alert("hide() called with invalid elementId: " + elementId);
	}
}

//function for showing element as inline
function showInline(elementId) {
	if(get(elementId)) {
		get(elementId).style.display = "inline";
	} else {
		alert("showInline() called with invalid elementId: " + elementId);
	}
}

//function for showing/hiding/inline display of an element
function showHide(displayType, elementId) {
	if(get(elementId) && displayType!= "") {
		if(displayType == "show" || displayType == "block") {
			show(elementId);
		} else if(displayType == "hide" || displayType == "hidden" || displayType == "none") {
			hide(elementId);			
		} else if(displayType == "inline" || displayType == "straight") {
			showInline(elementId);
		} else {
			alert("showHide() called with invalid displayType:" + displayType);
		}
	} else if(displayType == ""){
		alert("showHide() called with null displayType");
	} else {
		alert("showHide() called with invalid elementId");
	}
}

//function to show or hide a list of elements
function showHideList(displayType,listArray) {

	var displayTypes = {
		"show":"show",
		"block":"block",
		"hide":"hide",
		"hidden":"hidden",
		"none":"none",
		"inline":"inline",
		"straight":"straight"
	};

	var displayMethod;

	//make sure listArray is a list
	if(listArray.join) {
		if(displayType != "") {
			if(displayType in displayTypes) {
				//pick the right method
				if(displayType == "show" || displayType == "block") {
					displayMethod = show;
				} else if(displayType == "hide" || displayType == "hidden" || displayType == "none") {
					displayMethod = hide;
				} else if(displayType == "inline" || displayType == "straight") {
					displayMethod = showInline;
				}
				
				//do the right thing to all of the elements
				for(i = 0; i < listArray.length; i++) {
					displayMethod(listArray[i]);
				}
			} else {
				alert("showHideList() called with invalid displayType: " + displayType);
			}
		} else {
			alert("showHideList called with null displayType");
		}
	} else {
		alert("showHideList called with a non-array list");
	}
}

//utility method to show a list
function showList(listArray) {
	showHideList("show",listArray);
}

//utility method to hide a list
function hideList(listArray) {
	showHideList("hide",listArray);
}

//utility method to show a list inline
function showInlineList(listArray) {
	showHideList("inline",listArray);
}


//utility to find a character in a testValue
//sends back an array
function findChar(testValue,locateChar) {
	var returnArray = new Array();
	var currentIndex = 0;
	if (locateChar != "") {
		if(testValue != "") {
			for(i = 0; i < testValue.length; i++) {
				if(testValue.charAt(i) == locateChar) {
					returnArray[currentIndex++] = i;
				}
			}
		} else {
			alert("findChar() called with null testValue");
		}
	} else {
		alert("findChar() called with invalid locateChar");
	}
	
	return returnArray;
}

function flashBorder(elementId, borderWidth, borderStyle, color1, color2, times, interval) {

	var defaultTimes = new Number(4);

	//use a real element
	if(!elementId) {
		alert("flashBorder() called with null elementId");
	} else if(get(elementId) == false) {
		alert("flashBorder() called with invalid element");
	}
	
	//set borderWidth, borderStyle to passed in, then default to set value, then default to "1px"
	borderWidth = borderWidth ? borderWidth : (get(elementId).style.borderWidth != "") ? get(elementId).style.borderWidth : "1px";
	borderStyle = borderStyle ? borderStyle : (get(elementId).style.borderStyle) ? get(elementId).style.borderStyle : "solid";

	get(elementId).style.borderWidth = borderWidth;
	get(elementId).style.borderStyle = borderStyle;

	//borderColor
	color1 = color1 ? color1 : (get(elementId).style.borderColor != "") ? get(elementId).style.borderColor : "red";
	color2 = color2 ? color2 : "white";

	get(elementId).style.borderColor = color1;

	//flash interval
	interval = interval ? interval : "200";

	//flash decrementer
	times = !isNaN(times) ? (times < 0 ? defaultTimes : times) : defaultTimes;


	//flash if flashTimes remain
	if(times > 0) {
		setTimeout("flashBorder('" + elementId +"','" + borderWidth + "','" + borderStyle + "','" + color2 + "','" + color1 +"'," + (times-1) + "," + interval + ");",interval);
	}
}