// original javascript by max hoy (maxhoy.co.uk) //

//field tester
function checkIncluded(textField,title,restrict,required) {
var msg = "\n Please enter your ";
var msg2 = title;
	if(document.getElementById(textField).value==''){
		if(required == 1) {
			FCRes = 1;
			alertMsg = (msg+msg2);
			alertTxt = (alertTxt + alertMsg);
			if (FocusEl == "") {
				FocusEl = textField;
			}
		}
	} else {
		if(restrict == 1) {
			reg=/^[A-Za-z0-9\,\.\'\"\ \!\?\;\:\-\(\)\s\n]+$/;
			textTest = document.getElementById(textField).value;
			regCheckRes = reg.test(textTest);
			if (regCheckRes == false) {
				FCRes = 1;			
				msg4 = "\n Sorry. You can only enter alphabetical, nummerical and basic grammatical characters in ";
				msg5 = title;
				alertMsg = (msg4+msg5);
				alertTxt = (alertTxt + alertMsg);
				if (FocusEl == "") {
					FocusEl = textField;
				}
			} 
		}
	}
}

// email address tester
function checkEmail(emailField,required) {
	if (document.getElementById(emailField).value=='') {
		if(required == 1) {
		FCRes = 1;
		alertMsg = "\n Please enter your email address ";
		alertTxt = (alertTxt + alertMsg);
				if (FocusEl == "") {
					FocusEl = emailField;
				}
		}
	} else {
		// check character positions
		var locationOfAt = document.getElementById(emailField).value.indexOf("@");
		var locationOfDot = document.getElementById(emailField).value.indexOf(".");
		//alert ("at "+locationOfAt);
		//alert ("dot "+locationOfDot);
		
			if ((locationOfAt < 0) || (locationOfDot < 0)) {
			FCRes = 1;
			alertMsg = "\n Please enter a valid email address";
			alertTxt = (alertTxt + alertMsg);
				if (FocusEl == "") {
					FocusEl = emailField;
				}
			} else {
			// check first dot position
				if (locationOfDot < locationOfAt) {
				var field_array=document.getElementById(emailField).value.split("@");
				//alert(field_array[0]);
				//alert(field_array[1]);
				var locationOfDot2 = field_array[1].indexOf(".");
				//alert("dot2 "+locationOfDot2);
				var locationOfDot3 = (locationOfDot2+locationOfAt);
				//alert(locationOfDot3);
				}
				// check second dot position
				if (locationOfDot3 < locationOfAt) { 
				FCRes = 1;
				alertMsg = "\n Please enter a valid email address";
				alertTxt = (alertTxt + alertMsg);
				if (FocusEl == "") {
					FocusEl = emailField;
				}
				}
			}
	}
}

// check field is numeric
function checkNumber(numberField, title,required) {
fieldValue = document.getElementById(numberField).value;

// removes spaces from number before validation
//spaceIndex = fieldValue.indexOf(" ");
//while (spaceIndex > -1) {
//	spaceIndex = fieldValue.indexOf(" ");
//	fieldValue = fieldValue.replace(" ","");
//}

if (fieldValue=='') {
	if(required == 1) {
		FCRes = 1;
		var msg = "\n Please enter your ";
		var msg2 = title;
		var msg3 = " number";
		alertMsg = (msg+msg2+msg3);
		alertTxt = (alertTxt + alertMsg);
				if (FocusEl == "") {
					FocusEl = numberField;
				}
	}
	} else {
		NoRes = 0;
		//character checker
			for(i=0; i<fieldValue.length; i++) {
				if ((fieldValue.charAt(i) < "0") || (fieldValue.charAt(i) > "9" )) {
					// allow spaces
					if (fieldValue.charAt(i) != " ") { NoRes = 1; }
   				}
  			}
			if (NoRes != 0) {
				document.getElementById(numberField).focus();
				FCRes = 1;
				alertMsg = "\n Your phone number is not numeric";
				alertTxt = (alertTxt + alertMsg);
				if (FocusEl == "") {
					FocusEl = numberField;
				}
			}
	}
}

function checkCheckbox(checkBox,title,required) {
	if (document.getElementById(checkBox).checked == false) {
		FCRes = 1;
		var msg = "\n Please tick the  ";
		var msg2 = title;
		var msg3 = " checkbox";
		alertMsg = (msg+msg2+msg3);
		alertTxt = (alertTxt + alertMsg);
		if (FocusEl == "") {
			FocusEl = checkBox;
		}
	}
}