
jQuery(function($){
  
  $('#contact_name').parents('form').validate({
    rules: {
        
        'contact[name]': {
                      required: true, maxlength: 60        
        },  
        'contact[address]': {
                      required: true, maxlength: 150        
        },  
        'contact[zipcode]': {
                      required: true, maxlength: 7        
        },  
        'contact[town]': {
                      required: true, maxlength: 60        
        },  
        'contact[phone]': {
                      required: true, maxlength: 20        
        },  
        'contact[email]': {
                      email: true, required: true        
        },  
        'contact[message]': {
                      required: true, maxlength: 1000        
        },  
        'contact[need]': {
                              
        }    },    
    messages: {
              'contact[name]': {
                      required: "Ce champ est obligatoire.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (60 characters max)."; }        
        },        
        'contact[address]': {
                      required: "Ce champ est obligatoire.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (150 characters max)."; }        
        },        
        'contact[zipcode]': {
                      required: "Ce champ est obligatoire.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (7 characters max)."; }        
        },        
        'contact[town]': {
                      required: "Ce champ est obligatoire.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (60 characters max)."; }        
        },        
        'contact[phone]': {
                      required: "Ce champ est obligatoire.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (20 characters max)."; }        
        },        
        'contact[email]': {
                      required: "Ce champ est obligatoire."        
        },        
        'contact[message]': {
                      required: "Ce champ est obligatoire.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (1000 characters max)."; }        
        },        
        'contact[need]': {
                      required: "Ce champ est obligatoire."        
        }        
    },
    wrapper: 'ul class=error_list',
    errorElement: 'li',
    errorPlacement: function(error, element) 
    {
     if(element.parents('.radio_list').is('*') || element.parents('.checkbox_list').is('*'))
     {
       error.prependTo( element.parent().parent().parent() );
     }
     else
     {
       error.prependTo( element.parent().parent().prev().find('td.formerror') );
     }
   }
  
  });
  
  
});

/* for some reason the jQuery Validate plugin does not incluce a generic regex method */
jQuery.validator.addMethod(
  "regex",
  function(value, element, regexp) {
      if (regexp.constructor != RegExp)
          regexp = new RegExp(regexp);
      else if (regexp.global)
          regexp.lastIndex = 0;
      return this.optional(element) || regexp.test(value);
  },
  "Invalid."
);