//Main function that runs the error checking
function errorContact() {
 var fault = "";
 fault += checkName(document.contact.name.value);
 fault += checkCounty(document.contact.county.value);
 fault += checkEmail(document.contact.email.value);
 if ((document.contact.email.value) !=(document.contact.email2.value)) {
	  fault += "Email address are not the same.\n";
 }
 if ((document.contact.telephone1.value) !="") {
fault += checkTel1(document.contact.telephone1.value);}
 if ((document.contact.telephone2.value) !="") {
fault += checkTel2(document.contact.telephone2.value);}
 if ((document.contact.telephone3.value) !="") {
fault += checkTel3(document.contact.telephone3.value);}
 if (fault != "") {
	alert(fault);
	return false;
 }
 return true;
}
//Each of these check an input with differnt qualities.
function checkName (strng) {
 var error = "";
 if (strng == "") {
   error = "You didn't enter a name.\n";
}
return error;
}

function checkCounty (strng) {
 var error = "";
 if (strng == "") {
   error = "You didn't enter a county.\n";
}
return error;
}

function checkEmail (strng) {
	var error ="";
	if (strng !="") {
var emailFilter=/^.+@.+\..{2,3}$/;
if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
}
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]_\ ]/;
if (strng.match(illegalChars)) {
   error = "The email address contains illegal characters.\n";
}
	}
return error;
}

function checkTel1 (strng) {
	var error ="";
 var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
//strip out acceptable non-numeric characters

if (!(stripped.length == 11)) {
	error = "The home phone number is the wrong length. Make sure you included an area code.\n";
}
if (isNaN(parseInt(stripped))) {
   error = "The home phone number contains illegal characters.";
}
return error;
}
function checkTel2 (strng) {
	var error ="";
 var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
//strip out acceptable non-numeric characters

if (!(stripped.length == 11)) {
	error = "The work phone number is the wrong length. Make sure you included an area code.\n";
}
if (isNaN(parseInt(stripped))) {
   error = "The work phone number contains illegal characters.";
}
return error;
}
function checkTel3 (strng) {
	var error ="";
 var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
//strip out acceptable non-numeric characters

if (!(stripped.length == 11)) {
	error = "The mobile phone number is the wrong length. Make sure you included an area code.\n";
}
if (isNaN(parseInt(stripped))) {
   error = "The mobile phone number contains illegal characters.";
}
return error;
}

