<!-- BEGIN hmrn/data/treatment.tt -->

    [% # PROCESS dumper.tt dump = c.stash.errs %]
    [% # PROCESS dumper.tt dump = hmrn_data.treatments %]
    [% # PROCESS dumper.tt dump = hmrn_data.maps %]
	
	<script type="text/javascript">
		$(document).ready(function() {
			$("#treatment").validationEngine()
		})
	</script>
	<script language="javascript" type="text/javascript">
	<!--
		function showhide_tx_detail(f,v) {
			var t = f.tx_detail_id;
			var tx_type_ids = [ [% hmrn_data.maps.tx_type_ids.join(',') %] ];
			
			// does value of 'v' match any of tx_type_ids array:
			match = jQuery.grep(tx_type_ids, function (a) { return a == v; }); // alert(match);

			/* alternative:
			var match = 0;
			for (i=0; i<tx_type_ids.length; i++) {
				if (tx_type_ids[i] == v) {
					match = 1;
					break;
				}
			}
			if (match) { .. }
			*/
			
			if (match.length) { // ie contains the value of tx_type_id
				t.style.visibility='visible';
			}
			else {
				t.style.visibility='hidden';
			}			
		}

		[% visibility = error_tx_detail_id ? 'visible' : 'hidden' %]
		addLoadEvent(
			function() { 
				document.treatment.tx_detail_id.style.visibility='[% visibility %]';
			}
		);

		// tx_type_id onChange event for tx_detail_id select menu
		$(document).ready(function () {
			$('#tx_type_id').change( function() {
				$('#tx_detail').load('[% app_url %]/hmrn_data/treatment_options', {
					tx_type_id: $('#tx_type_id').val()	
				})
			})
		});		
	-->
	</script>
	
	<h3 class="header">Treatment/trial history:</h3>
    
	[% IF hmrn_data.treatments.size %]	
    <table class="indent">
        <tr>
			<td class="borderless"></td>
			<th scope="col">Centre</th>
			<th scope="col">Type</th>
 			<th scope="col">Detail</th>
 			<th scope="col">Start</th>
 			<th scope="col">End</th>
 			<th scope="col">Response</th>
        </tr>
		[% FOREACH episode IN hmrn_data.treatments %]
		<tr>
			<td>[% loop.count %]</td>
			<td>
				<input type="text" value="[% episode.location %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.tx_type %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.tx_detail %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.start_date.strftime('%d.%b.%Y') %]"
					[% site.html.readonly %] size="10" />
			</td>
			<td>
				<input type="text" value="[% episode.end_date.strftime('%d.%b.%Y') %]"
					[% site.html.readonly %] size="10" />
			</td>
			<td>
				<input type="text" value="[% episode.response %]" [% site.html.readonly %]
					size="3" />
			</td>
			[% IF user_can_enter_data %]
			<td>
				<a href="[% app_url %]/hmrn_data/edit_treatment/[% data.id %]/[% episode.id %]">
					edit
				</a>
			</td>
			[% END %]
		</tr>
		[% END %]
	</table>
	[% ELSE %]
		<p class="indent info">No previous treatment data recorded</p>
	[% END %]
	
	[% IF user_can_enter_data %]
    <p class="title">
		[% div_name = 'new_treatment'; INCLUDE site/snippets/toggleview.tt %]
		Add new entry
	</p>

    [% IF # provide a name="error" for internal anchor:
        error_tx_detail_id ||
        error_location_id  ||
        error_tx_type_id   ||
        error_start_date   ||
        error_end_date        
    %]<a name="error"><span>&nbsp;</span></a>[% END %]

	<div class="[% dfv_errors ? 'visible' : 'itemhidden' %]" id="[% div_name %]">
		<p class="indent warning">Minimum data entry: centre &amp; type</p>

	[% # PROCESS dumper.tt dump = c.validate('hmrn_treatment') # use this for validation %]
	
    <form method="post" name="treatment" id="treatment"
		action="[% app_url %]/hmrn_data/new_treatment/[% data.id %]/[% patient.id %]">

	  <table class="indent">	
		<tr>
			<td class="label">Centre:</td>
			<td class="content">
				<select name="location_id" class="validate[required]" id="location_id">
					<option value=""></option>
					[% FOREACH key IN hmrn_data.maps.location.keys.sort %]
                    <option value="[% hmrn_data.maps.location.$key %]">
                        [% key %]
					</option>
					[% END %]
				</select>
				[% error_location_id %]
			</td>
		</tr>        
		<tr>
			<td class="label">Type:</td>
			<td class="content">
				<select name="tx_type_id" id="tx_type_id" class="validate[required]"
						onchange="showhide_tx_detail(this.form,this.value)">
					<option value=""></option>
					[% FOREACH key IN hmrn_data.maps.tx_type.keys.sort %]
                    <option value="[% hmrn_data.maps.tx_type.$key %]">
                        [% key %]
					</option>
					[% END %]
				</select>
				[% error_tx_type_id %]
				
				<!-- // replaced by ajax output after selection of tx_type_id: -->
				<select name="tx_detail_id" id="tx_detail">
					[% IF hmrn_data.maps.tx_details.size %]
					<option value=""></option>
					[% FOREACH opt IN hmrn_data.maps.tx_details %]
						<option value="[% opt.key %]"
						[% 'selected' IF opt.key == data.tx_detail_id %]>
							[% opt.value %]
						</option>
					[% END; END %]
				</select>
				[% error_tx_detail_id %]
			</td>
		</tr>
		<tr>
			<td class="label">Start:</td>
			<td class="content">
				<input type="text" name="start_date" id="start_date" size="10"
                    class="validate[ajax[ajaxDate]] datepicker" />
				[% error_start_date %]
			</td>
		</tr>
		<tr>
			<td class="label">End:</td>
			<td class="content">
				<input type="text" name="end_date" id="end_date" size="10"
                    class="validate[ajax[ajaxDate]] datepicker" />
				[% error_end_date %]
			</td>
		</tr>
		<tr>
			<td class="label">Response:</td>
			<td class="content">
				<select name="response_id">
					<option value=""></option>
					[% FOREACH option IN hmrn_data.maps.response %]
                    <option value="[% option.value %]">[% option.key %]</option>
					[% END %]
				</select>
			</td>
		</tr>
	
		[% INCLUDE hmrn_submit %]
      </table>
    </form>
	</div>
	[% END %]
<!-- END hmrn/data/treatment.tt -->