﻿Type.registerNamespace("PPM.Client");

PPM.Client.PasswordStrength = function(passwordBox){
    PPM.Client.PasswordStrength.initializeBase(this);
}

PPM.Client.PasswordStrength.prototype = {
    initialize: function() {
        var passwordBox = $get(this.passwordBoxID);
        var strengthBar = $get(this.strengthBarID);
        var updateStrengthBar = false;
        
        this.onFocus = onFocus;
        this.onBlur = onBlur;
        $addHandlers(passwordBox, {focus:this.onFocus, blur:this.onBlur});
        PPM.Client.PasswordStrength.callBaseMethod(this, 'initialize');
        
        function onFocus() {
            updateStrengthBar = true;
            pollPassword();
            displayCtrl(strengthBar, true);
        }
        function onBlur() {
            updateStrengthBar = false;
            displayCtrl(strengthBar, false);
        }
        function pollPassword() { 
            if (!updateStrengthBar) return;
            
            var strengthRatio = getStrengthRatio();
        
            strengthBar.style.width = getBarWidth() + 'px';
            strengthBar.style.backgroundColor = getBarColor();
            passwordValue = passwordBox.value;
            
            setTimeout(pollPassword, 100);
            
            function getStrengthRatio() {
                var doc = $(document);
                
                if (doc.find('.passStrength10').length > 0) return .10;
                if (doc.find('.passStrength20').length > 0) return .20;
                if (doc.find('.passStrength30').length > 0) return .30;
                if (doc.find('.passStrength40').length > 0) return .40;
                if (doc.find('.passStrength50').length > 0) return .50;
                if (doc.find('.passStrength60').length > 0) return .60;
                if (doc.find('.passStrength70').length > 0) return .70;
                if (doc.find('.passStrength80').length > 0) return .80;
                if (doc.find('.passStrength90').length > 0) return .90;
                if (doc.find('.passStrength100').length > 0) return 1.00;
                return 0;
            }
            function getBarWidth() {
                return strengthRatio <= .10 ? 5 :
                       strengthRatio <= .20 ? 10 :
                       strengthRatio <= .30 ? 15 :
                       strengthRatio <= .40 ? 20 :
                       strengthRatio <= .50 ? 25 :
                       strengthRatio <= .60 ? 30 :
                       strengthRatio <= .70 ? 35 :
                       strengthRatio <= .80 ? 40 :
                       strengthRatio <= .90 ? 45 : 50;
            }
            function getBarColor() {
                return strengthRatio <= .60 ? 'red' :
                       strengthRatio <= .70 ? 'yellow' :
                       strengthRatio <= .90 ? 'orange' : 'green';
            }
        }
    },
    
    dispose: function() {
        PPM.Client.PasswordStrength.callBaseMethod(this, 'dispose');
        $removeHandler($get(this.passwordBoxID), 'focus', this.onFocus);
        $removeHandler($get(this.passwordBoxID), 'blur', this.onBlur);
    },
    
    get_strengthBarID: function() { return this.strengthBarID; },
    set_strengthBarID: function(id) { this.strengthBarID = id; },
    
    get_passwordBoxID: function() { return this.passwordBoxID; },
    set_passwordBoxID: function(id) { this.passwordBoxID = id; }
};

PPM.Client.PasswordStrength.registerClass('PPM.Client.PasswordStrength', Sys.Component);

if (typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();