/* Functions for validating various types of form elements */
// if an empty string, returns true (treat a string with spaces as empty)
function isEmpty(theField) {
    var valueString = String(theField.value)
	var x = 0;
	for (x=0; x < valueString.length; x++) {
		if(valueString.charAt(x) != ' ')
		return false;
	}
	return true;
}

// if not a valid email format, returns true
function badEmail(theEmail) {
    var emailAddr = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
    if (eval(theEmail.value.search(emailAddr)==-1)) {
        return true;
    }
    else
        return false;
}

// if 2 input fields don't match, returns true (use for confirm email, etc.)
function compareFields(the_field_name1,the_field_name2) 
{
    var field1 = the_field_name1.value;
    var field2 = the_field_name2.value;
    if (field1 != field2) {
        return true;
    }
    else
        return false;
}

// if radio button or series of checkboxes has not been checked, returns true
function radioNotChecked(theField) {
	myOption = -1;
	for (i=theField.length-1; i > -1; i--) {
		if (theField[i].checked) {
			myOption = i; i = -1;
		}
	}
	if (myOption == -1) {
		return true;
	}
	else
		return false;
}

// if a checkbox is not checked, returns true
function checkboxNotChecked(theField) {
	if (theField.checked) {
		return false;
	}
	else
		return true;
}

// if nothing selected in a pulldown menu, returns true
function dropDownNotSelected(theField)  {
	var myindex=theField.selectedIndex;
	if (myindex==0) {
		return true;
	}
	else {
		return false;
	}
}


// check if the value is an integer
function isInteger(the_value)
{
    var lnValue = the_value + "";	
    
    if (isEmpty(lnValue == ""))
		return false;

    for (var i = 0; i < lnValue.length; i++)
	{
    	if (lnValue.charAt(i) < "0" || lnValue.charAt(i) > "9")
    		return false;
	}
	
    return true;
}

/* Functions to create the error messaging */
// create an empty array
var errorFields = [];

// generate a ul of error fields
function appendToErrorList(errorFields) {
	for (var i=0; i<errorFields.length; i++) {
		var msg = document.getElementById(errorFields[i]).getAttribute("errorMsg");
		var ul = document.getElementById("formErrorList");
		var li = document.createElement("li");
		var litext = document.createTextNode(msg);
		li.appendChild(litext);
		ul.appendChild(li);
	}
}

// set error field class to error
function highlightErrorField(errorFields) {
	for (var i=0; i<errorFields.length; i++) {
		theParent = document.getElementById(errorFields[i]).parentNode;
		theParent.className = "error";
	}
}

// set error fields class to nothing
function resetErrorField(errorFields) { 
	for (var i=0; i<errorFields.length; i++) {
		theParent = document.getElementById(errorFields[i]).parentNode;
		theParent.className = "";
	}
}

// run all the error functions
function showError() {
	highlightErrorField(errorFields);
	show("formErrorMsg");
	appendToErrorList(errorFields);
}

// resests form to have 0 errors so that errors do not accumulate
function removeErrors() {
	resetErrorField(errorFields);
	for (i=0; i<errorFields.length; i++) {
		var child = document.getElementById("formErrorList").lastChild;
		var parent = document.getElementById("formErrorList");
		parent.removeChild(child);
	}
	hide("formErrorMsg");
	errorFields = [];
	return errorFields;
}

/* check for errors. if errors, do error messaging */
function validateForm() {
	removeErrors();
	lookForErrors()
	if (errorFields.length > 0) {
		showError(errorFields);
		scroll(0,0);
		return false;
	}
	else {
		return true;
	}
}

