[% META
    title  = 'Haematological Malignancy Research Network &raquo; Clinical Data'
%]

<!-- BEGIN [% template.name %] -->
	[% # PROCESS dumper.tt dump = request_data.as_tree %]
	[% # PROCESS dumper.tt dump = request_history %]
	[% # PROCESS dumper.tt dump = hmrn_data %]
	[% # PROCESS dumper.tt dump = validation_profile.constraints %]
	[% # PROCESS dumper.tt dump = demographic_data.practice %]
	[% # PROCESS dumper.tt dump = c.stash.errs %]
	
	[% addcss =	[
			'validationEngine.jquery.css' # for validationEngine
			'smoothness/jquery-ui-1.8.4.custom.css' # for DatePicker
		]
	%]

	[%
		data = request_data;
		report_data = data.request_report;
		patient = data.patient_case.patient;
		parameters = hmrn_data.maps.parameters;
	%]

	[% user_can_enter_data = c.user_can('edit_clinical_data')
        AND patient.nhs_number ? 1 : 0 # MUST have primary identifier (NHS No) %]

	[% # PROCESS site/js/jquery.tt # already loaded in html_wrapper %]
	<script type="text/javascript" src="/js/jquery.textarea-expander.js"></script>

	<script language="javascript" type="text/javascript">
		<!--
		function set_proposed(id, proposed) { $(id).val(proposed) }
		-->
	</script>

    [% BLOCK hmrn_submit;
		IF user_can_enter_data %]
		<tr class="borderless">
	        <td colspan="2">
                <span class="indent">
                    [% site.html.submit %]
                    [% site.html.reset %]
                </span>
			</td>
		</tr>
	[% END; END %]

	[% BLOCK build_select_field;
		class = validation_profile.required_fields.item(type).item(name)
			? 'validate[required]' : 'normal';	
	%]
	  <select name="[% name %]" class="[% class %]" id="[% name %]"
		[% UNLESS user_can_enter_data; site.html.disabled; END %]>
		  <option value=""></option>
		  [% FOREACH opt IN hmrn_data.maps.menu_options.item(name).split(',') %]
			  <option value="[% opt %]"
				  [% 'selected' IF hmrn_data.params.$type.$name == opt %]>
			  [% opt %]
			  </option>
		  [% END %]					
	  </select>
	[% END %]
	
	[% BLOCK build_text_field;
		constraint = validation_profile.constraints.item(name); # name;
		class = 'normal'; # maybe overridden in validation block
		
		IF user_can_enter_data;
			validate = [];
			
			IF validation_profile.required_fields.item(type).item(name);
				validate.push('required');
			ELSE;
				validate.push('optional'); # or empty textboxes fail validation
			END;
			
			IF constraint;
				validate.push("ajax[ajaxRange]");
	
				IF constraint.field_type == 'decimal';
					validate.push('custom[onlyDecimal]');
				ELSIF constraint.field_type == 'int';
					validate.push('custom[onlyNumber]');
				END;
			END;			
		END;
		
		IF validate.size;
			str = validate.join(',');
			class = "validate[$str]";
		END	%]

		<input type="text" name="[% name %]" id="[% type %]_[% name %]" size="[% size %]"
			class="[% class %]" value="[% hmrn_data.params.$type.$name %]"
			[% UNLESS user_can_enter_data; site.html.readonly; END %] />
	
		[% IF error; error; %]
		[% ELSIF user_can_enter_data;
			constraint = validation_profile.constraints.item(name);
			
			NS = constraint.field_type == 'int' ? # get rid of trailing .0
				10 * constraint.not_stated / 10 : constraint.not_stated;
			ND = constraint.field_type == 'int' ? # get rid of trailing .0
				10 * constraint.not_done / 10 : constraint.not_done;
				
		%]
			<span style="font-size: smaller">&#171;
				(<a href="#" onclick="javascript:set_proposed('#[% type %]_[% name %]', 
					'[% NS %]'); return false">
					<span class="acronym" title="not stated [[% NS %]]">NS</span>
				</a> |
				<a href="#" onclick="javascript:set_proposed('#[% type %]_[% name %]', 
					'[% ND %]'); return false">
					<span class="acronym" title="not done [[% ND %]]">ND</span>
				</a>)
			</span>
			[% IF units; '[' _ units _ ']'; END %]
		[% END %]		
	[% END %]

    [% IF user_can_enter_data %]
	<script type="text/javascript" src="/js/jquery-ui-1.8.4.custom.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $('.datepicker').datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy',
            });
        });
	</script>
    
    [% IF not_using_validationEngine_jquery # then use 'basic' jquery val %]
    <script type="text/javascript" src="/js/jquery.validate.js"></script>
    <script type="text/javascript"> 
	    $(document).ready(function() { 
	      $("#treatment").validate({ 
            rules: {
				[% c.jquery_validation_profile("${profile_name}").join(',') %]
            },
          });
        });
    </script>
    <script type="text/javascript">
        // override default message:
        jQuery.extend(jQuery.validator.messages, {
            remote:   "&#171; invalid entry",
            required: "&#171; required field",
        });
    </script>
    <!-- using validationEngine now:
      http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
    -->
    [% ELSE; INCLUDE site/js/validationEngine.tt; END %]
    [% END %]
	
	[% IF dfv_errors; # PROCESS dumper.tt dump = c.stash.errs %]
		<p class="highlight" align="center">
			<span class="blink bold">******** FORM FAILED VALIDATION ********</span>			
        </p>
        <p align="center" class="warning">jump to error(s) <a href="#error">&#187;</a></p>
	[% END %]
	
    [% IF c.user_can('edit_clinical_data');
        IF patient.nhs_number; user_can_enter_data = 1;
        ELSE %]
        <p class="highlight" align="center">
            Cannot enter/edit data without NHS number
        </p>
        [% END %]
    [% END %]

	[%
        INCLUDE hmrn/data/patient_summary.tt;
        INCLUDE hmrn/data/chronologies.tt;
        INCLUDE hmrn/data/antecedent.tt;
        INCLUDE hmrn/data/treatment.tt;
        INCLUDE hmrn/data/clinical_data.tt;
		INCLUDE hmrn/data/comments.tt;
    %]	
<!-- END [% template.name %] -->
