/*

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

*/

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++){
		if(!thisform[rfields[i]].disabled){ //only check the field if it is NOT disabled
			thisvalue = thisform[rfields[i]].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;
			}
		}
	}
	return true;
}
