// This does some basic form validation.  It's in javascript because for one,
// we're not submitting to our own actions in some of this (Salesforce, for
// example), and it's not a useful path of attack for malicious users.

// addValidation takes an array of arrays; each of the inner arrays contains
// three elements:  the id of the field to validate, a regex to validate it,
// and the error field to make visible in the event of an error.
function addValidation(fields) {
	// The interface for this function is kind of odd.  It was designed to be
	// aspect-oriented and sit next to the fields that were to be validated,
	// but there were a couple of problems assigning to form.onsubmit; this
	// will be fixed later.
	return function() {
		var errorsp = false;
		fields.each(function(val, i) {
			var field = $(val[0]);
			var regex = val[1];
			var error = $(val[2]);
			var error_msg = val[3];

			if( field == null || typeof(field) == 'undefined') {
				return true;
			}

			if( field.value == null || field.value == '' || /Enter/i.test(field.value)) {
			  error.style.visibility = 'visible';
  			  errorsp = true;
			} else if(regex.test(field.value)) {
				error.style.visibility = 'hidden';
			} else {
				if( error_msg != null) {
				  error.innerHTML = error_msg;
				}
				error.style.visibility = 'visible';
				errorsp = true;
			}
			// $#%@#
			error.style.display = 'inline';
		});
		return !errorsp;
	}
}
