<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<c:set var="browser" value="${header['User-Agent']}" scope="page" />
<SCRIPT type="text/javascript">
if (!String.prototype.trim) {
	  (function() {
	    // Make sure we trim BOM and NBSP
	    var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
	    String.prototype.trim = function() {
	      return this.replace(rtrim, '');
	    };
	  })();
	}
	
function selectClinicianforTheSelectedHospital(){
    if (!document.getElementById("selectedHospital"))
        return;
	var selectedHospitalId= document.getElementById("selectedHospital").value;
	var select=document.getElementById("clinician");
	
		if(select!=null){
			for(var i = 1; i <=select.options.length; i++) {
			  select.options.remove(i);
			  select=document.getElementById("clinician");
			 }
			select.innerHTML = "";
			
			
		}
		if(selectedHospitalId==""){
		
		}else{
			var hospitalselected = "hospital"+selectedHospitalId;
			var locationList = document.getElementById(hospitalselected).value;
			if(locationList.charAt(0) == '['){
    			locationList= locationList.substr(1);
    		}
    		if(locationList.charAt(locationList.length-1) == ']'){
    			locationList= locationList.substr(0,locationList.length-1);
    		}
    		
    		var res = locationList.split(",");
		    for(var i = 0; i <res.length; i++) {
				var opt = new Option(res[i].trim(),res[i].trim());
				select.options[i] = opt;
		 	}
		}
	}
</SCRIPT>


<script type="text/javascript">

function lookUpPatientFromPAS ()	{
	var isEnabled = ${webapp.allowLookup};
	console.log("lookup from PAS");
	if (!isEnabled)
		return;
	var patientHospitalNumber=document.getElementById('patienthospitalNumber').value;
	console.log("lookup for hosp num " + patientHospitalNumber);
	if(patientHospitalNumber.length>0){
		var btn = document.getElementById('pasSearchButton');
		console.log("disable btn " + btn);
		btn.disabled = true;	// prevent double-click
	    fullScreenPleaseWait();
		location.href='../emdt/referralsPatient.form?action=patientlookup&hospNumber='+patientHospitalNumber;
	}else{
		alert("Please enter the patient hospital number before searching PAS")
	}
}

function GetUrlValue(VarSearch){
    var SearchString = window.location.search.substring(1);
    var VariableArray = SearchString.split('&');
    for(var i = 0; i < VariableArray.length; i++){
    	var KeyValuePair = VariableArray[i].split('=');
    	  if(KeyValuePair[0] == VarSearch){
              return KeyValuePair[1];
          }
    }
    return '';
}
</script>
<style>
select {
	min-width: 18px;
	width: 215px;
	max-width: 215px;
}
div.legendBoxWrapper table	{
	width: 100%;
}
div.legendBoxWrapper table td.label	{
	width: 40%;
}
div.legendBoxWrapper table td.input	{
	width: 60%;
	text-align: left;
	vertical-align: top;
}
div.legendBoxWrapper table tr	{
	height: 2em;
}

</style>

<c:set var="referralsDefinition" value="${referralPatientDto.referralsDefinition}"></c:set>
<div class="rowDiv">
	<div class="fieldSetBoxRadios">
		<div class="legendBox">Clinicians</div>
		<div class="legendBoxWrapper">
			Clinician:
			<smvc:select path="selectedClinician" >
				<smvc:option value=""></smvc:option>
				<smvc:options items="${clinicians}"  />
			</smvc:select>
		</div>
	</div>
</div>
<div class="rowDiv">
	<div class="fieldSetBoxRadios">
		<div class="legendBox">Priority</div>
		<div class="legendBoxWrapper">
			Priority:
			<webapp:selectAndEditList path="referralPatient.priority"  domain="EMDT" list="PatientPriority" includeEmpty="false" disabled="${editDisabled}" currentValue="${referralPatientDto.referralPatient.priority}" />
		</div>
	</div>
</div>

<!-- Not required at the moment
<div class="rowDiv">
	<div class="fieldSetBoxRadios">
		<div class="legendBox">Referral Method</div>
		<div class="legendBoxWrapper">
			Referral Method:
			<c:set var="referralMethod" value="${currentCareSpell.referralDetails.referralMethod}"/>
			<c:if test="${empty currentCareSpell.referralDetails.referralMethod}">
				<c:set var="referralMethod" value="${webapp.defaultReferralMethod}"></c:set>
			</c:if>
			<webapp:selectAndEditList path="currentCareSpell.referralDetails.referralMethod"  domain="EMDT" list="referralMethod"
			 includeEmpty="false" disabled="${editDisabled}"/>" currentValue="${referralMethod}" />
		</div>
	</div>
