<%@page import="com.ardeo.entities.autoforms.FormView"%>
<%@ include file="/includes.jsp"%>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%--
<link rel="stylesheet" href="../tables.css" type="text/css" />
<link rel="stylesheet" href="../dialog.css" type="text/css" />
 --%>
<style>
.buttonDiv {
	border-bottom: 2px solid #B4B4B4;
	border-right: 2px solid #B4B4B4;
	background-color: #F2F2F2;
	float: left;
	width: 100px;
	height: 20px;
	font-family: arial;
	font-size: 15px;
	cursor: pointer;
	text-align: center;
	margin-left: 70px;
	padding-top: 5px;
}
</style>
<script type="text/javascript">
	function preSubmit() {
		//ensure multi-selections are selected
		var selectTags = document.getElementsByTagName("select");
		for (var i = 0; i < selectTags.length; i++) {
			if (selectTags[i].multiple) {
				var options = selectTags[i].options;
				for (var y = 0; y < options.length; y++) {
					options[y].selected = true;
				}
			}
		}
		return true;
	}

	function calldatepicker(triggerId, inputId) {
		$("#" + inputId).datepicker({
			showOn : "button",
			buttonImage : "../images/16x16/date.png",
			buttonImageOnly : true,
			dateFormat : "dd/mm/yy"
		});
		document.getElementById(triggerId).style.display = 'none';
	}
	function addEvent(dateFieldId, title) {
		var dateField = document.getElementById(dateFieldId);
		if (dateField) {
			if (!title)
				title = "";
			var date = dateField.value;
			window
					.open(
							"../calendar/editEventPopup.jsp?forward=closeWindow&id=new&type=event&date="
									+ date + "&title=" + title,
							'editEvent',
							'scrollbars=1,toolbar=0,status=1,menubar=1,resizable=1,location=0,directories=0,width=700,height=500,left=0,top=0');
		}
	}
	function save() {
        disableSaveButtonsAfterClick();
        document.getElementById("actionRequired").value = "save";
        document.getElementById("gotoPage").value = "meetingNote";
        preSubmit();
		document.getElementById("consultationNote").submit();
		return true;
	}
	function lockForm() {
		var r = confirmSummaryFormEditing();
		if (r) {
			var res = confirm("This form will be locked and no further changes will be possible. This change is not reversible.\n\nPlease confirm you want to proceed");
			if (res) {
				document.getElementById("gotoPage").value = "meetingNote";
				document.getElementById("actionRequired").value = "lockForm";
				document.getElementById("consultationNote").submit();
			}
		}
		return false;
	}
	function confirmSummaryFormEditing() {
		var frames = document.getElementsByTagName("iframe");
		if (frames.length > 0
				&& document.getElementById('isMeetingNote').value == 'isFromMeeting') {
			if (frames.length > 1
					|| (frames.length == 1 && frames[0].parentNode.id != 'divtoinsertTile')) {
				var r = confirm('Cancel editing Summary form, to continue editing Meeting Notes');
				if (r) {
					location.href = "resumeMeeting.form"
				} else {
					var iframe = frames[0];
					var doc = iframe.contentDocument.body.focus;
				}
			}
		}
		return true;

	}

	window.onload = function() {
		if (document.getElementById('divtoinsertTile')) // TODO this is possibly obsolete
			document.getElementById('divtoinsertTile').innerHTML = "<iframe src=\"tiles/formSummaryUpdate.jsp\" frameborder=\"0\" width=\"100%\" allowTransparency=\"true\"></iframe>"
	}
