function validateFormOnSubmit(theForm) {
	if(
		requireTextOnly(theForm.name) &&
		requireValue(theForm.business) &&
		requireValue(theForm.address1) &&
		requireValue(theForm.address2) &&
		requirePhone(theForm.phone) &&
		requireEmail(theForm.email)
	) {
		//alert("All fields contain valid information.");
		return true;
	}

	alert("One or more fields require correction.");
	return false;
}

function trim(s) {
	t=s.replace(/^\s+|\s+$/gi,'');
	return t;
}

function requireSelect(fld) {
	if(fld.value=='') {
		//alert("Please select an option.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requireValue(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	if(tfld.length==0) {
		//alert("A required field has not been filled in.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requireTextOnly(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var illegalChars=/[^a-z\.\,\-\ ]/i;
	if(tfld.length==0) {
		//alert("A required field has not been filled in.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(illegalChars.test(tfld)) {
		//alert("Field contains illegal characters. Use only plain text.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function optionalTextOnly(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var illegalChars=/[^a-z\.\,\-\ ]/i;
	if(tfld.length!=0) {
		if(illegalChars.test(tfld)) {
			//alert("Field contains illegal characters. Use only plain text.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requireNumberOnly(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var illegalChars=/\D/;
	if(tfld.length==0) {
		//alert("A field has not been filled in.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(illegalChars.test(tfld)) {
		//alert("A field contains illegal characters.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function optionalNumberOnly(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var illegalChars=/\D/;
	if(tfld.length!=0) {
		if(illegalChars.test(tfld)) {
			//alert("A field contains illegal characters.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requireUsername(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var illegalChars=/[^a-z0-9]/i;
	if(tfld.length==0) {
		//alert("You didn't enter a username.");
		fld.style.border='1px solid red';
	} else if((tfld.length<3) || (tfld.length>16)) {
		//alert("Username should be between 3 and 16 characters.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(illegalChars.test(tfld)) {
		//alert("Username contains illegal characters.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function optionalUsername(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var illegalChars=/[^a-z0-9]/i;
	if(tfld.length!=0) {
		if((tfld.length<3) || (tfld.length>16)) {
			//alert("Username should be between 3 and 16 characters.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else if(illegalChars.test(tfld)) {
			//alert("Username contains illegal characters.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requirePassword(fld) {
	var passFilter=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W\_]).{8,}$/;
	var illegalChars=/\s/;
	if(fld.value.length==0) {
		//alert("You didn't enter a password.");
		fld.style.border='1px solid red';
	} else if(fld.value.length<8) {
		//alert("Passwords must be at least 8 characters in length.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(illegalChars.test(fld.value)) {
		//alert("Passwords cannot contain spaces.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(!passFilter.test(fld.value)) {
		//alert("Password does not meet security requirements. It must contain A-Z, a-z, 0-9 and special characters.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function optionalPassword(fld) {
	var passFilter=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W\_]).{8,}$/;
	var illegalChars=/\s/;
	if(fld.value.length!=0) {
		if(fld.value.length<8) {
			//alert("Passwords must be at least 8 characters in length.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else if(illegalChars.test(fld.value)) {
			//alert("Passwords cannot contain spaces.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else if(!passFilter.test(fld.value)) {
			//alert("Password does not meet security requirements. It must contain A-Z, a-z, 0-9 and special characters.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function comparePassword(fld, cfld) {
	if(fld.value!=cfld.value) {
		//alert("Passwords do not match");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requireEmail(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var emailFilter=/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+[\.]?)+@(?:[a-z0-9-]+\.)+[a-z]{2,4}$/i;
	var illegalChars=/[\(\)\[\]\\\;\:\,\<\>]/i;
	if(tfld.length==0) {
		//alert("You didn't enter an email address.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(illegalChars.test(tfld)) {
		//alert("Email address contains illegal characters.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(!emailFilter.test(tfld)) {
		//alert("Invalid email address.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function optionalEmail(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var emailFilter=/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+[\.]?)+@(?:[a-z0-9-]+\.)+[a-z]{2,4}$/i;
	var illegalChars=/[\(\)\[\]\\\;\:\,\<\>]/i;
	if(tfld.length!=0) {
		if(illegalChars.test(tfld)) {
			//alert("Email address contains illegal characters.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else if(!emailFilter.test(tfld)) {
			//alert("Invalid email address.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requirePhone(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var stripped=tfld.replace(/[\(\)\.\-\s]/g, '');
	var illegalChars=/[^0-9\(\)\.\-\s]/;
	if(tfld.length==0) {
		//alert("You didn't enter a phone number.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(fld.value.match(illegalChars)) {
		//alert("Phone number contains illegal characters.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(!(stripped.length==10)) {
		//alert("Phone number is the wrong length. Include an area code.");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		fld.value=tfld.replace(/[\s\(]*(\d{3})[\)\s\.-]*(\d{3})[\s\.-]*(\d{4})[\s]*/, "($1) $2-$3");
		return true;
	}
}

function optionalPhone(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var stripped=tfld.replace(/[\(\)\.\-\s]/g, '');
	var illegalChars=/[^0-9\(\)\.\-\s]/;
	if(tfld.length!=0) {
		if(fld.value.match(illegalChars)) {
			//alert("Phone number contains illegal characters.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else if(!(stripped.length==10)) {
			//alert("Phone number is the wrong length. Include an area code.");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			fld.value=tfld.replace(/[\s\(]*(\d{3})[\)\s\.-]*(\d{3})[\s\.-]*(\d{4})[\s]*/, "($1) $2-$3");
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}

function requireDate(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var dateFilter=/^(0[1-9]|1[0-2])[\s\/\.-]*(0[1-9]|[1-2][0-9]|3[0-1])[\s\/\.-]*(19\d{2}|20\d{2})$/;
	var illegalChars=/[^0-9\s\/\.\-]/;
	if(tfld.length==0) {
		//alert("You didn't enter a date.");
		fld.style.border='1px solid red';
	} else if(fld.value.match(illegalChars)) {
		//alert("Date contains illegal characters. Enter a date as MM/DD/YYYY");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else if(!dateFilter.test(tfld)) {
		//alert("Invalid date. Enter a date as MM/DD/YYYY");
		fld.style.border='1px solid red';
		fld.style.background='#FFC1C2';
	} else {
		fld.style.border='';
		fld.style.background='';
		fld.value=tfld.replace(/^[\s\/\.-]*(\d{1,2})[\s\/\.-]*(\d{1,2})[\s\/\.-]*(\d{4})[\s\/\.-]*$/, "$1/$2/$3");
		return true;
	}
}

function optionalDate(fld) {
	var tfld=trim(fld.value);
	fld.value=tfld;
	var dateFilter=/^(0[1-9]|1[0-2])[\s\/\.-]*(0[1-9]|[1-2][0-9]|3[0-1])[\s\/\.-]*(19\d{2}|20\d{2})$/;
	var illegalChars=/[^0-9\s\/\.\-]/;
	if(tfld.length!=0) {
		if(fld.value.match(illegalChars)) {
			//alert("Date contains illegal characters. Enter a date as MM/DD/YYYY");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else if(!dateFilter.test(tfld)) {
			//alert("Invalid date. Enter a date as MM/DD/YYYY");
			fld.style.border='1px solid red';
			fld.style.background='#FFC1C2';
		} else {
			fld.style.border='';
			fld.style.background='';
			fld.value=tfld.replace(/^[\s\/\.-]*(\d{1,2})[\s\/\.-]*(\d{1,2})[\s\/\.-]*(\d{4})[\s\/\.-]*$/, "$1/$2/$3");
			return true;
		}
	} else {
		fld.style.border='';
		fld.style.background='';
		return true;
	}
}
