// Form validation


//This one does email and telephone only

function validateBrochureForm(f) {

// Strip whitespace and place into variables

	name			= stripWhitespace(f.name.value);
	name2			= stripWhitespace(f.name2.value);
	title			= stripWhitespace(f.title.value);
	name+=name2;
	
	address			= stripWhitespace(f.address.value);
	postcode		= stripWhitespace(f.postcode.value);
	
	email			= stripWhitespace(f.email.value);
	
	telephone		= stripWhitespace(f.telephone.value);
	
	
	
	//query				= stripWhitespace(f.query.value);
	
	
	// Check required fields have information
	
	if(title.length == 0)					{ alert('Please fill in the title field'); f.title.focus(); return false; }
	if(name.length == 0)					{ alert('Please tell us your name'); f.name.focus(); return false; }
	//if(telephone.length == 0)			{ alert('Please enter your phone number'); f.telephone.focus(); return false; }
	if(!validateTelephone(telephone) &&telephone.length !== 0) 	{alert ('This telephone number does not appeat to be valid, please check and try again'); f.telephone.focus(); return false;  }
	if(address.length == 0)					{ alert('We need your address to send you a brochure'); 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 appear to be valid, please check and try again'); f.postcode.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 all ok then return true (validation passed)
	
	return true;

}

//This one for callback

function validateCallback(f){

	// Strip whitespace and place into variables
	name				= stripWhitespace(f.name.value);
	//email				= stripWhitespace(f.email.value);
	//address				= stripWhitespace(f.address.value);
	telephone			= stripWhitespace(f.telephone.value);
	
	// Check required fields have information
	// in words;
	// required: name telephone
	
	
	
	
	if(name.length == 0 || name=='Your name' )	 { alert('Please enter a name.'); f.name.focus(); return false; }
	
	
	
	if(telephone.length == 0 || telephone=='Your phone number' )	 { alert('Please enter a 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 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, '');
}





