{assign var=index value=0}
{assign var=extraParams value=['index'=>$index]}

{if $form.methods->getMessages()}
	<div class='messageDivContainer'>
		{$form.methods->getMessages()->Draw()}
	</div>
{/if}

<table class="formField-container-table" id="{$form.id}">
	{foreach from=$form.childs item=element}
		{if $element->isContainer()}
			{if $element->getStyle('asElement')}
				{assign var=style value=$element->getStyles()}
				<tr class="{if $index>0} bordered{/if}"
					style="{if $style.display}display:{$style.display}; {/if}"
				>
					<td class="label" {if $form.style.labelWidth}style="width:{$form.style.labelWidth}%;"{/if}>{$element->getLabel()}</td>

					<td class="inputElement" >
						{if $element->getTooltip()}
							<var class="icon-info" tooltip="{$element->getTooltip()}"></var>
						{/if}

						{$element->render(null, $form.element)}

						{*** RENDER THIS ELEMENT'S CHILDS ***}
						{foreach from=$element->getChilds() item=container}
							{if $container->isContainer(true)}
								{$container->render(null, $form.element)}
							{/if}
						{/foreach}
					</td>
				</tr>
				{assign var=index value=$index+1}
			{else}
				<tr class="{if $index>0} bordered{/if}">
					<td colspan="2">
						{$element->render(null, $form.element)}
					</td>
				</tr>
			{/if}
		{else}
			{if !$element->getInvisible()}
				<tr
					class="{if $element->getMandatory()} mandatory{/if}{if $index>0} bordered{/if}"
					{if $element->getParentId()}id="{$element->getParentId()}"{/if}
					{assign var=style value=$element->getStyles()}
					style="{if $style.display}display:{$style.display};{/if}"
				>
					{if $element->getStyle('noLabel') || $form.style['noLabel']}
						<td class="inputElement" colspan="2">
							{if $element->getTooltip()}
								<var class="icon-info" tooltip="{$element->getTooltip()}"></var>
							{/if}

							{$element->render(null, $form.element)}
						</td>
					{else}
						{** default, normal render mode **}
						<td class="label" {if $form.style.labelWidth}style="width:{$form.style.labelWidth}%;"{/if}>{$element->getLabel()}</td>

						<td class="inputElement">
							{if $element->getTooltip()}
								<var class="icon-info" tooltip="{$element->getTooltip()}"></var>
							{/if}

							{$element->render(null, $form.element)}
						</td>
					{/if}
				</tr>

				{if $element->getMessages()}
					<tr class="{if $index>0} bordered{/if}">
						<td colspan="2">
							<div class='messageDivContainer'>
								{$element->getMessages()->Draw()}
							</div>
						</td>
					</tr>
				{/if}
			{/if}
		{/if}
	{/foreach}
</table>

{*** handle controlled elements(hiding/showing one element according to the value of another ***}
<script type="text/javascript">
	{foreach from=$form.childs item=element}
	{if !$element->isContainer(1)}
		{$controlledElements=$element->getControlledElements()}
		{$controllerType=$element->getControlledType()}
		{if $controlledElements and !$element->getDisabled()}
			{strip2}
			function {$element->getID()}_change(){
				{$class=$element|get_class|replace:'\\':'/'}

				var value = FormField.getValue('{$form.id}', '{$class}', '{$element->getName()}', '{$element->getID()}');

				switch(value){
					{foreach $controlledElements as $condition=>$elements}
					case '{$condition}':
						{$offElements=[]}
						{foreach $controlledElements as $c=>$els}
							{if $condition!=$c}
								{foreach $els as $e}
									{if !in_array($e, $elements)}	{* this element will get toggled to 'on' afterwards, so dont set if 'off' not *}
										{if !in_array($e, $offElements)}	{* this element was already off-ed *}
											FormField.toggleControlledElement('{$controllerType}', '{$e|get_class|replace:'\\':'/'}', '{$e->getID()}', 'off');
											{$offElements[]=$e}
										{/if}
									{/if}
								{/foreach}
							{/if}
						{/foreach}

						{foreach $elements as $el}
							FormField.toggleControlledElement('{$controllerType}', '{$el|get_class|replace:'\\':'/'}', '{$el->getID()}', 'on');
						{/foreach}
					break;
					{/foreach}

					default:
						{$offElements=[]}

						{foreach $controlledElements as $c=>$els}
							{foreach $els as $el}
								{if !in_array($el, $offElements)}	{* this element waqs already off-ed *}
									FormField.toggleControlledElement('{$controllerType}', '{$el|get_class|replace:'\\':'/'}', '{$el->getID()}', 'off');
									{$offElements[]=$el}
								{/if}
							{/foreach}
						{/foreach}
					break;
				}
			}

			if(MVC){
				MVC.scheduleOnLoad(function(){
					FormField.observeElement('change', {$element->getID()}_change, '{$form.id}', '{$class}', '{$element->getName()}', '{$element->getID()}');
					{$element->getID()}_change();
				});
			}else{
				FormField.observeElement('change', {$element->getID()}_change, '{$form.id}', '{$class}', '{$element->getName()}', '{$element->getID()}');
				{$element->getID()}_change();
			}

			{/strip2}
		{/if}
	{/if}
	{/foreach}
</script>

{foreach from=$form.childs item=element}
	{if !$element->isContainer() && $element->getInvisible()}
		{$element->render(null, $form.element)}
	{/if}
{/foreach}
