<?php

// initialize row alternating var
$_i = 0;
?>
<form action="<?php echo ($event->isInitialized() ? 'editeventaction.php' : 'addeventaction.php') ?>" method="post" name="event">
<?php Horde::pformInput() ?>
<input type="hidden" name="year" value="<?php if (isset($year)) echo $year ?>" />
<input type="hidden" name="month" value="<?php if (isset($month)) echo $month ?>" />
<input type="hidden" name="mday" value="<?php if (isset($day)) echo $day ?>" />
<input type="hidden" name="timestamp" value="<?php if (isset($timestamp)) echo $timestamp ?>" />
<input type="hidden" name="new_category" value="" />
<?php if (isset($url)): ?>
<input type="hidden" name="url" value="<?php echo $url ?>" />
<?php endif; ?>
<?php if ($event->isInitialized()): ?>
<input type="hidden" name="eventID" value="<?php echo $event->getID() ?>" />
<?php endif; ?>
<?php if ($event->isInitialized()): ?>
<input type="hidden" name="existingcalendar" value="<?php echo $event->getCalendar() ?>" />
<?php endif; ?>
<center>
<table border="0" cellspacing="0" cellpadding="4" align="center">

<!-- header -->
<tr>
 <td align="left" colspan="4" class="header"><b><?php echo $event->isInitialized() ? sprintf(_("Edit %s"), htmlspecialchars($event->getTitle())) : _("Add Event") ?></b></td>
</tr>

<!-- buttons -->
<tr>
 <td align="right" colspan="4">
  <?php echo implode("\n", $buttons); ?>
  <input type="submit" name="cancel" class="button" value="<?php echo _("Cancel") ?>" onclick="self.location = '<?php echo $cancelurl; ?>'; return false;" />
  <input type="button" name="resetButton" class="button" value="<?php echo _("Reset to Defaults") ?>" onclick="document.event.reset(); updateWday('start_wday'); updateWday('end_wday');" />
 </td>
</tr>

<!-- title -->
<tr>
 <td align="right" class="light"><b><?php echo Horde::highlightAccessKey(_("Title"), $ak = Horde::getAccessKey(_("Title"))) ?>&nbsp;&nbsp;</b></td>
 <td align="left" class="item<?php echo($_i++ % 2)?>" colspan="3">
  <input type="text" name="title" <?php echo (!empty($ak)) ? 'accesskey="' . $ak . '" ' : '' ?>value="<?php echo htmlspecialchars($event->getTitle()) ?>" size="40" maxlength="255" />
 </td>
</tr>

<!-- calendar -->
<?php if (!$prefs->isLocked('default_share') && count($calendars = Kronolith::listCalendars())): ?>
<tr>
 <td align="right" class="light"><b><?php echo Horde::label('targetcalendar', _("Calendar")) ?>&nbsp;&nbsp;</b></td>
 <td align="left" class="item<?php echo($_i++ % 2)?>" colspan="3">
    <select id="targetcalendar" name="targetcalendar">
    <?php
    foreach ($calendars as $id => $cal) {
        if ($cal->hasPermission(Auth::getAuth(), _PERMS_EDIT)) {
            $sel = ($id == $calendar_id) ? ' selected="selected"' : '';
            echo sprintf('<option value="%s"%s>%s</option>',
                         $id, $sel, $cal->getShareName()) . "\n";
        }
    } ?>
    </select>

 </td>
</tr>
<?php else: ?>
<input type="hidden" name="targetcalendar" value="<?php echo htmlspecialchars($calendar_id) ?>" />
<?php endif; ?>

<!-- category -->
<tr>
 <td align="right" class="light">
    <b><?php echo Horde::label('category', _("Category")) ?>&nbsp;&nbsp;</b>
 </td>
 <td align="left" class="item<?php echo($_i++ % 2)?>" colspan="3">
  <?php echo Kronolith::buildCategoryWidget('category', $event->getCategory(), !$prefs->isLocked('event_categories')) ?>
