{% extends "formunculous/builder_base.html" %}
{% load i18n %}

{% block extrastyle %}{{ block.super }}
<link href="{{ MEDIA_URL }}formunculous/css/formunculous.css" rel="stylesheet" type="text/css" />
<link href="{{ MEDIA_URL }}formunculous/css/apply.css" rel="stylesheet" type="text/css" />
<link href="{{ MEDIA_URL }}formunculous/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css">

{{ forms.media }}
{% endblock %}

{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/urlify.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/toggle_ck.js"></script>
{% if is_parent %}
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/subform.js"></script>
{% endif %}

<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/field_edit.js"></script>

<script type="text/javascript">

  jQuery(function($) {
  // Preview Dialog Handling
  $('#prev_dialog').dialog({
  autoOpen: false,
  modal: true,
  width: 650,
  height: 500,
  resizable: true,
  buttons: {
  "Close": function() {
  $(this).dialog("close");
  }
  },
  bgiframe: true
  });

  {% if formset.errors %}
  $(".formunculous_field_body").each(function() {
  if($(this).children('.formunculous_errors').size()>0)
  $(this).slideToggle(300);
  });
  {% endif %}

  });

  function show_preview_dialog(id)
  {
  current_app_id = id;
  jQuery("#prev_dialog").html('<iframe height="98%" frameborder="0" width="100%" style="border: 0;" src="{% url builder-preview-ad %}?ad=' + current_app_id + '">Your browser does not support iframes</iframe>');

  jQuery("#prev_dialog").dialog('open');
  }


  // These are rendered with the template for use in the field_edit.js 
  // and add_subform.js scripts
  var new_form_url = "{% url builder-add-field ad.slug %}";
  var drop_down_url = "{% url builder-add-dropdown %}";
  var new_subapp_url = "{% url builder-add-subapp %}";
  var delete_subapp_url = "{% url builder-delete-ad %}";
  var change_subapp_url = "{% url builder-change-subapp %}";
  var builder_index_url = "{% url builder-index %}";

</script>

{% endblock %}

{% block title %}
{% trans "Editing Form for " %} {{ ad.name }}
{% endblock %}


{% block content %}

<h1>{% trans "Editing Form for " %} {{ ad.name }}</h1>


<div id="prev_dialog" title="{% trans "Form Definition Preview" %}">
</div>
<div id="dropdown_dialog" title="{% trans "Field Dropdown Selections" %}">
</div>
{% if is_parent %}
  {% include "formunculous/includes/sub_app_dialogs.html" %}
{% endif %}


<div id="formunculous-page-container">
  <div id="formunculous-field-toolbar">
	<h4 style="line-height: 1.1em;margin:0;padding:0;">{% trans "Field Types" %}</h4>
	<ul class="formunculous_field_types">
	  {% for type in field_types %}
	  <li class="ui-state-default formunculous-new-field">
		<span class="ui-icon ui-icon-arrow-4" style="float:left;"></span>
		{{type.name}}
		<span style="display:none" class="field_type_selection">{{ type.type }}</span>
	  </li>
	  {% endfor %}
	</ul>
	<br />
	<div class="formunculous_info_box" style="padding:0;">
	  {% trans "Drag the desired field type above to the list on the right to add a new field to the form." %}
	</div>
  </div>
  <div id="formunculous-field-container">
	<div id="ajax_results"></div>
	<div id="formunculous-previewdiv">
	  {% if not is_parent %}
	  <a href="{% url builder-edit-fields ad.parent.slug %}" class="formunculous_uplink">
		{% trans "Edit Parent Form" %}
	  </a>
	  {% endif %}
	  <a href="{% if is_parent %}{% url builder-edit-ad ad.slug %}{% else %}{% url builder-edit-ad ad.parent.slug %}{% endif %}" class="formunculous_changelink">
		{% trans "Edit Form Definition" %}
	  </a>
	  <a href="#" class="formunculous_previewlink" onclick="javascript:show_preview_dialog({% if is_parent %}{{ ad.id }}{% else %}{{ ad.parent.id }}{% endif %});">
		{% trans "Preview" %}
	  </a>
	</div>
	<form method="POST" action="">{% csrf_token %}
	  <div class="formunculous_edit_buttons">
		<input type="submit" name="finish" value="Save and Finish" />
		<input type="submit" name="edit" value="Save and Continue Editing" />
	  </div>
	  {{ formset.management_form }}
	  {% if formset.errors %}
	  <div class="formunculous_errors">{% trans "There are errors in your form, please correct them" %}</div>
	  {% endif %}

	  <ul class="formunculous_field_list">
		{% for form in formset.forms %}
		{% include "formunculous/includes/fd_fields.html" %}
		{% endfor %}
	  </ul>
	  <div class="formunculous_edit_buttons">
		<input type="submit" name="finish" value="Save and Finish" />
		<input type="submit" name="edit" value="Save and Continue Editing" />
	  </div>
	</form>
  </div>
  <div style="clear:both"></div>
  {% if is_parent %}
    {% include "formunculous/includes/sub_app_table.html" %}
  {% endif %}
</div>

{% endblock %}
