<script type="text/javascript">
	$(document).ready(function(){
		$("form#form-form").validate();
		$("form#form-form #form-name").rules("add", {required: true});
		$("form#form-form #form-email").rules("add", {required: true});
		
		
		$('form#form-form').on( 'change', 'select.type', function(){
			var type = $(this).val();
			if( type == 'SELECT' || type == 'RADIO' || type == 'CHECKBOX' ){
				$(this).closest('li').find('.values').show();
			} else{
				$(this).closest('li').find('.values').hide();
			}
		});
		$('form#form-form select.type').trigger('change');
		$('form#form-form').on( 'click', 'a.add_value', function(e){
			e.preventDefault();
			$(this).closest('div.values').find('ul').append('<li><input type="text" name="' + $(this).attr('href').substr(1) + '" value="" /></li>');
		});
		var i = 0;
		$('form#form-form a.add_field').click(function(e){
			e.preventDefault();
			i++;
			$('ul#fields').append('<li><label>{{$lang.Name}}: <input type="text" name="aNewFields['+i+'][sName]" value="" /></label> <label class="checkbox"><input type="checkbox" name="aNewFields['+i+'][iRequired]" value="1" /> {{$lang.Required}}</label> <label class="checkbox"><input type="checkbox" name="aNewFields['+i+'][iEmail]" value="1" /> {{$lang.Form_email_field}}</label> <label>{{$lang.Type}}: <select name="aNewFields['+i+'][sType]" class="type">{{foreach from=['INPUT' => $lang.Form_field_type_INPUT, 'TEXT' => $lang.Form_field_type_TEXT, 'SELECT' => $lang.Form_field_type_SELECT, 'RADIO' => $lang.Form_field_type_RADIO, 'CHECKBOX' => $lang.Form_field_type_CHECKBOX] item=sType}}<option value="{{$sType@key}}">{{$sType}}</option>{{/foreach}}</select></label><div class="values" style="display: none;"><ul></ul><a class="add_value" href="#aNewFields['+i+'][aValues][]">{{$lang.Add_value}}</a></div></li>');
		});
	});
</script>
<h1>{{$lang.Edit_form}}</h1>
<form action="?p={{$_GET.p}}" method="post" id="form-form" >
	<fieldset>
		<input type="hidden" name="iForm" value="{{$aForm.iForm}}" />
		<table class="details">
			<tr>
				<th class="name">{{$lang.Name}}</th><td class="value"><input id="form-name" type="text" name="sTitle" value="{{$aForm.sTitle}}" class="input" /></td>
				<td class="help"></td>
			</tr><tr>
				<th class="name">{{$lang.Email}}</th><td class="value"><input id="form-email" type="text" name="sEmail" value="{{$aForm.sEmail}}" class="input" /></td>
				<td class="help"></td>
			</tr>
			<tr><td colspan="3">
				<ul id="fields">
					{{foreach from=$aForm.aFields item=aField}}
						<li>
							<label>{{$lang.Name}}: <input type="text" name="aFields[{{$aField.iFormField}}][sName]" value="{{$aField.sName}}" /></label>
							<label class="checkbox"><input type="checkbox" name="aFields[{{$aField.iFormField}}][iRequired]" value="1" {{if $aField.iRequired}}checked="checked"{{/if}} /> {{$lang.Required}}</label>
							<label class="checkbox"><input type="checkbox" name="aFields[{{$aField.iFormField}}][iEmail]" value="1" {{if $aField.iEmail}}checked="checked"{{/if}} /> {{$lang.Form_email_field}}</label>
							<label>{{$lang.Type}}:
								<select name="aFields[{{$aField.iFormField}}][sType]" class="type">
									{{foreach from=['INPUT' => $lang.Form_field_type_INPUT, 'TEXT' => $lang.Form_field_type_TEXT, 'SELECT' => $lang.Form_field_type_SELECT, 'RADIO' => $lang.Form_field_type_RADIO, 'CHECKBOX' => $lang.Form_field_type_CHECKBOX] item=sType}}
										<option value="{{$sType@key}}" {{if $sType@key == $aField.sType}}selected="selected"{{/if}}>{{$sType}}</option>
									{{/foreach}}
								</select>
							</label>
							<div class="values">
								<ul>
									{{foreach from=$aField.aValues item=aValue}}
										<li><input type="text" name="aFields[{{$aField.iFormField}}][aValues][]" value="{{$aValue.sValue}}" /></li>
									{{/foreach}}
								</ul>
								<a class="add_value" href="#aFields[{{$aField.iFormField}}][aValues][]">{{$lang.Add_value}}</a>
							</div>
						</li>
					{{/foreach}}
				</ul>
				<a class="add_field" href="#">{{$lang.Add_field}}</a>
			</td></tr>
		</table>
		
		<div class="save float">
		<div class="save floatcontainer"><input type="submit" class="submit" value="{{$lang.Save}}" name="sOption" /></div>
		</div>
		
	</fieldset>
</form>