function check_form(f)
{
	
  err = new Error();
  if (isempty(f.salutation)) err.add('Title is mandatory. Please re-enter.');
  if (isempty(f.first_name)) err.add('First name is mandatory. Please re-enter.');
  if (isempty(f.last_name)) err.add('Last name is mandatory. Please re-enter.');
  if (isempty(f.street)) err.add('Address line 1 is mandatory. Please re-enter.');
  if (isempty(f.city)) err.add('Town is mandatory. Please re-enter.');  
  
  
  if (isempty(f.zip))
  {
    err.add('Postcode is mandatory. Please re-enter.');
  }
  else
  {
	  var postcode = /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/;
	  if (!postcode.test(f.zip.value)) {
	    err.add('The postcode is not recognised. Please re-enter.');
	  }
  }
  
  if (f.elements[17].checked)
  {
    if (isempty(f.email))
	  err.add('In order to be added to the Powder Byrne mailing list please fill in your email.');
    else
	{
		emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
		if (!emailpat.test(f.email.value)) 
	    {
			err.add('Your email is not recognised as a valid format. Please re-enter.');
		}
	}
  }
  
  if (!isempty(f.phone))
  {
	  if (!checkUKTelephone(f.phone.value))
	  {
		  err.add(telNumberErrors[telNumberErrorNo] + ". Please re-enter");
	  }
  }
  
  if (f.elements[14].selectedIndex == 0) {
	  err.add('You must tell us where you heard of Powder Byrne.');
  }
  
  //if (isempty(f.referer_other)) err.add('Please specify how did you hear about Powder Byrne.');

  if (!f.elements[14].checked && !f.elements[15].checked)
  {
    err.add('You have not checked a brochure. Please select one or more brochures.');
  }
  
  if (err.isset()){
    err.show();
    return false;
  }
  return true;
}

function checkUKTelephone (telephoneNumber) {
	
  telNumberErrorNo = 0;

  // 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";
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";
