{% load suit_tags %}
<fieldset class="module aligned {{ fieldset.classes }}">
    {% if fieldset.name %}
        <h2 class="legend">{{ fieldset.name }}
            {% if fieldset.description %}
                <span class="description">{{ fieldset.description|safe }}</span>
            {% endif %}
        </h2>{% endif %}
    {% for line in fieldset %}
        <div class="control-group form-row{% if line.fields|length_is:'1' and line.errors %} error errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
            {% for field in line %}

                {# write special control tags only for first multi field #}
                {% if forloop.counter == 1 %}
                    <div{% if not line.fields|length_is:'1' %}
                        class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}"{% endif %}>

                    <div class="control-label">
                        {{ field.label_tag }}
                    </div>
                    <div class="controls">
                {% else %}
                    {{ field.label_tag }}
                {% endif %}

            {% if not field.is_readonly and field.errors %}<div class="inline error errors">{% endif %}

            {% if field.is_readonly %}
                <p>{{ field|field_contents_foreign_linked }}</p>
            {% else %}
                {{ field.field }}
            {% endif %}

            {% if field.field.help_text %}
                {% if not line.fields|length_is:'1' %}
                    <a title="{{ field.field.help_text|safe }}"><i
                            class="help-icon icon-question-sign icon-alpha75"></i></a>
                {% endif %}
            {% endif %}

            {# For single field errors#}
            {% if line.fields|length_is:'1' and line.errors %}
                <span class="help-inline">{{ line.errors }}</span>
            {% endif %}

            {# For multi field errors #}
            {% if field.errors and not line.fields|length_is:'1' and not field.is_readonly %}
                <a title="{{ field.errors|striptags }}"><i
                        class=" icon-exclamation-sign icon-alpha75"></i></a>
            {% endif %}

            {% if field.field.help_text %}
                {% if line.fields|length_is:'1' %}
                    <span class="{% if line.errors or field.errors %}help-block{% else %}help-inline{% endif %}">{{ field.field.help_text|safe }}</span>
                {% endif %}
            {% endif %}

            {% if not field.is_readonly and field.errors %}</div>{% endif %}

            {% if forloop.last %}
                </div>
                </div>
            {% endif %}

            {% endfor %}

        </div>
    {% endfor %}
</fieldset>
