/**
 * Make sure the required fields are filled in.
 *
 */

var theContactForm = $('contact');
theContactForm.observe('submit', function (ev) {
	var errors = Array();
	
	// Check the email
	var theEmailInput = theContactForm.getInputs('text','email_input')[0];
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
	if (!emailPattern.test(theEmailInput.value)) {
		errors.push("Your email address is invalid.");
	}
	
	// Check Name
	var fullnameInput = theContactForm.getInputs('text','fullname_input')[0];
	if (!fullnameInput.value.length) {
		errors.push("You need to enter a name.");
	}
	
	// Check Category
	var theCategorySelect = $('contactCategorySelect');
	if (theCategorySelect.value == '-1') {
		errors.push("You need to select a category.");
	}
	
	// Check Message
	var theMessageInput = $('contactNotesArea').value;
	if (!theMessageInput.length) {
		errors.push('You did not enter a message.');
	}
	
	// Show errors
	if (errors.length) {
		ev.stop();
		var alertMessage = "Please correct these errors: \n";
		for (var i =0;i<errors.length;i++) {
			alertMessage = alertMessage+errors[i]+"\n";
		}
		alert(alertMessage);
	}
});