/*  Javascript Library
*	Forms
*	
*	@Author Jebb Burditt
*	Date: May 2, 2007
*/

function toggle(n, id1, id2, id3, id4) {
	if (n == "true") {
		getElem(id1).style.display = 'block';
		if (id2) getElem(id2).style.display = 'block';
		if (id3) getElem(id3).style.display = 'block';
		if (id4) getElem(id4).style.display = 'block';
	} else {
		getElem(id1).style.display = 'none';
		if (id2) getElem(id2).style.display = 'none';
		if (id3) getElem(id3).style.display = 'none';
		if (id4) getElem(id4).style.display = 'none';
	}
}

// set a form controls value
function setValue(id, value, opt1){
	getElem(id).value = value;	
}

function checkText(id) {
	var val = getElem(id).value;
	if (val.length <= 0) {
		getElem(id).style.borderColor = "red";
		return "Please fill out " + writeID(id) + "\n";
	}
	getElem(id).style.borderColor = "";
	return "";
}

function checkRadio(id) {
	if (!getElem(id+"1").checked && !getElem(id+"2").checked) {
		getElem(id+"1").style.backgroundColor = "red";
		getElem(id+"2").style.backgroundColor = "red";
		return "Please choose a " + writeID(id) + "\n";
	}
	getElem(id+"1").style.backgroundColor = "";
	getElem(id+"2").style.backgroundColor = "";
	return "";
}

function checkRadio2(id) {
	if (!getElem(id+"1").checked && !getElem(id+"2").checked && !getElem(id+"3").checked) {
		getElem(id+"1").style.backgroundColor = "red";
		getElem(id+"2").style.backgroundColor = "red";
		getElem(id+"3").style.backgroundColor = "red";
		return "Please choose a " + writeID(id) + "\n";
	}
	getElem(id+"1").style.backgroundColor = "";
	getElem(id+"2").style.backgroundColor = "";
	getElem(id+"3").style.backgroundColor = "";
	return "";
}

function checkDropdown(id) {
	if (getElem(id).value == "-1") {
		getElem(id).style.backgroundColor = "red";
		return "Please select a " + writeID(id) + "\n";
	} else {
		getElem(id).style.backgroundColor = "";
	}
	return "";
}

function writeID(id) {
	id = id.replace(/_/g," ");
	return id;
}

//we will find that element no matter what... 
function getElem(id) { 
	if (document.all) { 
		return document.all[id]; 
	} else if (document.getElementById) { 
		return document.getElementById(id); 
	} else { 
		for (i = 1; i < document.layers.length; i++) { 
			if(document.layers[i].id == id) 
				return document.layers[i]; 
		}       
	}
	if (eval("document."+id)) {
		return eval("document."+id);	
	}
	return Null; 
}

function submitForm(id, procedure, action) {
	var proc = getElem('procType');
	if (procedure.length > 0 && proc)
		proc.value = procedure;
	var n = getElem(id);
	if (n) {
		if (action && action.length > 0) n.action = action;
		n.submit();
	}
}

//disable enter key (copy the commented lines of code into the page)
//document.onkeypress = disableEnter;
//if (document.layers) document.captureEvents(Event.KEYPRESS);	
function disableEnter(e) {
	var pK = e ? e.which : window.event.keyCode;
	return pK != 13;
}

function isNumeric(n) {
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	for (i = 0; i < n.length && IsNumber == true; i++) {
		Char = n.charAt(i);
		if (ValidChars.indexOf(Char) == -1) 
			IsNumber = false;
	}
	return IsNumber;
}