// Form validation



function validateForm(f) {
	

// Strip whitespace and place into variables

	property			= stripWhitespace(f.property.value);
	name			= stripWhitespace(f.name.value);
	email			= stripWhitespace(f.email.value);
	telephone		= stripWhitespace(f.telephone.value);


// Check required fields have information

	if(property.length == 0)					{ alert('You didn\'t say which property'); f.property.focus(); return false; }
	if(name.length == 0)					{ alert('Please tell us your name'); f.name.focus(); return false; }
	if(email.length == 0)					{ alert('Please enter your email address'); f.email.focus(); return false; }
	if(!validateEmail(email))				{ alert('This email address does not appear to be valid, please check it'); f.email.focus(); return false; }
	if(telephone.length == 0)			{ alert('Please enter your phone number'); f.telephone.focus(); return false; }
	if(!validateTelephone(telephone)) 	{alert ('This telephone number does not appeat to be valid, please check and try again'); f.telephone.focus(); return false;  }


	
	// If all ok then return true (validation passed)
	
	return true;

}

function validateRegistrationForm(f) {
	

// Strip whitespace and place into variables

	address			= stripWhitespace(f.address.value);
	postcode		= stripWhitespace(f.postcode.value);
	name			= stripWhitespace(f.name.value);
	email			= stripWhitespace(f.email.value);
	telephone		= stripWhitespace(f.telephone.value);


// Check required fields have information

	if(name.length == 0)					{ alert('Please tell us your name'); f.name.focus(); return false; }
	if(email.length == 0)					{ alert('Please enter your email address'); f.email.focus(); return false; }
	if(!validateEmail(email))				{ alert('This email address does not seem to be valid, please check it'); f.email.focus(); return false; }
	if(telephone.length == 0)				{ alert('Please enter your phone number'); f.telephone.focus(); return false; }
	if(!validateTelephone(telephone)) 		{ alert ('This telephone number does not seem to be valid, please check and try again'); f.telephone.focus(); return false;  }
	if(address.length == 0)					{ alert('Please tell us your address'); f.address.focus(); return false; }
	if(postcode.length == 0)				{ alert('Please enter your postcode'); f.postcode.focus(); return false; }
	if(!validatePostcode(postcode)) 		{ alert ('This postcode  does not seem to be valid, please check and try again'); f.postcode.focus(); return false;  }


	
	// If all ok then return true (validation passed)
	
	return true;

}




/* Function to validate email */
function validateEmail(email) {
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(email);
}
/* Function to validate Telephone number, ie 11-13 digits */

function validateTelephone(telephone) {
telephone = telephone.replace (/[^\d]/g,'');
var regex = /^\d{11,13}/;
return regex.test(telephone);
}

/* Remove white space from start & end of string */
function stripWhitespace(str) {
	str = this != window ? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

/* function to validate postcode */

function validatePostcode(postcode) {

var regex = /^[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d[A-Za-z]{2}$/;
return regex.test(postcode);
	
}






