// This function returns true if a string is empty, 
// (i.e. contains only whitespace)
function isEmpty(strIn) {
	//Assume the string is empty
	var empty = true;
			
	//Handle zero-length string
	if (strIn != null) {
		//For each character in the string
		for (i=0; i<strIn.length; i++) {
			//If the character is not a whitespace, then the string is not empty
			if (strIn.charAt(i) != " " && strIn.charAt(i) != "\t" && strIn.charAt(i) != "\n") {
				empty = false;
			}
		}
	}			
	//Return the end value
	return empty;
}
// This function returns true if the form is complete,
// (i.e. all required fields have non-whitespace characters in them).
function validateForm(objForm) {
		
	var completed = true;
	var errorsArray = new Array();
	var errorCount = -1;
			
	if (objForm == "") {
		completed = false;
		errorCount++;
		errorsArray[errorCount] = "No form supplied.";
	}
	else {
	
		//Check name
		if (isEmpty(objForm.elements["firstname"].value)) {
			completed = false;
			errorCount++;
			errorsArray[errorCount] = "No First Name supplied.";
		}

		//Check name
		if (isEmpty(objForm.elements["surname"].value)) {
			completed = false;
			errorCount++;
			errorsArray[errorCount] = "No Surname supplied.";
		}
		
		//Check email
		if (isEmpty(objForm.elements["email"].value)) {
			completed = false;
			errorCount++;
			errorsArray[errorCount] = "No Email supplied.";
		}	

		//Check for non-standard characters (CSS Site Attack Prevention)
		var bad_chars = new RegExp (/[\[\]\{\}\<\>\%\#\=]/);
		var input_string = objForm.elements["firstname"].value + objForm.elements["surname"].value + objForm.elements["email"].value + objForm.elements["tel"].value + objForm.elements["comments"].value;
		if (bad_chars.test(input_string) == true){
			completed = false;
			errorCount++;
			errorsArray[errorCount] = "Bad character identified  [ ] { } < > % # =";
		}	

	}
			
	if (errorsArray.length > 0) {
		var strErrorString = "Unable to submit your message, please correct the following errors:\n";
				
		for (i=0; i<errorsArray.length; i++) {
			strErrorString += "\t" +errorsArray[i] +"\n";
		}
		//Display the alert
		alert(strErrorString);
	}
	return completed;
}



