// Client-Side Validation of the Papers Submission Page

var VALIDATE = []; // map of validation values

var default_msg = "Invalid Characters";
var alphanumeric = /^([a-zA-Z0-9\-]+ *)+$/;
// files types allowed
var types_allowed = /.pdf$|.doc$|.docx$|.txt$/i;

VALIDATE["name_regex"] = alphanumeric;
VALIDATE["name_msg"] = default_msg;

VALIDATE["email_regex"] = /^[\w\.%\-]+@[\w.\-]+\.[a-zA-Z]{2,4}$/;
VALIDATE["email_msg"] = "E-mail must be in the form: user@domain.com.";

VALIDATE["school_regex"] = alphanumeric;
VALIDATE["school_msg"] = default_msg;

VALIDATE["committee_regex"] = alphanumeric;
VALIDATE["committee_msg"] = default_msg;

VALIDATE["country_regex"] = alphanumeric;
VALIDATE["country_msg"] = default_msg;


VALIDATE["papers_regex"] = types_allowed;
VALIDATE["papers_msg"] = "Need to upload a file for Topic 1";

VALIDATE["agree-to-code_msg"] = "Must agree to the Code of Conduct!"

// Start Here!
document.observe("dom:loaded", function() {
	
	$("papers_submit").observe("submit", validate);
	$$("#papers_submit input").invoke("observe", "change", checkThisField);
	$$("#papers_submit select").invoke("observe", "change", checkThisField);
});

// makes sure the form is good before submitting
function validate(event) {

	var valid = true;
	
	// check that they filled in 10 country spots
	$$("#papers_submit select").each(
		function(piece) {
			if(piece.value == "") {
				valid = false;
				showError(piece, "Missing Information");
			} else {
				clearError(piece);
			}
		});

	// go through all the input fields and check them
	$$("#papers_submit input").each(
		function(piece) {
			if(!checkField(piece)) {
				valid = false;
			}
		});
	
	// if all is good, submit!
	
	var form = $("papers_submit");
	if(valid) {
		clearError($("submit"));
		// let the form submit		
	} else {
		// stop submission
		Event.stop(event);	
		showError($("submit"),
			"There have been some errors. Please look over your form, fix them, and try again. If there are any problems, contact the administrator.");
	}
}

function checkThisField(event) {
	checkField(this);
}

// validates each input field after it is filled in
// immediately shows error if the input value is bad
// returns true if valid, false if invalid
function checkField(field) {

	// this is for the code of conduct box	
	if(field.type == "checkbox") {
		if(field.checked) {
			clearError(field);
			return true;
		} else {
			var msg = VALIDATE[field.name + "_msg"];
			showError(field, msg);
			return false;
		}
	}	
	
	// check all non submit/reset/checkbox input values
	if(field.type != "submit" && field.type != "reset" && field.type != "checkbox") {
		if(!field.value.match(VALIDATE[field.name + "_regex"]) && field.value != "") {
			// show error only if it hasn't been shown before
			var msg = VALIDATE[field.name + "_msg"];
			showError(field, msg);
			return false;
		} else {
			clearError(field);
			return true;
		}
	} else {
		return true;
	}
}

// shows that the given field has an error and the associated message
function showError(field, message) {
	if(!field.hasClassName("inputBad")) {
		field.addClassName("inputBad");
		var errorMsg = document.createElement("span");
		errorMsg.textContent = message;
		errorMsg.addClassName("errorMsg");
		field.parentNode.appendChild(errorMsg);
	// when missing info has been filled in but does not validate, just change the message
	} else if (field.hasClassName("inputBad") && field.next().textContent == "Missing information") {
		field.next().textContent = message;
	}
}

// the field has been fixed and should no longer show an error
function clearError(field) {
	if(field.hasClassName("inputBad")) {
		field.removeClassName("inputBad");
		var last = field.parentNode.lastChild;
		if(last.hasClassName("errorMsg")) {
			field.parentNode.removeChild(last);
		}
	}
}
