  [% META title = 'Admin &raquo; Referral Sources' -%]
<!-- BEGIN admin/config/referralsources/default.tt -->

	[% # PROCESS dumper.tt dump = c.stash.errs %]

  [% # PROCESS admin/system_nav.tt page = 'trials' %]

  [% referral_sources_dfv_js %]

	[%
		PROCESS site/autosuggest.tt
			varname     = 'organisation'
			param_id    = 'organisation_id'
			param_name  = 'parent_organisation'
			method_name = 'get_parent_organisations'
	%]
	[%
		PROCESS site/autosuggest.tt
			varname     = 'practice'
			param_id    = 'parent_practice_id'
			param_name  = 'parent_practice'
			method_name = 'get_parent_organisations'
			
			no_autosuggest_js = 1 # already loaded above
	%]
	
	<script language="JavaScript" type="text/javascript">
	<!-- Hide script
	//<![CDATA[
		function display(obj,id1,id2) {
			txt = obj.options[obj.selectedIndex].value;
			document.getElementById(id1).style.display = 'none';
			document.getElementById(id2).style.display = 'none';
			if ( txt.match(id1) ) {
				document.getElementById(id1).style.display = 'block';
			}
			if ( txt.match(id2) ) {
				document.getElementById(id2).style.display = 'block';
			}
		}
		$('#is_branch').live("click", function() {
			if (this.checked) {
				$('#parent').show();
			}
			else {
				$('#parent').hide();
			}
		});
	//]]> End script hiding -->
    </script>

	[% PROCESS admin/referral_nav.tt page = 'referral-sources' %]
	
    <h2 align="center">Referral Sources</h2>

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

    <p class="title">
		[% div_name = 'new'; INCLUDE site/snippets/toggleview.tt %]
		Add new entry
	</p>

	<div class="[% dfv_errors ? 'visible' : 'itemhidden' %]" id="[% div_name %]">
		
		<div class="indent">
			[% INCLUDE site/lookup.tt fname = 'popup2' # avoid interfering with 'help' %]
			
			[% INCLUDE site/snippets/help.tt file = 'newrefsrc' %]

            <form method="post" action="[% app_url %]/config/referral-sources/update"
			  [% referral_sources_onsubmit %] name="referral_source" id="referral_source">
			<table>
				<tr>
					<td class="label">Organisation Code:</td>
					<td class="content">
						[% PROCESS site/ajax_input.tt field = 'organisation_code' size = 6 %]
						[% error_organisation_code %]
					</td>
				</tr>
				<tr>
                    <td class="label">Active:</td>
                    <td>
                        <select name="is_active">
                        [% FOREACH option IN ['yes', 'no'] %]
                             <option value="[% option %]">
                                [% option %]
                            </option>
                        [% END %]
                        </select>
                        [% error_is_active %]
                    </td>
				</tr>
			</table>

			<p class="classname">
				<strong>Location Type:</strong>
				<select name="referral_type" [% site.html.onfocus %]							
						onchange="display(this,'hospital','practice');">
                    <option value="">--select one--</option>
					[% FOREACH type IN ['hospital', 'practice'] %]
						<option value="[% type %]">[% type %]</option>
					[% END %]
				</select>
				[% error_referral_type %]
			</p>

			[% # need to see errors if dfv_errors
				IF dfv_errors;  
					IF c.query.param('referral_type').match('hospital'); display = 'visible';
					ELSE; display = 'none'; END;
				ELSE; display ='none'; END;
			%]
			<div id="hospital" style="display: [% display %]">
				<table>
					<tr>
						<td class="label">Display name:</td>
						<td class="content">
							[% PROCESS site/ajax_input.tt field = 'display_name',
								id = 'ref_source_name', size = 65 %]
							[% error_display_name %]
							[% error_source_name # 'require_some' name %]
						</td>
					</tr>
					<tr>
						<td class="label">Parent Organisation:</td>
						<td class="search_bar">
							<input size="60" type="text" name="_parent_org" id="parent_organisation" />
							<input type="hidden" name="parent_organisation_id" id="organisation_id" />
							[% IF error_parent_organisation_id %]
								[% error_parent_organisation_id %]
							[% ELSE %]
								[ <a href="[% app_url %]/config/parent-organisations">new</a> ]
							[% END %]
						</td>
					</tr>
				</table>
			</div>
			
			[% # need to see errors if dfv_errors
				IF dfv_errors;  
					IF c.query.param('referral_type').match('practice'); display = 'visible';
					ELSE; display = 'none'; END;
				ELSE; display ='none'; END;
			%]
			<div id="practice" style="display: [% display %]">
				<table>
					<tr>
						<td class="label">Practice name:</td>
						<td class="content">
						<input type="text" name="practice_name" value="" size="50" />
							[% error_source_name # 'require_some' name %]
						</td>
					</tr>
					<tr>
						<td class="label">Practice address:</td>
						<td class="content">
						<input type="text" name="practice_address" value="" size="80" />
							[% error_practice_address %]
						</td>
					</tr>
					<tr>
						<td class="label">Post code:</td>
						<td class="content">
						<input type="text" name="practice_zip" value="" size="10" />
							[% error_practice_zip %]
						</td>
					</tr>
					<tr>
						<td class="label">Branch practice?</td>
						<td class="search_bar">
							<input type="checkbox" name="is_branch" id="is_branch" value="1" />
							<span id="parent" [% UNLESS dfv_errors || c.query.param('is_branch')
                  %]style="display: none"[% END %]>
								<input size="65" type="text" name="_parent_practice" id="parent_practice" />
								<input type="hidden" name="parent_practice_id" id="parent_practice_id" />
								[% IF error_parent_practice_id;
									error_parent_practice_id; ELSE %]
								[ <a href="[% app_url %]/config/parent-organisations">new</a> ]
								[% END %]
							</span>
						</td>
					</td>
				</table>
			</div>

			<p class="indent">
                <input type="submit" name="new_location" value="Submit">
            </p>
		</form>
		</div>
	</div>

	<hr />

	<p class="title">Search for an existing referral source:</p>
  
	<div class="indent">
		[% PROCESS search/referral_source_search.tt %]
	</div>
<!-- END admin/config/referralsources/default.tt -->
