<!-- ============================= -->
<!--          User Login Page      -->
<!-- ============================= -->
<script>
	function setBaseUrl() {
		baseUrl = window.location.origin
		doAjaxCall({
			type: 'GET',
			url: '/user/base_url',
			data: 'url='+ baseUrl
		})
	}
	setBaseUrl()
</script>
<div id="main">
	<form method="post" id="user_login_form">
		{% if keycloakConfigWebFile is not defined%}
			<aside class="header-component toolbar can-extend auto-extends">
				<h3>Login User</h3>
				<div class="extension">
					<div class="dropdown-pane">
						<ul>
							<li><button tabindex='50' type="submit" class="action action-confirm">Login</button></li>
						</ul>
					</div>
				</div>
			</aside>
		{% endif %}

		{% include 'index.html' %}

		<dl class="fieldlist">
			{% if keycloakConfigWebFile is defined %}
				{% if 'invalid_user' not in errors %}
				<dt class="info">Initializing Keycloak...</dt>
				<script>
					function keycloak_auth() {
						if (keycloak_initialized) {
							if (keycloak.authenticated) {
								$("<input />").attr("type", "hidden")
										.attr("name", "auth_token")
										.attr("value", keycloak.token)
										.appendTo("#user_login_form");
								$('#user_login_form').submit();
							} else {
								keycloak.login({
									scope: 'profile email group'
								})
							}
						} else {
							if (!keycloak_error) {
								setTimeout(keycloak_auth, 100)
							}
						}
					}
					keycloak_auth()
				</script>
				{% endif %}
			{% else %}
				<dt><label for="username">Login</label></dt>
				<dd><input tabindex='1' id="username" type="text" name="username" value="{{ data.username if 'username' in data else '' }}"/>
					<br/>
					{% if 'username' in errors %}
						<span class="errorMessage">{{ errors.username }}</span>
					{% endif %}
				</dd>

				<dt><label for="password">Password</label></dt>

				<dd><input tabindex='2' id="password" type="password" name="password" value="" />
					<br/>
					{% if 'password' in errors %}
						<span class="errorMessage">{{ errors.password }}</span>
					{% endif %}
				</dd>

				<dt class="info">New account</dt>
				<dd class="info" tabindex='3' onkeypress="redirectToHrefChild(this)"><a href="{{ deploy_context | safe }}/user/register" tabindex="-1"><u>Register</u></a></dd>
				<dt class="info">Password</dt>
				<dd class="info" tabindex='3' onkeypress="redirectToHrefChild(this)"><a href="{{ deploy_context | safe }}/user/recoverpassword" tabindex="-1"><u>Recover</u></a></dd>
				<dt class="info">More information</dt>
				<dd class="info" tabindex='3' onkeypress="redirectToHrefChild(this)"><a href="http://www.thevirtualbrain.org" title="Visit our website..." tabindex="-1">www.thevirtualbrain.org</a></dd>
				<dt class="info">Up-to-date</dt>
				<dd class="info" tabindex='3' onkeypress="redirectToHrefChild(this)"><a href="http://twitter.com/thevirtualbrain" title="Follow us on Twitter..." tabindex="-1">@thevirtualbrain</a></dd>
        	{% endif %}
			</dl>
   	</form>
</div>
