$(function() {

  $('form.form-detached').
    on('click', '.search', function(event) {
      event.preventDefault();
      processDetachedForm($(this).parents('form'));
    }).
    on('submit', function(event) {
      event.preventDefault();
      processDetachedForm($(this));
    });

  $('form.filter-form').
    on('click', '.search', function(event) {
      event.preventDefault();
      processFilterForm($(this).parents('form'), {submit: true});
    }).
    on('submit', function(event) {
      processFilterForm($(this), {submit: false});
    }).
    on('click', '.clear', function(event) {
      event.preventDefault();

      // Delete all parameters starting with q[
      _TABLE_.filterURLParameters(function(param) {
        return param.indexOf('q[') != 0;
      });
    }).
    on('change', '.number_field select', function(event) {
      event.preventDefault();
      $(this).parents('fieldset').find('input').attr('name', 'q[' + this.value + ']');
    });

  $('form.filter-form, form.form-detached').
    on('click', '.presentable', function(event) {
      event.preventDefault();
      var $this            = $(this);
      var $formGroup       = $this.parents('.form-group');
      var $closedContainer = $formGroup.find('.closed');

      updateToggleTitle($this);

      if ($closedContainer.length != 0) {
        $closedContainer.toggle();
      } else {
        var $selected = $formGroup.find('.radio.checked');
        var $radioSelection = $formGroup.find('.radio');

        if ($selected.length != 0) {
          $selected.removeClass('checked');
          $radioSelection.css('display','inline-block');
        } else {
          $radioSelection.hide();
          $selected = $formGroup.find('.radio label input[type="radio"]:checked').parents('.radio');
          $selected.addClass('checked').css('display','inline-block');
        }
      }


    });

  function updateToggleTitle($this) {
    switch($this.html()) {
      case "Expand":
        $this.html("Close");
        break;
      case "Close":
        $this.html("Expand");
        break;
      case "Add More":
        $this.html("Narrow");
        break;
      case "Narrow":
        $this.html("Add More");
        break;
    }
  }

  function processDetachedForm(detached) {
    var form = $('#' + detached.data('link'));
    mergeForms(form, detached);
    form.submit();
  }

  function processFilterForm(form, options) {
    var detached = $('#' + form.data('link'));
    mergeForms(form, detached);

    if (options.submit)
      form.submit();
  }

  function mergeForms(form, detached) {
    if (!form.data('merged')) {
      form.data('merged', true);
      detached.find(':input:not(:submit,:button)').clone().hide().appendTo(form);

      fieldOverrideSorting(form);
    }
  }

  function fieldOverrideSorting(form) {
    var field_sorting   = form.find('[data-sort]:first');
    var current_sorting = form.find('[name=sort]');

    if (field_sorting.val() && !current_sorting.val()) {
      current_sorting.val(field_sorting.data('sort'));
    }
  }
});

@@ -9,16 +9,42 @@
 on() %7B%0A%0A
+  /** Detached form%0A   */%0A
   $('for
@@ -54,32 +54,32 @@
 orm-detached').%0A
-
     on('click', 
@@ -986,32 +986,888 @@
  '%5D');%0A    %7D);%0A%0A
+  function processDetachedForm(detached) %7B%0A    var form = $('#' + detached.data('link'));%0A    mergeForms(form, detached);%0A    form.submit();%0A  %7D%0A%0A  function processFilterForm(form, options) %7B%0A    var detached = $('#' + form.data('link'));%0A    mergeForms(form, detached);%0A%0A    if (options.submit)%0A      form.submit();%0A  %7D%0A%0A  function mergeForms(form, detached) %7B%0A    if (!form.data('merged')) %7B%0A      form.data('merged', true);%0A      detached.find(':input:not(:submit,:button)').clone().hide().appendTo(form);%0A%0A      fieldOverrideSorting(form);%0A    %7D%0A  %7D%0A%0A  function fieldOverrideSorting(form) %7B%0A    var field_sorting   = form.find('%5Bdata-sort%5D:first');%0A    var current_sorting = form.find('%5Bname=sort%5D');%0A%0A    if (field_sorting.val() && !current_sorting.val()) %7B%0A      current_sorting.val(field_sorting.data('sort'));%0A    %7D%0A  %7D%0A%0A  /** Inputs folding%0A   */%0A
   $('form.filter
@@ -2751,18 +2751,16 @@
       %7D%0A
-%0A%0A
     %7D);%0A
@@ -3072,24 +3072,24 @@
 Add More%22);%0A
+
         brea
@@ -3104,837 +3104,8 @@
 %0A  %7D
-%0A%0A  function processDetachedForm(detached) %7B%0A    var form = $('#' + detached.data('link'));%0A    mergeForms(form, detached);%0A    form.submit();%0A  %7D%0A%0A  function processFilterForm(form, options) %7B%0A    var detached = $('#' + form.data('link'));%0A    mergeForms(form, detached);%0A%0A    if (options.submit)%0A      form.submit();%0A  %7D%0A%0A  function mergeForms(form, detached) %7B%0A    if (!form.data('merged')) %7B%0A      form.data('merged', true);%0A      detached.find(':input:not(:submit,:button)').clone().hide().appendTo(form);%0A%0A      fieldOverrideSorting(form);%0A    %7D%0A  %7D%0A%0A  function fieldOverrideSorting(form) %7B%0A    var field_sorting   = form.find('%5Bdata-sort%5D:first');%0A    var current_sorting = form.find('%5Bname=sort%5D');%0A%0A    if (field_sorting.val() && !current_sorting.val()) %7B%0A      current_sorting.val(field_sorting.data('sort'));%0A    %7D%0A  %7D
 %0A%7D);

Reorder code cc:@Thorsson