<%@ 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('selectedClinician').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 i = 0; i< msg.actuallListLength; i++) {
						var loc="../emdt/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="selectSpecialitypopup.jsp" %>
<%-- EMDT-695 don't include data for popup if Add Referral not in use
<%@ include file="selectReferralDetailspopup.jsp"%>
--%>
<input type="hidden" value="false" id="unsaveButtonId" />
<smvc:form name="referralsPatientForm" modelAttribute="referralPatientDto" method="post"  id="referralPatientDtoId" action="../emdt/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 cssClass="error" path="*"></smvc:errors>
	<div class="container">
	<tiles:insertTemplate template ="emdtFormMenu.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 id="savePatientButton" class="rightSaveDiv" onclick="savePatient('<c:out value="${sessionScope.parentScreen}"/>');" id="save"><br/>Save Patient Details</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 class="rightCenterCommentDiv" style="display: none;">
	<A HREF="referralsPatient.form#commentsAnchor">Click here to give Comments</A>
	</div>
	<br />
	<br />
	<br />
	<%-- EMDT-695 hide Add Referral button temporarily
	<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 id="messagesContainer" style="width: 500px;">
	<c:choose>
		<c:when
			test="${requestScope.successMsg ne null || requestScope.errorMsg ne null}">
			<c:if test="${requestScope.successMsg ne null}">
				<span class="info"><c:out value="${requestScope.successMsg}" /></span>
			</c:if>
			<c:if
				test="${(null ne requestScope.errorMsg) and ('null' ne requestScope.errorMsg) and (fn:length(requestScope.errorMsg) ne 0)}">
				<span class="error"><c:out value="${requestScope.errorMsg}"/></span>
				<br />
			</c:if>
		</c:when>
		<c:otherwise>
		  	<c:if
				test="${(null ne msg) and ('null' ne msg) and (fn:length(msg) ne 0)}">
				<span class="error"><c:out value="${msg}"/></span>
				<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>
	<input type="hidden" id="createCareSpell" name="createCareSpell" value=""/> 
	<tiles:insertTemplate template ="patientDetailsBox.jsp" ></tiles:insertTemplate>
	</div>
	</div>

	<script type="text/javascript">
		function dateChanged(calendar) { }
	</script>

</smvc:form>