jQuery(document).ready( function() {

  // We target the friendly URL directly, without posting the form
  jQuery('input[name="Ntt"]').closest('form').submit(function(){

      // get search terms and clean them
      var searchTerms = jQuery(this).find('input[name="Ntt"]').val();
      searchTerms = searchTerms.replace(/[^a-z0-9_àáâãäçèéêëìíîïñòóôõöùúûüýÿ ]/gi, ' ');
      var tokens = searchTerms.split(' ');
      searchTerms = '';
      for(var i = 0; i < tokens.length; i++)
      {
        var token = jQuery.trim(tokens[i]);
        if(token != '')
        {
          searchTerms += token + ' ';
        }
      }
      searchTerms = jQuery.trim(searchTerms);
      searchTerms = searchTerms.replace(/[ ]/gi, '+');

      var action = jQuery(this).attr('action');
      if(!action.match(/\/$/))
      {
        action += '/';
      }

      // specific Action?
      var specificAction = '';
      var actionInput = jQuery(this).find('#header_search_type');
      if(actionInput)
      {
        var searchAction = actionInput.val();
        if(typeof searchAction != 'undefined' && searchAction != 'all')
        {
          specificAction = '?Action=' + searchAction;
        }
      }

      window.location = action + searchTerms + specificAction;
      return false;
  });

});

