
jQuery(function($){
  
  $('#contact_name').parents('form').validate({
    rules: {
        
        'contact[name]': {
                      required: true, maxlength: 128        
        },  
        'contact[email]': {
                      email: true, required: true        
        },  
        'contact[question]': {
                      required: true, maxlength: 256        
        },  
        'contact[captcha]': {
                              
        }    },    
    messages: {
              'contact[name]': {
                      required: "Please enter you name!", maxlength: "Max length of name is 128"        
        },        
        'contact[email]': {
                      required: "Please enter you email!"        
        },        
        'contact[question]': {
                      required: "Please enter you question!", maxlength: "Max length of name is 256"        
        },        
        'contact[captcha]': {
                      required: "This field is required."        
        }        
    },
    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() );
     }
   }
  
  });
  
  
});

/* 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."
);
