	[% META title = 'Request &raquo; New' -%]

<!-- BEGIN request/add_new.tt -->
	[% # PROCESS dumper.tt dump = c.cfg('msg').dfv_msgs %]
	[% # PROCESS dumper.tt dump = additional_options %]
	[% # PROCESS dumper.tt dump = practitioners %]
	[% # PROCESS dumper.tt dump = error_codes %]
	[% # PROCESS dumper.tt dump = c.stash.errs %]
	[% # PROCESS dumper.tt dump = case.as_tree %]

	<h2 class="header">New request</h2>

    [% IF dfv_errors %]
        <p class="error">[% c.cfg('msg').dfv_errors %]</p>
    [% END %]

	[% IF practitioners; # gp referral
			PROCESS site/autosuggest.tt;
		ELSE;
			PROCESS site/autosuggest.tt
				varname         = 'referrer_name'
				param_id        = 'referrer_id'
				param_name      = 'referrer_name'
				method_name     = 'get_clinicians'
				optional_params = [ "source_id=${case.referral_source_id}" ]
				watermark       = 'no';
		END;
	%]

    <script type="text/javascript">
        jQuery( function() {
            $("#is_valid_date_min").Watermark("mi");
            $("#is_valid_date_hour").Watermark("hr");
            $("#is_valid_date_day").Watermark("dd");
            $("#is_valid_date_month").Watermark("MM");
            $("#is_valid_date_year").Watermark("yyyy");
        } );
    </script>

    [% BLOCK validate_date; IF c.cfg('use_cgi_ajax') %]
        onChange="do_ajax(
			['is_valid_date_day','is_valid_date_month','is_valid_date_year',
				'is_valid_date_hour','is_valid_date_min' ],
            ['bad_datetime']
		); return true;"
    [% END; END %]

    [% new_request_dfv_js %]

    [% PROCESS request/patient_data.tt %]

	<h4>Request details:</h4>

    <form method="post" action="[% app_url %]/request/new_request/[% case.id %]"
		name="new_requests" id="new_requests" [% new_request_onsubmit %]>

		<input type="hidden" name="referral_source_id" value="[% case.referral_source_id %]" />
		<input type="hidden" name="patient_id" value="[% case.patient_id %]" />
		[% IF csrf_token; PROCESS site/csrf.tt; END %]

    <div class="indent">
    <table>
        [% UNLESS c.cfg('settings').autoincrement_requests %]
		<tr>
            <td class="label">Lab No:</td>
            <td class="content">
				<input class="field" type="text" name="request_number" id="is_valid_request_no"
					[% IF c.cfg('use_cgi_ajax') %]
						onChange="do_ajax(['is_valid_request_no'], ['bad_labno']); return true;"
					[% END %] [% site.html.on_focus %] size="12" />
					[% error_request_number %]
                    <span class="ajax_error"><em id="bad_labno"></em></span>
			</td>
		</tr>
        [% END %]

        <tr>
            <td class="label">Specimen:</td>
            <td class="content">
                <input class="field" type="text" name="specimen" size="12" id="is_valid_specimen"
                [% IF c.cfg('use_cgi_ajax') %]
                onChange="do_ajax(['is_valid_specimen'], ['bad_specimen']); return true;"
                [% END %] [% site.html.on_focus %] />
                [% error_specimen %]
                <span class="ajax_error"><em id="bad_specimen"></em></span>
			</td>
		</tr>
		<tr>
            <td class="label">Taken:</td>
			<td class="content">
                <input type="text" name="day" size="2" id="is_valid_date_day"
					   value="[% c.query.param('day') %]"
					[% PROCESS validate_date %] maxlength="2" class="[% class %]"
					   [% site.html.on_focus %] />

				<input type="text" name="month" size="2" id="is_valid_date_month"
					   value="[% c.query.param('month') %]"
					[% PROCESS validate_date %] maxlength="2" class="[% class %]"
					   [% site.html.on_focus %] />

				<input type="text" name="year" size="4" id="is_valid_date_year"
					   value="[% c.query.param('year') %]"
					[% PROCESS validate_date %] maxlength="4" class="[% class %]"
					   [% site.html.on_focus %] />
				<b>@</b>
				<input type="text" name="hour" size="2" id="is_valid_date_hour"
					   value="[% c.query.param('hour') %]"
					[% PROCESS validate_date %] maxlength="2" class="[% class %]"
					   [% site.html.on_focus %] />
				<input type="text" name="minute" size="2" id="is_valid_date_min"
					   value="[% c.query.param('minute') %]"
					[% PROCESS validate_date %] maxlength="2" class="[% class %]"
					   [% site.html.on_focus %] />
				[% UNLESS error_day || error_year # set server-side by DFV if date invalid %]
					<span class="ajax_error"><em id="bad_datetime"></em></span>
				[% END %]
                [% error_day || error_year # set server-side by DFV if date invalid %]
            </td>
        </tr>

		<tr>
			[% IF practitioners %]
			<td class="label">GP:</td>
			<td class="content">
				<select name="referrer_code">
					<option value="">--select one--</option>
				[% FOREACH entry IN practitioners.keys.sort %]
					<option value="[% practitioners.item(entry) %]">
                        [% entry %]
                    </option>
				[% END %]
					<option value="[% default_code %]">[UNLISTED GP]</option>
				</select>
				[% error_referrer_code %]
			</td>
			[% ELSE %]
			<td class="label">Clinician:</td>
			<td class="search_bar">
				<input style="width: 150px" type="text" name="_referrer" id="referrer_name" />
				<input type="hidden" name="referrer_code" id="referrer_id" />
                [% IF error_referrer_code %]
                    [% error_referrer_code %]
                [% ELSE %]
                    [ <a href="[% app_url %]/config/clinicians"
					  target="_blank">new</a> ]
                [% END %]
            </td>
			[% END %]
		</tr>

      [% IF trials.size %]
		<tr>
			<td class="label">Trial/study:</td>
			<td class="content">
				<select name="trial_id" id="trial_id" [% site.html.on_focus %]>
					<option value="">&nbsp;</option>
					[% FOREACH trial IN trials %]
					[% NEXT UNLESS trial.active.match('yes') # skip inactive %]
					<option value="[% trial.id %]">
						[% trial.trial_name %]
					</option>
					[% END %]
				</select>
				[% error_trial_id %]

				<span class="box">Trial No:</span>
					<input class="field" type="text" name="trial_number" size="12"
						[% site.html.on_focus %] />
					[% error_trial_number %]
			</td>
		</tr>
      [% END %]

		<tr>
            <td class="label">External Ref:</td>
            <td class="content">
                [% class = error_specimen.match(c.cfg('msg').dfv_msgs.need_block_ref)
                    ? 'input_error' : 'field' %]
                <input class="[% class %]" type="text" name="external_reference"
                    size="25" [% site.html.on_focus %] />
				[% error_external_reference %]
			</td>
		</tr>
	</table>
    </div>

    [% IF has_doi %]
        <p class="indent"><span class="highlight">
            [% c.messages('patient').biohazard %]</span></p>
    [% END %]

	[% PROCESS request/request_options.tt %]
	[% PROCESS request/request_consent.tt %]
	[% PROCESS request/errors.tt %]

    <p class="indent">[% site.html.submit %] [% site.html.reset %]</p>

    </form>

	[% PROCESS site/field_focus.tt form_name = 'new_requests',
		field_name = 'request_number' %]
<!-- END request/add_new.tt -->
