<!-- BEGIN patient/default.tt -->
    [% META title = 'Registration &raquo; Register New Patient' -%]

    <h2 class="header">Register new patient</h2>

    [% # PROCESS dumper.tt dump = maybe_duplicate %]

    [% js_new_patient_dfv_js %]

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

	[% PROCESS site/autosuggest.tt %]
    <script type="text/javascript">
        jQuery( function() {    
            $("#is_valid_date_day").Watermark("dd");
            $("#is_valid_date_month").Watermark("mm");
            $("#is_valid_date_year").Watermark("yyyy");
        } );
    </script>

	<div id="bad_patient"></div>

    <form action="[% app_url %]/patient/add_new" method="post" name="add_new" id="add_new"
        [% js_new_patient_onsubmit %]>

	[% PROCESS patient/pas.tt IF pas_query %]
	[% PROCESS patient/pds.tt IF pds_query %]
	[% PROCESS patient/duplicate.tt IF maybe_duplicate %]

    <div class="indent">
      <table>
        <tr>
            <td class="label">Last Name:</td>
            <td class="content">
                <input class="field" type="text" name="last_name" size="30"
					id="last_name" [% site.html.on_focus %]
                    value="[% c.query.param('last_name') %]" />
                [% error_last_name %]
            </td>
        </tr>

        <tr>
            <td class="label">First Name:</td>
            <td class="content">
                <input class="field" type="text" name="first_name" size="30"
					id="first_name" [% site.html.on_focus %]
                    value="[% c.query.param('first_name') %]" />
                [% error_first_name %]
            </td>
        </tr>

        <tr>
            <td class="label">Middle Name(s):</td>
            <td class="content">
                <input class="field" type="text" name="middle_name" size="30"
                    value="[% c.query.param('middle_name') %]" [% site.html.on_focus %]/>
                [% error_middle_name %]
            </td>
        </tr>

        <tr>
            [% SET class = error_patient_id ? 'input_error' : 'field' %]
            <td class="label">NHS No:</td>
            <td class="content">
                <input class="[% class %]" type="text" name="nhs_number" size="12"
					   id="is_valid_and_unique_nhsno" value="[% c.query.param('nhs_number') %]"
                    [% IF c.cfg('use_cgi_ajax') %]
                    onChange="do_ajax(['is_valid_and_unique_nhsno'], ['bad_nhsno']); return true;"
                    [% END %]
                [% site.html.on_focus %] />
                [% error_nhs_number %]
                <span class="ajax_error"><em id="bad_nhsno"></em></span>
            </td>
        </tr>

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

        <tr>
            [% SET class = error_year || error_day ? 'input_error' : 'field' %]
            <td class="label">DoB:</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 %] />
				[% UNLESS error_day || error_year # set if date invalid %]
					<span class="ajax_error"><em id="bad_date"></em></span>
				[% END %]
                [% error_day || error_year # set if date invalid %]
            </td>
        </tr>

        <tr>
            <td class="label">Gender:</td>
            <td class="content">
                 [% FOREACH entry IN ['M','F','U'] %]
                <input type="radio" value="[% entry %]" name="gender"
					[% IF entry.match(c.query.param('gender')) %]checked[% END %]  />
					[% entry %]
                [% END %]
                [% error_gender %]
            </td>
        </tr>

        <tr>
            <td class="label">Source:</td>
			<td class="search_bar">
				<input style="width: 300px" type="text" name="_source" id="location_name" />
				<input type="hidden" id="source_id" name="referral_source_id" />
                [% IF error_referral_source_id %]
                    [% error_referral_source_id %]
                [% ELSE %]
                    [ <a href="[% app_url %]/config/referral-sources">new</a> ]
                [% END %]
			</td>
        </tr>

    [% BLOCK validate_patient; IF c.cfg('use_cgi_ajax') %]
        onChange="do_ajax(
            ['last_name', 'first_name', 'is_valid_nhsno', 'is_valid_date_day',
            'is_valid_date_month', 'is_valid_date_year', 'unit_number'],
			['bad_patient']); return true;"
    [% END; END %]

        <tr>
            [% SET class = error_patient_id ? 'input_error' : 'field' %]
            <td class="label">Unit number:</td>
            <td class="content">
                <input class="[% class %]" type="text" name="unit_number" size="12"
					id="unit_number" [% site.html.on_focus %]
					   [% # PROCESS validate_patient # doesn't work if ! unit_no %]
					   value="[% c.query.param('unit_number') %]" />
                [% error_unit_number %]
            </td>
        </tr>
      </table>
    </div>

	[% IF c.query.param('_skip_pas') # flag to skip pas query if already done %]
	  <input type="hidden" name="_pas_confirmed" value="1" />
	[% END %]
	
	[% IF nhs_number_duplicate # set in duplicate.tt %]
	    <p class="error">
			Duplicate NHS number must be resolved before record can be submitted.
		</p>
    [% ELSE %]
		<p class="center">
			[% site.html.submit %]
			[% site.html.reset %]
		</p>
	[% END %]
    </form>

	<p>[% site.html.back_button %]</p>
<!-- END patient/default.tt -->