// Form validation


//This one does email and telephone only - although, telephone validation not required right now

function validateForm(f) {
	
	

// Strip whitespace and place into variables
	name				= stripWhitespace(f.name.value);
	email				= stripWhitespace(f.email.value);
	//telephone			= stripWhitespace(f.telephone.value);
	postcode			= stripWhitespace(f.postcode.value);
	//query				= stripWhitespace(f.query.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 appear to be valid, please check it'); f.email.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 appear to be valid, please check and try again'); f.postcode.focus(); return false;  }
	
	
	
	// If all ok then hide form and return true (validation passed)
	document.getElementById("overlay").style.display="none";
	document.getElementById("careplanform-container").style.display="none";
	return true;

}






/* 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);
	
}



/* 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, '');
}





