<!-- 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 clear_tx_cycles() {
			$('#tx_cycles').val("");
		}
	
		function showhide_tx_cycles(tx_type_val) {
			var tx_type_ids_with_cycles = [ 3, 6 ];    // 3 is chemotherapy, 6 is clinical trial
			match_has_cycles = jQuery.grep(tx_type_ids_with_cycles, function (a) { return a == tx_type_val; }); //alert(match_has_cycles);
			if (match_has_cycles.length) { // ie show if the type has cycles data
				$('#tx_cycles').addClass("validate[optional,custom[onlyDecimal]]");      //adds jquery validation for cycles (can use optional or required)
				$('#tblRowTxCycles').show();                                            //shows table row containing cycles
			}
			else {
				$('#tx_cycles').removeClass("validate[optional,custom[onlyDecimal]]");  //removes jquery validation for cycles (can use optional or required)
				$('#tblRowTxCycles').hide();    
			}			 
			//			$('#tblRowTxCycles').hide();
		}	
	
		function showhide_tx_detail(f,v) {
			var t = f.tx_detail_id;
			var tx_type_ids = [ [% hmrn_data.maps.tx_type_ids.join(',') %] ];
			
			showhide_tx_cycles(v);
			
			// 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 %]';
				showhide_tx_cycles( $('#tx_type_id').val() );
			}
		);

		// 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">Cycles</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.tx_cycles %]"
					[% site.html.readonly %] size="4" />
			</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     ||
		error_tx_cycles		
    %]<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); clear_tx_cycles();">
					<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 id="tblRowTxCycles" style="display:none;">
			<td class="label">No. cycles:</td>
			<td class="content">
				<input type="text" name="tx_cycles" id="tx_cycles" size="4" /> <!--class="validate[required,custom[onlyDecimal]]" added in 'show' javascript -->
				<span style="font-size: smaller">&#171;
					( <a href="#" onclick="javascript:set_proposed('#tx_cycles', '8888'); return false">
						<span class="acronym" title="not stated [8888]">NS</span>
					</a> |
					<a href="#" onclick="javascript:set_proposed('#tx_cycles', '9999'); return false">
						<span class="acronym" title="not done [9999]">ND</span>
					</a> )
				</span>
				[% error_tx_cycles %]
			</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 -->