//Validation
function validEmail(sEmail) {
	//var re = /[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;
	var re = new RegExp("^[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9]@[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9][\.][a-z0-9]{2,4}$");
	//test the supplied email address
	if(!re.test(sEmail)) {
		alert('please enter a valid email address');
		return false;
	}
	return true;
}
	
//Func for determining empty values
function isBlank(s) {
	for (i=0;i<s.length;i++ ) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function tooLong(s,len) {
	if (s.length > len) {
		 return true;
	}
	return false;
}

function nonAlpha(s,field) {
	var regStr = new RegExp("[^a-zA-Z-\']");
	if(regStr.test(s)) {
		return true;
	}
	return false;
}

function nonAlphaNum(s,field)	{
	var regStr = new RegExp("[^a-zA-Z0-9_\.-]");
	if(regStr.test(s)) {
		return true;
	}
	return false;
}

function numSpace(s,field)	{
	var regStr = new RegExp("[^0-9-, ]");
	if(regStr.test(s)) {
		return true;
	}
	return false;
}

function nonAlphaSpace(s,field) {
	var regStr = new RegExp("[^a-zA-Z-\', ]");
	if(regStr.test(s)) {
		return true;
	}
	return false;
}

function validFm(fm) {

	//Ensure we have values in the form fields we require
	if(isBlank(fm.contact.value) || nonAlphaSpace(fm.contact.value,'Your name')) {
		alert('The Your name field is required and can only contain letters, apostrophes and hyphens.');
		fm.contact.focus();
		return false;
	}
	//Work out which contact method is selected
	for (var i=0; i<fm.contactpref.length; i++)  {
		if (fm.contactpref[i].checked)  {
			contprefValue = fm.contactpref[i].value;
		}
	}

	if( contprefValue == 'Email' ) {
		
		if (isBlank( fm.contactemail.value)) {
			alert('You have selected email as your preferred method of contact but have not provided an email address.');
			fm.contactemail.focus();
			return false;
		}
		if (!validEmail( fm.contactemail.value)) {
			fm.contactemail.focus();
			return false;
		}
	}
	//Otherwise preferred method of contact is telephone
	else {
		
		if (isBlank( fm.telno.value)) {
			alert('You have selected telephone as your preferred method of contact but have not provided a telephone number.');
			fm.telno.focus();
			return false;
		}
		if (numSpace( fm.telno.value)) {
			alert('You have selected telephone as your preferred method of contact but have not provided a valid telephone number. Please note this field can only accept numbers, spaces and hyphens.');
			fm.telno.focus();
			return false;
		}
	}
	if(isBlank(fm.contactmsg.value)) {
		alert('The comments/message field is required. Please leave your comments/message.');
		fm.contactmsg.focus();
		return false;
	}

	if(isBlank(fm.seccode.value)) {
		alert('Please enter the security code as displayed in the image. This is an anti-spam measure.');
		fm.seccode.focus();
		return false;
	}

	return true;
	
}