{% extends "base.html" %}
{% load i18n %}

{% block sub_title %}{% trans "Log In" %} - {% endblock %}

{% block main_content %}
<div class="new-narrow-panel">
<h2 class="hd">{% trans "Log In" %}</h2>
<form action="" method="post" class="con">{% csrf_token %}
    <label for="login">{% trans "Email or Username" %}</label>
        <input type="text" name="login" id="login" value="" class="input" />
	<label for="password">{% trans "Password" %}</label>
	<a href="{{ SITE_ROOT }}accounts/password/reset/">{% trans "(forgot password)" %}</a>
	<input type="password" name="password" id="password" value="" class="input" autocomplete="off" />
    {% if form.captcha %}
    <label>{% trans "CAPTCHA" %}</label><br />
    {{ form.captcha }}
    <p class="clear"><a href="#" id="refresh-captcha">{% trans "Not clear? Refresh it." %}</a></p>
    {% endif %}
    <input type="hidden" name="next" value="{% if next %}{{ next|escape }}{% else %}{{ SITE_ROOT }}{% endif %}" />
    {% if form.errors %}
      {% if form.captcha.errors %}
        <p class="error">{{ form.captcha.errors}}</p>
      {% elif form.errors.freeze_account %}
        <p class="error">{{ form.errors.freeze_account }}</p>
      {% else %}
        <p class="error">{% trans "Incorrect email or password" %}</p>
      {% endif %}
    {% else %}
	<p class="error hide"></p>
    {% endif %}

    <label class="checkbox-label">
        <input type="checkbox" name="remember_me" class="vam" />
        <span class="vam">{% blocktrans %}Remember me for {{remember_days}} days {% endblocktrans %}</span>
    </label>

    <input type="submit" value="{% trans "Log In" %}" class="submit" />
    {% if enable_signup %}
    <a href="{{ signup_url }}">{% trans "Signup" %}</a>
    {% endif %}

    {% if enable_shib_login %}
    <button id="shib-login" class="submit fright">{% trans "Shibboleth" %}</button>
    {% endif %}

    {% if enable_krb5_login %}
    <button id="krb5-login" class="submit fright">{% trans "Kerberos" %}</button>
    {% endif %}
</form>
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('[name="login"]').focus();

function setCaptchaInputWidth() {
    $('#id_captcha_1').css({'width': $('.input').outerWidth() - $('.captcha').width() - 10});
}
$(window).load(setCaptchaInputWidth);
$('.captcha').load(setCaptchaInputWidth);

$('#refresh-captcha').parent().css({'text-align':'right', 'margin':0});
$('#refresh-captcha').click(function() {
    $.ajax({
        url: '{% url 'captcha-refresh' %}',
        dataType:'json',
        cache:false,
        success: function(data) {
            $('.captcha').attr('src', data['image_url']);
            $('#id_captcha_0').val(data['key']);
        },
        error: function() {
            $('.error').removeClass('hide').html("{% trans "Failed to refresh the CAPTCHA, please try again later." %}");
        }
    });
    return false;
});
$('input[type="submit"]').click(function(){
    if (!$.trim($('input[name="login"]').val())) {
        $('.error').removeClass('hide').html("{% trans "Email or username cannot be blank" %}");
        return false;
    }
    if (!$.trim($('input[name="password"]').val())) {
        $('.error').removeClass('hide').html("{% trans "Password cannot be blank" %}");
        return false;
    }
});
// set tabindex
$(function() {
    $('input:not([type="hidden"])').each(function(index) {
        $(this).attr('tabindex', index + 1);
    });
});

{% if enable_shib_login %}
$(function() {
    $('#shib-login').click(function() {
        window.location = "{% url 'shib_login' %}{% if next %}?next={{ next|escape }}{% endif %}";
        return false;
    });
});
{% endif %}

{% if enable_krb5_login %}
$(function() {
    $('#krb5-login').click(function() {
        window.location = "{% url 'krb5_login' %}{% if next %}?next={{ next|escape }}{% endif %}";
        return false;
    });
});
{% endif %}
</script>
{% endblock %}
