var strongPassword = 0;

var passwordPolicy = {
    Length: 1,
    Letters: 2,
    Caps: 4,
    Digits: 8,
    Special: 16
}

var ethalonWeight = passwordPolicy.Special | passwordPolicy.Digits | passwordPolicy.Caps | passwordPolicy.Letters | passwordPolicy.Length;

strongPassword = $('#strongPassword').val();

$(function () {
    $("form").validate();

    $.validator.addMethod("validatePasswordStrength", function (value, element) {
        var result = validatePasswordStrength($("#password").val());

        return result;
    });

    if (strongPassword == 1) {
        $("#password").rules("add", {
            validatePasswordStrength: true,
            messages: {
                validatePasswordStrength: UserResources.PasswordTooWeak
            }
        });

        $("#password").keyup(function () {
            var weight = getPasswordWeight($("#password").val());

            if (weight & passwordPolicy.Special) {
                $('#special').removeClass('invalid').addClass('valid');
            } else {
                $('#special').removeClass('valid').addClass('invalid');
            }

            if (weight & passwordPolicy.Digits) {
                $('#digits').removeClass('invalid').addClass('valid');
            } else {
                $('#digits').removeClass('valid').addClass('invalid');
            }

            if (weight & passwordPolicy.Caps) {
                $('#caps').removeClass('invalid').addClass('valid');
            } else {
                $('#caps').removeClass('valid').addClass('invalid');
            }

            if (weight & passwordPolicy.Letters) {
                $('#letters').removeClass('invalid').addClass('valid');
            } else {
                $('#letters').removeClass('valid').addClass('invalid');
            }

            if (weight & passwordPolicy.Length) {
                $('#length').removeClass('invalid').addClass('valid');
            } else {
                $('#length').removeClass('valid').addClass('invalid');
            }
        }).focus(function () {
            $('#pswd_info').show();
        }).blur(function () {
            $('#pswd_info').hide();
        })
    }
});

function bindPwEvents() {
    $("#pass").keyup(function () {
        var weight = getPasswordWeight($("#pass").val());

        if (weight & passwordPolicy.Special) {
            $('#pwspecial').removeClass('invalid').addClass('valid');
        } else {
            $('#pwspecial').removeClass('valid').addClass('invalid');
        }

        if (weight & passwordPolicy.Digits) {
            $('#pwdigits').removeClass('invalid').addClass('valid');
        } else {
            $('#pwdigits').removeClass('valid').addClass('invalid');
        }

        if (weight & passwordPolicy.Caps) {
            $('#pwcaps').removeClass('invalid').addClass('valid');
        } else {
            $('#pwcaps').removeClass('valid').addClass('invalid');
        }

        if (weight & passwordPolicy.Letters) {
            $('#pwletters').removeClass('invalid').addClass('valid');
        } else {
            $('#pwletters').removeClass('valid').addClass('invalid');
        }

        if (weight & passwordPolicy.Length) {
            $('#pwlength').removeClass('invalid').addClass('valid');
        } else {
            $('#pwlength').removeClass('valid').addClass('invalid');
        }
    });
}

function getPasswordWeight(password) {
    var weight = 0;

    $.ajax({
        type: 'POST',
        url: base + "login.ivp/getpasswordweightajax",
        data: { password: password },
        async: false,
        dataType: "json",
        success: function (result) {
            weight = ((result.hasOwnProperty('IsOK') && result.IsOK)) ? Number(result.Result) : 0;
        }
    });

    return weight;
}

function validatePasswordStrength(password) {
    var isValid = 0;

    $.ajax({
        type: 'POST',
        url: base + "login.ivp/validatepasswordstrengthajax",
        data: { password: password },
        async: false,
        dataType: "json",
        success: function (result) {
            isValid = ((result.hasOwnProperty('IsOK') && result.IsOK)) ? result.Result == "TRUE" : false;
        }
    });

    return isValid;
}