<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<script src="../js/ReferralsDefinition.js" type="text/javascript"></script>
<link rel="stylesheet" href="../css/popup.css" type="text/css">
<%@ include file="PrintPopup.jsp" %>
<script type="text/javascript">
var unsavedData = null;
function savePatient(parentScreen){
	if ($("#savePatientButton").hasClass("rightSaveDivDisabled"))	{
    	console.log("ignore double click");
		return;
	}

	var reslt=true;
	if('ReferAPatient' == parentScreen){
		reslt = nhsNumberValidation();
	}
    if(reslt){
		document.forms[0].actionRequired.value='SAVE';
		$("#pleaseWait").show();
		$(".rightSaveDiv").attr('class',"rightSaveDivDisabled");
		$("input").prop('readonly', true);
		$("textarea").prop('readonly', true);
		$("select").prop('readonly', true);
		setClinicians();
		document.forms[0].submit();
	}else{
		return false;
	}
}
function convertInputToDiv(isPatientInfoReadonly){
	if(isPatientInfoReadonly == 'true'){
		var inputs = document.getElementsByTagName('input');
		for(var i=0;i<inputs.length;i++){
			var input=inputs[i];
			var id=input.id;
			var type=input.type;
			if(id!=null && id.length>0){
				if(type!='hidden' && (id!="selectedClinician" && id!="patienthospitalNumber")){
					var element=document.getElementById(id);
					var value=element.value
					if(type=='checkbox'){
						 value=element.checked;
					}
					var parentNode=element.parentNode;
					var childnodes=parentNode.childNodes;
					for(var j=0;j<childnodes.length;j++){
						if(childnodes[j].tagName=="INPUT"){
							childnodes[j].style.display="none";
						}else{
							parentNode.removeChild(childnodes[j]);
							j--;
						}
					}
					var h=document.createElement("H8")
					var new_id = id + "_ro";
					h.setAttribute("id", new_id);
					if(type=='checkbox'){
						if (value == true)
							value = "Yes";
						else 
							value = "No";
					}
					var t=document.createTextNode(value);
					h.appendChild(t);				
					parentNode.appendChild(h);
				}
			}
		}
		var textareas = document.getElementsByTagName('textarea');
		for(var i=0;i<textareas.length;i++){
			var textarea=textareas[i];
			var id=textarea.id;
			var type=textarea.type;
			if(id!=null && id.length>0){
				if(id!="comments"){
					var element=document.getElementById(id);
					var value=element.value;
					var parentNode=element.parentNode;
					var childnodes=parentNode.childNodes;
					for(var j=0;j<childnodes.length;j++){
						if(childnodes[j].tagName=="TEXTAREA"){
							childnodes[j].style.display="none";
						}else{
							parentNode.removeChild(childnodes[j]);
							j--;
						}
					}
					var h=document.createElement("H8")
					var new_id = id + "_ro";
					h.setAttribute("id", new_id);
  					var t=document.createTextNode("");
					h.appendChild(t);				
  					parentNode.appendChild(h);
					var para=document.createElement("pre");
					var node=document.createTextNode(value);
					para.appendChild(node);
					parentNode.appendChild(para);
				}
			}
		}
	}
}
function makeNewReferral(){
	var selectedClinician=document.getElementById('selectedClinician1').value;
	var answer = confirm("Add new speciality for this patient?");
	if (answer){
		$.ajax( {
			type : "POST",
			url : "../emdt/selectReferralsDefinition.form",
			data : "task=14&goToPage=emdt&${_csrf.parameterName}=${_csrf.token}",
			success : function(msg) {
				var table=document.getElementById('addSpecialitytoTable');
				var rowCount = table.rows.length;
				if(rowCount <= 0){
					var row = table.insertRow(rowCount);
					var column = row.insertCell(0);
					column.setAttribute("width", "70%");
					column.setAttribute("align", "center");
					 for (var index = 1; index<= msg.referralsDefinitionListLength; index++) {
						 for (var i = 0; i< msg.actuallListLength; i++) {
							 if(msg.referralsDefinitionList[i].orderIndex==index){
								var loc="../referrals/referralsPatient.form?newReferral=true&referralDefId="+msg.referralsDefinitionList[i].id+"&selectedClinician="+selectedClinician;
								var div = document.createElement("button");
								div.setAttribute( "onclick", "location.href='"+loc+"'" );
								div.style.marginBottom = '10px';
								div.style.color='#000000';
								div.style.cursor='pointer';
								div.style.fontWeight='bold';
								div.style.height='48px';
								div.style.textAlign='center';
								div.style.verticalAlign='middle';
								div.style.width='270px';
								var element =document.createTextNode( msg.referralsDefinitionList[i].title);
								div.appendChild(element);
								column.appendChild(div);
							 }
						 }
					 }
				 }
				 selectSpecialityForNewReferral();
			}
		});
	}
	else{
		return false;
	}
}
function addReferralDetails(){
	document.getElementById('addReferralDetailsDivId').style.display='none';
	document.getElementById('referringHospitalId').value=document.getElementById('referringHospital').value;
	document.getElementById('referringSpecialistUsername').value=document.getElementById('referringSpecialistUser').value;
	document.getElementById('referredFromUsername').value=document.getElementById('referredFromUser').value;
	document.getElementById('referredToUsername').value=document.getElementById('referredToUser').value;
	document.getElementById('referralMethodId').value=document.getElementById('referralMethod').value;
	document.getElementById('addReferralDetails').value="true";
	savePatient('ReferAPatient');
}
/* $( document ).ready(function() {
	convertInputToDiv('${isPatientInfoReadonly}');
}); */
</script>
<%@ include file="../../emdt/tiles/selectSpecialitypopup.jsp" %>
<%@ include file="../../emdt/tiles/selectReferralDetailspopup.jsp"%>
<input type="hidden" value="false" id="unsaveButtonId" />
<smvc:form name="referralsPatientForm" modelAttribute="referralPatientDto" method="post"  id="referralPatientDtoId" action="../referrals/referralsPatient.form">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" value="false" id="addReferralDetails" name="addReferralDetails"/>
	<input type="hidden" id="referringHospitalId" name="referringHospital"/>
	<input type="hidden" id="referringSpecialistUsername" name="referringSpecialistUser"/>
	<input type="hidden"  id="referredFromUsername" name="referredFromUser"/>
	<input type="hidden"  id="referredToUsername" name="referredToUser"/>
	<input type="hidden"  id="referralMethodId" name="referralMethod"/>
	<smvc:hidden path="actionRequired" />
	
	<smvc:errors></smvc:errors>
	<div class="container"><tiles:insertTemplate template="/referrals/tiles/referralsFormMenu.jsp" ></tiles:insertTemplate>
	<div class="bodySection">
	<c:if test="${referralPatientDto.referralPatient.id ne null }">
		<div style="float: left;">&nbsp;<a href="javascript:sendRequest('printPopup', 'null');" title="Print..."><img src="../images/16x16/printer.png" alt="Printer icon" border="0"/> Print </a>&nbsp;</div>
	</c:if>	<div id="pleaseWait" style="display: none; float: left; margin-left: 300px; margin-top: 20px;"><img src="../images/spinner_large.gif"/> Please wait...</div>
	<div class="rightSaveDiv" onclick="savePatient('${sessionScope.parentScreen}');" id="save"><br/>Save Patient Details</div>
	<div id="addLookupResultsDiv" style="visibility: hidden;"><input type="checkbox" id="addLookupResults" name="addLookupResults" checked=""/> Import Results after Save
	</div>
	
	<div class="menuSub2Holder">
		<c:if test="${referralPatientDto.referralPatient.id ne null }">
		<div class="rightButtonDiv" onclick="makeNewReferral();"><br />Add New Speciality</div>
		</c:if>
	</div>
	<div id="addReferralDetailsDivId">
	<c:if test="${referralPatientDto.referralPatient.id ne null and referralPatientDto.currentCareSpell.referralDetails eq null }">
		<br /><br /><br /><br /><br />
		<sec:authorize access="hasAnyRole('ADMIN','MANAGEMENT')">
			<div class="menuSub2Holder">
				<div class="rightButtonDiv" onclick="selectReferralDetails();"> <br />Add Referral </div>
			</div>
		</sec:authorize>
	</c:if></div>
	<div>
	<c:choose>
		<c:when
			test="${requestScope.successMsg ne null || requestScope.errorMsg ne null}">
			<c:if test="${requestScope.successMsg ne null}">
				<div class="message"><img
					src="../images/16x16/information.png" alt="i" />
					<c:out value="${requestScope.successMsg}"/></div>
			</c:if>
			<c:if
				test="${(null ne requestScope.errorMsg) and ('null' ne requestScope.errorMsg) and (fn:length(requestScope.errorMsg) ne 0)}">
				<div class="error"><img src="../images/16x16/error.png"
					alt="e" /><c:out value="${requestScope.errorMsg}"/></div>
				<br />
			</c:if>
		</c:when>
		<c:otherwise>
		  	<c:if
				test="${(null ne msg) and ('null' ne msg) and (fn:length(msg) ne 0)}">
				<div class="error"><img src="../images/16x16/error.png"
					alt="e" /><c:out value="${msg}"/></div>
				<br />
			</c:if>
		</c:otherwise>
	</c:choose>
	<c:if test="${requestScope.nhsWarning ne null}">
		<div class="error"><img
			src="../images/16x16/error.png" alt="e" />
			<c:out value="${requestScope.nhsWarning}"/></div>
	</c:if>
	<div class="error"><br/>
	<spring:bind path="referralPatientDto.*">
		<c:if test="${not empty status.errorMessages}">
			<c:forEach items="${status.errorMessages}" var="error">
				<img src="../images/16x16/error.png" alt="i" />&nbsp;<c:out
					value="${error}" escapeXml="false" />	<!-- TODO why does this need to not be escaped? -->
				<br />
			<input type="hidden" id="errorMsg" value="${error}"/>
			</c:forEach>
		</c:if>
		
	</spring:bind>
	</div>
	</div>
	<input type="hidden" id="createCareSpell" name="createCareSpell" value=""/> 
	<tiles:insertTemplate template="patientDetailsBox.jsp"/>
	</div>
	</div>
</smvc:form>
