
<h1 class="head"><i class="fa fa-user-plus" aria-hidden="true"></i> Benutzer anlegen</h1>

<%= form_for(@user) do |f| %>

<script type="text/javascript">
function vorschlag() {
   var vorsch = '';
   var len = Math.floor(8.5+Math.random()*6);
   var letters_left  = "qwertasdfgzxcvbQWERTASDFGZXCVB"+"123456"+'!$%';
   var letters_right = "uiophjklnmUIOPHJKLNM"+"7890"+'.,';
   var left=1;
   for( var i=0; i<len; ++i ) {
      var from = (left ? letters_left : letters_right);
      vorsch += from.substr(Math.floor(Math.random()*from.length), 1);
      left = 1-left;
   }
   var p1 = document.getElementById('user_password');
   var p2 = document.getElementById('user_password_confirmation');
   // change inputs to type „text“
   p1.setAttribute('type', 'text');
   p2.setAttribute('type', 'text');
   p1.setAttribute('value', vorsch); p1.value = vorsch;
   p2.setAttribute('value', vorsch); p2.value = vorsch;
}
</script>

<% unless @user.errors.empty? %>
    <p id="errorExplanation" class="notice notice__error">
	Fehlerhafte Eingaben:<br />
      <% @user.errors.each do |error| %>
        <%= error %><br />
      <% end %>
    </p>
  <% end %>

  <table border="0" class="form--settings">
    <tr><td>Benutzername:</td><td> <%= f.text_field :loginname %> </td></tr>
    <% if theUser(session).is_admin? %>
      <tr><td>Benutzerrolle:</td><td> <%= f.select :team_id, Team.order(:id).to_a.collect {|p| [p.name, p.id]} %> </td></tr>
      <tr><td>Benutzerverwaltungsrechte:</td><td> <%= admin_select(f) %> </td></tr>
    <% end %>
    <tr><td>Passwort:</td><td> <%= f.password_field :password %>&nbsp;<input type="button" value="Vorschlag..." onclick="vorschlag();"></td></tr>
    <tr><td>Passwort bestätigen:</td><td> <%= f.password_field :password_confirmation %> </td></tr>
    <tr>
      <td title="Zu verwaltende Bundesländer">Bundesländer:</td>
      <td class='form--selection-list'>
        <% for b in states do %>
            <label>
            <%= check_box_tag('user[federal_state_ids][]', b.id) %>
            <%= b.name %>
            </label>
            <br />
          <%  end %>
        </td>
      </tr>
    </table>
    <div class="from--submit">
      <a href="<%= url_for :action => "index" %>">
        <button type="button">
          <i class="fa fa-times" aria-hidden="true"></i>
        </button>
      </a>
      <button type="submit">
      <i class="fa fa-check" aria-hidden="true"></i>
      </button>
    </div>
    <% end %>
    </div>
