<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<script type="text/javascript">
function makeNewReffer(nhsNumber,status){
	var errMsg;
	if(document.getElementById('errorMsg')!=null){
		errMsg = document.getElementById('errorMsg').value;
	}
	if(errMsg!=null && errMsg!='' ){
		if(errMsg=="Patient with this NHS number already exists." && status=="true"){
		var answer = confirm(errMsg+"Do You Want to Make a new referral for this existing patient.");
		if (answer){
			location.href='../nationalMDT/referralsPatient.form?newReferral=true&nhsNumber='+nhsNumber;
		}
		else{
		}
	}	
 }
}
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
					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 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 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);
				}
			}
		}
	}
}
</script>

<c:if test="${referralPatientDto.referralPatient ne null && referralPatientDto.referralPatient ne null}">
	<tiles:insertTemplate template="/nationalMDT/tiles/nationalMDTDetailsTopBar.jsp" flush="true"/>
</c:if>

<input type="hidden" value="false" id="unsaveButtonId" />
<smvc:form name="referralsPatientForm" modelAttribute="referralPatientDto"
	method="post" onsubmit="return submitTheForm();">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<smvc:hidden path="actionRequired" />
	<smvc:errors></smvc:errors>
	<div class="container">
	<tiles:insertTemplate template ="nationalMDTFormMenu.jsp" ></tiles:insertTemplate>
	<div class="bodySection">
	<div class="rightSaveDiv" onclick="saveNMDMPatient('<c:out value="${sessionScope.parentScreen}"/>');" id="save"><br/>Save Patient Details</div>
	<br/>
	<br/>
	<br/>
	<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>
	
	<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" />
				<br />
			<input type="hidden" id="errorMsg" value="<c:out value="${error}"/>"/>
			</c:forEach>
		</c:if>
		
	</spring:bind><br />
	</div>
	</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>

<script>
	convertInputToDiv('<c:out value="${isPatientInfoReadonly}"/>');
</script>