var arrFieldNames = new Array("name", "email", "message");
var arrFieldTitles = new Array("Name", "E-Mail Address", "Your Message");
var formName, formEmail, formiMessage;
var objError; // will be set as object for error message in body
var boolError = false; // on initial rendering, don't want to look for this object in formatForm function

// Set up fields for each type of form -- either hidden or viisible, or different text
function formatForm() {
	// alert("begin formatting");
	// reset error message and all fields to white background
	if (boolError == true) {
		objError.className = "hidden";
	}
	for (i = 0; i < document.feedback.elements.length; i++) {
		var tmpField = document.feedback.elements[i];
		if (tmpField.type == "text" || tmpField.type == "textarea") {
			tmpField.className = "formField";
		}
	}
}

function validateForm () {
	objError = document.getElementById("error");
	var strInvalidFields = "Please correctly fill in the highlighted fields:<br />";
	// alert("begin validating");
	// reset arrays
	formName = document.feedback.name;
	formEmail = document.feedback.email;
	formMessage = document.feedback.message;
	// set up array to validate required fields used by all form type
	var arrValAll = new Array(formName, formEmail, formMessage);
	// set up arrays to validate additional required fields per form type
	var arrValAdd = new Array(); // array for additional required fields
	var arrInvalid = new Array(); // array for invalid fields
	for (u = 0; u < arrValAll.length; u++) {
		// alert(arrValAll[u].value);
		if (arrValAll[u].value == "") { // if empty, add to invalid array and hilite background of field
			arrInvalid[arrInvalid.length] = arrValAll[u];
			arrValAll[u].style.background = "#E9B974";
		} else if (arrValAll[u].name == "email") {
                	// it's the email field and it has a value
                	if (!isValidEmail(arrValAll[u].value)) {
                    		arrInvalid[arrInvalid.length] = "Email Address is invalid";
                    		arrValAll[u].style.background = "#E9B974";
			}
            	}
	}

	// compare invalid form fields with non-technical names of fields
	for (x = 0; x < arrInvalid.length; x++) {
		// alert(arrInvalid[x].name);
		for (f = 0; f < arrFieldNames.length; f++) {
			if (arrInvalid[x].name == arrFieldNames[f]) {
				arrInvalid[x] = arrFieldTitles[f]; // change name of field in invalid array
				// alert(arrFieldTitles[f]);
			}
		}
	}
	// alert(arrInvalid.length);
	if (arrInvalid.length == 0) {
		// alert("valid");
		return true ;
	} else {
		strInvalidFields += "<span class=\"smallBold\">";
		for (g = 0; g < arrInvalid.length; g++) { // writes out list of fields for error box
			strInvalidFields += arrInvalid[g] + "<br />";
		}
		strInvalidFields += "</span>";
		var objMsg = document.getElementById("error_msg");
		objMsg.innerHTML = strInvalidFields;
		objError.className = "visible";
	        scrollToTop();
		// alert(arrInvalid);
		// objError.className = "visible";
        	// scrollToTop();
		return false;
	}
	return false;
}


function isValidEmail(email) {
    var result = true;
    if (email != null && email.length > 0) {
        var emailRegxp = /^.+@.+\..{2,4}$/;
        result = emailRegxp.test(email);
    }
    return result;
}


// used to scroll to show the error msg
function scrollToTop() {
    window.scrollTo(0, 0);
}
