/**
 * @name		jQuery Password Strength Plugin
 * @version		1.0
 */

(function($) {
	$.fn.passwordStrength = function(property) {
		var options = $.extend({
			messageBlockSelector: null,
			strengthSettings: {
				week: {cssClass: 'week', html: 'Слабый уровень надежности'},
				middle: {cssClass: 'middle', html: 'Средний уровень надежности'},
				good: {cssClass: 'good', html: 'Хороший уровень надежности'},
				strong: {cssClass: 'strong', html: 'Высокий уровень надежности'}
			}
		}, property);

		var messageBlock = $(options.messageBlockSelector);

		this.on('keyup', function() {
			var password = $(this).val().trim(),
				strength = false;

			if (/^[a-z0-9]{8,}$/.test(password) && /[0-9]/.test(password)) {
				strength = 'week';
			}
			if (/^[a-zA-Z0-9]{9,}$/.test(password) && /[0-9]/.test(password) && /[A-Z]/.test(password)) {
				strength = 'middle';
			}
			if (/^[a-zA-Z0-9]{10,}$/.test(password) && /[0-9]/.test(password) && /[A-Z]/.test(password) && password.match(/[A-Z]/g).length > 1) {
				strength = 'good';
			}
			if (/^[a-zA-Z0-9]{12,}$/.test(password) && /[0-9]/.test(password) && /[A-Z]/.test(password) && password.match(/[A-Z]/g).length > 3) {
				strength = 'strong';
			}

			$(options.strengthSettings).each(function(key, value) {
				if (key != strength) {
					messageBlock.removeClass(value.cssClass);
				}
			});

			if (strength) {
				messageBlock.addClass(options.strengthSettings[strength].cssClass).html(options.strengthSettings[strength].html);
			} else {
				messageBlock.html('');
			}
		});
	};
})(jQuery);
