<!-- BEGIN result/data_entry.tt -->
    [% # PROCESS dumper.tt dump = resultable_tests %]
    [% # PROCESS dumper.tt dump = data_options_map %]
    
  <p class="indent"><span class="title">Results:</span>
    [% div_name = sectionName _ '_data'; INCLUDE site/snippets/toggleview.tt %]
  </p>  
  
  <div id="[% div_name %]" class="itemhidden">
    <!-- // discontinued
    <p class="indent">
    [% IF c.query.param('results_only') %]
      <a href="[% app_url %]/result/=/[% data.id %]">[show all results]</a>
    [% ELSE %]
      <a href="[% app_url %]/result/=/[% data.id %]?results_only=1">[show results only]</a>
    [% END %]
    </p>
    -->
    
    <div class="test_results">
      [% UNLESS is_locked # can still view results section if locked %]
      <form action="[% app_url %]/result/data_entry/[% data.id %]" method="post"
        name="[% sectionName | lower %]_results" id="[% sectionName | lower %]_results">
          <input type="hidden" name="_section_id" value="[% this_section.id %]" />
          <input type="hidden" name="_section_name" value="[% section %]" />
      [% END %]
      
        [% sorted_tests = natural_sort(resultable_tests);
          # PROCESS dumper.tt dump = sorted_tests;
        %]

        <table class="results">
          <tr>
            [% count = 1 %]
             
            [% FOREACH test_name IN sorted_tests;
                  # PROCESS dumper.tt dump = this_section.test.item(test_name);
                test_data = this_section.test.item(test_name);                  
                  # PROCESS dumper.tt dump = test_data;
                data_type = data_map.lab_test_data_type.item(test_data.id).data_type;
                  # PROCESS dumper.tt dump = data_type;

                UNLESS test_data.result; # unless already have result:
                    NEXT UNLESS test_data.is_active.match('yes'); # skip inactive test
                    NEXT IF data_type AND data_type.active.match('no'); # skip inactive result entry
                    # can't do next line - skips Cut-up & processing tests:
                    # NEXT IF data_map.requested_tests.size
                    #   AND NOT data_map.requested_tests.item(test_data.id);
                END;
                
                # NEXT IF c.query.param('results_only') AND NOT test_data.result; # discontinued

			     IF test_data.is_active.match('no');
                                      class = 'grey'; 
              ELSIF test_data.result; class = 'title';
              ELSIF data_map.requested_tests.item(test_data.id); class = 'red';
              ELSIF test_data.status; class = 'red'; # awaiting result
              ELSE; class = 'normal'; END;
            %]
            <td align="right" width="70">
              <span class="[% class %]">[% test_name | html %]</span>
            </td>
            <td width="250">
              [% IF ! data_type.id OR is_locked # assume free text %]
                <input type="text" name="lab_test_id_[% test_data.id %]"
                  value="[% test_data.result %]" size="20" [% IF is_locked %]
                    readonly="readonly"[% END %] />
              [% ELSE %]
              <select name="lab_test_id_[% test_data.id %]">
                <option value="">&nbsp;</option>
                [% FOREACH opt IN data_map.data_options.item(data_type.id);
                    # PROCESS dumper.tt dump = [opt, test_data.result];
                    NEXT IF opt.is_active.match('no')
                      AND test_data.result != opt.value; # for legacy data before opt disabled
                %]
                <option value="[% opt.value %]"
                    [% 'selected' IF test_data.result == opt.value %]>
                  [% opt.value %]
                </option>
                [% END %]
              </select>
              [% END %]
            </td>
      
          [% # n_cols = data_type.type == 'text' ? 2 : 6 # text longer than others %]
          [% UNLESS loop.last OR # don't start new table unless multiple of $n_cols:
            count % 3 %]
              </tr>
            </table>
            <table class="results">
              <tr>
          [% END %]              
      
          [% count = count + 1 %]                
      [% END %]
      
          </tr>
        </table>
      
        <!-- BEGIN result summary -->
          [% IF this_section.has_result_summary.match('yes')
              AND active_tests.size # only show results summaries if at least 1 test %]
            <p class="indent">
              <span class="title">Results summary:</span>
            </p>
            
            [% IF results_summary_opts.item(section) # may not have select menu opts
                AND NOT is_locked %]
            <blockquote>
                <select name="_results_summary">
                    [% label = '' # initialise var in case optgroup %]
                  <option value="">&nbsp;</option>
                  [% FOREACH option IN results_summary_opts.item(section);
                        NEXT UNLESS option.is_active.match('yes'); 
                        # PROCESS dumper.tt dump = option; %]
                    [% IF option.description.match('~'); # numerical ordering
                        split = option.description.split('~'); # PROCESS dumper.tt dump = split;
                    %]
                        <option>[% split.1 # RHS value %]</option>
                    [% ELSIF option.description.match('::'); # optgroup symbol
                        split = option.description.split('::'); # PROCESS dumper.tt dump = split;
                    %]                    
                        [% IF split.0 != label # time to change optgroup label: %]
                            [% '</optgroup>' IF label # close opgroup block %]
                            [% label = split.0 # set new label %]
                            <optgroup label="[% label %]">
                                <option>[% split.1 # RHS value %]</option>
                        [% ELSE # discard split.0 (optgroup label) %]
                            <option>[% split.1 # RHS value %]</option>
                        [% END %]
                        [% '</optgroup>' IF loop.last %]                        
                    [% ELSE # not optgroup or numerical ordering %]                    
                        <option>[% option.description %]</option>
                    [% END %]                    
                  [% END %]                    
                </select>
            </blockquote>
            [% END %]
            
            <blockquote>
              <p class="classname">
                <textarea name="results_summary" rows="1" cols="80"
                    class="expand20-200" [% IF is_locked %]readonly="readonly"[%
                    END %]>[% result_summaries.item(section).results_summary # DO NOT WRAP THIS %]</textarea>                        
              </p>
            </blockquote>
          [% END %]
        <!-- END result summary -->

    [% UNLESS is_locked %]
        [% IF this_section.has_test_sign_out.match('yes')
            AND c.user_can('modify_results') %]
          <p class="indent">
            <span class="title">Sign-off all tests in this section:</span>
            <input type="checkbox" name="complete_all_tests" value="1" />
          </p>
        [% END %]
        
        <p align="center">
            [% site.html.submit %]
            [% # site.html.reset # disabled due to complaints !! %]
        </p>     
      </form>
    [% END %]

	  <!-- added by pete for canned report (results_template) -->
	  [% IF this_section.has_result_summary.match('yes') AND active_tests.size
		AND NOT is_locked %]		
        <!-- the javascript function to change the source of the frame -->
        <script type="text/javascript">
          function load_[% sectionName %]_template()  {
            var html = document.getElementById('[% sectionName %]_id').contentWindow.document.body.innerHTML; 
            document.[% sectionName %]_results.results_summary.value=document.[% sectionName %]_results.results_summary.value + html
          }
        </script>
        
		<!-- set up a frame to hold the information we require -->
        <p class="indent">
          <span class="title">Results summary templates:</span>
		  [% div_name = sectionName _ '_tmpl';
			INCLUDE site/snippets/toggleview.tt %]
        </p> 
  
        <div id="[% div_name %]" class="itemhidden">
          <iframe name="[% sectionName %]_frame" class="indent"
            id="[% sectionName %]_id" src="#" width="600"></iframe>
          
          <!-- set up a form to search for a canned report file locally and submit
              to a javascript function -->
          <form name="[% sectionName %]_tmpl" enctype="multipart/form-data"
              method="post" action="[% app_url %]/ajax/load_template"
              target="[% sectionName %]_frame">
            <p class="indent">
              <!-- set up a file type input field which submits the form on change -->
              <input type="file" name="template" size="30" onChange="form.submit()" />
              <input type="button" onclick="load_[% sectionName %]_template();"
                  value="Import Template" /> 
            </p>    
          </form>
        </div>        
	  [% END %]
      
    </div>
  </div>  
<!-- BEGIN result/data_entry.tt -->
