<%= render :partial => 'abfrage_head' %>
<%= form_tag({:action => big_session[:abfrage][:art]==2 ? "taet_typ" : "schutzgr"}) %>
<input type="hidden" name="origin" value="naces" />

  <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>
</p>
<p>Sie können hier nach Nace-Code Filtern. Wenn Sie keinen Code wählen, wird nicht nach Nace-Code gefiltert.</p>

<table border="0">
  <tr>
    <td>Nace-Abschnitt:</td>
    <td>
      <select id="abschnitt" name="settings[abschnitt]" onchange="filterList()" class="multi">
        <option value="-1" title="Alle anzeigen">Alle anzeigen</option>
        <%= options_for_select(@nace_abschnitte_collected) %>
      </select>
    </td>
  </tr>
  <tr>
    <td>Nace-Code oder Text:</td>
    <td><input type="text" id="nace-text" name="settings[nace-text]"><br></td>
  </tr>
  <tr>
    <td>
      Auswahl:<br>
      <input type="checkbox" name="settings[sel_all]" id ="sel_all" value="1"<%= ' checked ' if session[:sel_all]=="0" %>
             onclick ="if (this.checked) selectAll()"> Automatisch alle ausw&auml;hlen<br>
      <button type="button" onclick="selectAll();">
        Alle ausw&auml;hlen
      </button>

    </td>

    <td>
      <select id="naces" name="nace[]" multiple class="multi" size="20">
        <%= option_groups_from_collection_for_select(@nace_abschnitte_filtered, big_session[:abfrage][:naces]) %>
      </select>
    </td>
  </tr>
</table>



<script type="text/javascript" language="javascript">


  function filterList() {
    var group = $('abschnitt');
    var txt = $('nace-text').value;
    $('naces').select('optgroup').each(function(g){
      var isEmpty = true;
      g.select('option').each(function(o){
        if ((group.value=='-1' || g.label==group.options[group.selectedIndex].text)
          && ((o.value.indexOf(txt)==0) || (o.text.toLowerCase().indexOf(txt.toLowerCase(),7)>=0))) {
          o.show();
          if ($('sel_all').checked) o.selected = true;
          isEmpty = false;
        }
        else {
          o.selected = false;
          o.hide();
        }
      });
      if (isEmpty) {
        g.hide();
      }
      else {
        g.show();
      }
    });
  }

  function selectAll() {
    $('naces').select('option').each(function(o){
      if (o.visible())
        o.selected = true;
    })
  }

  new Form.Element.Observer(
  'nace-text',
  0.4,  // 400 milliseconds
  function(el, value){
    filterList();
  }
)

  filterList();

</script>
