function checkForm(){
	
	var message = "";
	
	message += checkRequired();
	
	if(message==""){
		message += checkTelephone();
		
		message += checkEmail();
	}
	
	if(message == ""){
		return true;
	}
	else{
		alert(message);
		return false;
	}
}

function checkRequired(){
	var feedback = "";
	
	if(document.contactForm.name.value == "" || (document.contactForm.email.value == "" && document.contactForm.telephone.value == "") || document.contactForm.address1.value == "" || document.contactForm.address2.value == "" || document.contactForm.address3.value == "" || document.contactForm.postCode.value == "" || document.contactForm.type.value == "" || document.contactForm.date.value == "" || document.contactForm.location.value == ""){
		feedback += "Please enter all required fields\n";
	}
	
	return feedback;
}

function checkEmail(){
	var feedback = "";
	
	str = document.contactForm.email.value;

	if(str!=""){
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1)
			feedback =  "Invalid E-mail Address\n";
	
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
			feedback =  "Invalid E-mail Address\n";
	
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
			feedback =  "Invalid E-mail Address\n";
	
		if (str.indexOf(at,(lat+1))!=-1)
			feedback =  "Invalid E-mail Address\n";
		
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
			feedback =  "Invalid E-mail Address\n";
	
		if (str.indexOf(dot,(lat+2))==-1)
			feedback =  "Invalid E-mail Address\n";
		
		if (str.indexOf(" ")!=-1)
			feedback =  "Invalid E-mail Address\n";	
	}
		
	return feedback;
}

function checkTelephone(){
	var feedback = "";
	
	if(document.contactForm.telephone.value!=""){
		result = checkUKTelephone(document.contactForm.telephone.value);
	
		if(result==false){
			feedback = telNumberErrors[telNumberErrorNo];
		}
	}
	return feedback;
}

function checkUKTelephone (telephoneNumber) {

  // Convert into a string and check that we were provided with something
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;
  
  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Remove spaces from the telephone number to help validation
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  // Remove hyphens from the telephone number to help validation
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
  
  // Finally check that the telephone number is appropriate.
  exp = /^(01|02|03|05|070|077|07624|078|079)[0-9]+$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
