// on-line form validation script copyright 2002 by kevin foster

function validate() {
	var Form = document.sendMessage;
	var Required = new Array();
	var Optional = new Array("name", "email", "message", "add_to_list");
	if ((Form.add_to_list.checked) && (!Form.email.value)) {
		alert("if you want me to keep you updated, i'll need to have your email address.\n\nplease click \"OK\" to go back and fix this before submitting the form.\n");
		return false;
	}
	for (var i = 0; i < Optional.length; i++) {
		if ((Form[Optional[i]].value) && (Form[Optional[i]].value != 'on')) { 
			Required.push(Optional[i]); 
		}
	}
	if (Required.length == 0) { 
		alert("the form you're trying to submit appears to be blank.\n\nplease click \"OK\" to go back and fix this before submitting the form.\n");
		return false;
	}
	var Errors = new Array();
	var email_error = "";
	var bad_error = "";
	for (var i in Required) { 
		if ((Required[i] == "email") && (Form.email.value != "") && (!checkEmail(Form.email.value))) { 
			Errors.push("mal_email");
		} else if ((Required[i] != "email") && (!checkField(Form[Required[i]].value))) { 
			Errors.push("bad_" + Required[i]);
		} 
	}
	if (Errors.length != 0) { 
		var bad_fields = new Array();
		var funky_email = 0;
		for (var j in Errors) {
			if (Errors[j].substring(0, 3) == "bad") {
				bad_fields.push(Errors[j].substring(4, Errors[j].length));
			} else if (Errors[j].substring(0, 3) == "mal") {
				funky_email = 1;
			}
		}
		var errorMessage = "";
		if (Errors.length == 1) { errorMessage += "the following error was detected in the form you're trying to submit:\n\n" } 
		else { errorMessage += "the following errors were detected in the form you're trying to submit:\n\n" }
		if (bad_fields.length == 1) { 
			errorMessage += "\tplease restrict your entries in the \"" + bad_fields[0] + "\" field to \n";
			errorMessage += "\tletters, numbers, spaces, underscores, commas, periods, \n";
			errorMessage += "\tapostrophes, dashes, asterisks, question marks, \n";
			errorMessage += "\texclamation points, quote marks and parentheses. \n\n";
		} else if (bad_fields.length > 1) { 
			errorMessage += "\tplease restrict your entries in the following fields to \n";
			errorMessage += "\tletters, numbers, spaces, underscores, commas, periods, \n";
			errorMessage += "\tapostrophes, dashes, asterisks, question marks, \n";
			errorMessage += "\texclamation points, quote marks and parentheses: \n\n";
			for (var l in bad_fields) {
				errorMessage += "\t\t" + bad_fields[l] + "\n";
			}
			errorMessage += "\n";
		}
		if (funky_email == 1) { errorMessage += "\tthe e-mail address you entered does not appear to be validly-formatted.\n\n" }
		if (Errors.length == 1) { errorMessage += "please click \"OK\" to go back and fix this before submitting the form.\n" } 
		else { errorMessage += "please go back and fix these errors before submitting the form.\n" }
		alert(errorMessage);
		return false;
	};
	return true;
}
// create a method of the Array object to duplicate Perl's "push" function for older browsers
function push() {
	var subscript = this.length;
	for (var i = 0; i < push.arguments.length; ++i) {
		this[subscript] = push.arguments[i];
		subscript++;
	}
}
Array.prototype.push = push;
// check field values for "illegal" characters
function checkField(str) {
	if (window.RegExp) {
		var reg_exp = /([^\w\s\!"',-:\(\)\?\.@]+)/g;
		if (reg_exp.test(str)) {
			return false;
		}
		return true;
	} else {
		for (var i = 0; i < str.length; i++) { 
			var ch = str.charAt(i);
			if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch < "0" || "9" < ch)) && 
				((ch != '_') && (ch != ' ') && (ch != '*') && (ch != ",") && (ch != ".") && (ch != "'") && (ch != "-") && 
				(ch != "?") && (ch != "+") && (ch != "!") && (ch != "\(") && (ch != "\)") && (ch != "\""))) { 
				return false;
			}
		}
	}
	return true;
}
// validate "email" field
function checkEmail(str) {
	if (window.RegExp) {
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
		var reg1 = new RegExp(reg1str);
		var reg2 = new RegExp(reg2str);
		if (!reg1.test(str) && reg2.test(str)) { return true } 
		else { return false }
	} else if (str.indexOf ('@',0) == -1 || str.indexOf ('.',0) == -1) {
			return false;
	}
	return true;
}
