
jQuery(function($){
  
  $('#search_query').parents('form').validate({
    rules: {
        
        'search[query]': {
                      required: true, maxlength: 30, minlength: 3        
        }    },    
    messages: {
              'search[query]': {
                      required: "This field is required.", maxlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too long (30 characters max)."; }, minlength: function(a, elem){ return "\"" + $(elem).val() + "\" is too short (3 characters min)."; }        
        }        
    },
    wrapper: 'div class="merchant-txt-box"',
    errorElement: 'div',

    success:  function(label) {
      
      label.parents('.merchant-input-area').find('.merchant-tick-box img').attr('src', "/images/tick.gif");
    },

    
    failure: function(label) {
    
      label.parents('.merchant-input-area').find('.merchant-tick-box img').attr('src', "/images/minus-bt-big.gif");
    },
    
    
    errorPlacement: function(error, element) 
    {
      var inputDiv = null;
    
      inputDiv =  element.parents('.merchant-input-area');
      if (inputDiv.find('.merchant-txt-box')) {
      
        inputDiv.find('.merchant-txt-box').remove();
      }
      
      if (inputDiv.find('.for-error').length) {
        error.appendTo(inputDiv.find('.for-error'));
      }
      else {
        error.appendTo( inputDiv);
      }
       
    }
  
  });
  
  
});

/* 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."
);