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

<!-- BEGIN [% template.name %] -->

	[% # PROCESS dumper.tt dump = request_data.request_report.diagnosis.diagnostic_category_id %]
	[% # PROCESS dumper.tt dump = hmrn_data.maps.diagnostic_categories %]
	[% # 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 %]

	[% # PROCESS dumper.tt dump = hmrn_data.maps.parameters %]
	[% # PROCESS dumper.tt dump = hmrn_data.maps %]
	[% # PROCESS dumper.tt dump = hmrn_data %]
	[% # PROCESS dumper.tt dump = hmrn_data.params %]
	[% # PROCESS dumper.tt dump = hmrn_data.maps.parameter_menu_items %]


	[% 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;

		diagnostic_category_id = request_data.request_report.diagnosis.diagnostic_category_id;
		diagnostic_category_desc = hmrn_data.maps.diagnostic_categories.$diagnostic_category_id;

	%]

	[% 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 %]
                    [% IF delete_section %]
                      [ <a href="[% app_url %]/hmrn_data/delete_section_data/[%
                        data.id %]/[% delete_section %]">delete ...</a> ]
                    [% END %]
                </span>
			</td>
		</tr>
	[% END; END %]

    [% BLOCK define_shared_with; # is param shared with any other section ?
        # may not be possible - class is redefined
		shared_with = 0;
		class = [];
        validation_profile.required_fields.item(type).item(name)
			? class.push('validate[required]') : class.push('normal');
		IF hmrn_data.maps.parameters.$name.in_categories.size > 1;
			shared_with = hmrn_data.maps.parameters.$name.in_categories;
            class.push('hmrn-shared-param');
		END;
    END %]

    [% BLOCK build_select_field;
        # INCLUDE dumper.tt dump = hmrn_data.maps.parameters.$name.in_categories.join(', ');
        # PROCESS define_shared_with;
		shared_with = 0;
		class = [];
        validation_profile.required_fields.item(type).item(name)
			? class.push('validate[required]') : class.push('normal');
		IF hmrn_data.maps.parameters.$name.in_categories.size > 1;
			shared_with = hmrn_data.maps.parameters.$name.in_categories;
            class.push('hmrn-shared-param');
		END;
	%]
		<select name="[% name %]" class="[% class.join(' ') %]" id="[% name %]"
            [% IF shared_with %]
                title="Value shared with: [% shared_with.join(', ') %]"
            [% END %] [% site.html.disabled UNLESS user_can_enter_data %]>
            <option value=""></option>
            [% FOREACH opt IN hmrn_data.maps.parameter_menu_items %]
            	[% IF opt.param_name == name %]
            		<option value="[% opt.item_value %]" [% 'selected'
                        IF hmrn_data.params.$type.$name == opt.item_value %]>
							[% opt.item_value %]
                    </option>
                [% END %]
			[% END %]
        </select>
	[% END %]

	[% BLOCK build_text_field;
        # INCLUDE dumper.tt dump = hmrn_data.maps.parameters.$name.in_categories;
		constraint = validation_profile.constraints.item(name); # name;
		class = []; # set after validation

		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.push("validate[$str]");
        ELSE; class.push('normal');	END;

        # PROCESS define_shared_with;
		shared_with = 0;
#		class = [];
#        validation_profile.required_fields.item(type).item(name)
#			? class.push('validate[required]') : class.push('normal');
		IF hmrn_data.maps.parameters.$name.in_categories.size > 1;
			shared_with = hmrn_data.maps.parameters.$name.in_categories;
            class.push('hmrn-shared-param');
		END;
    %]
		<input type="text" name="[% name %]" id="[% type %]_[% name %]" size="[% size %]"
			class="[% class.join(' ') %]" value="[% hmrn_data.params.$type.$name %]"
            [% IF shared_with %]
                title="Value shared with: [% shared_with.join(', ') %]"
            [% END %] [% site.html.disabled UNLESS user_can_enter_data %] />

		[% 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/referral.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 %] -->
