<%= error_messages_for 'query' %>

<div class="box">
  <div class="tabular">

    <p><label for="query_name"><%=l(:field_name)%></label>
    <%= text_field 'query', 'name', :size => 80 %></p>

    <% if User.current.admin? || User.current.allowed_to?(:manage_public_queries, @query.project) %>
    <p><label><%=l(:field_visible)%></label>
      <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PRIVATE %> <%= l(:label_visibility_private) %></label>
      <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PUBLIC %> <%= l(:label_visibility_public) %></label>
      <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_ROLES %> <%= l(:label_visibility_roles) %>:</label>
      <% Role.givable.sorted.each do |role| %>
        <label class="block role-visibility"><%= check_box_tag 'query[role_ids][]', role.id, @query.roles.include?(role), :id => nil %> <%= role.name %></label>
      <% end %>
      <%= hidden_field_tag 'query[role_ids][]', '' %>
    </p>
    <% end %>
  </div>

  <fieldset id="filters"><legend><%= l(:label_filter_plural) %></legend>
    <%= render :partial => 'queries/filters', :locals => {:query => query}%>
  </fieldset>

</div>

<%= javascript_tag do %>
$(document).ready(function(){
  $("input[name='query[visibility]']").change(function(){
    var roles_checked = $('#query_visibility_1').is(':checked');
    var private_checked = $('#query_visibility_0').is(':checked');
    $("input[name='query[role_ids][]'][type=checkbox]").attr('disabled', !roles_checked);
    if (!private_checked) $("input.disable-unless-private").attr('checked', false);
    $("input.disable-unless-private").attr('disabled', !private_checked);
  }).trigger('change');
});
<% end %>
