// JavaScript Document
function checkForm(theForm) {
  	var name = theForm.name.value;
	var email = theForm.email.value;
	var phone = theForm.phone.value;
	var message = theForm.message.value; 
	var security_check = theForm.check_code.value; 

  	var emailpatern = /^[^@ ]+@[^@ ]+\.[^@ \.]+$/;
  	var emailmatch = emailpatern.test(email);
 	
	var ValidPhoneChars = "0123456789()+ -";
	var ValidNumChars = "0123456789";
	var IsPhoneNumber=true;
	var IsNotNumber=true;
	
	for (i = 0; i < phone.length && PhoneNumber == true; i++) {
		Char = phone.charAt(i);
		if (ValidPhoneChars.indexOf(Char) == -1) {
			IsPhoneNumber=false;
		}
	}
	for (i = 0; i < name.length && IsNotNumber == true; i++) {
		Char = name.charAt(i);
		if (ValidNumChars.indexOf(Char) != -1) {
			IsNotNumber=false;
		}
	}

	//check through the form	
	var validform = true;
	var focuspoint = 0;
	var alerttext = "";
	
	if (name == "") {
		validform = false;
		alerttext = alerttext + "Please specify your Name\n";
		if (focuspoint == 0) {
			focuspoint = "1";
		}
	}
	if (IsNotNumber == false) {
		validform = false;
		alerttext = alerttext + "Your Name cannot contain numbers\n";
		if (focuspoint == 0) {
			focuspoint = "1";
		}
	}
	if (email.indexOf('www.') != -1) {
		validform = false;
		alerttext = alerttext + "An Email address cannot contain 'www.'\n";
		if (focuspoint == 0) {
			focuspoint = "2";
		}
	}
	if (!emailmatch) {
		validform = false;
		alerttext = alerttext + "Please include a valid Email address\n";
		if (focuspoint == 0) {
			focuspoint = "2";
		}
	}
	if (phone != "") {
		if (IsPhoneNumber != true) {
			validform = false;
			alerttext = alerttext + "The Telephone Number you entered is not valid\n";
			if (focuspoint == 0) {
				focuspoint = "3";
			}
		}	
	}
	if (message.length < 10) {
		validform = false;
		alerttext = alerttext + "Please include a  Message\n";
		if (focuspoint == 0) {
			focuspoint = "4";
		}
	}
	if (security_check.length != 4) {
		validform = false;
		alerttext = alerttext + "Please enter the 4 security digits\n";
		if (focuspoint == 0) {
			focuspoint = "5";
		}
	}
	
	if (!validform) {	
		alert(alerttext);
		switch (focuspoint){
		case "0":  
		break;
		case "1":  
			theForm.name.focus();  
		break;
		case "2":  
			theForm.email.focus();  
		break;
		case "3":  
			theForm.phone.focus();  
		break;
		case "4":  
			theForm.message.focus();  
		break;
		case "5":  
			theForm.check_code.focus();  
		break;
		}
		return false;
	} else {
		return true;
	}					
}