<tal:block
  xmlns:tal="http://xml.zope.org/namespaces/tal"
  xmlns:metal="http://xml.zope.org/namespaces/metal"
  xmlns:i18n="http://xml.zope.org/namespaces/i18n"
  tal:define="field_id here/getId;
              form_id python: here.getForm().id;
              selection_name here/getSelectionName;
              requested_selection_name here/getRequestedSelectionName;
              selection_index here/getSelectionIndex;
              selection here/getSelection;
              portal_url_string here/getPortalUrlString;
              real_context here/getContext;
              context_url real_context/getUrl;
              md5_string here/getMD5Checksum;
              line_list here/query;
              is_domain_tree_mode here/isDomainTreeMode;
              is_report_tree_mode here/isReportTreeMode;
              is_domain_tree_supported here/isDomainTreeSupported;
              is_report_tree_supported here/isReportTreeSupported;
              show_select_column here/showSelectColumn;
              show_search_line here/showSearchLine;
              ">
  <!-- ListBox starts here.  -->
  <input type="hidden" name="list_selection_name" value="default" tal:attributes="value selection_name" />
  <input type="hidden" name="list_selection_name" value="default"
                       tal:attributes="value selection_name;
                                       name string:${field_id}_list_selection_name" />
  <input tal:condition="md5_string" type="hidden" name="md5_object_uid_list" value="checksum" tal:attributes="value md5_string" />

  <tal:replace tal:content="nothing">
    Because TAL cannot accept unbalanced tags, the support for a domain tree is realized by a macro.
    The macro ListBoxContainer is a no-op, if not in domain tree mode. Otherwise, the macro constructs
    a domain tree and extracts the body in an extra table.
  </tal:replace>

  <tal:block tal:define="expand python: 0">
    <tal:block metal:define-macro="ListBoxContainer">
      <tal:block tal:condition="expand">
        <table tal:condition="is_domain_tree_mode"
          style="width: 100%;" cellpadding="0" cellspacing="0"
          tal:define="selected_domain_path here/getSelectedDomainPath">
          <tr>
            <td valign="top">
              <select name="domain_root_url"
                tal:attributes="onChange string:submitAction(this.form, '${context_url}/setDomainRoot')">
                <tal:block tal:repeat="c here/getDomainRootList">
                  <option value="base_domain"
                    tal:define="path python: c[0]; title python: c[1]"
                    tal:attributes="selected python: path == selected_domain_path; value path"
                    tal:content="title"
                    i18n:translate="" i18n:domain="ui">Base Domain</option>
                </tal:block>
              </select>
              <table id="listbox_domain_tree_table" cellpadding="0"
                tal:attributes="id string:${field_id}_domain_tree_table"
                tal:define="report_tree_list python: here.makeReportTreeList(report_path = selected_domain_path, unfolded_list = selection.getDomainList(), is_report_opened = False);
                            total_depth python: max([report_tree.depth for report_tree in report_tree_list] + [-1])">
                <tr tal:repeat="report_tree report_tree_list">
                  <tal:block tal:repeat="i python: range(report_tree.depth)">
                    <td width="16" nowrap="nowrap">&nbsp;</td>
                  </tal:block>
		  <td colspan="1" style="text-align: left; vertical-align: top; white-space: nowrap;"
                    tal:attributes="colspan python: total_depth - report_tree.depth + 1">
                    <a href="method"
                      tal:condition="report_tree/is_open"
                      tal:attributes="href string:foldDomain?domain_url=${report_tree/domain_url}&form_id=${form_id}&list_selection_name=${selection_name}&domain_depth:int=${report_tree/depth}">-&nbsp;<b tal:content="report_tree/obj/getTranslatedTitleOrId">Domain</b></a>
                    <a href="method"
                      tal:condition="not: report_tree/is_open"
                      tal:attributes="href string:unfoldDomain?domain_url=${report_tree/domain_url}&form_id=${form_id}&list_selection_name=${selection_name}&domain_depth:int=${report_tree/depth}">+&nbsp;<tal:block tal:content="report_tree/obj/getTranslatedTitleOrId">Domain</tal:block></a>
                  </td>
                </tr>
              </table>
            </td>
            <td style="vertical-align: top">
              <tal:block metal:define-slot="ListBoxBody" />
            </td>
          </tr>
        </table>
        <tal:block tal:condition="not: is_domain_tree_mode">
          <tal:block metal:define-slot="ListBoxBody" />
        </tal:block>
      </tal:block>
    </tal:block>
  </tal:block>

  <tal:block tal:define="expand python: 1">
    <tal:block metal:use-macro="template/macros/ListBoxContainer">
      <tal:block metal:fill-slot="ListBoxBody">
        <div class="ListSummary">
          <table cellpadding="0" cellspacing="0">
            <tr>
              <td style="height: 10px">
                <img src="Left.png" alt="left"
                  tal:attributes="src string:${portal_url_string}/images/Left.png" />
              </td>
              <td class="Top" colspan="2" style="height: 10px">
                <img src="spacer.png" style="width: 5px; height: 10px;" alt="spacer"
                  tal:attributes="src string:${portal_url_string}/images/spacer.png" />
              </td>
              <td class="Top" colspan="3" style="height: 10px">
                <img src="spacer.png" style="width: 5px; height: 10px" alt="spacer"
                  tal:attributes="src string:${portal_url_string}/images/spacer.png" />
              </td>
            </tr>
            <tr id="listbox_title_line" tal:attributes="id string:${field_id}_title_line">
              <td class="Left" style="width: 17px">
                <img src="spacer.png" style="width: 5px; height: 5px" alt="spacer"
                  tal:attributes="src string:${portal_url_string}/images/spacer.png" />
              </td>
	      <td style="white-space: nowrap; vertical-align: middle">
                <input tal:condition="python: is_domain_tree_supported or is_report_tree_supported"
                  type="image" src="text_block.png" id="listbox_flat_list_mode"
                  title="Flat List" name="setFlatListMode:method"
                  value="1" alt="Flat List"
                  tal:attributes="src string:${portal_url_string}/images/text_block.png;
                                  id string:${field_id}_flat_list_mode"
                  i18n:domain="ui" i18n:attributes="title" />
                <input tal:condition="is_report_tree_supported"
                  type="image" src="view_tree.png" id="listbox_report_tree_mode"
                  title="Report Tree" name="setReportTreeMode:method"
                  value="1" alt="Report Tree"
                  tal:attributes="src string:${portal_url_string}/images/view_tree.png;
                                  id string:${field_id}_report_tree_mode"
                  i18n:domain="ui" i18n:attributes="title" />
                <input tal:condition="is_domain_tree_supported"
                  type="image" src="view_choose.png" id="listbox_domain_tree_mode"
                  title="Domain Tree" name="setDomainTreeMode:method"
                  value="1" alt="Domain Tree"
                  tal:attributes="src string:${portal_url_string}/images/view_choose.png;
                                  id string:${field_id}_domain_tree_mode"
                  i18n:domain="ui" i18n:attributes="title" />
              </td>
              <td style="width: 100%; vertical-align: middle">
                &nbsp;
                <a id="listbox_title" href="list"
                  tal:attributes="id string:${field_id}_title; href here/getListActionUrl">
                  <tal:block tal:content="here/getTitle" i18n:domain="ui" i18n:translate=""/>:</a>
                <span id="listbox_record_number"
                  tal:attributes="id string:${field_id}_record_number"
                  i18n:translate="" i18n:domain="ui"><tal:block tal:replace="here/total_size" i18n:name="number">0</tal:block> record(s)</span>
                <span id="listbox_item_number"
                    tal:attributes="id string:${field_id}_item_number"
                    i18n:translate="" i18n:domain="ui"> - <tal:block tal:replace="python: len(here.getCheckedUidList())" i18n:name="number">0</tal:block> item(s) selected</span>
              </td>
	      <td style="white-space: nowrap; vertical-align: middle; text-align: center;">
                <input tal:condition="python: here.current_page > 0"
                  id="listbox_previous_page" type="image" src="1leftarrowv.png"
                  title="Previous Page" name="previousPage:method"
                  tal:attributes="id string:${field_id}_previous_page;
                                  name string:${field_id}_previousPage:method;
                                  src string:${portal_url_string}/images/1leftarrowv.png"
                  i18n:domain="ui" i18n:attributes="title" />
              </td>
	      <td style="white-space: nowrap; vertical-align: middle; text-align: center">
                <select id="listbox_page_selection" name="list_start" title="Change Page" size="1"
                  tal:define="lines here/getMaxLineNumber"
                  tal:attributes="id string:${field_id}_page_selection;
                                  name string:${field_id}_list_start;
                                  onChange string:submitAction(this.form, '${context_url}/${field_id}_setPage')"
                  i18n:domain="ui" i18n:attributes="title">
                  <option value="0"
                    tal:repeat="p python: range(0, here.total_pages)"
                    tal:attributes="selected python: p == here.current_page;
                                    value python: p * lines"
                    i18n:domain="ui" i18n:translate=""><tal:block tal:replace="python: p + 1" i18n:name="page">0</tal:block> of <tal:block tal:replace="here/total_pages" i18n:name="total_pages">1</tal:block></option>
                </select>
              </td>
	      <td style="white-space: nowrap; vertical-align: middle; text-align: center">
                <input tal:condition="python: here.current_page < here.total_pages - 1"
                  id="listbox_next_page" type="image" src="1rightarrowv.png"
                  title="Next Page" name="nextPage:method"
                  tal:attributes="id string:${field_id}_next_page;
                                  name string:${field_id}_nextPage:method;
                                  src string:${portal_url_string}/images/1rightarrowv.png"
                  i18n:domain="ui" i18n:attributes="title" />
              </td>
            </tr>
          </table>
        </div>
        <div class="ListContent">
          <table cellpadding="0" cellspacing="0">
            <tr id="listbox_label_line" tal:attributes="id string:${field_id}_label_line">
              <td tal:condition="is_report_tree_mode"
                class="Data" style="width: 50px; text-align: left; vertical-align: middle">
                <select name="report_root_url"
                  tal:attributes="onChange string:submitAction(this.form, '${context_url}/setReportRoot')">
                  <tal:block tal:repeat="c here/getReportRootList">
                    <option value="base_domain"
                      tal:define="path python: c[0]; title python: c[1]"
                      tal:attributes="selected python: path == here.getSelectedReportPath(); value path"
                      tal:content="title" i18n:domain="ui" i18n:translate="">Domain</option>
                  </tal:block>
                </select>
              </td>
              <td tal:condition="show_select_column"
                class="Data" style="width: 50px; text-align: center; vertical-align: middle">
                <input id="listbox_check_all" type="image"
                  name="checkAll:method" value="1"
                  src="checkall.png" alt="Check All" title="Check All"
                  tal:attributes="id string:${field_id}_check_all;
                                  name string:${field_id}_checkAll:method;
                                  src string:${portal_url_string}/images/checkall.png"
                  i18n:domain="ui" i18n:attributes="title" />&nbsp;<input id="listbox_uncheck_all" type="image" name="uncheckAll:method" value="1"
                  src="%(portal_url_string)s/images/decheckall.png" style="border: 0" alt="Uncheck All" title="Uncheck All"
                  tal:attributes="id string:${field_id}_uncheck_all;
                                  name string:${field_id}_uncheckAll:method;
                                  src string:${portal_url_string}/images/decheckall.png"
                  i18n:domain="ui" i18n:attributes="title" />
              </td>
              <tal:block tal:repeat="value here/getLabelValueList">
                <tal:block tal:define="sql python: value[0];
                                       title python: value[1];
                                       sort_order python: value[2]">
                  <td tal:condition="sql" class="Data">
                    <a tal:attributes="href string:setSelectionQuickSortOrder?selection_name=${selection_name}&amp;sort_on=${sql}&amp;form_id=${form_id}"
                      tal:content="title" i18n:domain="ui" i18n:translate="">ID</a>
                    <img tal:condition="python: sort_order == 'ascending'"
                      src="1bottomarrow.png" alt="Ascending Display" title="Ascending Display"
                      tal:attributes="src string:${portal_url_string}/images/1bottomarrow.png"
                      i18n:domain="ui" i18n:attributes="title" />
                    <img tal:condition="python: sort_order == 'descending'"
                      src="1toparrow.png" alt="Descending Display" title="Descending Display"
                      tal:attributes="src string:${portal_url_string}/images/1toparrow.png"
                      i18n:domain="ui" i18n:attributes="title" />
                  </td>
                  <td tal:condition="not: sql" class="Data" tal:content="title"
                    i18n:domain="ui" i18n:translate="">
                    ID
                  </td>
                </tal:block>
              </tal:block>
            </tr>

            <tr tal:condition="python: show_search_line or is_report_tree_mode"
              id="listbox_search_line"
              tal:attributes="id python: show_search_line and ('%s_search_line' % field_id) or ('%s_report_depth_line' % field_id)">
              <tal:block tal:condition="is_report_tree_mode">
                <td class="Data" style="width: 50px; text-align: left; vertical-align: middle" colspan="1"
                  tal:attributes="colspan python: show_search_line and 1 or (len(here.getSelectedColumnList()) + show_select_column + 1)"
                  tal:define="index python: selection_index is not None and '&amp;selection_index=%s' % selection_index or '';
                              is_report_opened python: int(not here.getSelection().isReportOpened());
                              url here/getUrl"><tal:block tal:repeat="i python: range(0, 6)">&nbsp;<a href="?selection_name=default&amp;selection_index=0&amp;report_depth:int=0"
                            tal:attributes="href string:${url}?selection_name=${requested_selection_name}${index}&amp;report_depth:int=${i}"
                            tal:content="i">0</a></tal:block>&nbsp;-&nbsp;<a href="?selection_name=default&amp;selection_index=0&amp;is_report_opened:int=0"
                                tal:attributes="href string:${url}?selection_name=${requested_selection_name}${index}&amp;is_report_opened:int=${is_report_opened}"
                                tal:content="python: is_report_opened and 'Show' or 'Hide'"
                                i18n:domain="ui" i18n:translate="">Show</a>
                </td>
              </tal:block>
              <td tal:condition="python: show_select_column and show_search_line"
                class="Data" style="width: 50px; text-align: center; vertical-align: middle">
                <input id="listbox_select" type="image" src="exec16.png"
                  title="Action" alt="Action" name="Base_doSelect:method"
                  tal:attributes="id string:${field_id}_select;
                                  src string:${portal_url_string}/images/exec16.png"
                  i18n:domain="ui" i18n:attributes="title" />
              </td>
              <tal:block tal:condition="show_search_line"
                tal:repeat="value here/getSearchValueList">
                <td class="DataB"
                  tal:define="alias python: value[0];
                              param python: value[1];
                              search_field python: value[2]">
                  <span tal:condition="alias" style="font-size: -3">
                    <tal:block tal:condition="python: search_field is not None"
                      tal:replace="structure python: search_field.render(value = param, key = alias)" />
                    <input tal:condition="python: search_field is None" name="id" size="8" value=""
                      tal:attributes="name alias; value param" />
                  </span>
                </td>
              </tal:block>
            </tr>

            <tal:block tal:repeat="line line_list" tal:define="checked_uid_set here/getCheckedUidSet">
              <tr id="listbox_data_line_0" class="DataA"
                tal:define="css python: test(repeat['line'].index % 2, 'DataB', 'DataA')"
                tal:attributes="id string:${field_id}_data_line_${repeat/line/index}; class css">
                <td tal:condition="is_report_tree_mode" class="DataA" align="left" valign="middle"
                  tal:attributes="class css"
                  tal:define="section_name python: line.getContext() is not None and line.getContext().getTitleOrId() or ''">
                  <a tal:condition="section_name"
                    tal:define="method_id python: line.isOpen() and 'foldReport' or 'unfoldReport'"
                    tal:attributes="href string:${method_id}?report_url=${line/getDomainUrl}&amp;form_id=${form_id}&amp;list_selection_name=${selection_name}"
                    tal:content="structure python: '%s%s&nbsp;%s' % ('&nbsp;&nbsp;' * line.getDepth(), line.isOpen() and '-' or '+', section_name)" />
                </td>
                <td tal:condition="here/showSelectColumn"
                  class="DataA" style="width: 50px; text-align: center; vertical-align: middle"
                  tal:attributes="class css">
                  <input tal:condition="python: not line.isSummary() and line.getObject() is not None"
                    type="checkbox" value="checked" id="listbox_cb_1" name="uids:list"
                    tal:attributes="checked python: line.getUid() in checked_uid_set;
                                    value line/getUid;
                                    id string:${field_id}_cb_${line/getUid}" />
                  <tal:block tal:condition="python: line.isSummary() or line.getObject() is None"></tal:block>
                </td>
                <tal:block tal:repeat="value line/render">
                  <td tal:define="html python: value[0];
                                  original_value python: value[1];
                                  error python: value[2]"
                    class="DataA" align="left"
                    tal:attributes="class python: error and (css + 'Error') or css;
                                    align python: isinstance(original_value, (float, int, long)) and 'right' or 'left'">
                    <input tal:condition="not: repeat/value/index"
                      type="hidden" value="1" name="listbox_uid:list"
                      tal:attributes="value python: line.getUid() or '';
                                      name string:${field_id}_uid:list" />
                    <span tal:replace="structure html">value</span>
                  </td>
                </tal:block>
              </tr>
            </tal:block>

            <tr tal:condition="here/showStat" id="listbox_stat_line"
              tal:attributes="id string:${field_id}_stat_line">
              <td tal:condition="is_report_tree_mode" class="Data">&nbsp;</td>
              <td tal:condition="show_select_column" class="Data">&nbsp;</td>
              <tal:block tal:repeat="value here/getStatValueList">
                <td class="Data" align="left"
                  tal:define="original_value python: value[0]; processed_value python: value[1]"
                  tal:attributes="align python: isinstance(original_value, (float, int, long)) and 'right' or 'left'"
                  tal:content="structure processed_value" />
              </tal:block>
            </tr>
          </table>
        </div>
      </tal:block>
    </tal:block>
  </tal:block>
  <!-- ListBox ends here.  -->

</tal:block>
