<h1>{$page.title}</h1>

{display var=$smarty.session.company}

{include_php file="shared_handle:preferences/groupUpdate"}

{assign var="currentCategory" value=""}

<form method="post" action="index.php">
<input type="hidden" name="pg" value="{$page.name}" />

{literal}
<script type="text/javascript">
function checkNumber(value, min, max, precision) {
	if(value == '') return;
	var message = '';
	if(value < min || value > max) {
		message = 'Value must be between ' + min + ' and ' + max + '.\n\n';
		if(precision > 0) {
			message += 'NOTE: Value may be truncated after ' + precision + ' decimal places.';
		}
	}
	if (value.match(/.*\..*\./)) {
		message += 'Please enter a valid number.';
	}
	if(message) alert(message);
}
</script>
{/literal}
{display var=$prefValues}
<table>
{foreach from=$aPrefs item=row}
{* display var=$row *}
{assign var="lockedName" value="`$row.name`_locked"}
{if $currentCategory neq $row.category}
	{assign var="currentCategory" value=$row.category}
	<tr><td colspan="2"><h2 class="withBG">{$currentCategory}</h2></td></tr>
{/if}
<tr><td valign="top">{$row.description}</td>
{if $row.inputType|test_or:"select-one":"select-multiple":"radios":"checkboxes"}
	{assign_array var="aOptions" values=$row.validInput}
	{assign_array var="aValues" values=$prefValues[$row.name]}
	{assign_array var="aDefaults" values=$row.defaultValue}
	{* display var=$aDefaults *}
	{display var=$aValues}
{/if}
	<td valign="top">
{if $row.inputType eq "string"}
	<input type="text" size="{$row.inputSize}" maxlength="{$row.validInput}" name="prefs[{$row.name}]" value="{$prefValues[$row.name]|default:$row.defaultValue}"{if $prefValues[$lockedName]} disabled="disabled"{/if} />
{elseif $row.inputType eq "integer"}
	<input type="text" size="{$row.inputSize}" name="prefs[{$row.name}]" onkeyup="if(this.value.match(/[^0-9]/))this.value = this.value.replace(/[^0-9]/,'');" onblur="checkNumber(this.value, {$row.validInput});" value="{$prefValues[$row.name]|default:$row.defaultValue}"{if $prefValues[$lockedName]} disabled="disabled"{/if} />
{elseif $row.inputType eq "float"}
	<input type="text" size="{$row.inputSize}" name="prefs[{$row.name}]" onkeyup="if(this.value.match(/[^0-9\.]/))this.value = this.value.replace(/[^0-9\.]/,'');" onblur="checkNumber(this.value, {$row.validInput});" value="{$prefValues[$row.name]|default:$row.defaultValue}"{if $prefValues[$lockedName]} disabled="disabled"{/if} />
{elseif $row.inputType eq 'select-one' OR $row.inputType eq 'select-multiple'}
	<select name="prefs[{$row.name}]{if $row.inputType eq 'select-multiple'}[]" multiple="multiple" size="{$row.inputSize}{/if}"{if $prefValues[$lockedName]} disabled="disabled"{/if}>
	{foreach from=$aOptions item="option"}
		<option value="{$option}"{if (!$prefValues[$row.name] AND $option|@inArray:$aDefaults) OR ($option|@in_array:$aValues)} selected="selected"{/if}>{$option}</option>
	{/foreach}</select>
{elseif $row.inputType eq "radios"}
	{foreach from=$aOptions item="option"}
		<input type="radio" name="prefs[{$row.name}]" value="{$option}"{if (!$prefValues[$row.name] AND $option|@inArray:$aDefaults) OR ($option eq $prefValues[$row.name])} checked="checked"{/if}{if $prefValues[$lockedName]} disabled="disabled"{/if}> {$option}<br />
	{/foreach}
{elseif $row.inputType eq "checkboxes"}
	{foreach from=$aOptions item="option"}
		<input type="checkbox" name="prefs[{$row.name}][]" value="{$option}"{if (!$prefValues[$row.name] AND !$row.name|@inArray:$savedPrefs AND $option|@inArray:$aDefaults) OR ($option|@inArray:$aValues)} checked="checked"{/if}{if $prefValues[$lockedName]} disabled="disabled"{/if}> {$option}<br />
	{/foreach}
	<input type="hidden" name="prefs[{$row.name}][]" value="END:{$row.name}" /> 
{elseif $row.inputType eq "regex"}
	<input type="text" size="{$row.inputSize}" name="prefs[{$row.name}]" onchange="if(!this.value.match({$row.validInput})) alert ('\'' + this.value + '\' is not valid for \'{$row.description}\'.')" value="{$prefValues[$row.name]|default:$row.defaultValue}"{if $prefValues[$lockedName]} disabled="disabled"{/if} />
{else}
	Unrecognized Input Type! <b>({$row.inputType})</b>
{/if} 
	</td>
</tr>
{/foreach}
</tr>
</table>

<div align="right">
	<input type="submit" name="prefAction" value="Save Changes" />
</div>
</form>