</td>
</tr>

<!-- location -->
<tr>
 <td align="right" class="light"><b><?php echo Horde::highlightAccessKey(_("Location"), $ak = Horde::getAccessKey(_("Location"))) ?>&nbsp;&nbsp;</b></td>
 <td align="left" class="item<?php echo($_i++ % 2)?>" colspan="3">
  <input type="text" name="location" <?php echo (!empty($ak)) ? 'accesskey="' . $ak . '" ' : '' ?> value="<?php echo htmlspecialchars($event->getLocation()) ?>" size="40" maxlength="255" />
 </td>
</tr>

<!-- start date -->
<tr>
 <td align="right" class="light">
    <b><?php echo Horde::label('start_year', _("Start On")) ?>&nbsp;&nbsp;</b>
 </td>
 <td align="left" class="item<?php echo($_i % 2)?>" colspan="3">
  <?php echo $event->html('start_year') ?>
  -
  <?php echo $event->html('start_month') ?>
  -
  <?php echo $event->html('start_day') ?>
<?php if (($browser->isBrowser('msie') && $browser->getMajor() >= 4) ||
          ($browser->isBrowser('mozilla') && $browser->getMajor() >= 5)): ?>
  <span id="start_wday"></span>
  <script language="JavaScript" type="text/javascript">
   updateWday('start_wday');
  </script>
<?php endif; ?>
 </td>
</tr>

<!-- start time -->
<tr>
 <td align="right" class="light">
    <b><?php echo Horde::label('start_hour', _("At")) ?>&nbsp;&nbsp;</b>
 </td>
 <td align="left" class="item<?php echo($_i++ % 2)?>" colspan="3">
  <?php echo $event->html('start_hour') ?>
  &nbsp;:&nbsp;
  <?php echo $event->html('start_min') ?>
  <?php
  if (!$prefs->getValue('twentyFour')) {
      if ($event->getStartDate('a') == 'am') {
          $am = ' checked="checked"';
          $pm = '';
      } else {
          $am = '';
          $pm = ' checked="checked"';
      }
  ?>
  <input id="am" type="radio" name="am_pm" value="AM"<?php echo $am ?> onclick="document.event.whole_day.checked=false;updateEndDate();" /><label for="am" onclick="document.event.whole_day.checked=false;updateEndDate();">AM</label>
  <input id="pm" type="radio" name="am_pm" value="PM"<?php echo $pm ?> onclick="document.event.whole_day.checked=false;updateEndDate();" /><label for="pm" onclick="document.event.whole_day.checked=false;updateEndDate();">PM</label>
  <?php } ?>
 </td>
</tr>

<!-- end date -->
<tr>
 <td align="right" class="light"><b><?php echo _("Duration") ?>&nbsp;&nbsp;</b></td>
 <td align="left" class="item<?php echo($_i % 2)?>">
  <input type="radio" name="end_or_dur" value="0" checked="checked" />
 </td>
 <td class="item<?php echo($_i % 2)?>" align="right">
    <?php echo Horde::label('end_year', _("End On")) ?>&nbsp;&nbsp;
 </td>
 <td align="left" class="item<?php echo($_i % 2)?>">
  <?php echo $event->html('end_year') ?>
  -
  <?php echo $event->html('end_month') ?>
  -
  <?php echo $event->html('end_day') ?>
<?php if (($browser->isBrowser('msie') && $browser->getMajor() >= 4) ||
          ($browser->isBrowser('mozilla') && $browser->getMajor() >= 5)): ?>
  <span id="end_wday"></span>
  <script language="JavaScript" type="text/javascript">
   updateWday('end_wday');
  </script>
<?php endif; ?>
 </td>
</tr>

