/*

This function is a form validation function, it will check all specified fields for values and validate email addresses
(email address field names MUST contain the word 'email' and non-email fields MUST NOT contain the word 'email')

Your form must contain a hidden field called 'required_fields' listing the field names seperated by a comma
and a hidden field called 'required_names' listing the 'nice' names of those fields on the same order

eg:
<input type="hidden" name="required_fields" value="fm_name,fm_email,fm_address">
<input type="hidden" name="required_names" value="Name,Email address,Home address">
*/

function checkForm(thisform){
	
	var rfields = thisform.required_fields.value.split(",");
	var rnames = thisform.required_names.value.split(",");
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/; 
	for(i=0;i<rfields.length;i++){
		var field = thisform[rfields[i]];
		if(!field.disabled){ //only check the field if it is NOT disabled
			if(field.type=='text'){
				thisvalue = field.value;
				if(thisvalue == ""){
					alert("'" + rnames[i] + "' is a required field.");
					return false;
				}else if(rfields[i].toLowerCase().indexOf("email")!=-1 && !re.test(thisvalue)){
					alert("The '" + rnames[i] + "' field should be a valid email address.");
					return false;
				}
			}else if(field.type=='checkbox'){
				if(!field.checked){
					alert("'" + rnames[i] + "' must be checked.");
					return false;
				}
			}
		}
	}
	return true;
}