﻿jQuery.fn.passwordStrength = function(options) {
    return this.each(function() {
        var that = this; that.opts = {};
        that.opts = jQuery.extend({}, jQuery.fn.passwordStrength.defaults, options);

        that.div = jQuery(that.opts.targetDiv);
        that.defaultClass = that.div.attr('class');

        that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;

        v = jQuery(this)
		.keyup(function() {
		    if (typeof el == "undefined")
		        this.el = jQuery(this);
		    var s = getPasswordStrength(this.value);
		    var p = this.percents;
		    var t = Math.floor(s / p);

		    var pass_value = '';
		    if (s < 50) {
		        pass_value  = 'minimalny';
		    }
		    else if (s < 70) {
		        pass_value  = 'normalny';
		    }
		    else {
		        pass_value  = 'wysoki';
		    }

		    jQuery('span[id="pass_value"]').text(pass_value);

		    if (100 <= s)
		        t = this.opts.classes.length - 1;

		    this.div
				.removeAttr('class')
				.addClass(this.defaultClass)
				.addClass(this.opts.classes[t]);

		})
    });

    function getPasswordStrength(H) {
        var D = (H.length);
        if (D > 5) {
            D = 5
        }
        var F = H.replace(/[0-9]/g, "");
        var G = (H.length - F.length);
        if (G > 3) { G = 3 }
        var A = H.replace(/\W/g, "");
        var C = (H.length - A.length);
        if (C > 3) { C = 3 }
        var B = H.replace(/[A-Z]/g, "");
        var I = (H.length - B.length);
        if (I > 3) { I = 3 }
        var E = ((D * 10) - 20) + (G * 10) + (C * 15) + (I * 10);
        if (E < 0) { E = 0 }
        if (E > 100) { E = 100 }
        return E
    }

    function randomPassword() {
        var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#jQuery_+";
        var size = 10;
        var i = 1;
        var ret = ""
        while (i <= size) {
            jQuerymax = chars.length - 1;
            jQuerynum = Math.floor(Math.random() * jQuerymax);
            jQuerytemp = chars.substr(jQuerynum, 1);
            ret += jQuerytemp;
            i++;
        }
        return ret;
    }

};

jQuery.fn.passwordStrength.defaults = {
    classes: Array('is10', 'is20', 'is30', 'is40', 'is50', 'is60', 'is70', 'is80', 'is90', 'is100'),
    targetDiv: '#passwordStrengthDiv',
    cache: {}
}
jQuery(document)
.ready(function() {
    jQuery('input[class="password"]').passwordStrength();
});

