<%@page import="com.ardeo.entities.autoforms.FormDefinition"%>
<%@page import="com.ardeo.entities.autoforms.FormView"%>
<%@ include file="/includes.jsp" %>
<script>
	
	function previousPage() {
		rememberSaveState();
		document.getElementById("actionRequired").value = "previous";
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	
	function nextPage() {
		rememberSaveState();
		document.getElementById("actionRequired").value = "next";
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	
	function updateReferences() {
		if (confirm('Updating will revert any fields that have defaults from other forms, back to their default values.\nDoing so may potentially discard your later entries.\n\nPlease confirm this is OK') == 0)
			return;
		document.getElementById("actionRequired").value = "updateReferences";
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	
	function fileUploader(id) {
		document.getElementById("actionRequired").value = "fileUpload";
		document.getElementById("actionButton").value = id;
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	
	
	function fileDeleteAction(id, fileNameSel) {
		if(confirm("Are you sure that you want to delete the file?")){
			document.getElementById("actionRequired").value = "fileDelete";
			document.getElementById("actionButton").value = id;
			document.getElementById("fileName").value=fileNameSel;
			preSubmit();
			document.getElementById("consultationNote").submit();
		}
	}
	function fileViewAction(id, fileNameSel) {
		document.getElementById("actionRequired").value = "fileView";
		document.getElementById("actionButton").value = id;
		document.getElementById("fileName").value=fileNameSel;
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	function userCmd(id) {
		document.getElementById("actionRequired").value = "userCmd";
		document.getElementById("actionButton").value = id;
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	function checkComment() {

		if (document.getElementById('comments')) {
			var comment = document.getElementById('comments').value;
			if (comment != null && trimAll(comment).length > 0) {
				return window.confirm("You have a Note that is not added yet.\nSaving the form will discard your Note.\n\nProceed?");
			}
		}
		if (document.getElementById('my_file'))	{
			var fileControl = document.getElementById('my_file').value;
			if(fileControl!=null && trimAll(fileControl).length>0){
				return window.confirm("You have a file that is not attached yet in the Note.\nSaving the form will discard your file.\n\nProceed?");
			}
		}
		return true;
	}
	function save() {
		var proceed=checkComment();
		if(proceed){
			document.getElementById("actionRequired").value = "save";
			// disable both the bottom button and the big button when either are clicked
	        var bottomSaveButton = document.getElementById("saveDataButton");
	        var topSaveButton = document.getElementById("save");
	        if ((topSaveButton && topSaveButton.disabled) || (bottomSaveButton && bottomSaveButton.disabled))	{
	        	console.log("ignore double click");
	        	return;
	        }
			if (bottomSaveButton)	{
				// this is an A tag
	        	bottomSaveButton.disabled=true; 
	        	bottomSaveButton.innerHTML="<img src='../images/ajax-loader-bl.gif'/> Saving..."; 
	        	bottomSaveButton.href='#'; 
			}
			if (topSaveButton)	{
				// this is a DIV tag
	        	topSaveButton.disabled=true; 
	        	topSaveButton.style.cursor = 'wait';
	        	topSaveButton.style.color = 'grey';
	        	topSaveButton.innerHTML="<br/><img src='../images/ajax-loader-bl.gif'/> Saving..."; 
			}
			fullScreenPleaseWait();
			preSubmit();
			document.getElementById("consultationNote").submit();
		}
	}
	
	function cancel() {
		if (!confirm("Are you sure you want to cancel changes?"))	{
			return;
		}
		document.getElementById("actionRequired").value = "cancel";
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	
	function print() {
		rememberSaveState();
		document.getElementById("actionRequired").value = "print";
		preSubmit();
		document.getElementById("consultationNote").submit();
	}
	// note: because we want to pass the note_id parameter to the final page used to render the PDF (which is emdt/printFormPdf.form), 
	// we want to stop 'convertJspToPdf.form' taking the parameter, hence this is encoded separately
	function makePdf(formLabel,noteId)	{
			var filename = encodeURIComponent(formLabel);
			var urlToRender = encodeURIComponent("emdt/printFormPdf.form?note_id="+noteId);
			window.location.href = '../emdt/convertJspToPdf.form?jspUrl='  + urlToRender + '&isLandscapePrint=N&filename=' + filename;
		}
		
	function update() {
		document.getElementById("actionRequired").value = "update";
		preSubmit();
		document.getElementById("consultationNote").submit();
	}

    function refreshChildList(parentListName,childFieldId) {
        if(document.getElementById("childListId2Filter") != null){
            document.getElementById("childListId2Filter").value = childFieldId;
        }
        if(document.getElementById("parentListName") != null){
            document.getElementById("parentListName").value = parentListName;
        }
        
        document.getElementById("actionRequired").value = "filterList";
        preSubmit();
        document.getElementById("consultationNote").submit();
    }
	
	function changelog(noteId) {
		window.open("../emdt/viewChangelog.form?noteId="+noteId,'changelog','scrollbars=1,toolbar=1,status=1,menubar=1,resizable=1,location=0,directories=0,width=600,height=400,left=0,top=0');
	}
	
	function rememberSaveState() {
		removeDisabledAttribute();
		if(unsavedData) {
			var form = document.getElementById('consultationNote');
			var input = document.createElement("input");
			input.setAttribute("type", "hidden");
			input.setAttribute("name", "requiresSave");
			input.setAttribute("value", "true");
			form.appendChild(input);
		}
	}
	
function gotoPage(pageNum) {
	if(pageNum) {
		rememberSaveState();
		document.getElementById("actionRequired").value = "goto";
		document.getElementById("gotoPageNum").value = pageNum;
		document.getElementById("consultationNote").submit();
	}
}
function removeDisabledAttribute(){
	var elem=document.getElementById("consultationNote").elements;
	for(var i = 0; i < elem.length; i++){
		if(elem[i].type!="hidden"){
			elem[i].disabled = false;
		}
    } 
}
</script>
<c:if test="${printForm ne null }">
	<script type="text/javascript">
	window.open('../emdt/printForm.jsp','printForm','scrollbars=1,toolbar=1,status=1,menubar=1,resizable=1,location=0,directories=0,width=600,height=400,left=0,top=0');
	</script>
</c:if>

<c:set var="patientHistoryItems" value="${consultationNoteForm.patientHistoryItems }"/>
<c:set var="currentNoteStatus" value="${fn:length(patientHistoryItems)}"/>
<!-- Spacer table to outline nav controls -->
<c:forEach var="item" items="${patientHistoryItems }" varStatus="s1">
	<c:if test="${item.note.guid eq consultationNoteForm.view.note.guid}">
		<c:set var="currentNoteStatus" value="${s1.count}"></c:set>
	</c:if>
</c:forEach>
<table class="navButtonBar" width="600px">
	<tr> 
		<td><span class="title">Speciality:</span><br/><webapp:listValue domain="EMDT" listName="Speciality" key="${emdt.currentCareSpell.primarySite}" /></td>
		<td><span class="title">Created:</span> <br/><fmt:formatDate value="${consultationNoteForm.view.note.creationDate}" pattern="dd MMM yyyy HH:mm:ss" /> </td>
		<td><span class="title">Updated:</span><br/><fmt:formatDate value="${consultationNoteForm.view.note.modifiedDate}" pattern="dd MMM yyyy HH:mm:ss" /></td>
		<td class="buttonCell">
			Record
			<c:choose>
				<c:when test="${currentNoteStatus eq 1 }">
					<img src="../images/16x16/action_back_disabled.png" alt="Previous Note (disabled)" border="0"/>
				</c:when>
				<c:otherwise>
					<c:forEach var="item" items="${patientHistoryItems }" varStatus="s1">
						<c:if test="${s1.count eq currentNoteStatus-1}">
							<a href="consultationNote.form?cnNoteId=<c:out value="${item.note.guid}"/>&filterFormId=<c:out value="${consultationNoteForm.view.form.id}"/>" title="Previous Note"><img src="../images/16x16/action_back.png" alt="Previous Note" border="0"/></a>
						</c:if>
					</c:forEach>
				</c:otherwise>
			</c:choose>
			<c:out value="${currentNoteStatus}"/> of  <c:out value="${fn:length(patientHistoryItems)}"/>
			<c:choose>
				<c:when test="${currentNoteStatus eq fn:length(patientHistoryItems)}">
					<img src="../images/16x16/action_forward_disabled.png" alt="Next Note (disabled)" border="0"/>
				</c:when>
				<c:otherwise>
					<c:forEach var="item" items="${patientHistoryItems}" varStatus="s1">
						<c:if test="${s1.count eq currentNoteStatus+1}">
							<a href="consultationNote.form?cnNoteId=<c:out value="${item.note.guid}"/>&filterFormId=<c:out value="${consultationNoteForm.view.form.id}"/>" title="Next Note"><img src="../images/16x16/action_forward.png" alt="Next Note" border="0"/></a>					
						</c:if>
					</c:forEach>
				</c:otherwise>
			</c:choose>
			&nbsp;
		</td>
	</tr>
</table>
<br/>
<!-- Spacer table to outline nav controls -->
<table class="navButtonBar">
 <tr>
  <td>
	
	<table>
		<tr>
	<td class="buttonCell">
		<c:choose>
		<c:when test="${consultationNoteForm.view.previousPageAvailable eq true}">
			<a href="javascript:previousPage()" title="Previous page"><img src="../images/16x16/action_back.png" alt="Previous page" border="0"/></a>
		</c:when>
		<c:otherwise>
			<img src="../images/16x16/action_back_disabled.png" alt="Previous page (disabled)" border="0"/>
		</c:otherwise>	</c:choose>
	</td>
	<td class="buttonCell">
		<select name="pageSelector" id="pageSelector" title="Go to page" onchange="gotoPage(this.options[this.selectedIndex].value)">
			<c:set var="consultationNoteFormView" value="${consultationNoteForm.view}"/>
			<%
			FormView view= (FormView)pageContext.getAttribute("consultationNoteFormView"); 
			FormDefinition def = view.getForm();
				int pgNum = view.getCurrentPageIndex();
				int count = def.getChildren().size();
				for(int i = 0; i < count; i++) {
					String selected = pgNum == i ? "selected='selected'" : "";
					String pageLabel = def.getChildren().get(i).getLabel();
					if (pageLabel.length() > 20)
						pageLabel = pageLabel.substring(0, 20) + "...";
			%>
			<option value="<%=(i + 1)%>" <%=selected%>><%=pageLabel%></option>
			<%
				}
			%>
		</select>
	</td>
	<td class="buttonCell">
		<c:choose>
			<c:when test="${consultationNoteForm.view.nextPageAvailable eq true}">
				<a href="javascript:nextPage()" title="Next page"><img src="../images/16x16/action_forward.png" alt="Next page" border="0"/></a>			</c:when>
			<c:otherwise>
				<img src="../images/16x16/action_forward_disabled.png" alt="Next page (disabled)" border="0"/>
			</c:otherwise>	
		</c:choose>
	</td>
	<c:if test="${consultationNoteForm.view.note.id ne null}">
		<td class="buttonCell">
			&nbsp;<a href="javascript:changelog('<c:out value="${consultationNoteForm.view.note.guid}"/>');" title="View changes log"><img src="../images/16x16/script_key.png" alt="View changes log" border="0"/> View changes</a>&nbsp;
		</td>
	</c:if>
	<td class="buttonCell" >
			&nbsp;<div onclick="javascript:print();" style="cursor: pointer;color:#4d4d4d;"><img  src="../images/16x16/printer.png" alt="Printer icon" border="0" title="Print..."/>Print</div>&nbsp;
	</td>
	<%--
		SPIRE-242
		Temporary change to only enable PDF button for specific forms
		these are the final forms:
		<c:if test="${formView.form.name eq 'TriageAssessment' or formView.form.name eq 'ChemoF11' or formView.form.name eq 'ChemoF13' or formView.form.name eq 'ChemoF1' or formView.form.name eq 'ChemoF10' or formView.form.name eq 'ChemoF12' or formView.form.name eq 'ChemoF7' or formView.form.name eq 'SACTF06' }">
	 --%>
	<c:if test="${consultationNoteForm.view.form.parent eq 'ChemoG1'}">
			<c:if test="${consultationNoteForm.view.form.name eq 'TriageAssessment' or consultationNoteForm.view.form.name eq 'ChemoF11' or consultationNoteForm.view.form.name eq 'ChemoF13' or consultationNoteForm.view.form.name eq 'ChemoF4NN' }">
			<td class="buttonCell">
				<c:if test="${consultationNoteForm.note.id ne null}">
				<a href="javascript:makePdf('<c:out value="${consultationNoteForm.view.form.label}"/>','<c:out value="${consultationNoteForm.view.note.guid}"/>')" title="Download Form as PDF"><img src="../emdt/images/16x16/pdf.gif" alt="pdf"/> PDF </a>
				</c:if>
				<c:if test="${consultationNoteForm.view.note.id eq null}">
				<a href="javascript:alert('Please save the form before making a PDF')" title="Download Form as PDF"><img src="../emdt/images/16x16/pdf.gif" alt="pdf"/> PDF </a>
				</c:if>
			</td>
		</c:if>
	</c:if>
	<td class="buttonCell">
			&nbsp;<a href="javascript:cancel()" title="Cancel"><img src="../images/16x16/Undo.png" alt="Cancel" border="0"/>Cancel </a>&nbsp;
	</td>
	<td class="buttonCell">
		<c:if test="${! consultationNoteForm.note.locked }">
		&nbsp;<a href="javascript:updateReferences()" title="Update references to latest values"><img src="../images/16x16/edit.png" border="0"/> Update</a>
		</c:if>	
	</td>
	<td class="buttonCell">
		<c:if test="${! consultationNoteForm.view.note.locked }">
		&nbsp;<a href="javascript:save()" title="Save"  id="saveDataButton" style="padding: 2px;"><img src="../images/16x16/accept.png" alt="Save" border="0" /> Save</a>
		</c:if>
	</td>
	<c:if test="${consultationNoteForm.view.note.editable ne true}">
		<td class="buttonCell" >
			<!-- <input class="disabledButton" type="button" value="Save" disabled="true" title="No permission" /> -->
		</td>
	</c:if>
	</tr>
	</table>

  </td>
 </tr>
</table>