<script type="text/javascript">
//<![CDATA[
function add_filter() {
  select = $('add_filter_select');
  field = select.value
  Element.show('tr_' +  field);
  check_box = $('cb_' + field);
  check_box.checked = true;
  toggle_filter(field);
  select.selectedIndex = 0;

  for (i=0; i<select.options.length; i++) {
    if (select.options[i].value == field) {
      select.options[i].disabled = true;
    }
  }
}

function toggle_filter(field) {
  check_box = $('cb_' + field);
  if (check_box.checked) {
    Element.show("operators_" + field);
    Form.Element.enable("operators_" + field);
    toggle_operator(field);
  } else {
    Element.hide("operators_" + field);
    Form.Element.disable("operators_" + field);
    enableValues(field, []);
  }
}

function enableValues(field, indexes) {
  var f = $$(".values_" + field);
  for(var i=0;i<f.length;i++) {
    if (indexes.include(i)) {
      Form.Element.enable(f[i]);
      f[i].up('span').show();
    } else {
      f[i].value = '';
      Form.Element.disable(f[i]);
      f[i].up('span').hide();
    }
  }
  if (indexes.length > 0) {
    Element.show("div_values_" + field);
  } else {
    Element.hide("div_values_" + field);
  }
}

function toggle_operator(field) {
  operator = $("operators_" + field);
  switch (operator.value) {
    case "!*":
    case "*":
    case "t":
    case "w":
    case "o":
    case "c":
      enableValues(field, []);
      break;
    case "><":
      enableValues(field, [0,1]);
      break;
    case "<t+":
    case ">t+":
    case "t+":
    case ">t-":
    case "<t-":
    case "t-":
      enableValues(field, [2]);
      break;
    default:
      enableValues(field, [0]);
      break;
  }
}

function toggle_multi_select(el) {
  var select = $(el);
  if (select.multiple == true) {
    select.multiple = false;
  } else {
    select.multiple = true;
  }
}

function submit_query_form(id) {
  selectAllOptions("selected_columns");
  $(id).submit();
}

function apply_filters_observer() {
  $$("#query_form input[type=text]").invoke("observe", "keypress", function(e){
    if(e.keyCode == Event.KEY_RETURN) {
      submit_query_form("query_form");
    }
  });
}
Event.observe(document,"dom:loaded", apply_filters_observer);
//]]>
</script>

<table width="100%">
<tr>
<td>
<table>
<% query.available_filters.sort{|a,b| a[1][:order]<=>b[1][:order]}.each do |filter| %>
    <% field = filter[0]
       options = filter[1] %>
    <tr <%= 'style="display:none;"' unless query.has_filter?(field) %> id="tr_<%= field %>" class="filter">
    <td class="field">
        <%= check_box_tag 'f[]', field, query.has_filter?(field), :onclick => "toggle_filter('#{field}');", :id => "cb_#{field}" %>
        <label for="cb_<%= field %>"><%= filter[1][:name] || l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) %></label>
    </td>
    <td class="operator">
        <%= label_tag "op_#{field}", l(:description_filter), :class => "hidden-for-sighted" %>
        <%= select_tag "op[#{field}]", options_for_select(operators_for_select(options[:type]),
                        query.operator_for(field)), :id => "operators_#{field}",
                        :onchange => "toggle_operator('#{field}');" %>
    </td>
    <td class="values">
    <div id="div_values_<%= field %>" style="display:none;">
    <% case options[:type]
    when :list, :list_optional, :list_status, :list_subprojects %>
        <span class="span_values_<%= field %>">
          <%= select_tag "v[#{field}][]", options_for_select(options[:values], query.values_for(field)), :class => "values_#{field}", :id => "values_#{field}_1", :multiple => (query.values_for(field) && query.values_for(field).length > 1) %>
          <%= link_to_function image_tag('bullet_toggle_plus.png'), "toggle_multi_select('values_#{field}_1');" %>
        </span>
    <% when :date, :date_past %>
        <span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :size => 10, :class => "values_#{field}", :id => "values_#{field}_1" %> <%= calendar_for "values_#{field}_1" %></span>
        <span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field, 1), :size => 10, :class => "values_#{field}", :id => "values_#{field}_2" %> <%= calendar_for "values_#{field}_2" %></span>
        <span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :size => 3, :class => "values_#{field}" %> <%= l(:label_day_plural) %></span>
    <% when :string, :text %>
        <span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :class => "values_#{field}", :id => "values_#{field}", :size => 30 %></span>
    <% when :integer, :float %>
        <span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field), :class => "values_#{field}", :id => "values_#{field}_1", :size => 6 %></span>
        <span class="span_values_<%= field %>"><%= text_field_tag "v[#{field}][]", query.value_for(field, 1), :class => "values_#{field}", :id => "values_#{field}_2", :size => 6 %></span>
    <% end %>
    </div>
    <script type="text/javascript">toggle_filter('<%= field %>');</script>
    </td>
    </tr>
<% end %>
</table>
</td>
<td class="add-filter">
<%= label_tag('add_filter_select', l(:label_filter_add)) %>
<%= select_tag 'add_filter_select', options_for_select([["",""]] + query.available_filters.sort{|a,b| a[1][:order]<=>b[1][:order]}.collect{|field| [ field[1][:name] || l(("field_"+field[0].to_s.gsub(/_id$/, "")).to_sym), field[0]] unless query.has_filter?(field[0])}.compact),
                                    :onchange => "add_filter();",
                                    :name => nil %>
</td>
</tr>
</table>
<%= hidden_field_tag 'f[]', '' %>
