<%@page import="org.springframework.security.core.AuthenticationException"%>
<%@page import="org.springframework.security.web.WebAttributes"%>

<%@ include file="/includes.jsp" %>
<c:if test="${not empty param.login_error}">
	<c:set var="showForgetPassword" value='<%=((AuthenticationException) session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).getMessage()%>' />
</c:if>

<script type="text/javascript">
function disableOnSubmit()	{
	document.getElementById('pleasewait').style.visibility='visible';
	document.getElementById('loginbutton').disabled=true;
	document.getElementById('loginform').submit();
}
function showhospital(){
	var mode=	document.getElementById('j_mode').value;
	if(mode === 'local'){
		document.getElementById('hospitalSelection').style.display='none';
	}else{
		document.getElementById('hospitalSelection').style.display='';
	}
	setUserHospital();
}
function setUserHospital() {
    if (document.getElementById('j_mode')) {

		var mode = document.getElementById('j_mode').value;
		if (mode !== 'local') {
			var username = document.getElementById('j_username').value;
			if (document.getElementById(username) != null && document.getElementById(username).value != null) {
				var hospId = document.getElementById(username).value;
				document.getElementById('currentUserSelectedHospital').value = hospId;
			} else {
				document.getElementById('currentUserSelectedHospital').value = "";
			}
		}
	}
}
</script>
<form action="<c:url value='/login' />" method='POST' id="loginform">
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
	<table class="dialogBox">
		<tr >
			<td colspan="2" class="dialogTitle"><spring:message code="emdt.login.login" text="Login" /></td>
		</tr>
		<tr>
			<td height="5" colspan="2">&nbsp;</td>
		</tr>
			<%--<td><hidden property="includeDomain" ></hidden><input type="checkbox" name="includeDomainCheck" onclick="document.forms[0].domain.disabled=!this.checked;document.forms[0].includeDomain.value=String(this.checked);" /></td>--%>
	
		<tr>
			<td class="label"><label for="j_username"><spring:message code="emdt.login.username" text="Username" /></label></td>
			<td class="input">
				<input type="text" id="j_username" name="username" size="18" maxlength="100" autofocus autocomplete="off"/>
			</td>
		</tr>
		<tr>
			<td class="label"><label for="j_password"><spring:message code="emdt.login.password" text="Password" /></label></td>
			<td class="input">
				<input type="password" id="j_password" name="password" size="18" maxlength="100" autocomplete="off">
			</td>
		</tr>
		<c:if test="${'true' eq isAuthenticationLdap}">
			<tr>
				<td class="label"><spring:message code="emdt.login.method" text="Log in using:" /></td>
						<td class="input">
				<%
		           String modeKey=null;
		           String previousLoginHospital = null;
				   Cookie[] cookies = null;
				   cookies = request.getCookies();
				   if( cookies != null ){
					   for (Cookie cookie : cookies) {
						   if (cookie.getName().equals("modeKey")) {
							   modeKey = cookie.getValue();
						   } else if (cookie.getName().equals("previousLoginHospital")) {
							   previousLoginHospital = cookie.getValue();
						   }
					   }
				  }
				%>
					<c:set var="mode" value="<%=modeKey%>"/>
					<c:set var="previousHospital" value="<%=previousLoginHospital%>"/>
					<select id="j_mode" name="j_mode"  onchange="showhospital();">
						<c:forEach items="${domainNames}" var="ldapServer">
							<option value="<c:out value="${ldapServer}"/>" <c:if test="${mode ne null && mode eq ldapServer}">selected="selected"</c:if>><c:out value="${ldapServer}"/></option>
						</c:forEach>
						<option value="local" <c:if test="${mode ne null && mode eq 'local'}">selected="selected"</c:if>>eMDT authentication</option>
					</select>
				</tr>
				<tr <c:if test="${mode ne null && mode eq 'local'}"> style="display: none;"</c:if> id="hospitalSelection" >
					<td class="label"><label for="currentUserSelectedHospital"> <spring:message code="emdt.login.hospital" text="Hospital" /></label></td>
					<td class="input"><select name="currentUserSelectedHospital"  id="currentUserSelectedHospital">
							<option value=""></option>
							<c:forEach var="hospital" items="${hospitalsList}">
								<option value="<c:out value="${hospital.id}"/>" <c:if test="${previousHospital ne null && previousHospital eq hospital.id}"> selected="selected"</c:if> ><c:out value="${hospital.hospitalName }"/></option>
							</c:forEach>
						</select>
					</td>
				</tr>
		</c:if>
		<tr height="25px">
			<td colspan="2" align="right" style="padding-right: 6px; padding-bottom: 6px;">
				<input type="submit" value='<spring:message code="emdt.login.login" text="Log in" />' onclick="return disableOnSubmit();" id="loginbutton"/>
			</td> 
		</tr>
	</table>
	
	<div id="pleasewait" style="visibility: hidden;"><img src="images/ajax-loader-bl.gif"/> <spring:message code="emdt.login.pleasewait" text="Please wait..." /></div>
</form>
<c:if test="${not empty param.login_error}">
<c:set var="ERROR" value="<%=((AuthenticationException) session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).getCause()%>"/>
	<span class="error">
		<spring:message code="emdt.login.unsuccessful" text="Your login attempt was not successful."/><br/><br/>
    	Reason:
    <c:choose>
    <c:when test="${ERROR ne null && ERROR eq 'DATABASE_DOWN_ERROR'}">
     	It was not possible to connect to the database. Please contact your system administrator.
    </c:when>
    <c:otherwise>
       <%= ((AuthenticationException) session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).getMessage() %>
    </c:otherwise>
</c:choose>

		<%-- see EMDT-721
          <c:if test="${showForgetPassword eq 'Bad credentials'}">
		    	<br/>
		    	<br/>
				<a href="requestpassword.jsp"><b><spring:message code="emdt.login.forgot" text="Did you forget your password? Click here to reset"/></b></a>
		    	<br/>
		    	<br/>
			</c:if>
		--%>
	</span>
</c:if>
