<tal:defs define="dummy view/update" />
<html metal:use-macro="view/@@standard_macros/page" i18n:domain="schooltool">
<head>
  <title metal:fill-slot="title"
         tal:content="view/title">Add/edit event</title>
  <script type="text/javascript" metal:fill-slot="extrahead">
    function recurrence_changed() {
        var recurrence = document.getElementById('field.recurrence');
        var recurrence_box = document.getElementById('recurrence_box');
        var update_box = document.getElementById('update_box');
        if (recurrence.checked) {
            recurrence_box.style.display = 'block';
            update_box.style.display = 'block';
        } else {
            recurrence_box.style.display = 'none';
            update_box.style.display = 'none';
        }
    }
    function recurrence_type_changed() {
        var form = document.getElementById('form');
        var recurrence = document.getElementById('field.recurrence_type');
        var weekly_row = document.getElementById('weekly_row');
        var monthly_box = document.getElementById('monthly_box');
        if (recurrence.value == 'weekly') {
            weekly_row.style.display = 'block';
            monthly_box.style.display = 'none';
        } else if (recurrence.value == 'monthly') {
            weekly_row.style.display = 'none';
            monthly_box.style.display = 'block';
        } else {
            weekly_row.style.display = 'none';
            monthly_box.style.display = 'none';
        }
    }

    first_run = true;
    stored_duration = null;
    stored_duration_type = null;

    function allday_changed() {
        /// XXX: No effect in IE
        var form = document.getElementById('form');
        var allday = document.getElementById('field.allday');
        var start_time = document.getElementById('field.start_time');
        var duration = document.getElementById('field.duration');
        var duration_type = document.getElementById('field.duration_type');

        current_duration = duration.value;
        current_duration_type = duration_type.value;

        if (allday.checked) {
            start_time.disabled = true;
            duration_type.disabled = true;

            if (!first_run) {
                if (stored_duration) {
                     duration.value = stored_duration;
                }
                else if (current_duration_type != 'days'){
                     duration.value = 1;
                }
                duration_type.value = 'days';
                stored_duration = current_duration;
                stored_duration_type = current_duration_type;
            }
        } else {
            start_time.disabled = false;
            duration_type.disabled = false;

            if (!first_run) {
                if (stored_duration) {
                     duration.value = stored_duration;
                     duration_type.value = stored_duration_type;
                }
                stored_duration = current_duration;
                stored_duration_type = current_duration_type;
            }
        }
        first_run = false;
    }
    function range_changed() {
        var range_c = document.getElementById('field.range_c');
        var range_u = document.getElementById('field.range_u');
        var count = document.getElementById('field.count');
        var until = document.getElementById('field.until');
        count.disabled = !range_c.checked;
        until.disabled = !range_u.checked;
    }
    function onloadhook() {
        if (!document.getElementById('recurrence_box'))
            return;

        recurrence_changed();
        var recurrence = document.getElementById('field.recurrence');
        addEvent(recurrence, "click", recurrence_changed);

        range_changed();
        var range_c = document.getElementById('field.range_c');
        var range_u = document.getElementById('field.range_u');
        var range_f = document.getElementById('field.range_f');
        addEvent(range_c, "click", range_changed);
        addEvent(range_u, "click", range_changed);
        addEvent(range_f, "click", range_changed);

        recurrence_type_changed();
        var form = document.getElementById('form');
        var recurrence_type = form['field.recurrence_type'];
        addEvent(recurrence_type, "change", recurrence_type_changed);
        addEvent(recurrence_type, "keyup", recurrence_type_changed);

        /* Hide the help paragraphs if JavaScript is working */
        var weekly_par = document.getElementById('weekly_par');
        var monthly_par = document.getElementById('monthly_par');
        monthly_par.style.display = 'none';
        weekly_par.style.display = 'none';

        allday_changed();
        var allday = document.getElementById('field.allday');
        addEvent(allday, "click", allday_changed);
    }
    function addEvent(obj, event, handler) {
      if (obj.addEventListener) {
          obj.addEventListener(event, handler, false);
      } else if (obj.attachEvent) {
          obj.attachEvent("on" + event, handler);
      }
    }
    addEvent(window, "load", onloadhook);
  </script>
</head>
<body>
<metal:nothing metal:fill-slot="content-header" />

