

//
// Form Validation
//


function checkEmail(str){
	var errorString = "";			
	var emailFilter = /^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(str))) { 
		errorString = "\n - Email address is INVALID.";
	}
	var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (str.match(illegalChars)) {
		errorString = "\n - Email address contains ILLEGAL characters.";			
	}
	return errorString;
}

function checkForNumbers(str){
	var numberFilter = /^[a-z A-Z]+$/;
	if(!(numberFilter.test(str))){
		return true;
	}
	else{
		return false;	
	}
}
	
function checkForLetters(str){
	var letterFilter = 	/^[0-9\ ]+$/;
	if(!(letterFilter.test(str))){
		return true;
	}
	else{
		return false;	
	}
}

function checkForIllegalChars(str){
	var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if(str.match(illegalChars)){
		return true;
	}
	else{
		return false;	
	}
}

function validateContactForm(form){
	var errorString = "";				
	if(form.name.value == ""){
		errorString += "\n - Name is empty.";
	}
	else{
		if(checkForNumbers(form.name.value)){
			errorString += "\n - Name contains NUMBERS.";
		}
		if(checkForIllegalChars(form.name.value)){
			errorString += "\n - Name contains ILLEGAL characters.";	
		}
	}
	if(form.country.value == ""){
		errorString += "\n - Country is empty.";
	}

	if(form.email.value == ""){
		errorString += "\n - Email Address is empty.";
	}
	else{
		if(form.email.value != ""){
			errorString += checkEmail(form.email.value);	
		}
	}
	
	if(form.address.value == ""){
		errorString += "\n - Address is empty."
	}
	
	if(form.message.value == ""){
		errorString += "\n - Message is empty."
	}
	
	//Control Statement
	if(errorString == ""){
		return true;
	}
	else{
		alert("The following errors occurred...\n" + errorString + "\n\n...Please make corrections and re-submit the form");
		return false;
	}
}

