function validEmail(str) {
//Function downloaded from http://www.webreference.com/js/tips/000310.html
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    return true;
  }
  return false;
}


function validateForm(){
        
theForm = document.forms["contact"];

var msg = "";
var focusField = "";

if (theForm.fname.value == ""){
msg += "  - First Name\n";
focusField = (focusField == "") ? "fname" : focusField;
}

if (theForm.lname.value == ""){
msg += "  - Last Name\n";
focusField = (focusField == "") ? "lname" : focusField;
}

if (theForm.phone.value == ""){
msg += "  - Telephone\n";
focusField = (focusField == "") ? "phone" : focusField;
}


if (theForm.church.value == ""){
msg += "  - Your Church\n";
focusField = (focusField == "") ? "church" : focusField;
}


if(theForm.email1.value != ""){
	if (validEmail(theForm.email1.value)){
		if(((theForm.email1.value)!=(theForm.email2.value))){
			msg += "  - Your email addresses do not match\n";
			focusField = (focusField == "") ? "email1" : focusField;
		}
	} else {
	 	msg += "  - Provide a valid email address\n";
		focusField = (focusField == "") ? "email1" : focusField;
	}
}

if ((theForm.email2.value == "")||(theForm.email2.value == "")){
	msg += "  - Your Email\n";
	focusField = (focusField == "") ? "email2" : focusField;
}

if (theForm.comments.value == ""){
msg += "  - Comments\n";
focusField = (focusField == "") ? "comments" : focusField;
}

if (msg == ""){
//Everything is OK submit the form
return true;
} else {
//Display errors:
msgAll = "Please provide or correct the following information:\n";
msgAll += "\n"+msg;
msgAll += "\nClick OK to make corrections.";
alert(msgAll);
eval("theForm." + focusField + ".focus()");
eval("theForm." + focusField + ".select()");
return false;
}

return false;
}
