function validate(checks) {
	/*
	~ provides a function named validate() which takes an array of triples:
		[
		[<mixed>, <pattern>, <error_message>],
		[<mixed>, <pattern>, <error_message>],
		]
		example:
		[
		[inputEmail,   'email',     'Incorrect Email Address'],
		[inputAddress, 'non_blank', 'Please Fill in your Address'],
		[inputPhone,   'non_blank', 'Please Fill in your Phone Number'],
		]
		~ <subject>
		* what should be checked
		if the <pattern> is 'email' <mixed> is an element
		if the <pattern> is 'non_blank' <mixed> is an element
		if the <pattern> is 'radiogroup' <mixed> is the array of radio buttons in the group
		if the <pattern> is 'identity' <mixed> is an array of strings
		if the <pattern> is 'chekcbox' <mixed> is the checkbox element
	~ <pattern>
		* string that describes what <string> must fulfill

		'email'
			checks against a pattern created by PPK:
			/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
			first parameter should be the text input element object
			
		'non_blank'
			means that the string is not the empty string ''
			first parameter should be the text input element object
			
		'radiogroup'
			check that some radio button has been checked

		'password'
			check that password contains between 8 and 15 characters with at least 2 numerals

		'identity'
			checks that an array of strings are identical (useful for repeat password check)
			parameters should be text input elements

		'checkbox'
			checks that a chekcbox is checked
			first parameter should be the checkbox element

		<error_message>
			* the alert message that will display if any of the triples don't match the type

	~ returns true if all the triples pass the test

	how to use:
		1. this script should be included in the head for example
		2. called like this:
		var inputEmail = document.getElementById('input_email');
		var inputAddress = document.getElementById('input_address');
		var checks = [
			[inputEmailValue, 'email', 'Incorrect Email'],
			[inputAddressValue, 'non_blank', 'Please Fill in Address']
		];
		var theForm = document.getElementById('myform').onsubmit = function () {
			return validate(checks);
		}
	*/
	var check;
	var subject;
	var pattern;
	var error_message;

	var reEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	// loop through all the checks
	for(var i = 0; i < checks.length; i++) {
		check = checks[i];
		pattern = check[1];
		error_message = check[2];
		switch(pattern) {
			case 'non_blank':
				subject = check[0].value;
				if(subject == '') {
					check[0].focus();
					alert(error_message);
					return false;
				}
				break;
			case 'email':
				subject = check[0].value;
				if(!reEmail.test(subject)) {
					check[0].focus();
					alert(error_message);
					return false;
				}
				break;
			case 'radiogroup':
				subject = check[0];
				var found = false
				for(var j = 0; j < subject.length; j++) {
					if(subject[j].checked == true) {
						found = true;
					}
				}
				if(!found) {
					alert(error_message);
					return false;
				}
				break;
			case 'password':
				subject = check[0].value;
				re8_15 = /^[a-zA-Z0-9]{8,15}$/;
				re2_num = /[0-9]+\w*[0-9]+/;
				if(!re8_15.test(subject) || !re2_num.test(subject)) {
					check[0].focus();
					alert(error_message);
					return false;
				}
				break;
			case 'identity':
				subject = check[0];
				for(var j = 1; j < subject.length; j++) {
					if(subject[j-1].value != subject[j].value) {
						check[0][0].focus();
						alert(error_message);
						return false;
					}
				}
				break;
			case 'checkbox':
				subject = check[0];
				if(!subject.checked) {
					check[0].focus();
					alert(error_message);
					return false;
				}
				break;
		}
	}
	return true;
}