<!-- end time -->
<tr>
 <td>&nbsp;</td>
 <td class="item<?php echo($_i % 2)?>" align="left">&nbsp;</td>
 <td class="item<?php echo($_i % 2)?>" align="right">
  <?php echo _("At") ?>
 </td>
 <td class="item<?php echo($_i++ % 2)?>" align="left">
  <?php echo $event->html('end_hour') ?>&nbsp;:&nbsp;<?php echo $event->html('end_min') ?>
  <?php
  if (!$prefs->getValue('twentyFour')) {
      if ($event->getEndDate('G') < 12) {
          $am = ' checked="checked"';
          $pm = '';
      } else {
          $am = '';
          $pm = ' checked="checked"';
      }
  ?>
  <input id="eam" type="radio" name="end_am_pm" value="AM"<?php echo $am ?> onclick="document.event.end_or_dur[0].checked=true;updateDuration()" /><label for="eam" onclick="document.event.end_or_dur[0].checked=true;updateDuration()">AM</label>
  <input id="epm" type="radio" name="end_am_pm" value="PM"<?php echo $pm ?> onclick="document.event.end_or_dur[0].checked=true;updateDuration()" /><label for="epm" onclick="document.event.end_or_dur[0].checked=true;updateDuration()">PM</label>
  <?php } ?>
 </td>
</tr>

<!-- duration -->
<tr>
 <td>&nbsp;</td>
 <td class="item<?php echo($_i % 2)?>" align="left" valign="top">
  <input type="radio" name="end_or_dur" value="1" />
 </td>
 <td class="item<?php echo($_i % 2)?>" align="right" valign="top">
    <?php echo Horde::label('dur_day', _("Lasts")) ?>&nbsp;&nbsp;
 </td>
 <td class="item<?php echo($_i++ % 2)?>" align="left" valign="top">
  <input id="allday" name="whole_day" type="checkbox" value="1" onclick="setWholeDay(this.value); updateEndDate(); document.event.end_or_dur[1].checked = true"<?php $dur = $event->getDuration(); if ($dur->wholeDay) echo ' checked="checked"' ?> />
  <?php echo Horde::label('allday', _("All day")) ?><br />
  <?php echo sprintf(_("%s Day(s) %s Hour(s) %s Minutes"), $event->html('dur_day'), $event->html('dur_hour'), $event->html('dur_min')) ?>
 </td>
</tr>

<!-- alarm -->
<tr>
 <td align="right" class="light">
    <b><?php $ak = Horde::getAccessKey(_("Alarm")); echo Horde::highlightAccessKey(_("Alarm"), $ak); ?>&nbsp;&nbsp;</b>
  </td>
 <td class="item<?php echo($_i++ % 2)?>" align="left" valign="top" colspan="3">
  <?php
  if ($event->isInitialized() && $event->alarm > 0) {
      $alarm_set = true;
      if ($event->alarm % 10080 == 0) {
          $alarm_value = $event->alarm / 10080;
          $alarm_unit = 'week';
      } elseif ($event->alarm % 1440 == 0) {
          $alarm_value = $event->alarm / 1440;
          $alarm_unit = 'day';
      } elseif ($event->alarm % 60 == 0) {
          $alarm_value = $event->alarm / 60;
          $alarm_unit = 'hour';
      } else {
          $alarm_value = $event->alarm;
          $alarm_unit = 'min';
      }
  } else {
      $alarm_value = 15;
      $alarm_unit = 'min';
      $alarm_set = false;
  }
  ?>
  <input id="noalarm" name="alarm" type="radio" value="0"<?php if (!$alarm_set) echo ' checked="checked"' ?> /><label for="noalarm" accesskey="<?php echo $ak ?>"> <?php echo _("None") ?></label><br />
  <input name="alarm" type="radio" value="1"<?php if ($alarm_set) echo ' checked="checked"' ?> />
  <input type="text" size="2" name="alarm_value" value="<?php echo $alarm_value ?>" onchange="document.event.alarm[1].checked=true" />&nbsp;
  <select name="alarm_unit" onchange="document.event.alarm[1].checked=true">
   <option value="1"<?php echo ($alarm_unit == 'min') ? ' selected="selected"' : '' ?>><?php echo _("Minute(s)") ?></option>
   <option value="60"<?php echo ($alarm_unit == 'hour') ? ' selected="selected"' : '' ?>><?php echo _("Hour(s)") ?></option>
   <option value="1440"<?php echo ($alarm_unit == 'day') ? ' selected="selected"' : '' ?>><?php echo _("Day(s)") ?></option>
   <option value="10080"<?php echo ($alarm_unit == 'week') ? ' selected="selected"' : '' ?>><?php echo _("Week(s)") ?></option>
  </select>
 </td>
