{% extends "accounts.html" %}
{% load i18n %}
{% block title %}{% trans "Password Modification" %}{% endblock %}

{% block extra_style %}
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/bootstrap.popover.min.css" />
{% endblock %}

{% block main_panel %}
<div class="new-narrow-panel">
  <h2 class="hd">{% trans "Password Modification" %}</h2>
  <form action="" method="post" class="con">{% csrf_token %}
    <label for="id_old_password">{% trans "Current Password" %}</label>
    {{ form.old_password }}     {{ form.old_password.errors }}
    <label for="id_new_password1">{% trans "New Password" %}</label>
    {{ form.new_password1 }}    {{ form.new_password1.errors }}
    <div id="pwd_strength"></div>
    <label for="id_new_password2">{% trans "Confirm Password" %}</label>
    {{ form.new_password2 }}    {{ form.new_password2.errors }}
    <p class="error hide"></p>
    <input type="submit" value="{% trans "Submit" %}" class="submit" />
  </form>
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript" src="{{MEDIA_URL}}js/bootstrap.min.js"></script>
<script type="text/javascript">
$('[type="password"]').addClass('input');

{% if strong_pwd_required %}
{% include "snippets/password_strength_js.html" %}
var passwd_tip = "{% blocktrans %}Passwords must have at least {{min_len}} characters and contain {{level}} of the following: uppercase letters, lowercase letters, numbers, and symbols.{% endblocktrans%}";
$("#id_new_password1")
.popover({ // bootstrap plugin 'popover'
    container: 'body',
    content: passwd_tip,
    template: '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>',
    trigger: 'focus'
})
.keyup(function() {
    var pwd = $(this).val();
    if ($.trim(pwd)) {
        var level = getStrengthLevel(pwd);
        showStrength(level);
    } else {
        $("#pwd_strength").html("");
    }
});
{% endif %}

$('form').submit(function(){
    var old_pwd = $.trim($('input[name="old_password"]').val()),
        pwd1 = $.trim($('input[name="new_password1"]').val()),
        pwd2 = $.trim($('input[name="new_password2"]').val()),
        level = getStrengthLevel(pwd1);

        if (!old_pwd) {
            $('.error').html("{% trans "Current password cannot be blank" %}").removeClass('hide');
            return false;
        }
        if (!pwd1) {
            $('.error').html("{% trans "Password cannot be blank" %}").removeClass('hide');
            return false;
        }
        if (!pwd2) {
            $('.error').html("{% trans "Please enter the password again" %}").removeClass('hide');
            return false;
        }
        if (pwd1 != pwd2) {
            $('.error').html("{% trans "Passwords don't match" %}").removeClass('hide');
            return false;
        }
        {% if strong_pwd_required %}
            if (level < {{level}}) {
                $('.error').html(passwd_tip).removeClass('hide');
                return false;
            }
        {% endif %}
   });
</script>
{% endblock %}
