function check_form(f)
{
  address_ind = false;
  telephone_ind = false;
  email_ind = false;
  
  err = new Error();
  if (isempty(f.title)) 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.address_line_1)) address_ind = true;
	
  if (address_ind)
  {
    if (isempty(f.town)) err.add('Town is mandatory. Please re-enter.');
  
    if (isempty(f.postcode))
    {
      err.add('Postcode is mandatory. Please re-enter.');
    }
    else
    {
	  var postcode = /(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})/;
	  if (!postcode.test(f.postcode.value)) {
	    err.add('The postcode is not recognised. Please re-enter.');
	  }
    }
  }
  
  if (!isempty(f.email))
  {
    email_ind = true;
	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.telephone))
  {
    telephone_ind = true;
	if (!checkUKTelephone(f.telephone.value))
	{
	  err.add(telNumberErrors[telNumberErrorNo] + ". Please re-enter");
	}
  }
  
  if (!address_ind && !telephone_ind && !email_ind)
    err.add("You must provide at least one of your address, email or telephone to complete the registration.");
  
  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";
