<div i18n:domain="schooltool"
     class="calendar-event">

  <tal:block replace="resource_library:schooltool.calendar" />

  <script type="text/javascript">
    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() {
        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>

  <form id="form" class="standalone" method="post" enctype="multipart/form-data"
        tal:attributes="action request/URL">

    <div class="status"
         tal:condition="view/errors">
      <div class="summary ui-state-error ui-corner-all">
        <span class="ui-icon ui-icon-alert">icon</span>
        <tal:block i18n:translate="">
          Please correct the marked fields below.
        </tal:block>
      </div>
    </div>

    <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><span i18n:translate="">Event details</span></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="widget">
          <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="">Reserve Resources</label>
        </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><span i18n:translate="">Reserved Resources</span></legend>
        <table width="100%" condition="view/resources">
          <tr>
            <th align="left">Title</th>
            <th align="left">Type</th>
          </tr>
          <tal:loop repeat="resource view/resources">
            <tr>
              <td>
                <tal:block condition="resource/url">
                  <a tal:content="structure resource/title"
                     tal:attributes="href resource/url">resource title</a>
                </tal:block>
                <tal:block condition="not:resource/url">
                  <span tal:content="structure resource/title">resource title</span>
                </tal:block>
              </td>
              <td>
                <span tal:content="resource/type">resource type</span>
              </td>
            </tr>
          </tal:loop>
        </table>

        <p tal:condition="not:view/resources">No resources reserved</p>
        <br />

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

    <div class="buttons">
      <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>

</div>