</tr>

<!-- description -->
<tr class="smallheader">
 <td align="left" colspan="4" class="smallheader">
    <?php echo Horde::label('description', _("<b>Description</b> (optional)")) ?>&nbsp;&nbsp;
</td>
</tr>
<tr>
 <td>&nbsp;</td>
 <td class="item<?php echo($_i++ % 2)?>" colspan="3" align="left">
  <textarea wrap="hard" id="description" name="description" cols="40" rows="4"><?php echo htmlspecialchars($event->getDescription()) ?></textarea>
 </td>
</tr>

<!-- recurrence -->
<tr class="smallheader">
  <td align="left" colspan="4" class="smallheader">
    <?php echo _("<b>Recurrence</b> (optional)") ?>&nbsp;&nbsp;
  </td>
</tr>
<tr>
 <td class="light" align="right" valign="top"><b><br /><?php echo Horde::label('recurnone', _("Pattern")) ?>&nbsp;&nbsp;</b></td>
 <td class="item<?php echo($_i++ % 2)?>" align="left" valign="top" colspan="3">
  <?php $_j = $_i ?>
  <table cellpadding="0" cellspacing="0" border="0" width="95%">
   <tr>
    <td nowrap="nowrap" class="item<?php echo($_j++ % 2)?>">
     <input id="recurnone" type="radio" name="recur" onclick="clearFields(0);" value="<?php echo KRONOLITH_RECUR_NONE ?>"<?php if ($event->hasRecurType(KRONOLITH_RECUR_NONE)) echo ' checked="checked"' ?> /><label for="recurnone"> <?php echo _("None") ?></label>
    </td>
   </tr>
   <tr>
    <td nowrap="nowrap" class="item<?php echo($_j++ % 2)?>">
     <input id="recurdaily" type="radio" name="recur" onclick="setInterval('recur_daily_interval');" value="<?php echo KRONOLITH_RECUR_DAILY ?>"<?php if ($event->hasRecurType(KRONOLITH_RECUR_DAILY)) echo ' checked="checked"' ?> /><label for="recurdaily"> <?php echo _("Daily: Recurs every") ?>&nbsp;</label>
     <input type="text" name="recur_daily_interval" size="2" onkeypress="setRecur(1);" onchange="setRecur(1);" value="<?php echo $event->hasRecurType(KRONOLITH_RECUR_DAILY) ? $event->getRecurInterval() : '' ?>" />&nbsp;<?php echo _("day(s)") ?>
    </td>
   </tr>
   <tr>
    <td nowrap="nowrap" class="item<?php echo($_j++ % 2)?>">
     <input id="recurweekly" type="radio" name="recur" onclick="setInterval('recur_weekly_interval');" value="<?php echo KRONOLITH_RECUR_WEEKLY ?>"<?php if ($event->hasRecurType(KRONOLITH_RECUR_WEEKLY)) echo ' checked="checked"' ?> /><label for="recurweekly"> <?php echo _("Weekly: Recurs every") ?>&nbsp;</label>
     <input type="text" name="recur_weekly_interval" size="2" onkeypress="setRecur(2);" onchange="setRecur(2);" value="<?php echo $event->hasRecurType(KRONOLITH_RECUR_WEEKLY) ? $event->getRecurInterval() : '' ?>" />&nbsp;
     <?php echo _("week(s) on:") ?><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <label for="mo"><?php echo _("Mo") ?></label><input id="mo" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_MONDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_MONDAY)) echo ' checked="checked"' ?> />&nbsp;
     <label for="tu"><?php echo _("Tu") ?></label><input id="tu" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_TUESDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_TUESDAY)) echo ' checked="checked"' ?> />&nbsp;
     <label for="we"><?php echo _("We") ?></label><input id="we" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_WEDNESDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_WEDNESDAY)) echo ' checked="checked"' ?> />&nbsp;
     <label for="th"><?php echo _("Th") ?></label><input id="th" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_THURSDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_THURSDAY)) echo ' checked="checked"' ?> />&nbsp;
     <label for="fr"><?php echo _("Fr") ?></label><input id="fr" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_FRIDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_FRIDAY)) echo ' checked="checked"' ?> />&nbsp;
     <label for="sa"><?php echo _("Sa") ?></label><input id="sa" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_SATURDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_SATURDAY)) echo ' checked="checked"' ?> />&nbsp;
     <label for="su"><?php echo _("Su") ?></label><input id="su" type="checkbox" name="weekly[]" onclick="setRecur(2);" value="<?php echo KRONOLITH_MASK_SUNDAY ?>"<?php if ($event->recurOnDay(KRONOLITH_MASK_SUNDAY)) echo ' checked="checked"' ?> />&nbsp;
    </td>
   </tr>
   <tr>
    <td nowrap="nowrap" class="item<?php echo($_j++ % 2)?>">
     <input id="recurmonthday" type="radio" name="recur" onclick="setInterval('recur_day_of_month_interval');" value="<?php echo KRONOLITH_RECUR_DAY_OF_MONTH ?>"<?php if ($event->hasRecurType(KRONOLITH_RECUR_DAY_OF_MONTH)) echo ' checked="checked"' ?> /><label for="recurmonthday"> <?php echo _("Monthly: Recurs every") ?>&nbsp;</label>
     <input type="text" name="recur_day_of_month_interval" size="2" onkeypress="setRecur(3);" onchange="setRecur(3);" value="<?php echo $event->hasRecurType(KRONOLITH_RECUR_DAY_OF_MONTH) ? $event->getRecurInterval() : '' ?>" />&nbsp;<?php echo _("month(s)") . ' ' . _("on the same date") ?>
    </td>
   </tr>
   <tr>
    <td nowrap="nowrap" class="item<?php echo($_j++ % 2)?>">
     <input id="recurmonthweek" type="radio" name="recur" onclick="setInterval('recur_week_of_month_interval');" value="<?php echo KRONOLITH_RECUR_WEEK_OF_MONTH ?>"<?php if ($event->hasRecurType(KRONOLITH_RECUR_WEEK_OF_MONTH)) echo ' checked="checked"' ?> /><label for="recurmonthweek"> <?php echo _("Monthly: Recurs every") ?>&nbsp;</label>
     <input type="text" name="recur_week_of_month_interval" size="2" onkeypress="setRecur(4);" onchange="setRecur(4);" value="<?php echo $event->hasRecurType(KRONOLITH_RECUR_WEEK_OF_MONTH) ? $event->getRecurInterval() : '' ?>" />&nbsp;<?php echo _("month(s)") . ' ' . _("on the same weekday") ?>
    </td>
   </tr>
   <tr>
    <td nowrap="nowrap" class="item<?php echo($_j++ % 2)?>">
     <input id="recuryear" type="radio" name="recur" onclick="setInterval('recur_yearly_interval');" value="<?php echo KRONOLITH_RECUR_YEARLY ?>"<?php if ($event->hasRecurType(KRONOLITH_RECUR_YEARLY)) echo ' checked="checked"' ?> /><label for="recuryear"> <?php echo _("Yearly: Recurs every") ?>&nbsp;</label>
     <input type="text" name="recur_yearly_interval" size="2" onkeypress="setRecur(5);" onchange="setRecur(5);" value="<?php echo $event->hasRecurType(KRONOLITH_RECUR_YEARLY) ? $event->getRecurInterval() : '' ?>" />&nbsp;<?php echo _("year(s)") ?>
    </td>
   </tr>
  </table>
 </td>