</div>
 -->
<div class="pdLeftColumn">
	<div class="fieldSetSectionHolder marginBottomNone">

		<div class="fieldSetBox">
			<div class="legendBox">Patient Details</div>
			<div class="legendBoxWrapper">
				<table>
					<tr>
						<td class="label">Hospital Number</td>
						<td class="input">
						<c:choose>
							<c:when test="${webapp.allowLookup}">
								<c:choose>
									<c:when test="${referralPatientDto.referralPatient.id ne null and referralPatientDto.referralPatient.hospitalNumber ne null}">
										<smvc:input path="referralPatient.hospitalNumber" onkeypress="if(event.keyCode == 13) lookUpPatientFromPAS()" id="patienthospitalNumber" readonly="true" />
									</c:when>
									<c:otherwise>
										<smvc:input path="referralPatient.hospitalNumber" onkeypress="if(event.keyCode == 13) lookUpPatientFromPAS()" id="patienthospitalNumber"/>
									</c:otherwise>
								</c:choose>
								<button id="pasSearchButton" onclick="lookUpPatientFromPAS()" style="cursor: pointer;"><img src="../images/16x16/zoom.png" alt="magnifying glass icon" title="Fetch patient details from PAS" /><img alt="spinner" src="../images/ajax-loader-bl.gif" title="Fetching patient details from PAS..." style="display: none;" id="pasSpinner"/></button>


							</c:when>
							<c:otherwise>
								<smvc:input path="referralPatient.hospitalNumber" id="patienthospitalNumber" />
							</c:otherwise>
						</c:choose>
					</tr>
				
					<tr>
						<td class="label">NHS Number</td>
						<td class="input"><c:choose>
							<%-- Only show PAS lookup when in management mode --%>
							<c:when test="${webapp.roleManagement}">
								<smvc:input path="referralPatient.nhsNumber" id="nhsNumberId"   disabled="${isPatientInfoReadonly}"/>
									<!-- 
									No searches on NHS number required at the moment
								<smvc:input path="referralPatient.nhsNumber" id="nhsNumberId"
									onkeypress="if(event.keyCode == 13) lookUpPatient()"  disabled="${isPatientInfoReadonly}"/>
								<a href="javascript:lookUpPatient()"
									title="Look Up Patient By NHS number"><img
									src="../images/16x16/zoom.png" /> </a>
									 -->
							</c:when>
							<c:otherwise>
								<smvc:input path="referralPatient.nhsNumber" id="nhsNumberId" />
							</c:otherwise>
						</c:choose></td>
					</tr>
				
					<tr>
						<td class="label">Title</td>
						<td class="input">
							<smvc:input path="referralPatient.title" cssClass="mediumTxtBox" maxlength="255"  disabled="${isPatientInfoReadonly}"/>
						</td>
					</tr>
					<tr>
						<td class="label">Surname</td>
						<td class="input">
							<smvc:input path="referralPatient.surname" maxlength="255"  disabled="${isPatientInfoReadonly}"/>
						</td>
					</tr>
					<tr>
						<td class="label">First name</td>
						<td class="input"><smvc:input path="referralPatient.forename" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Date of Birth</td>
						<td class="input">
							<smvc:input path="strDateOfBirth" cssClass="mediumTxtBox" placeholder="dd/mm/yyyy" disabled="${isPatientInfoReadonly}" />
							<c:if test="${not isPatientInfoReadonly}">
								<img title='Click to choose date' style='cursor: pointer'
									src='../images/16x16/date.png' id='strDateOfBirth_DateTrigger' />
							</c:if>
						</td>
					</tr>
					<tr>
						<td class="label">Date of Death</td>
						<td class="input">
							<smvc:input path="strDateOfDeath" cssClass="mediumTxtBox" placeholder="dd/mm/yyyy" disabled="${isPatientInfoReadonly}" />
							<c:if test="${not isPatientInfoReadonly}">
								<img title='Click to choose date' style='cursor: pointer'
									 src='../images/16x16/date.png' id='strDateOfDeath_DateTrigger' />
							</c:if>
						</td>
					</tr>
					<tr>
						<td class="label">Deceased</td>
						<td class="input"><smvc:checkbox path="referralPatient.deathIndicator" id="deathIndicator" disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Gender</td>
						<td class="input"><webapp:selectAndEditList domain="EMDT" patientInfoReadonly="${isPatientInfoReadonly}" path="referralPatient.sex" list="SexCode" currentValue="${referralPatientDto.referralPatient.sex}" includeEmpty="true" /></td>
					</tr>
				</table>
			</div>
		</div>
	</div>