</script>
<script type="text/javascript" src="../emdt/tiles/fileContent.js"></script>
<smvc:form modelAttribute="consultationNoteForm" id="consultationNote" enctype="multipart/form-data" action="../meetings/consultationNote.form?${_csrf.parameterName}=${_csrf.token}" method="POST">
<input type="hidden" id="csrfparameterName" value="<c:out value="${_csrf.parameterName}"/>" />
<input type="hidden" id="csrfparametervalue" value="<c:out value="${_csrf.token}"/>" />
	<input id="overrideModifications" name="overrideModifications" value="no" type="hidden"/>
	<c:choose>
		<c:when test="${noteModifiedByOtherUser eq true }">
			<input id="noteModifiedByOtherUser" value="yes"  type="hidden" />
			<%@ include file="../../emdt/tiles/noteModifiedDetailPopup.jsp"%>
		</c:when>
		<c:otherwise>
			<input id="noteModifiedByOtherUser" value="no"  type="hidden" />
		</c:otherwise>
	</c:choose>
	<c:set var="formHasErrors">
		<smvc:errors path="*"/>
	</c:set>

	<c:if test="${! empty(requestScope.noteSavedSuccess) && empty(formHasErrors)}">
		<span class="info">
			<c:out value="${requestScope.noteSavedSuccess}"/>
		</span>
	</c:if>
	<c:if test="${requestScope.errorMsg ne null}">
		<span class="error"><c:out value="${requestScope.errorMsg}"/></span>
	</c:if>
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<c:set var="currentFormView" value="${consultationNoteForm.view}"></c:set>
	<%
		FormView currentFormView = (FormView) pageContext.getAttribute("currentFormView");
			session.setAttribute("currentFormView", currentFormView);
	%>
	<c:if test="${consultationNoteForm.view.note.locked eq true}">
		<div style="color: red">
			<img src="../emdt/images/16x16/locked.png" />This form is locked
		</div>
	</c:if>
	<smvc:errors cssClass="error"></smvc:errors>
	<smvc:hidden path="letterPath" />
	<smvc:hidden path="actionRequired" id="actionRequired" />
	<input type="hidden" value="false" id="unsaveButtonId" />
	<input type="hidden" name="gotoPageNum" id="gotoPageNum" />
	<input type="hidden" name="actionButton" id="actionButton" value="" />
	<input type="hidden" id="childListId2Filter" name="childListId2Filter"
		value="" />
	<input type="hidden" id="parentListName" name="parentListName" value="" />
	<input type="hidden" name="gotoPage" id="gotoPage" value="meetingNote" />
	<input type="hidden" name="isMeetingNote" id="isMeetingNote" value="<c:out value="${isFromMeeting}"/>">
	<%@ include file="../../emdt/tiles/attachFilePopupInForms.jsp" %>
	<table width="100%" class="tableHeader">
		<tr>
			<td>&nbsp;<c:out value="${formDef.label}"/></td>
			<td align="right"><c:out value="${consultationNoteForm.view.currentPage.label}"/>
				(page <c:out value="${consultationNoteForm.view.currentPageIndex + 1}"/> of
				<c:out value="${consultationNoteForm.view.totalPages}"/>)</td>
			<td align="right" width="1%"></td>
		</tr>
	</table>
	<tiles:insertTemplate template="${consultationNoteForm.view.renderer}" />
	<table width="100%">
		<tr>
			<td></td>
			<sec:authorize access="hasAnyRole('MEETING_ADMIN','ADMIN')">
				<td align="center"><c:if
						test="${meetingForm.locked ne 'true' && consultationNoteForm.view.note.locked ne true}">
						<button onclick="return lockForm();">
							<img src="../images/16x16/lock.png" border="0" /> Lock Form
						</button>
					</c:if></td>
				<td><c:if
						test="${meetingForm.locked ne 'true' && consultationNoteForm.view.note.locked ne true}">
						<button onclick="return save();" title="Save" id="saveDataButton">
							<img src="../images/16x16/accept.png" alt="Save" border="0" />
							Save
						</button>
					</c:if></td>
			</sec:authorize>
			<td></td>
		</tr>
	</table>
	<smvc:errors cssClass="error"></smvc:errors>

</smvc:form>