</tr>

<!-- recur end date -->
<tr>
 <td align="right" class="light">
    <b><?php echo Horde::label('recur_enddate_year', _("Recur Until")) ?>&nbsp;&nbsp;</b>
 </td>
 <td align="left" class="item<?php echo($_i++ % 2)?>" colspan="3">
  <input id="recurnoend" type="radio" name="recur_enddate_type" value="none"<?php echo ($event->hasRecurEnd() ? '' : ' checked="checked"') ?> /><label for="recurnoend"> <?php echo _("No end date") ?></label><br />
  <input type="radio" name="recur_enddate_type" value="date"<?php echo ($event->hasRecurEnd() ? ' checked="checked"' : '') ?> />
  <?php echo $event->html('recur_enddate_year') ?> - <?php echo $event->html('recur_enddate_month') ?> - <?php echo $event->html('recur_enddate_day') ?>
<?php if (($browser->isBrowser('msie') && $browser->getMajor() >= 4) ||
          ($browser->isBrowser('mozilla') && $browser->getMajor() >= 5)): ?>
   <span id="recur_end_wday"></span>
   <script language="JavaScript" type="text/javascript">
    updateWday("recur_end_wday");
   </script>
<?php endif; ?>
 </td>
</tr>

<?php if ($conf['metadata']['keywords']): ?>
<!-- keywords -->
<tr class="smallheader">
 <td colspan="4" class="smallheader" align="left"><?php echo _("<b>Keywords</b> (optional)") ?></td>
