﻿function password_level(minlen, maxlen) {
    var self = this;
    this.minLen = parseInt(minlen);
    this.maxLen = parseInt(maxlen);
    this.getPasswordLevel = function(pwd) {
        //[1弱，2中，3强，4最佳]        
        var score = 0, d_score = 0, l_score = 0;
        /*不规范格式，强度为-1*/
        if (!pwd.chkPassword()) { return 0; }
        if (self.hasNum(pwd)) { score++; }
        if (self.hasChar(pwd)) { score++; }
        if (self.hasSpecCode(pwd)) { score++; }
        /*纯数字、字母、特殊符号，强度为  1弱  */
        if (score == 1) { return 1; }
        //长度超过10
        if (pwd.length >= (self.minLen + (self.maxLen - self.minLen) / 2)) { l_score = l_score + 1; }
        var temp = (self.minLen + (self.maxlen - self.minLen) / 2);
        //含连续重复字母/数字超过一半
        if (self.hasSame(pwd)) { d_score++; }
        //含连续递增字母/数字超过一半
        if (self.hasIncre(pwd)) { d_score++; }
        //含连续递减字母/数字超过一半
        if (self.hasDescend(pwd)) { d_score++; }
        //两种字符组合，含或连续重复，或连续递增，或连续递减字符达到一半长度

        if (score == 2 && d_score > 0 && l_score > 0) { return 2; } //两种字符组合，含或连续重复，或连续递增，或连续递减字符达到一半长度
        else if (score == 2 && d_score > 0 && l_score == 0) { return 1; }
        else if (score == 2 && l_score > 0) { return 3; }
        else if (score == 2 && l_score == 0) { return 2; }
        if (score == 3 && d_score == 0 && l_score > 0) { return 4; } //三种字符，无重复，递增，递减一半，且长度超出10
        else if (score == 3 && d_score > 0 && l_score > 0) { return 3; } //三种字符，有重复，递增，递减一半，且长度超出10
        else if (score == 3 && d_score == 0 && l_score == 0) { return 3; } //三种字符，无重复，递增，递减一半，且长度小于10
        else return 2; //三种字符，有重复，递增，递减一半，且长度小于10
    }
    this.hasNum = function(pwd) { return pwd.match(/[0-9]+/); }
    this.hasChar = function(pwd) { return pwd.match(/[a-zA-Z]+/); }
    this.hasSpecCode = function(pwd) { return pwd.match(/[^a-zA-Z0-9]+/); }
    this.hasSame = function(pwd) {
        var length = 0;
        var tmpLen = 0;
        var i = 0;
        for (i = 1; i < pwd.length; i++) {
            var f1 = self.asc(pwd.substr(i, 1));
            var f2 = self.asc(pwd.substr(i - 1, 1));
            if (f1 == f2) { tmpLen++; } else { tmpLen = 1; }
            if (tmpLen > length) { length = tmpLen; }
        }
        if (length >= (pwd.length / 2)) { return true; } else { return false; }
    }
    
    this.hasDescend = function(pwd) {
        var length = 0;
        var tmpLen = 0;
        var i = 0;
        for (i = 1; i < pwd.length; i++) {
            var f1 = self.asc(pwd.substr(i, 1));
            var f2 = self.asc(pwd.substr(i - 1, 1));
            if (f1 == (f2 - 1)) { tmpLen++; } else { tmpLen = 1; }
            if (tmpLen > length) { length = tmpLen; }
        }
        if (length >= (pwd.length / 2)) { return true; } else { return false; }
    }
    this.hasIncre = function(pwd) {
        var length = 0;
        var tmpLen = 0;
        var i = 0;
        for (i = 1; i < pwd.length; i++) {
            var f1 = self.asc(pwd.substr(i, 1));
            var f2 = self.asc(pwd.substr(i - 1, 1));
            if (f1 == (f2 + 1)) { tmpLen++; } else { tmpLen = 1; }
            if (tmpLen > length) { length = tmpLen; }
        }
        if (length >= (pwd.length / 2)) { return true; } else { return false; }
    }
    
    this.asc = function(chr) { return chr.charCodeAt(0); }
}

function password_level_Layer(ul_id, txt_id) {
    var self = this;
    this.ul = "#" + ul_id;
    this.password = "#" + txt_id;
    this.level_calculator = new password_level(6, 16);
    this.set_btn_event = function(page_obj) {
        $(self.password).bind("keyup", function() { return page_obj.show_level(false); });
        $(self.password).bind("blur", function() { return page_obj.show_level(true); });
    }
    this.show_level = function(showMessage) {
        var level = self.level_calculator.getPasswordLevel($(self.password).val());
        self.show_layer(level);
        //if (showMessage) { page.showValidateMessage(self.password, level != 0, level != 0?" ":"密码长度为6-16位！"); }
    }
    this.show_layer = function(no) {        
        var alis = $(self.ul).children("li");
        for (i = 0; i < alis.length; i++) { $(alis[i]).removeClass(); }
        if (no != 0) { $(alis[no - 1]).addClass("li_" + no); }
    }
    this.show_layer(0);
}