<script language="JavaScript" type="text/javascript">
<!--
<?php if (isset($js_right)): ?>
var users = new Array(<?php echo $js_user ?>);

<?php foreach ($js_right as $key => $val): ?>
var <?php echo $key ?> = new Array(<?php echo $val ?>);
<?php endforeach; ?>
<?php endif; ?>

function selectRule(num)
{
<?php if ($canEdit): ?>
    if (num == <?php echo count($curr_acl) ?>) {
        resetForm();
    } else {
        document.acl.share_user.value = users[num];  
        document.acl.share_user.disabled = true;
<?php if (!empty($js_right)): ?>
<?php foreach ($js_right as $key => $val): ?>
        if (<?php echo $key ?>[num] > 0) {
            document.acl.elements['acl[<?php echo $key ?>]'].checked = true;
        } else {
            document.acl.elements['acl[<?php echo $key ?>]'].checked = false;
        }
<?php endforeach; ?>
<?php endif; ?>
        document.acl.actionID.value = 'imp_acl_edit';
        document.acl.fbutton.value = '<?php echo addslashes(_("Modify")) ?>';
    }
<?php endif; ?>
}

function resetForm()
{
    document.acl.reset();
    document.acl.share_user.disabled = false;
    document.acl.actionID.value = 'imp_acl_set';
    document.acl.fbutton.value = '<?php echo addslashes(_("Create")) ?>';
    if (document.acl.number) {
        for (var i = 0; i < document.acl.number.length -1 ; i++) {
            document.acl.number[i].checked = false;
        }
    }
}

var loading;
function ACLFolderChange(clear)
{
    if (document.folders.folder[document.folders.folder.selectedIndex].value != '') {
        if (loading == null || clear != null) {
            loading = true; 
            disableButtons(); 
            document.folders.submit();
        }
    }
}

function disableButtons() {
    if (document.acl.fbutton) {
        document.acl.fbutton.disabled = true;
    }
    if (document.acl.resetbut) {
        document.acl.resetbut.disabled = true;
    }    
    if (document.acl.back) {
        document.acl.back.disabled = true;
    }    
    if (document.acl.number) {
        for (var i = 0; i < document.acl.number.length; i++) {
            document.acl.number[i].disabled = true;
        }
    }
    return true;
}


// -->
</script>

<form method="post" name="folders" action="<?php echo Horde::applicationUrl('acl.php') ?>">
<input type="hidden" name="actionID" value="" />
<?php Util::pformInput() ?>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
  <td colspan="2" class="header"><?php echo _("Share mail folders") ?></td>
  <?php Prefs_UI::generateNavigationCell('acl') ?>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
  <td colspan="2">

    <table width="100%" cellspacing="0">
    <tr>
      <td align="left" class="header" nowrap="nowrap"><?php echo sprintf(_("Current access to %s"), String::convertCharset($folder, 'UTF7-IMAP')) ?></td>
      <td align="right" class="header" nowrap="nowrap">
        <?php echo Horde::link('', _("Change Folder"), 'smallheader', '', 'ACLFolderChange(true); return false;') ?>
        <?php echo Horde::img('shared.png', _("Change Folder")) ?></a>
        <select name="folder" onchange="ACLFolderChange()"><?php echo $options ?></select>&nbsp;
      </td>
    </tr>
    </table>
  </td>
</tr>
</form>
<form method="post" name="acl" action="<?php echo Horde::applicationUrl('acl.php') ?>">
<input type="hidden" name="actionID" value="imp_acl_set" />
<input type="hidden" name="folder" value="<?php echo $folder ?>" />
<?php Util::pformInput() ?>
<?php if (!count($curr_acl)): ?>
<tr>
  <td colspan="2" class="item" align="left"><i><b>&nbsp;<?php echo _("The current list of users with access to this folder could not be retrieved") ?></b></i></td>
</tr>
<?php else: ?>
<?php $i = 1; foreach ($curr_acl as $index => $rule): ?>
<tr>
  <td class="light" align="right"><b><?php echo $i++ ?>&nbsp;</b></td>
  <td class="item" align="left">
    <input class="item" type="radio" name="number" value="<?php echo $i-2 ?>" <?php if (in_array($index, $protected)) echo 'disabled ' ?>onclick="selectRule(<?php echo $i - 2 ?>)" />&nbsp;&nbsp;
    <b><?php echo $index ?></b>&nbsp;&nbsp;
    <?php foreach ($rule as $key => $val): ?><?php echo $key ?>,&nbsp;<?php endforeach; ?>
  </td>
</tr>
<?php endforeach; ?>
<tr>
  <td class="light" align="right"><b><?php echo _("New") ?>&nbsp;</b></td>
  <td class="item" align="left">
    <input class="item" type="radio" checked name="number" value="<?php echo $i - 1 ?>" onclick="selectRule(<?php echo $i - 1 ?>)" />&nbsp;&nbsp;
  </td>
</tr>
<?php endif; ?>

<tr><td>&nbsp;</td></tr>
<tr>
  <td colspan="2" class="header"><b class="smallheader"><?php echo _("Grant access") ?></b></td>
</tr>

<?php if ($canEdit): ?>
<tr>
  <td class="light" align="right"><b><?php echo _("User") ?>&nbsp;</b></td>
  <td class="item" align="left">
    &nbsp;<input class="fixed" type="text" name="share_user" />
  </td>
</tr>
<?php foreach ($rights as $right => $desc): ?>
<tr>
  <td class="light" align="right"><b><?php echo $right ?>&nbsp;:&nbsp;</b></td>
  <td class="item" align="left">
    <input type="checkbox" name="acl[<?php echo $right ?>]" />
    &nbsp;<?php echo $desc ?>
  </td>
</tr>
<?php endforeach; ?>
<tr>
  <td>&nbsp;</td>
  <td class="header" valign="middle">
    <input type="button" class="button" name="fbutton" onclick="disableButtons(); document.acl.share_user.disabled = false; document.acl.submit();" value="<?php echo _("Create") ?>" />
    <input type="button" class="button" name="resetbut" onclick="resetForm()" value="<?php echo _("Reset") ?>" />
    <input type="button" class="button" name="back" onclick="disableButtons(); document.location.href='<?php echo $prefs_url ?>'" value="<?php echo _("Return to Options") ?>" />
  </td>
</tr>

<?php else: ?>
<tr>
  <td colspan="2" class="item" align="left"><i><b>&nbsp;<?php echo _("You do not have permission to change access to this folder") ?></b></i></td>
</tr>
<tr>
  <td colspan="2" class="header" valign="middle">
    <input type="button" class="button" name="back" onclick="disableButtons(); document.location.href='<?php echo $prefs_url ?>'" value="<?php echo _("Return to Options") ?>" />
  </td>
</tr>
<?php endif; ?>
</table>
</form>