</tr>
<tr>
 <td></td>
 <td class="item<?php echo($_i++ % 2)?>" colspan="3" align="left">
  <table border="0" cellspacing="2" cellpadding="2">
   <?php include_once KRONOLITH_BASE . '/config/keywords.php'; $count = 0; $_j = $_i ?>
   <?php foreach ($keywords as $cat => $list): ?>
   <?php if ($count % 3 == 0): ?>
   <tr>
   <?php endif; ?>
    <td class="item<?php echo($_j++ % 2)?>" valign="top">
     <b><?php echo $cat ?></b><br />
    <?php for ($k = 0; $k < count($list); $k++): ?>
     <input type="checkbox" id="keyword<?php echo $k ?>" name="keywords[]" value="<?php echo htmlspecialchars($list[$k]) ?>"<?php if ($event->hasKeyword($list[$k])) echo ' checked="checked"' ?> />
     <?php echo Horde::label('keyword' . $k, htmlspecialchars($list[$k])) ?><br />
    <?php endfor; ?>
    </td>
   <?php if (($count % 3 == 2) || ($count == count($keywords) - 1)): ?>
   </tr>
   <?php endif; $count++; ?>
   <?php endforeach; ?>
  </table>
 </td>
</tr>
<?php endif; ?>

<!-- buttons -->
<tr>
 <td align="right" colspan="4">
  <?php echo implode("\n", $buttons); ?>
  <input type="submit" name="cancel" class="button" value="<?php echo _("Cancel") ?>" onclick="self.location = '<?php echo $cancelurl; ?>'; return false;" />
  <input type="button" name="resetButton" class="button" value="<?php echo _("Reset to Defaults") ?>" onclick="document.event.reset(); updateWday('start_wday'); updateWday('end_wday');" />
  </td>
 </tr>
</table>

</center>
</form>

<script language="JavaScript" type="text/javascript">
<!--
document.event.title.focus();
//-->
</script>
