{% extends "formunculous/base.html" %}

{% load i18n  widget_type formunculous_tags %}
{% block css %}
{{ block.super }}
{{ form.media }}
<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" />
<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.formset.js"></script>

{% if subforms %}
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/apply_sub_form.js"></script>

<script type="text/javascript">
  jQuery(function($) {
  {% for subform in subforms %}
  $('#formset-{{ subform.formset.prefix }} .apply_sub_form_form').formset({
  prefix: '{{ subform.formset.prefix }}',
  formCssClass: 'dynamic-formset-{{subform.formset.prefix}}',
  addText: 'Add Another Form',
  addCssClass: 'apply_sub_form_addlink',
  deleteText: 'Remove',
  deleteCssClass: 'apply_sub_form_deletelink',
  maxForms: {% if not subform.formset.max_num %}0{% else %}{{ subform.formset.max_num }}{% endif %},
  alertText: '{% trans "You have reached the maximum number of forms for this group"%}'
  });
  {% endfor %}
  });
</script>
{% endif %}

{% if history %}
{% if not ad.email_only %}
<script type="text/javascript" src="{{ MEDIA_URL }}formunculous/js/history.js"></script>
<script type="text/javascript">
  var historyURL = '{% url formunculous-apply-history %}'
</script>
{% endif %}
{% endif %}


{% endblock %}


{% block title %}{{ ad.name }}{% endblock %}
{% block content_title %}{{ ad.name }}{% endblock %}
{% block content %}

{% if history %}
{% if not ad.email_only %}
<div class="formunculous_tab_bar">
  <ul>
	<li class="here" id="formunculous-current">
	  <a href="#" class="formunculous_currentlink">{% trans "Current" %}</a>
	</li>
	<li id="formunculous-history">
	  <a href="#" class="formunculous_historylink">{% trans "History" %}</a>
	</li>
  </ul>
</div>


<div class="formunculous_history_bar">
  <div class="formunculous_history_list">
	<p>View previously completed {{ ad.name }} forms:</p>
	<ul>
	  {% for item in history %}
	  <li>
		<a href="#" onclick="getHistory({{ item.id }},this);">{{ item.submission_date|date:"D d M Y" }} {{ item.submission_date|time:"g:i A" }}</a>
	  </li>
	  {% endfor %}
	</ul>
  </div>
  <div id="formunculous-history-app">
	
  </div>
</div>
{% endif %}
{% endif %}

<div class="formunculous_container">
  {% if message %}
  <div class="apply_messages">{{ message }}</div>
  {% endif %}

  <form method="post" action="{% url formunculous-apply ad.slug %}"{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}


	{% if form.errors %}
	<div class="apply_field_errors">{% trans "There are errors in your form, please correct them" %}
	</div>
	{% endif %}

	<fieldset class="apply_fieldset">
	  {% for field in fields %}
  	  {% if field.field.is_hidden %}
	  {{ field.field }}
	  {% else %}

	  {% render_as_template field.pre_text %}

	  {% ifchanged field.group %}
	  {% if field.group %}
	  <div class="apply_form_group">
		{% else %}
		{% if not forloop.first %}
	  </div>
	  {% endif %}
	  {% endif %}
	  {% endifchanged %}
	  {% widget_type field.field.field.widget as widget %}
	  <div class="apply_field_row{% cycle ' odd' '' %}{% if field.field.errors %} apply_row_errors{% endif%}">
		<div class="apply_field_errors">{{ field.field.errors }}</div>
		<div class="apply_field{% if field.required %} apply_required{% endif %}">
		  {%  ifequal widget "CheckboxInput" %}
		  {{ field.field }}
		  {% if field.field.label %}
		  <label for="{{ field.field.auto_id}}" class="apply_cb_label">
			{{field.field.label}}
		  </label>
		  {% endif %}
		  {% else %}
  		  {% if field.field.label %}
 		  {{ field.field.label_tag }}
		  {% endif %}
		  {{ field.field }}
		  {% endifequal %}
		</div>
		<div style="clear:both;"></div>
		{% if field.field.help_text %}
		<div class="apply-info-box">
		  {{ field.field.help_text }}
		</div>
		{% endif %}
	  </div>
	  {% if forloop.last and field.group %}
</div>
{% endif %}
{% render_as_template field.post_text %}
{% endif %}
{% endfor %}
</fieldset>


{# Begin displaying all the subform formsets #}
{% for subform in subforms %}
<div class="apply_sub_form_container">
  <div class="apply_sub_form_title">
	<span class="apply_sub_form_field_collapse" style="margin-top: 6px"></span>
	<h3>{{ subform.sub_ad.name }}</h3>
  </div>
  <div class="apply_sub_form_body" id="formset-{{ subform.formset.prefix }}">
	{{ subform.formset.management_form }}
	{% if subform.formset.non_form_errors %}
	<div class="apply_field_errors">
	  {{ subform.formset.non_form_errors }}
	</div>
	{% endif %}
	{% for form in subform.forms %}
	<div class="apply_sub_form_form{% cycle ' ' " apply_sub_form_odd" %}">
	  {% if form.form.errors %}
	  <div class="apply_field_errors">
		{% trans "There are errors in your form, please correct them" %}
	  </div>
	  {% endif %}
	  <fieldset class="apply_fieldset">
		{% for field in form.fields %}
  		{% if field.field.is_hidden %}
	    {{ field.field }}
		{% else %}

	    {% render_as_template field.pre_text %}

	    {% ifchanged field.group %}
	    {% if field.group %}
	    <div class="apply_form_group">
	      {% else %}
	      {% if not forloop.first %}
	    </div>
		{% endif %}
	    {% endif %}
		{% endifchanged %}
		{% widget_type field.field.field.widget as widget %}
	    <div class="apply_field_row{% cycle ' odd' ' even' %}{% if field.field.errors %} apply_row_errors{% endif%}">
	      <div class="apply_field_errors">{{ field.field.errors }}</div>
	      <div class="apply_field{% if field.required %} apply_required{% endif %}">
			{%  ifequal widget "CheckboxInput" %}
			{{ field.field }}
			{% if field.field.label %}
			<label for="{{ field.field.auto_id}}" class="apply_cb_label">
			  {{field.field.label}}
			</label>
			{% endif %}
			{% else %}
  			{% if field.field.label %}
 			{{ field.field.label_tag }}
			{% endif %}
			{{ field.field }}
			{% endifequal %}
	      </div>
		  <div style="clear:both;"></div>
		  {% if field.field.help_text %}
		  <div class="apply-info-box">
			{{ field.field.help_text }}
		  </div>
		  {% endif %}
		</div>
	    {% if forloop.last and field.group %}
    </div>
    {% endif %}
	{% render_as_template field.post_text %}
	{% endif %}
	{% endfor %}
  </div>
  
  {% endfor %}
</div>
</div>
{% endfor %}
{% if not preview %}
<div class="apply_submit{% if subforms %} apply_sub_form_submit{% endif %}">
  {% if ad.authentication %}<input type="submit" name="save" value="Save" />{% endif %}<input type="submit" name="submit" value="Submit" />
</div>
{% endif %}

</form>
</div>
{% endblock %}
