// basic javascript functions and validation


/** prints the properties of an obj */
function debugObj(obj, filter) {
	try {
		var props = ""
		for(ndx in obj) {
			if (filter == null || filter.exec(ndx)) {
				if (typeof(obj[ndx]) == "function") {
					props += ndx + "= function ";
				}
				else {
					props += ndx + "=" + obj[ndx] + " ";
				}
			}
		}
		alert(obj + ":\n" + props)
	}
	catch (e) {
		//alert("Exception caught in debugObj:" + e);
		for(ndx in obj) {
			if (filter == null || filter.exec(ndx)) {
				props += ndx + " ";
			}
		}
		alert("Error found, only printing property names... \n" + obj + ":\n" + props)
	}
}

// for use of setValidationClasses
var useClasses = true
var validClass = 'valid'
var errorClass = 'error'

/**
 * Attempts to set the focus to the first element in the first form on the doc
 */
function setFirstFocus(idx) {
	if (idx == null) {
		idx = 0; // to skip the first few ISU forms
	}
	
	//alert("Starting Set First Focus!! (" + idx + ")");
	obj = null;
	
	try {
		for(el=0; el< document.forms[idx].elements.length; el++) {
			obj = document.forms[idx].elements[el];		
			if (document.forms[idx].elements[el].type.toUpperCase() != "HIDDEN") {	            
				//alert("Yes! " + obj + " " + obj.name);
				document.forms[idx].elements[el].focus();
				document.forms[idx].elements[el].select();
				break;
			}
			else {
				//alert("Not: " + obj + " " + obj.name);
			}
		}
	}
	catch(ex) {
		//alert("startFirstFocus() encountered a problem: \n" + ex)
		//debugObj(obj);
		//alert(ex);
	}
}

/** shows the strength of the password in HTML */
function showPasswordStrength(pass, show_id) {
	try {
	
		// start with a weak password level
		var strength = 1;
		var message = "";
			
		// test: wild charaters
		if (/[^a-z]/.exec(pass)) {
			strength += 2;
		}
		else {
			message = "Try putting in unique charaters or numbers ($#@!)";
		}
	
		// test: 6 chars or more
		if (pass.length < 6) {
			strength = 0;
			message = "You must have at least six characters";
		}
		
		var text = "";
		switch(strength) {
			case 3: text = "Good password"; break;
			case 2: text = "Average password: "; break;
			case 0: text = "Invalid password: "; break;
			case 1: 
			default:text = "Weak password: ";
		}
		message = text + message;
		
		// set the message
		var obj = document.getElementById(show_id);
		if (strength < 3) {
			obj.title = message;
			obj.style.visibility = "visible";
			obj.style.display = "inline";
		}
		else {
			obj.style.visibility = "hidden";
			obj.style.display = "none";
		}
	}
	catch (e) {
		alert ("showPasswordStrength: ".e);
	}
}

/** makes the class of input fields change when an error is found
 * the original class will be reapplied before revalidation */
function setValidationClasses(original,error) {
	useClasses = true
	validClass = original
	errorClass = error
}

/** hides the error message of an input control */ 
function clearError(id) {
	try {
		msg = document.getElementById(id)
		if (useClasses) {
			msg.className = validClass
		}
		else {
			msg.style.visibility = "hidden"
			msg.style.display = "none"
		}
	}
	catch (ex) {
		//alert("clearError(`"+id+"`) encountered a problem: \n" + ex)
	}
}

/** hides the error message of an input control */ 
function setError(id) {
	try {
		msg = document.getElementById(id)
		if (useClasses) {
			msg.className = errorClass
		}
		else {
			msg.style.visibility = "visible";	
			msg.style.display = "inline";
		}
	}
	catch (ex) {
		alert("setError(`"+id+"`) encountered a problem: \n" + ex)
	}
}

/** validates a field that can't be left empty */ 
function validate(id, regex, hidden) {
	var valid = true;
	
	try {
		var control = document.getElementById(id)
		if (! regex.exec(control.value)) {
			setError(hidden)
			if (useClasses) {
				control.className = errorClass
			}
			valid = false;
		}
		else {
			// clear any old remaining errors
			clearError(hidden)
			if (useClasses) {
				control.className = validClass
			}
		}
	}
	catch (ex) {
		if (control == null) {
			//debug:
			alert("the control \""+id+"\" dosen't exist!");
		}
		else {
			alert("validate encountered an error:\n" + ex + "\nControl is: " + control)
		}
		valid = true;//false;
	}
	return valid;
}

/** validates a field that can't be left empty */ 
function validate_custom(id, test, hidden) {
	var valid = true;

	try {
		var control = document.getElementById(id)
		//alert("test result: " + test);
		if (! test) {
			setError(hidden)
			if (useClasses) {
				control.className = errorClass
			}
			valid = false;
		}
		else {
			// clear any old remaining errors
			clearError(hidden)
			if (useClasses) {
				control.className = validClass
			}
		}
	}
	catch (ex) {
		if (control == null) {
			//debug:
			alert("the control \""+id+"\" dosen't exist!");
		}
		else {
			alert("validate encountered an error:\n" + ex + "\nControl is: " + control)
		}
		valid = true;//false;
	}
	return valid;
}

/** sets this control as valid */ 
function unvalidate(id, hidden) {
	var valid = true;
	
	try {
		var control = document.getElementById(id)

		// clear any old remaining errors
		clearError(hidden)
		if (useClasses) {
			control.className = validClass
		}
	}
	catch (ex) {
		if (control == null) {
			//debug:
			alert("the control \""+id+"\" dosen't exist!");
		}
		else {
			alert("validate encountered an error:\n" + ex + "\nControl is: " + control)
		}
		valid = true;//false;
	}
	return valid;
}


