
function validateForm(form, rules) {

	var passed = true;
	
	//clear out any old errors
	$('.vhelper').remove();
	
	//loop through the validation rules and check for errors
	$.each(rules, function(field) {
	
		var val = $.trim($("#" + field).val());

		$.each(this, function() {
	
			//check if the input exists
			if ($("#" + field).attr("id") != undefined) {
				var valid = true;
	
				if (this['allowEmpty'] && val == '') {
	
					//do nothing
	
				} else if (this['rule'].match(/^range/)) {
	
					var range = this['rule'].split('|');
	
					if (val < parseInt(range[1])) {
						valid = false;
					}
	
					if (val > parseInt(range[2])) {
						valid = false;
					}
				} else if (this['negate']) {
					if (val.match(eval(this['rule']))) {
						valid = false;
					}
				} else if (!val.match(eval(this['rule']))) {
					valid = false;
				}
	
				if (!valid) {
					$("#" + field).after('&nbsp;&nbsp;' + this['message'] + "");
					$("#" + field).parent().addClass("error");
					passed = false;
				}
			}
		});
	});
	
	return passed;
}
