{% load i18n %}
function getStrengthLevel(pwd) {
    var num = 0;

    if (pwd.length < {{min_len}}) {
        return 0;
    } else {
        for (var i = 0; i < pwd.length; i++) {
            // return the unicode
            // bitwise OR
            num |= getCharMode(pwd.charCodeAt(i));
        };
        return calculateBitwise(num);
    };
}

function getCharMode(n) {
    if (n >= 48 && n <= 57) // nums
        return 1;
    if (n >= 65 && n <= 90) // uppers
        return 2;
    if (n >= 97 && n <= 122) // lowers
        return 4;
    else
        return 8;
}

function calculateBitwise(num) {
    var level = 0;
    for (var i = 0; i < 4; i++){
        // bitwise AND
        if (num&1) level++;
        // Right logical shift
        num>>>=1;
    }
    return level;
}

function showStrength(level) {
    var strength_ct = $("#pwd_strength");
    var strength = [
        "{% trans "too weak" %}",
        "{% trans "weak" %}",
        "{% trans "medium" %}",
        "{% trans "strong" %}"
    ];
    switch (level) {
        case 0:
            strength_ct.html(strength[0]).css("color", '#F00000');
            break;
        case 1:
            strength_ct.html('<span style="background:#db4747;">' + strength[1] + '</span><span>' + strength[2] + '</span><span>' + strength[3] + '</span>');
            break;
        case 2:
            strength_ct.html('<span>' + strength[1] + '</span><span style="background:#fdd64d;">' + strength[2] + '</span><span>' + strength[3] + '</span>');
            break;
        case 3:
        case 4:
            strength_ct.html('<span>' + strength[1] + '</span><span>' + strength[2] + '</span><span style="background:#4aa323;">' + strength[3] + '</span>');
            break;
    }
}
