<%= form_for(@agent,
             :as => :agent,
             :url => @agent.new_record? ? agents_path : agent_path(@agent),
             :method => @agent.new_record? ? "POST" : "PUT") do |f| %>
  <% if @agent.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@agent.errors.count, "error") %> prohibited this Agent from being saved:</h2>
      <ul>
      <% @agent.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class='pull-right well description span6'>
    <%= @agent.html_description unless @agent.new_record? %>
  </div>

  <div class='pull-right well event-descriptions span6 hidden'>
  </div>

  <% if @agent.new_record? %>
    <div class="control-group type-select">
      <%= f.label :type, :class => 'control-label' %>
      <div class="controls">
        <%= f.select :type, options_for_select(Agent.types.map(&:to_s).sort.map {|type| [type.gsub(/^.*::/, ''), type] }, @agent.type), {}, :class => 'span4 select2' %>
      </div>
    </div>
  <% end %>

  <div class="control-group">
    <%= f.label :name, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :name, :class => 'span4' %>
    </div>
  </div>

  <div class="control-group">
    <%= f.label :schedule, :class => 'control-label' %>
    <div class="controls schedule-region" data-can-be-scheduled="<%= @agent.can_be_scheduled? %>">
      <%= f.select :schedule, options_for_select(Agent::SCHEDULES.map {|s| [s.humanize.titleize, s] }, @agent.schedule), {}, :class => 'span4' %>
      <span class='cannot-be-scheduled text-info'>This type of Agent cannot be scheduled.</span>
    </div>
  </div>

  <div class='event-related-region' data-can-create-events="<%= @agent.can_create_events? %>">
    <div class="control-group">
      <%= f.label :keep_events_for, "Keep events", :class => 'control-label' %>
      <div class="controls">
        <%= f.select :keep_events_for, options_for_select(Agent::EVENT_RETENTION_SCHEDULES, @agent.keep_events_for), {}, :class => 'span4' %>
      </div>
    </div>
  </div>

  <div class="control-group">
    <%= f.label :sources, :class => 'control-label' %>
    <div class="controls link-region" data-can-receive-events="<%= @agent.can_receive_events? %>">
      <% eventSources = (current_user.agents - [@agent]).find_all { |a| a.can_create_events? } %>
      <%= f.select(:source_ids,
                   options_for_select(eventSources.map {|s| [s.name, s.id] },
                                      @agent.source_ids),
                   {}, { :multiple => true, :size => 5, :class => 'span4 select2' }) %>
      <span class='cannot-receive-events text-info'>This type of Agent cannot receive events.</span>
      <span class="propagate-immediately"><br>
         <%= f.label :propagate_immediately, :class => 'control-label' do %>Propagate immediately
            <%= f.check_box :propagate_immediately %> 
         <% end %>
      </span>
    </div>
  </div>

  <div class="control-group">
    <%= f.label :options, :class => 'control-label' %>
    <div class="controls">
      <textarea rows="10" id="agent_options" name="agent[options]" class="span8 live-json-editor <%= (@agent.new_record? && @agent.options == {}) ? "showing-default" : "" %>">
        <%= Utils.jsonify((@agent.new_record? && @agent.options == {}) ? @agent.default_options : @agent.options) %>
      </textarea>
    </div>
  </div>

  <div class='form-actions' style='clear: both'>
    <%= f.submit :class => "btn btn-primary" %>
  </div>
<% end %>
