<% var e, error, n, notice; %>

<% if (!_.isEmpty(m.errors)) { %>
	<% foreach (m.errors as e => error) { %>
		<div class="error">
			{{zenario.htmlspecialchars(error.message, true)}}
		</div>
	<% } %>
<% } %>	
<% foreach (m.notices as n => notice) { %>
	<div class="{{notice.type|escape}}">
		<% if (notice.html) { %>
			{{notice.message}}
		<% } else { %>
			{{zenario.htmlspecialchars(notice.message, true)}}
		<% } %>
	</div>
<% } %>

<% if (m.fields) { %>
	<%
		var f,
			field,
			ff = m.fields[0],
			fl = m.fields.length,
			lf = m.fields[fl-1],
			pgf = ff._lastVisibleGroupingField,
			lgf = lf._lastVisibleGroupingField,
			rowHasError = false;
	%>
	
	<% foreach (m.fields as f => field) { %>
		<% if (field.error) { %>
			<% rowHasError = true; %>
			<% break; %>
		<% } %>	
	<% } %>
	
	<% if (ff._lastVisibleGrouping !== ff.grouping) { %>
		<% if (pgf) { %>
			</fieldset>
			<% if (pgf.post_grouping_html) { %>
				{{pgf.post_grouping_html}}
			<% } %>
			<% if (!ff.grouping || m.grouping.start_new_grouping_wrapper) { %>
				</x-zenario-grouping-wrapper>
			<% } %>
		<% } %>
		<% if (ff.grouping) { %>
			<% if (!pgf || m.grouping.start_new_grouping_wrapper) { %>
				<x-zenario-grouping-wrapper
					class="
						zfea_grouping_wrapper
						{{m.grouping.grouping_wrapper_css_class|escape}}
					"
					style="{{m.grouping.grouping_wrapper_style|escape}}"
				>
					<% if (m.grouping.grouping_wrapper_icon_class) { %>
						<div class="zfea_grouping_wrapper_icon {{m.grouping.grouping_wrapper_icon_class|escape}}">
							<% if (zenario.defined(m.grouping.grouping_wrapper_record_count)) { %>
								<span class="zfea_grouping_wrapper_record_count">{{m.grouping.grouping_wrapper_record_count|escape}}</span>
							<% } %>
						</div>
					<% } %>
			<% } %>
			<% if (m.grouping.pre_grouping_html) { %>
				{{m.grouping.pre_grouping_html}}
			<% } %>
			<fieldset
				class="
					zfea_form_grouping zfea_form_grouping__{{ff.grouping|escape}}
					{{m.grouping.grouping_css_class|escape}}
					
					<% if (ff.type == 'radio') { %>
						zfea_form_grouping_with_input zfea_form_grouping_with_radio
						
						<% if (!_.isEmpty(ff.value)) { %>
							zfea_form_grouping_with_input_selected zfea_form_grouping_with_radio_selected
						<% } %>
					<% } %>
					<% if (ff.type == 'checkbox') { %>
						zfea_form_grouping_with_input zfea_form_grouping_with_checkbox
						
						<% if (!_.isEmpty(ff.value)) { %>
							zfea_form_grouping_with_input_selected zfea_form_grouping_with_checkbox_selected
						<% } %>
					<% } %>
				"
				style="{{m.grouping.grouping_style|escape}}"
				onclick="{{m.grouping.grouping_onclick|escape}}"
			>
		<% } %>
	<% } %>
	<% if (ff.legend) { %>
		<legend class="zfea_form_legend zfea_form_legend_for__{{ff._id|escape}}"><span>{{ff.legend}}</span></legend>
	<% } %>
	
	<% if (ff.error_on_form_message && m.thus.errorOnForm) { %>
		<div class="error">
			{{zenario.htmlspecialchars(ff.error_on_form_message, true)}}
		</div>
	<% } %>
	
	<div id="{{m.lib.idPrefix()|escape}}row__{{ff._id|escape}}" style="{{ff.row_style|escape}}" class="
		zfea_form_row
		zfea_form_row__{{ff._id|escape}}
		{{rowHasError? 'zenario_row_with_error' : ''}}
		{{ff.error? 'zenario_row_with_error_on_first_field' : ''}}
		{{ff.row_class|escape}}
		{{ff.pressed? 'zfea_row_pressed' : ''}}
		{{ff.onoff? 'zenario_onoffswitch zenario_onoffswitch_visitor' : ''}}
		{{ff._hideOnOpen? 'zfea_form_hide_row_on_open' : ''}}
		{{ff.indent? 'zfea_form_indent_level zfea_form_indent_level_' +  (1*ff.indent) : ''}}
		zfea_row_for_{{ff.snippet? 'snippet' : ff.upload? 'upload' : ff.pick_items? 'pick_items' : ff.type |escape}}
		
		<% if (fl > 1) { %>
			zfea_row_with_multiple_fields zfea_row_with_{{fl}}_fields
		<% } else { %>
			zfea_row_with_1_field
		<% } %>
		
		<% if (ff.pre_field_html || ff.post_field_html) { %>
			zfea_row_with_pre_or_post_field_html
		<% } %>
		
		{{m.lib.classesForRowContents('zfea_row_with_', m.fields)}}
	">
		<div class="
			zfea_row_fields
			{{ff._hideOnOpen? 'zfea_form_hide_on_open' : ''}}
			{{ff._showOnOpen? 'zfea_form_show_on_open' : ''}}
		">	
			<% if (ff.label !== undefined && !(ff.type == 'checkbox' || ff.type == 'radio')) { %>
				<div class="title">
					<label for="{{ff._id|escape}}">{{ff.label|escape}}</label>
					
					<% if (ff.pre_field_post_label_html) { %>
						{{ff.pre_field_post_label_html}}
					<% } %>
				</div>
			<% } %>
			<div class="
				zfea_row_fields_inner
				{{ff._hideOnOpen? 'zfea_form_hide_on_open' : ''}}
				{{ff._showOnOpen? 'zfea_form_show_on_open' : ''}}
			">
				{{m.lib.microTemplate(m.lib.mtPrefix + '_field', m.fields)}}
			</div>
			<% foreach (m.fields as f => field) { %>
				<% if (field.error && field.error != true) { %>
					<div class="zfea_inline_error">
						{{zenario.htmlspecialchars(field.error, true)}}
					</div>
				<% } %>	
			<% } %>	
		</div>
	</div>
	
	<% if (ff._isLastRow) { %>
		<% if (ff.grouping) { %>
			</fieldset>
			<% if (lgf && lgf.post_grouping_html) { %>
				{{lgf.post_grouping_html}}
			<% } %>
			</x-zenario-grouping-wrapper>
		<% } %>
	<% } %>
<% } %>