$(document)
  .ready(
    function(){
      var self = $('script[src$=input\.password\.js]');
      $('head').append($('<link href="' + self.attr('src').replace(/[^\/]+\/input\.password\.js$/, 'style/password.css') + '" rel="stylesheet" type="text/css" />'));
      var shortPass = '<span style="color:#f00;">太短</span>'
      var longPass = '<span style="color:#f00;">太长</span>'
      var badPass = '<span style="color:#900;">太差</span>'
      var normalPass = '<span style="color:#8F8700">一般</span>'
      var goodPass = '<span style="color:#00A45D;">很好</span>'
      var strongPass = '<span style="color:#00c60d;">极佳</span>'

      function checkRepetition(pLen, str){
        res = ""
        for ( i = 0; i < str.length; i++ ) {
          repeated = true
          for ( j = 0; j < pLen && (j + i + pLen) < str.length; j++ )
            repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + pLen))
          if (j < pLen)
            repeated = false
          if (repeated) {
            i += pLen - 1
            repeated = false
          }
          else {
            res += str.charAt(i)
          }
        }
        return res
      }
      $("input[type=password][vtype=strength]")
        .each(
          function(){
            var score = 0;
            var selfui = this;
            var ui =
              $('<div class="PasswordStrengthResult"><div class="info"><div class="title">密码强度：</div><div class="result"></div></div><div class="umask"><div class="mask"><img src="/shared/images/s.gif" /></div></div></div>');
            $(this).addClass('PasswordStrengthInput').after(ui).keyup( function(){
              $('>*>.result', ui).html(passwordStrength($(this).val()));
              $('>.umask>.mask', ui).css('width', 100 - score + "%");
            }).parents('form:eq(0)').submit( function(){
              if (score < 35) {
                $(selfui).addClass("PasswordNotStrongEnough").one('focus', function(){
                  $(selfui).removeClass('PasswordNotStrongEnough');
                });
              }
              return score >= 35;
            });
            var passwordStrength = function(password){
              score = 0
              if (password.length < 6) {
                return shortPass;
              }
              if (password.length > 79) {
                return longPass;
              }
              score += password.length * 4
              score += (checkRepetition(1, password).length - password.length) * 1
              score += (checkRepetition(2, password).length - password.length) * 1
              score += (checkRepetition(3, password).length - password.length) * 1
              score += (checkRepetition(4, password).length - password.length) * 1
              if (password.match(/(.*[0-9].*[0-9].*[0-9])/))
                score += 5
              if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
                score += 5
              if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
                score += 5
              if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))
                score += 10
              if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))
                score += 10
              if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))
                score += 10
              if (password.match(/^\w+$/) || password.match(/^\d+$/))
                score -= 10
              if (score < 0)
                score = 0
              if (score > 100)
                score = 100
              switch (true) {
                case (score < 35):
                  return badPass;
                case (score < 65):
                  return normalPass;
                case (score < 85):
                  return goodPass;
                default:
                  return strongPass;
              }
            }
          });
    });