</div>
<div class="pdRightColumn">
	<div class="fieldSetSectionHolder">
		<div class="fieldSetBox">
			<div class="legendBox">Patient Address</div>

			<div class="legendBoxWrapper">

				<table>
					<tr>
						<td class="label">Address</td>
						<td class="input"><smvc:textarea
								path="referralPatient.address" cols="20" rows="3" disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Postcode</td>
						<td class="input"><smvc:input path="referralPatient.postcode"
													  maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Country</td>
						<td class="input"><smvc:input path="referralPatient.country" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Home Phone</td>
						<td class="input"><smvc:input path="referralPatient.homePhoneNumber" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Mobile Phone</td>
						<td class="input"><smvc:input path="referralPatient.mobilePhoneNumber"
													  maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Work Phone</td>
						<td class="input"><smvc:input path="referralPatient.workPhoneNumber" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Email Address</td>
						<td class="input"><smvc:input path="referralPatient.emailAddress" maxlength="255"  disabled="${isPatientInfoReadonly}"/>
							<img src="images/imgEmailIcon.gif" alt="" /></td>
					</tr>
				</table>

			</div>
		</div>
	</div>
</div>

<c:if test="${webapp.properties['emdt.patientInfo.showAllergies'] ne false }">
	<div class="rowDiv" style="width: 100%;">
		<div class="fieldSetBox">
			<div class="legendBox">Allergies</div>
			<div class="legendBoxWrapper">
				<smvc:textarea path="referralPatient.allergies"  rows="3" disabled="${isPatientInfoReadonly}" cssStyle="width: 100%;" />
			</div>
		</div>
	</div>
</c:if>


