<%= render :partial => 'abfrage_head' %>
<%= form_tag({:action => :result}, :onsubmit => 'return proof()') %>
<input type="hidden" name="origin" value="filter_columns" />

<p>
  <button type="submit">
    <%= image_tag('16-em-check.png',{:border => 0}) %>
  </button>
  <button type="button" onclick="window.location='<%= url_for :action => "index" %>'">
    <%= image_tag('16-em-cross.png',{:border => 0}) %>
  </button> <br />
  Präselektion <%= select_tag 'chooser', options_for_select($prepared,big_session[:abfrage][:spalten]?'custom':'complicated') , :onchange => 'apply_selection(this.value)' %>
</p>


<table border="0" width ="100%" cellspacing ="10" id="boxen">
  <tr>
    <td id ="betrieb">
        <% prev = nil %>
        <% for col in $columns %>
          <% #puts col %>
          <% col_name = col.first.split(":") %>
          <% col_name.insert(0,"") if col_name.length==1 %>
          <% if col_name.first!=prev %>
            <% unless prev.nil? %>
              </div>
            <% end %>
            <div class="col_group">
              <h2><%= col_name.first %></h2>
            <% prev = col_name.first %>
          <%end%>
              <%= check_box_tag 'columns[]', col.last, (big_session[:abfrage][:spalten].include?(col.last.to_s) rescue true) %> <%= col_name.last %> <br />
        <% end %>
            </div>
    </td>

    <td>
      <% if [2,6].include?(big_session[:abfrage][:art]) %>
        <%= render :partial => 'columns', :locals => {:box_name => 'columns_taet[]', :cols => $werte[2], :sess_vals => big_session[:abfrage][:spalten_taet], :heading => 'Tätigkeiten'} %>
      <%  end %>

      <% if [3,6].include?(big_session[:abfrage][:art]) %>
        <%= render :partial => 'columns', :locals => {:box_name => 'columns_freis[]', :cols => $werte[3], :sess_vals => big_session[:abfrage][:spalten_freis], :heading => 'Freisetzungen'} %>
      <%  end %>

      <% if [4,6].include?(big_session[:abfrage][:art]) %>
        <%= render :partial => 'columns', :locals => {:box_name => 'columns_abw[]', :cols => $werte[4], :sess_vals => big_session[:abfrage][:spalten_abw], :heading => 'Verbringung von Abwasser'} %>
      <%  end %>

      <% if [5,6].include?(big_session[:abfrage][:art]) %>
        <%= render :partial => 'columns', :locals => {:box_name => 'columns_abf[]', :cols => $werte[5], :sess_vals => big_session[:abfrage][:spalten_abf], :heading => 'Verbringung von Abfall'} %>
      <%  end %>
    </td>

  </tr>
</table>


<script language="javascript" type="text/javascript">
  selector = <%= $prepared.to_json %>;
  columnse = <%= $columns.to_json %>;
              
         
              
  function apply_selection(group_id) {
    if (group_id=='custom') return;
    var group;
    var element;
    var init = false;
    if (group_id=='all') init = true;
    $('boxen').select('input').each(function(i){
      i.checked = init;
    });
    if (group_id=='all' || group_id=='none') return;
    selector.each(function(g){
      if (g[2]==group_id) {
        group = g[1];
      }
    });
    columnse.each(function(col){
      group.each(function(e){
        if (e==col[0]){
          element = $('betrieb').select('input')[col[1]]
          //element = $('column'+col[1]);
          element.checked = true;
          //col.checked = true;
        }
      });
    });

      <% if [2,6].include?(big_session[:abfrage][:art]) %>
        select_boxes("columns_taet", group_id, <%= $werte[2] %>, <%= StartController::EXTRA_EIGENSCHAFTEN_GRUPPEN[2] %> );
      <%  end %>

      <% if [3,6].include?(big_session[:abfrage][:art]) %>
        select_boxes("columns_freis", group_id, <%= $werte[3] %>, <%= StartController::EXTRA_EIGENSCHAFTEN_GRUPPEN[3] %> );
      <%  end %>

      <% if [4,6].include?(big_session[:abfrage][:art]) %>
        select_boxes("columns_abw", group_id, <%= $werte[4] %>, <%= StartController::EXTRA_EIGENSCHAFTEN_GRUPPEN[4] %> );
      <%  end %>

      <% if [5,6].include?(big_session[:abfrage][:art]) %>
        select_boxes("columns_abf", group_id, <%= $werte[5] %>, <%= StartController::EXTRA_EIGENSCHAFTEN_GRUPPEN[5] %> );
      <%  end %>



  }

  function select_boxes(box_name, selection, all_cols, sel_cols) {
    var group;
     sel_cols.each(function(g){
      if (g[2]==selection) {
        group = g[1];
      }
    });
    all_cols.each(function(col){
      group.each(function(e){
        if (e==col[0]){
          var element = $$('input.'+box_name)[col[1]]
          //element = $('column'+col[1]);
          element.checked = true;
          //col.checked = true;
        }
      });
    });
  }

  function proof() {
    var proof = false
   $('checkboxes').select('input').each(function(i){
      if (i.checked == true){

        proof = true;
      }
    });
    if (proof) {
     return true;}
    else {
    alert('Sie haben keine Betriebsspezifische Ergebnisspalte ausgewählt');
      return false;
    };
  }
       
   <% unless big_session[:abfrage][:spalten] %>
      apply_selection('complicated');
   <% end %>
</script>
