function validateForm(form, rules) {
  //clear out any old errors
  jQuery(".error-message").hide();
  // By default we consider the form as valid
  var valid_form = true;
  //loop through the validation rules and check for errors
  jQuery.each(rules, function(field) 
  {

	var val = jQuery.trim(jQuery("#" + field).val()); // value of the selected field
    
    jQuery.each(this, function() 
	{

      //check if the input exists
      if (jQuery("#" + field).attr("id") != undefined) 
	  {
        var valid = true; // By default the field is supposed as valid
        
        if (this['allowEmpty'] && val == '') // if empty and allowEmpty == true
		{
			//do nothing
        } 
		else if (this['rule'].match(/^range/)) // value between 2 numbers
		{
			var range = this['rule'].split('|');
			if (val < parseInt(range[1])) // less
			{
				valid = false;
			}
			if (val > parseInt(range[2])) // more
			{
				valid = false;
			}
			//alert('coucou 1');
        } 
		else if (this['negate']) // must not match with the regex
		{
			if (val.match(eval(this['rule']))) 
			{
				valid = false;
			}
			//alert('coucou 2');
        } 
		else if (!val.match(eval(this['rule']))) // must match with the regex
		{
			valid = false;
			//alert(this['rule']+'coucou 3');
        }
        
        if (!valid) {		
          // We change the class of the label (ex : highlight)
          jQuery("label[for='" + field + "']").addClass("error");
		  
		  // We change the class of the field (ex : red border)
		  jQuery("#"+field).addClass("form-error"); 
		  
		  // We add the message after the field
		  jQuery("#"+field).after('<div class="error-message">'+this['message']+'</div>'); 
		  
		  // The form is not valid, it must no be submitted
		  valid_form = false;
        }
      }
    });
  });
  
	// We disable the form submission if an error has occured
	if(!valid_form)
	{
		return false;		
	}
	
  return true;
}