<metal:block metal:fill-slot="body">
  <form id="form" class="standalone" method="post" enctype="multipart/form-data"
        tal:attributes="action request/URL">
    <h3 i18n:translate="">Event information</h3>

    <div class="error" tal:condition="view/error" tal:content="view/error" />
    <input type="hidden" tal:condition="request/date|nothing"
           name="date" tal:attributes="value request/date" />
    <fieldset>
      <legend i18n:translate="">Event details</legend>

      <div class="row" tal:define="widget nocall:view/title_widget">
        <div metal:use-macro="context/@@form_macros/widget_row" />
      </div>
      <div class="row" tal:define="widget nocall:view/start_date_widget">
        <div metal:use-macro="context/@@form_macros/widget_row" />
      </div>
      <div class="row" tal:define="widget nocall:view/allday_widget">
        <div metal:use-macro="context/@@form_macros/widget_row" />
      </div>
      <div class="row" tal:define="widget nocall:view/start_time_widget">
        <div metal:use-macro="context/@@form_macros/widget_row" />
      </div>

      <div class="row">
        <tal:def define="widget nocall:view/duration_widget">
          <div metal:use-macro="context/@@form_macros/widget_row" >
            <metal:block fill-slot="extra">
              <tal:comment condition="nothing">
                Very similar to Repeat Every field
              </tal:comment>
              <select name="field.duration_type" size="1"
                      id="field.duration_type"
                      tal:define="duration_type python: view.duration_type_widget._getFormValue() or 'minutes'">
                <option i18n:translate="" value="minutes"
                    tal:attributes="selected python: duration_type == 'minutes'">
                  Minutes</option>
                <option i18n:translate="" value="hours"
                    tal:attributes="selected python: duration_type == 'hours'">
                  Hours
                </option>
                <option i18n:translate="" value="days"
                    tal:attributes="selected python: duration_type == 'days'">
                  Days
                </option>
              </select>
            </metal:block>
          </div>
        </tal:def>

      </div>
      <div class="row" tal:define="widget nocall:view/location_widget">
        <div metal:use-macro="context/@@form_macros/widget_row" />
      </div>
      <div class="row" tal:define="widget nocall:view/description_widget">
        <div metal:use-macro="context/@@form_macros/widget_row" />
      </div>

      <div class="row" tal:condition="view/show_book_checkbox">
        <div class="field">
          <input class="hiddenType" id="field.book.used" name="field.book.used" type="hidden" value="" />
          <input class="checkboxType" id="field.book" name="field.book" type="checkbox" value="on"
                 tal:attributes="checked request/field.book|nothing" />
          <label for="field.book" i18n:translate="">Book Resources</label>
        </div>
      </div>

      <div class="row" tal:define="widget nocall:view/recurrence_widget">
        <div metal:use-macro="context/@@form_macros/checkbox_widget " />
      </div>

      <div id="recurrence_box">
        <div class="controls" id="update_box">
          <input class="button-ok" name="UPDATE" type="submit"
            value="Update form" i18n:attributes="value" />
        </div>
        <!-- Repeat every [count] [freq [v]] -->
        <div class="row">
          <div class="row" tal:define="widget nocall:view/interval_widget">
            <div metal:use-macro="context/@@form_macros/widget_row">
              <metal:block fill-slot="extra">
              <tal:comment condition="nothing">
              <!-- Sadly, the Zope 3 SelectWidget is brain-dead:
                   - no "id" attribute on the select element
                   - renders two divs for no reason
                   TODO: follow it up in Zope 3. -->
              </tal:comment>
              <select name="field.recurrence_type" size="1"
                      id="field.recurrence_type"
                      tal:define="recurrence_type python: view.recurrence_type_widget._getFormValue() or 'daily'">
                <option i18n:translate="" tal:attributes="selected python:recurrence_type == 'daily'" value="daily">day</option>
                <option i18n:translate="" tal:attributes="selected python:recurrence_type == 'weekly'" value="weekly">week</option>
                <option i18n:translate="" tal:attributes="selected python:recurrence_type == 'monthly'" value="monthly">month</option>
                <option i18n:translate="" tal:attributes="selected python:recurrence_type == 'yearly'" value="yearly">year</option>
              </select>
              </metal:block>
            </div>
          </div>
        </div>
        <!-- weekly box -->
        <fieldset id="weekly_row">
          <legend i18n:translate="">Weekly</legend>
          <p id="weekly_par" i18n:translate="">
            This section only applies when weekly recurrence is selected.
          </p>
          <input name="field.weekdays-empty-marker" type="hidden" value="1" />
          <span tal:repeat="term view/weekdays_widget/vocabulary">
            <input type="checkbox" name="field.weekdays:list"
                   tal:attributes="
                   id string:weekday_${term/token};
                   value term/token;
                   disabled python:view.weekdayDisabled(term.token);
                   checked python:view.weekdayChecked(term.token)"
                   />
            <label class="plain"
                   tal:attributes="for string:weekday_${term/token}"
                   tal:content="python:view.weekdays_widget.textForValue(term)" />
          </span>
        </fieldset>
        <!-- monthly box -->
        <fieldset id="monthly_box" class=""
                  tal:define="monthly_value python:view.monthly_widget._getFormValue()">
          <legend i18n:translate="">Monthly</legend>
          <p id="monthly_par" i18n:translate="">
            This section only applies when the monthly recurrence is selected.
          </p>
          <div class="row">
            <input type="radio" name="field.monthly" value="monthday" id="monthly_m"
              tal:attributes="checked python:monthly_value == 'monthday'"/>
            <label for="monthly_m" class="plain" i18n:translate="">
              Day <span tal:replace="view/getMonthDay" i18n:name="monthday"/> of
              every month</label>
          </div>
          <div class="row">
            <input type="radio" name="field.monthly" value="weekday" id="monthly_w"
              tal:attributes="checked python:monthly_value == 'weekday'"/>
            <label for="monthly_w" class="plain" i18n:translate="">
              <span tal:replace="view/getWeekDay" i18n:name="weekday" /> of
              every month</label>
          </div>
          <div class="row" tal:condition="view/getLastWeekDay">
            <input type="radio" name="field.monthly" value="lastweekday" id="monthly_l"
              tal:attributes="checked python:monthly_value == 'lastweekday'"/>
            <label for="monthly_l" class="plain" i18n:translate="">
              <span tal:replace="view/getLastWeekDay" i18n:name="weekday" /> of
              every month</label>
          </div>
        </fieldset>
        <!-- Range selection -->
        <fieldset tal:define="range_value
                  python:view.range_widget._getFormValue() or 'forever'">
          <div class="row">
            <input type="radio" name="field.range" value="count" id="field.range_c"
                   tal:attributes="checked python:range_value == 'count'"/>
            <label for="field.range_c" class="plain" i18n:translate="">Repeat</label>
            <input type="text" name="field.count" id="field.count" size="3"
                   tal:attributes="value view/count_widget/_getFormValue" />
            <tal:block i18n:translate="">times</tal:block>
            <tal:block tal:define="widget nocall:view/count_widget">
              <div class="error" tal:define="error widget/error"
                   tal:condition="error" tal:content="structure error">
                The Error
              </div>
            </tal:block>
          </div>

          <div class="row">
            <input type="radio" name="field.range" value="until" id="field.range_u"
                   tal:attributes="checked python:range_value == 'until'"/>
            <label for="field.range_u" class="plain" i18n:translate="">Repeat until</label>
            <input type="text" name="field.until" id="field.until"
                   tal:attributes="value view/until_widget/_getFormValue"/>
            <img class="CalIcon" onClick="clickWidgetIcon('field.until');"
                 id="field.untilIcon" alt="[popup calendar]"
                 i18n:attributes="alt"
                 tal:attributes="src context/++resource++calwidget-icon.gif"/>
            <div id="field.untilDiv"
                 style="background: #fff; position: absolute; display: none;">
            </div>

            <tal:block tal:define="widget nocall:view/until_widget">
              <div class="error" tal:define="error widget/error"
                   tal:condition="error" tal:content="structure error">
                The Error
              </div>
            </tal:block>
          </div>
          <div class="row">
            <input type="radio" name="field.range" value="forever" id="field.range_f"
                   tal:attributes="checked python:range_value == 'forever'"/>
            <label for="field.range_f" class="plain" i18n:translate="">
              Repeat forever</label>
          </div>
          <tal:block tal:define="widget nocall:view/range_widget">
            <div class="error" tal:define="error widget/error"
                 tal:condition="error" tal:content="structure error">
              The Error
            </div>
          </tal:block>
        </fieldset>
        <div class="row" tal:define="widget nocall:view/exceptions_widget">
          <div metal:use-macro="context/@@form_macros/datepicker_widget" />
        </div>
      </div>
      <input type="hidden" tal:condition="request/cancel_url|nothing"
             tal:attributes="value request/cancel_url"
             name="cancel_url" />
      <input type="hidden" tal:condition="request/back_url|nothing"
             tal:attributes="value request/back_url"
             name="back_url" />
    </fieldset>

    <div class="resources" tal:condition="view/show_book_link">
      <fieldset>
        <legend i18n:translate="">Booked Resources</legend>
        <table width="100%" condition="context/resources">
          <tr>
            <th align="left">Title</th>
            <th align="left">Type</th>
          </tr>
          <tal:loop repeat="resource context/resources">
            <tr>
              <td>
                <p tal:replace="resource/title">resource title</p>
              </td>
              <td>
                <p tal:replace="resource/type">resource type</p>
              </td>
            </tr>
          </tal:loop>
        </table>

        <p tal:condition="not:context/resources">No Resources Booked</p>
        <br />

        <a tal:attributes="href string:${context/@@absolute_url}/booking.html"
           i18n:translate="">Book resources</a>
      </fieldset>
    </div>

    <div class="controls">
      <input type="submit" class="button-ok" name="UPDATE_SUBMIT"
             tal:attributes="value view/submit_button_title"
             i18n:attributes="title" title="Shortcut: Alt-A" accesskey="A" />
      <input type="submit" class="button-cancel" name="CANCEL" value="Cancel"
             i18n:attributes="value cancel-button" />
    </div>
  </form>
</metal:block>
</html>