<div class="pdLeftColumn">
<c:if test="${emdt.prefShowPatientInfoExtraDemographics}">
		<div class="fieldSetSectionHolder">
			<div class="fieldSetBox">
				<div class="legendBox">Additional Patient Details</div>


				<div class="legendBoxWrapper">
				
				<table>
					<tr>
						<td class="label">Marital Status</td>
						<td class="input"><webapp:selectAndEditList domain="EMDT" patientInfoReadonly="${isPatientInfoReadonly}" 
							path="referralPatient.maritalStatus" list="MaritalStatus"
							currentValue="${referralPatientDto.referralPatient.maritalStatus}"
							includeEmpty="true" />
							</td>
					</tr>
					<tr>
						<td class="label">Religion</td>
						<td class="input"><webapp:selectAndEditList path="referralPatient.religion"
							patientInfoReadonly="${isPatientInfoReadonly}" domain="EMDT" list="Religion"
							currentValue="${referralPatientDto.referralPatient.religion}" includeEmpty="true" /></td>
					</tr>
					<tr>
						<td class="label">Ethnic Group</td>
						<td class="input"><webapp:selectAndEditList path="referralPatient.ethnicity"
							patientInfoReadonly="${isPatientInfoReadonly}"
							domain="EMDT" list="Ethnicity"
							currentValue="${referralPatientDto.referralPatient.ethnicity}"
							includeEmpty="true" /></td>
					</tr>
					<!-- Not currently required by Spire
					<tr>
						<td class="label">Nationality</td>
						<td class="input"><webapp:selectAndEditList path="referralPatient.nationality"
							patientInfoReadonly="${isPatientInfoReadonly}"
							domain="EMDT" list="Nationality"
							currentValue="${referralPatientDto.referralPatient.nationality}"
							includeEmpty="true" /></td>
					</tr>
					<tr>
						<td class="label">Language</td>
						<td class="input">
							<webapp:selectAndEditList path="referralPatient.language"
								patientInfoReadonly="${isPatientInfoReadonly}"
								domain="EMDT" list="Language"
								currentValue="${referralPatientDto.referralPatient.language}"
								includeEmpty="true" />
							</td>
					</tr>
					<tr>
						<td class="label">Occupation</td>
						<td class="input"><smvc:input path="referralPatient.occupation" maxlength="255" /></td>
					</tr>
					<tr>
						<td class="label">Reg. Disabled</td>
						<td class="input"><smvc:radiobutton value="Yes"
							path="referralPatient.registeredDisabled" />
						Yes &nbsp;&nbsp;
						<smvc:radiobutton value="No"
							path="referralPatient.registeredDisabled" />
						No &nbsp;&nbsp;</td>
					</tr>
					 -->
				</table>
					
				</div>
			</div>
		</div>

	</c:if>
	<c:if test="${emdt.prefShowPatientInfoNextOfKin}">
		<div class="fieldSetSectionHolder marginBottomNone">

			<div class="fieldSetBox">
				<div class="legendBox">Next of Kin</div>


				<div class="legendBoxWrapper">
				
					<table>
						<tr>
							<td class="label">Surname</td>
							<td class="input"><smvc:input path="referralPatient.nokSurname" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
						</tr>
						<tr>
							<td class="label">First Name</td>
							<td class="input"><smvc:input path="referralPatient.nokForenames" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
						</tr>
						<tr>
							<td class="label">Telephone</td>
							<td class="input"><smvc:input path="referralPatient.nokTelephoneNumber1"
								maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
						</tr>
						<tr>
							<td class="label">Telephone 2</td>
							<td class="input"><smvc:input path="referralPatient.nokTelephoneNumber2"
								maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
						</tr>
						<tr>
							<td class="label">Address</td>
							<td class="input"><smvc:textarea
									path="referralPatient.nokAddress" cols="20" rows="3"  disabled="${isPatientInfoReadonly}"/></td>
						</tr>
						<tr>
							<td class="label">Postcode</td>
							<td class="input"><smvc:input path="referralPatient.nokPostcode" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
						</tr>
						<tr>
							<td class="label">Relationship to patient</td>
							<td class="input"><webapp:selectAndEditList path="referralPatient.nokRelationship"
								patientInfoReadonly="${isPatientInfoReadonly}"
								domain="EMDT" list="PatientNOKRelationship"
								currentValue="${referralPatientDto.referralPatient.nokRelationship}"
								includeEmpty="true" /></td>
						</tr>
					</table>
				
				</div>
			</div>
		</div>
		<div class="fieldSetSectionHolder txtCenter marginBottomNone">&nbsp;</div>
	</c:if>

</div>

<div class="pdRightColumn">



	<div class="fieldSetSectionHolder">
		<div class="fieldSetBox">
			<div class="legendBox">GP Details</div>


			<div class="legendBoxWrapper">
				<table>
					<tr>
						<td class="label">GMP Code</td>
						<td class="input"><smvc:input path="referralPatient.gpGmpCode"
						cssClass="mediumTxtBox" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Surname</td>
						<td class="input"><smvc:input path="referralPatient.gpSurname" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">First Name</td>
						<td class="input"><smvc:input path="referralPatient.gpForename" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Telephone</td>
						<td class="input"><smvc:input path="referralPatient.gpTelephoneNumber"
						maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Fax Number</td>
						<td class="input"><smvc:input path="referralPatient.gpFaxNumber" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Email Address</td>
						<td class="input"><smvc:input path="referralPatient.gpEmailAddress" maxlength="255"  disabled="${isPatientInfoReadonly}"/>
					<img src="images/imgEmailIcon.gif" alt="" /></td>
					</tr>
					<tr>
						<td class="label">Surgery Address</td>
						<td class="input"><smvc:textarea
							path="referralPatient.gpSurgeryAddress" cols="20" rows="3"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
					<tr>
						<td class="label">Postcode</td>
						<td class="input"><smvc:input path="referralPatient.gpPostcode"
						cssClass="mediumTxtBox" maxlength="255"  disabled="${isPatientInfoReadonly}"/></td>
					</tr>
				</table>
			</div>
		</div>
	</div>
