// JavaScript Document for Ken Hicks :: Created by Bluewire Media


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 validateForm(form){
	var errorString = "";				
	if(form.fname.value == "" || form.lname.value == ""){
		errorString += "\n - First OR Last Name is empty.";
	}
	else{
		//if(checkForNumbers(form.name.value)){
		//	errorString += "\n - Name contains NUMBERS.";
		//}
		if(checkForIllegalChars(form.fname.value) || checkForIllegalChars(form.lname.value)){
			errorString += "\n - First OR Last Name contains ILLEGAL characters.";	
		}
	}
	if(form.phone.value == "" && form.email.value == ""){
		errorString += "\n - Please provide EITHER a Phone Number OR Email Address.";
	}
	else{
		if(form.email.value != ""){
			errorString += checkEmail(form.email.value);	
		}
		if(form.phone.value != ""){
			if(checkForLetters(form.phone.value)){
				errorString += "\n - Phone Number contains LETTERS.";
			}
			if(checkForIllegalChars(form.phone.value)){
				errorString += "\n - Phone contains ILLEGAL characters.";	
			}
			if((form.phone.value).length < 8){
				errorString += "\n - Phone Number is to short. (Phone Numbers are usually 8 digits or longer)";
			}
		}
	}
	if(form.category.value == "0" || form.category.value == 0){
		errorString += "\n - Please choose a category for your inquiry."
	}
	
	//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;
	}
}