<script language="JavaScript" type="text/javascript">
<!--

var editURL = '<?php echo str_replace('&amp;', '&', Util::addParameter(Horde::url($registry->get('webroot', 'horde') . '/services/shares/edit.php?app=kronolith'), 'share', '@ID@')) ?>';
var cancelSubmit = false;
var fields = new Array();
<?php foreach ($personal_calendars as $id => $cal): ?>
fields['<?php echo addslashes($id) ?>'] = new Array(
        "<?php echo addslashes($cal->get('name')) ?>",
        "<?php echo preg_replace('(\r\n|\n|\r)', '\n', addslashes($cal->get('desc'))) ?>");
<?php endforeach; ?>

function newChoice()
{
    var action = document.shares.share.options[document.shares.share.selectedIndex].value;
    if (action == -1) {
        document.shares.save.value = '<?php echo addslashes(_("Create")) ?>';
        document.shares.accesslist.style.display = 'none';
        document.shares.reset();
        clearForm();
    } else {
        document.shares.save.value = '<?php echo addslashes(_("Change")) ?>';
        document.shares.accesslist.style.display = 'inline';
        updateForm(action);
    }
}

function updateForm(share)
{
    document.shares.edit_share.value = share;
    document.shares.id.value = fields[share][0];
    document.shares.description.value = fields[share][1];
}

function clearForm()
{
    document.shares.edit_share.value = '';
    document.shares.id.value = '';
    document.shares.description.value = '';
    document.shares.accesslist.style.display = 'none';
    cancelSubmit = true;
}

function checkSubmit()
{
    cancel = cancelSubmit;
    cancelSubmit = false;
<?php if ($browser->getBrowser() == 'msie' && $browser->getMajor() >= 4): ?>
    window.event.returnValue = !cancel;
<?php endif; ?>
    return !cancel;
}

function updateFBUrl()
{
    cals = new Array();
    for (i = 0; i < document.fb.fbcals.options.length; i++) {
        if (document.fb.fbcals.options[i].selected) {
            cals[cals.length] = document.fb.fbcals.options[i].value;
        }
    }

    if (cals.length == 0) {
        document.fb.fburl.value = '';
        return;
    } else if (cals.length == 1) {
        document.fb.fburl.value = '<?php echo Horde::applicationUrl('fb.php', true, -1) ?>?c=' + escape(cals[0]);
    } else {
        document.fb.fburl.value = '<?php echo Horde::applicationUrl('fb.php', true, -1) ?>?c[]=' + escape(cals[0]);
        for (i = 1; i < cals.length; i++) {
            document.fb.fburl.value += '&c[]=' + escape(cals[i]);
        }
    }
}

//-->
</script>

<form method="post" name="shares" action="<?php echo Horde::applicationUrl('calendars.php') ?>" onsubmit="return checkSubmit();">
<input type="hidden" name="actionID" value="save" />
<input type="hidden" name="edit_share" value="" />

<div class="header">
 <?php echo _("My Calendars") ?>&nbsp;
 <select name="share" onchange="newChoice()">
  <option value="-1"><?php echo _("Select a calendar") ?></option>
  <?php foreach ($personal_calendars as $id => $cal): ?>
  <option value="<?php echo $id ?>"><?php echo $cal->get('name') ?></option>
  <?php endforeach; ?>
 </select>
</div>
<br class="spacer" />

<table width="100%" cellspacing="0">
<tr>
 <td nowrap="nowrap" align="right">
  <b><?php echo _("Calendar Name") ?></b>&nbsp;
 </td>
 <td width="100%">
  <input type="text" name="id" size="30" maxlength="60" />
 </td>
</tr>

<tr>
 <td></td>
 <td>
  <input type="button" name="accesslist" class="button" onclick="popup(editURL.replace(/%40ID%40/, document.shares.edit_share.value));" value="<?php echo _("Permissions") ?>" style="display:none" />
 </td>
</tr>

<tr>
 <td align="right" valign="top" nowrap="nowrap">
  <b><?php echo _("Calendar Description") ?></b>&nbsp;
 </td>
 <td>
  <textarea wrap="hard" name="description" rows="4" cols="60"></textarea>
 </td>
</tr>
</table>

<br class="spacer" />
<div>
 <input type="submit" name="save" class="button" onclick="document.shares.actionID.value = 'save'; return true" value="<?php echo $actionID == 'edit' ? _("Change") : _("Create") ?>" />
 <input type="submit" name="delete" class="button" onclick="document.shares.actionID.value = 'delete'" value="<?php echo _("Delete") ?>" />
 <input type="submit" name="new" class="button" onclick="clearForm(); document.shares.save.value = '<?php echo _("Create") ?>';" value="<?php echo _("Reset") ?>" />
 <input type="submit" name="back" class="button" onclick="cancelSubmit = true; document.location.href = '<?php echo Horde::url(Util::addParameter($registry->get('webroot', 'horde') . '/services/prefs.php', array('group' => 'share', 'app' => 'kronolith')), true) ?>'" value="<?php echo _("Back to Calendar Options") ?>" />
</div>
</form>

<br />
<div class="header">
 <?php echo _("My Free/Busy URL") ?>&nbsp;
</div>
<br class="spacer" />

<form name="fb">
<p class="control">
 <?php echo _("Choose the calendars that you want to be included in your Free/Busy listings:") ?>
</p>

<p>
 <select multiple="multiple" name="fbcals" size="5" onchange="updateFBUrl()">
  <?php $calendars = Kronolith::listCalendars(); foreach ($calendars as $id => $cal): ?>
  <option value="<?php echo $id ?>"><?php echo $cal->get('name') ?></option>
  <?php endforeach; ?>
 </select>
</p>

<br class="spacer" />
<p class="control">
 <?php echo _("Copy the URL below for use wherever you need your Free/Busy URL:") ?>
</p>

<p>
 <input type="text" size="80" readonly="readonly" name="fburl" id="fburl" />
</p>
</form>