</div>
<table width="100%">
	<tr>
		<td width="115"></td>
		<td align="right"><div class="rightSaveDiv"
				onclick="savePatient('<c:out value="${sessionScope.parentScreen}"/>');" id="save">
				<br />Save Patient Details
			</div></td>
	</tr>
	<tr height="20">
		<td></td>
	</tr>
</table>
<div id="commentsDiv" class="commentsBox">
	<div>
		<a name="commentsAnchor"></a>
	</div>
	<div class="commentsHeading">
		<img src="../images/16x16/comments.png" /> Comments<br />&nbsp;
	</div>
	
	
	
	<table>
	
		<c:set var="comment" value='<%=request.getAttribute("commentList")%>' />
		<c:forEach var="report" items='${comment}'>
			<tr>
				<td width="70%" class="commentsUserName"><c:out value="${report.username}"/><br />
					<c:out value="${report.hospitalCode}"/></td>
				<td width="30%" class="commentsText"><c:out value="${report.comment}"/></td>
			</tr>
			<tr>
				<td class="commentsDate" colspan="2"><fmt:formatDate
						value="${report.dateOfComment}" pattern="dd MMM yyyy, HH:mm" /></td>
			</tr>
			<tr>
				<td colspan="2"><hr color="lightgray" size="1" /></td>
			</tr>
		</c:forEach>
	
	
		<!--  start here -->
		<%--
		    <tr><td width="20%" align="left" valign="top">Hospital</td>
			<td width="80%" align="left" valign="top">
				<select name="selectedHospital"  id="selectedHospital" onchange="selectClinicianforTheSelectedHospital()">
						<option value="">---select a hospital----</option>
						<c:forEach var="hospitals" items="${webapp.hospMap}">
						  <option value="<c:out value="${hospitals.key}"/>"<c:if test="${referralsDefinition.hospitalId eq hospitals.key}"> selected="selected"</c:if> ><c:out value="${hospitals.value}"/></option>
						</c:forEach>
				</select>
				<c:forEach var="hospitals" items="${webapp.usersByHospitalIdMap}">
					<input type="hidden" name="hospital<c:out value="${hospitals.key}"/>" id ="hospital<c:out value="${hospitals.key}"/>" 
					value="<c:forEach var="userId" items="${hospitals.value}"><c:out value="${webapp.userIdUsernameMap[userId]},"></c:out> </c:forEach>"/>
				</c:forEach>
			</td>
		</tr>
		<tr><td width="20%" align="left" valign="top">Add User</td>
		<td width="80%" align="left" valign="top">
		<!--  testing  done-->
		<select id="clinician" name="clinician"></select>
		 <c:forEach var="user" items="${webapp.userNameNameMap}">
			<input type="hidden" id="<c:out value="${user.key}"/>" value="<c:out value="${user.value}"/>"/>
		 </c:forEach>
		<a  onclick="addClinician();" title="Add selected Clinician to referral pathway"><img src="../images/add.png" id="Addcomment" border="0" alt="Add" onmouseover="this.style.cursor = cursorPointer;" onload="selectClinicianforTheSelectedHospital();"  /></a>
		</td>
		</tr>
		
		<tr>
			<td valign="top">User<input type="hidden" name="clinicianList" id="clinicianList"></td>
			<td>
			
			<table width=40% id="clinicianTblId" border="1" bordercolor="#F8F8FF">
				<c:if test="${fn:length(referralsDefinition.clinicians)>0}">
					<c:forEach var="user" items="${referralsDefinition.clinicians}" varStatus="i">
						<!-- <tr id="cliniciantable<c:out value="${user.name}"/>">
							<td><c:out value="${user.name}"/></td>
							<td align="center">
							<a onclick="deleteRow('cliniciantable<c:out value="${user.name}"/>','clinicianTblId')">
							<img src="../images/delete.png" border="0" alt="Delete"
								onmouseover="this.style.cursor = cursorPointer; " /></a></td>
							<td style="display: none;"><c:out value="${user.name}"/></td>
						</tr> -->
					</c:forEach>
				</c:if>
			</table>
			 
			</td>
		</tr>
	
		--%>
		<!--  end  here -->

		<tr>
			<td class="commentsLabel">Send a comment or question about this
				page:</td>
			<td><textarea name="comments" cols="80" rows="3"></textarea>
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td class="commentsLabel" align="right">(Max 250 characters)</td>
		</tr>

	</table>
</div>
