
// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

// validate the form
function validateForm(f, preCheck){
	var errors = '';
	if(preCheck != null) errors += preCheck;
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.name;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				errors += n+' cannot be empty.\n\n'; continue;
			}
			if(v == e.defaultValue){
				errors += n+' cannot use the default value.\n\n'; continue;
			}
			if(e.isAlpha){
				if(!isAlpha(v)){
					errors += n+' can only contain characters A-Z a-z.\n\n'; continue;
				}
			}
			if(e.isNumeric){
				if(!isNumeric(v)){
					errors += n+' can only contain characters 0-9.\n\n'; continue;
				}
			}
			if(e.isAlphaNumeric){
				if(!isAlphaNumeric(v)){
					errors += n+' can only contain characters A-Z a-z 0-9.\n\n'; continue;
				}
			}
			if(e.isEmail){
				if(!isEmail(v)){
					errors += v+' is not a valid email.\n\n'; continue;
				}
			}
			if(e.isLength != null){
				var len = e.isLength;
				if(!isLength(v,len)){
					errors += n+' must contain only '+len+' characters.\n\n'; continue;
				}
			}
			if(e.isLengthBetween != null){
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)){
					errors += n+' cannot contain less than '+min+' or more than '+max+' characters.\n\n'; continue;
				}
			}
			if(e.isPhoneNumber){
				if(!isPhoneNumber(v)){
					errors += v+' is not a valid US phone number.\n\n'; continue;
				}
			}
			if(e.isDate){
				if(!isDate(v)){
					errors += v+' is not a valid date.\n\n'; continue;
				}
			}
			if(e.isMatch != null){
				if(!isMatch(v, e.isMatch)){
					errors += n+' does not match.\n\n'; continue;
				}
			}
			}
			if(t.indexOf('select') != -1){
				if(isEmpty(e.options[e.selectedIndex].value)){
					errors += n+' needs an option selected.\n\n'; continue;
				}
			}
			if(t == 'file'){
				if(isEmpty(v)){
					errors += n+' needs a file to upload.\n\n'; continue;
				}
			}
	}
	if(errors != '') alert(errors);
	return errors == '';
}

/*
All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.


isEmail = true;          // valid email address
isAlpha = true;          // A-Z a-z characters only
isNumeric = true;        // 0-9 characters only
isAlphaNumeric = true;   // A-Z a-z 0-9 characters only
isLength = number;       // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true;    // valid US phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true;           // valid date. See "isDate()" comments for the formatting rules
isMatch = string;        // must match string
optional = true;         // element will not be validated
*/


// All of the previous JavaScript is coded to process
// any form and should be kept in an external file if
// multiple forms are being processed.

// This function configures the previous
// form validation code for this form.

function configureValidation(f){
	f.name.isAlpha = true;
	f.email.isEmail = true;
	f.additional_comments.optional = true;
	f.How_did_you_find_our_site_text.optional = true;
	f.find_what_you_came_for_text.optional = true;
	return validateForm(f);
}
