﻿PasswordStrength = 0;

function testPassword(passwd)
{
    changepassword_resetwarnings();
    var intScore = 0;
    var strVerdict = "weak";
    var strLog = "";

    // PASSWORD LENGTH
    if (passwd.length < 6)                         // length 4 or less
    {
        intScore = 0;
        updatepmeter(passwd, intScore);
        return;
    }
    else if (passwd.length > 4 && passwd.length < 8) // length between 5 and 7
    {
        intScore = (intScore + 6);
    }
    else if (passwd.length > 7 && passwd.length < 16)// length between 8 and 15
    {
        intScore = (intScore + 12);
    }
    else if (passwd.length > 15)                    // length 16 or more
    {
        intScore = (intScore + 18);
    }


    // LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
    if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
    {
        intScore = (intScore + 1);
    }

    if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
    {
        intScore = (intScore + 5);
    }

    // NUMBERS
    if (passwd.match(/\d+/))                                 // [verified] at least one number
    {
        intScore = (intScore + 10);
    }

    if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
    {
        intScore = (intScore + 5);
    }


    // SPECIAL CHAR
    if (passwd.match(/.[!,@,#,$,£,%,^,&,*,?,_,~]/))            // [verified] at least one special character
    {
        intScore = (intScore + 10);
    }

    // [verified] at least two special characters
    if (passwd.match(/(.*[!,@,#,$,£,%,^,&,*,?,_,~].*[!,@,#,$,£,%,^,&,*,?,_,~])/))
    {
        intScore = (intScore + 5);
    }


    // COMBOS
    if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
    {
        intScore = (intScore + 2);
    }

    if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
    {
        intScore = (intScore + 2);
    }

    // [verified] letters, numbers, and special characters
    if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,£,%,^,&,*,?,_,~])|([!,@,#,$,£,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
    {
        intScore = (intScore + 2);
    }

    updatepmeter(passwd, intScore);
}

function updatepmeter(passwd, score)
{
    var bcolor;
    var bwidth;
    var btext;

    if (passwd.length > 5 && score < 16)
        score = 16;

    if (score < 16)
    {
        bcolor = '#676767';
        bwidth = '40';
        btext = 'Too short';
    }

    else if (score > 15 && score < 25)
    {
        bcolor = '#ffcc33';
        bwidth = '80';
        btext = 'Fair';

    }
    else if (score > 24 && score < 35)
    {
        bcolor = '#6699cc';
        bwidth = '120';
        btext = 'Good';

    }
    else
    {
        bcolor = '#008000';
        bwidth = '160';
        btext = 'Strong';
    }

    $('#pmeterstring').html(btext);
    $('#pmeterstring').css({ color: bcolor });
    $('#pmeterbar').css({ width: bwidth + 'px' });
    $('#pmeterbar').css({ backgroundColor: bcolor });

    PasswordStrength = score;

    toggle_change_password();
}

function toggle_change_password()
{
    if (PasswordStrength < 16)
    {
        $('#btn_changepassword').removeClass("button").addClass("buttondisabled");
    }
    else
    {
        $('#btn_changepassword').removeClass("buttondisabled").addClass("button");
    }
}