/*-------------------------------文档信息-----------------------------------
///功能：安全设置——数据销毁密码
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strDivSafe = "divForSafe";      //安全设置设置销毁密码的层ID

//控制设置功能
function isControl() {
    $("#dataUnload", "#idDiv_psafe").click(function () {
        if ($("#dataUnload", "#idDiv_psafe").is(":checked")) {      //选中 
            $("#bindEmpwd", "#idDiv_psafe").html("<font color='green'>已设置</font>");
        }
        else {      //No checked 
            $("#bindEmpwd", "#idDiv_psafe").html("<font color='red'>未设置</font>");
        }
    });
}

//初始密码维护
function updateSafe() {
    creatDiv_ShowDialog(strDivSafe, "初始化数据", false, false, "", "400");
    var elements = createInputs_safe();
    $("#" + strDivSafe + "_divform").append(elements);
    var strForm = "";
    strForm = "<div class=\"clr\"></div>";
    strForm += "<div class=\"box_btn_width\">";
    strForm += "    <div class=\"box_btn\">";
    strForm += "        <div class=\"btn01\"><a onclick=\"updateEmpwd()\">保存退出</a></div>";
    strForm += "    </div>";
    strForm += "</div>";
    $("#" + strDivSafe + "_divform").append(strForm);
    //$("#" + strDivSafe).dialog("option", "position", "center");
    //$('#' + strDivSafe + "_content").css("margin-top", "-95px");
    $("#oldEmpwd", "#" + strDivSafe).focus();
    elements = null;
    strForm = null;
}

//生成表单，修改时，添加数据
function createInputs_safe() {
    var eleArray = [];
    eleArray.push('<ul id="logInfo">');
    eleArray.push('<li class="bott" style="margin:0;">登陆密码：<input type="password" maxlength="50" name="oldEmpwd" id="oldEmpwd" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('<li class="bott" style="margin:0;">设置初始密码：<input type="password" maxlength="50" name="neEmpwd" id="neEmpwd" onkeyup="changeLevelEmpwd.call(this)" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    //eleArray.push('<li class="gaga" style="text-align:left;padding-left:90px;margin:0;padding-top:0px;height: 56px; line-height: 26px;font-size:12px;"><span>密码长度6-50位，必须使用英文字母<br/>（区分大小写）、数字、特殊字符组成。<br/>密码强度为高才能保存。</span></li>');
    eleArray.push('<li class="gaga" style="text-align:left;padding-left:90px;margin:0;padding-top:10px;"><font color="gray">密码强度:<span id="level"></span></font><table class="Emmima" style="width:80px;height:10px; margin:0px 0 0 20px;"><tr><td></td><td></td><td></td></tr></table></li>');
    eleArray.push('<li>确认初始密码：<input type="password" maxlength="50" name="newaginEmpwd" id="newaginEmpwd" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('</ul>');
    return eleArray.join("");
}

//chang密码的强度
function changeLevelEmpwd() {
    var pwd = $(this).val();
    var level = getLevelEmpwd(pwd);
    var msg = ["低", "低", "中", "高"];
    if (level == -1 || level == 0) {//如果level大于0   , $("#divMimalog_divform")
        $("table.Emmima td").css({ "background-image": "url('../../Content/Default/images/mima_02.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
    } else {
        $("table.Emmima td:lt(" + (level) + ")").css({ "background-image": "url('../../Content/Default/images/mima_01.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
        $("table.Emmima td:gt(" + (level - 1) + ")").css({ "background-image": "url('../../Content/Default/images/mima_02.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
    }
    if (level == -1) {
        $("#level", $("#" + strDivSafe + "_divform")).text("");
    } else {
        $("#level", $("#" + strDivSafe + "_divform")).text(msg[level]);
    }
}

//获取密码的强度等级
function getLevelEmpwd(pwd) {
    var score = 0;
    if (pwd.length <= 5) {
        score = 0;
    } else {
        if (/[0-9]/.test(pwd)) {
            score++;
        }
        if (/[a-z]/.test(pwd)) {
            score++;
        }
        if (/[A-Z]/.test(pwd)) {
            score++;
        }
        if (/\W/.test(pwd)) {
            score++;
        }
    }
    if (!pwd) {
        score = -1;
    }
    if (score >= 3) {
        return 3;
    } else {
        return score;
    }
}

// 保存初始密码
function updateEmpwd() {
    var oldPwd = convertSpecial($("#oldEmpwd", $("#" + strDivSafe + "_divform")).val());
    var newPwd1 = convertSpecial($("#neEmpwd", $("#" + strDivSafe + "_divform")).val());
    var newPwd2 = convertSpecial($("#newaginEmpwd", $("#" + strDivSafe + "_divform")).val());
    if (oldPwd == null || oldPwd == "") {
        window.parent.cihoAlert(msg.ma.pleaseInpLogmin, null, function () { $("#oldEmpwd", $("#" + strDivSafe + "_divform")).focus(); });
        return;
    }
    else if (newPwd1 == null || newPwd1 == "") {
        window.parent.cihoAlert(msg.ma.inputInitCode, null, function () { $("#neEmpwd", $("#" + strDivSafe + "_divform")).focus(); });
        return;
    }
    else if (newPwd2 == null || newPwd2 == "") {
        window.parent.cihoAlert(msg.ma.maforOk, null, function () { $("#newaginEmpwd", $("#" + strDivSafe + "_divform")).focus(); });
        return;
    }
    if (newPwd1 == oldPwd) {    //初始密码与登录密码一致
        window.parent.cihoAlert(msg.ma.oldEqualNew, null, function () {
            clearSafePwd();
        });
        return;
    }
    if (newPwd1 != newPwd2) {   //初始密码与确认密码不一致
        window.parent.cihoAlert(msg.ma.checkMa, null, function () {
            clearSafePwd();
        });
        return;
    } else {
        //        if (getLevelEmpwd(newPwd1) < 3) {       //密码强度过低
        //            window.parent.cihoAlert(msg.ma.pwdStrong, null, function () {
        //                clearSafePwd();
        //            });
        //            return;
        //        }
        //        else {
        var strItem = "{\"option\":\"9\",\"LoginPasswd\":\"" + oldPwd + "\",\"InitDBPassWord\":\"" + newPwd2 + "\"}";
        var s = ParseData(window.external.DoOptions("TXtsz", "DoOthers", strItem));
        if (s.ReturnCode == "0") {
            window.parent.cihoAlert(s.MSG);
            $("#bindEmpwd", "#idDiv_psafe").html("<font color='green'>已设置</font>");
            $("#safeBtn", "#idDiv_psafe").val("重置密码");
            //$("#" + strDivSafe).dialog("close");
            cihoDlgClose(strDivSafe);
            oldPwd = null;
            newPwd1 = null;
            newPwd2 = null;
            strItem = null;
            s = {};
        }
        else {
            window.parent.cihoAlert(s.MSG);
        }
        //}
    }
}

//安全密码错误，清空，设置焦点
function clearSafePwd() {
    $("#neEmpwd", $("#" + strDivSafe + "_divform")).val("");
    $("#newaginEmpwd", $("#" + strDivSafe + "_divform")).val("");
    $("#level", $("#" + strDivSafe + "_divform")).text("");
    $("table.Emmima td").css({ "background-image": "url('../../Content/Default/images/mima_02.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
    $("#neEmpwd", $("#" + strDivSafe + "_divform")).focus();
}

//保存安全设置的数据
function saveSafeInit() {
    var strInitpwd = $("#dataUnload", "#idDiv_psafe").attr("checked");
    if (true == checkInitCode(strInitpwd)) {
        var objResult = ParseData(window.external.DoOptions("TXtsz", "DoOthers", "{'option':'10','IsUsedInitDBPassWord':'" + strInitpwd + "'}"));
        window.parent.cihoAlert(objResult.MSG);
    }
    strInitpwd = null;
}

//验证在check上checkbox时，是否设置了密码
function checkInitCode(strInitpwd) {
    if (strInitpwd == true) {
        var objRet = ParseData(window.external.DoOptions('TXtsz', 'GetList', '{}'));
        var data = objRet.Records;
        if (data != "{}" && data.length != 0) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].a2 == "InitDBPassWord") {
                    if (data[i].a3 == "") {
                        window.parent.cihoAlert(msg.ma.pleasePutinitCode);
                        return false;
                    }
                }
            }
        }
    }
    return true;
}