/*-------------------------------文档信息-----------------------------------
///功能：修改密码
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var dlgId = "divMimalog";   //层ID

//打开修改密码窗口
function alterItemP0704() {
    createMimaDialog(dlgId, "385", "405", true, "修改密码", false, false);
    var elements = createInputs();
    $("#" + dlgId + "_divform").append(elements);
    //原密码获得焦点
    $('#oldPwd').focus();
}

//生成表单，修改时，添加数据
function createInputs() {
    var eleArray = [];
    eleArray.push('<ul id="logInfo">');
    eleArray.push('<li class="bott" style="margin:0;">原&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：<input type="password" maxlength="50" name="oldPwd" id="oldPwd" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('<li class="bott" style="margin:0;">新&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：<input type="password" maxlength="50" name="newPwd" id="newPwd" onkeyup="changeLevelPwd.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="mima" 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="newaginPwd" id="newaginPwd" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('</ul>');
    return eleArray.join("");
}

//创建窗口
function createMimaDialog(dlgId, iHeight, iWidth, bModal, title, bResizable, bcloseOnEscape) {
    createMimaDlg(dlgId, iHeight, iWidth, bModal, title, bResizable, bcloseOnEscape);
    //整个模板窗口的内容
    var strPop = "";
    strPop += "<div id=\"" + dlgId + "_pop_top\" class=\"pop\" style=\"width:380px;padding:0px;\">";
    strPop += "    <div class=\"pop_x\"><a id=\"idP0704Close\" href=\"#\" onclick=\"cihoDlgClose('" + dlgId + "','true');return false;\"></a></div>";
    strPop += "    <div class=\"pop_top\">";
    strPop += "        <h2>修改密码</h2>";
    strPop += "    </div>";

    strPop += "     <div class=\"pop_box\">";
    //START OF CONTENT
    strPop += "         <div class=\"pop_disp\">";
    strPop += "        <form id=\"" + dlgId + "_divform\">";
    strPop += "           </form>";
    strPop += "    </div>";
    strPop += "    <div class=\"clr\"></div> ";
    //END OF CONTENT

    strPop += "         <div class=\"clr\"></div>";
    strPop += "         <div class=\"box_btn_width\">";
    //START OF BUTTON
    strPop += "             <div class=\"box_btn\">";
    strPop += "            <div class=\"btn02\"><a href=\"#\" onclick=\"UpdatePwd()\">保存</a></div>";
    strPop += "            <div class=\"btn03\"><a href=\"#\" onclick=\"cihoDlgClose('" + dlgId + "','true');return false;\">退出</a></div>";
    strPop += "               </div>";
    //END OF BUTTON

    strPop += "            </div>";
    strPop += "        </div>";
    strPop += "    </div>";

    $("#" + dlgId).append(strPop);
    //增加窗口的拖动功能
    //$("#" + dlgId + "_pop_top").parent().parent().draggable({ "scroll": false });
    $("#" + dlgId).dialog("option", "width", iWidth);
}

////关闭窗口的方法
//function closeMimaDlg(dlgId) {
//    //$("#" + dlgId).dialog("close");
//    cihoDlgClose(dlgId);
//    return false;
//}


//创建dialog的方法
function createMimaDlg(dlgId, iHeight, iWidth, bModal, strTitle, bResizable, bcloseOnEscape) {
    //创建弹出窗口的遮罩层
    $("body").append("<div id='body_" + dlgId + "' class='menu_dlg_shield'></div>");
    $("#body_" + dlgId).css("display", "block");
    $("#body_" + dlgId).height(window.parent.document.documentElement.clientHeight);

    $("#" + dlgId).addClass("menu_dlg_bg");
    $("#" + dlgId).height(window.parent.document.documentElement.clientHeight);
    $("#" + dlgId).show()
    $('#' + dlgId).append('<div id="' + dlgId + '_content" class="menu_dlg_frm"></div>');
    $('#' + dlgId + "_content").css("margin-top", "90px");
}

////创建dialog的方法
//function createMimaDlg(dlgId, iHeight, iWidth, bModal, strTitle, bResizable, bcloseOnEscape) {
//    $("#" + dlgId).dialog({
//        draggable: false,
//        autoOpen: true,
//        height: iHeight,
//        width: iWidth,
//        modal: bModal,
//        title: strTitle,
//        zIndex: 10000,
//        dialogClass: "panel window",
//        resizable: bResizable,
//        //resizable: true,
//        closeOnEscape: bcloseOnEscape,
//        close: function () {
//            //window.parent.closeShieldDiv(dlgId);
//        }
//    });

//    //清空层中的内容
//    $("#" + dlgId).empty();

//    //清除dialog原有的标题栏样式
//    $(".ui-dialog-titlebar").remove();
//}

//检查密码强度的方法
function changeLevelPwd() {
    var pwd = $(this).val();
    var level = getLevelPwd(pwd);
    var msg = ["低", "低", "中", "高"];
    if (level == -1 || level == 0) {//如果level大于0   , $("#divMimalog_divform")
        $("table.mima td").css({ "background-image": "url('../Content/Default/images/mima_02.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
    } else {
        $("table.mima td:lt(" + (level) + ")").css({ "background-image": "url('../Content/Default/images/mima_01.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
        $("table.mima 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", $("#divMimalog_divform")).text("");
    } else {
        $("#level", $("#divMimalog_divform")).text(msg[level]);
    }
}

//获取密码的强度等级
function getLevelPwd(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 UpdatePwd() {
    var oldPwd = convertSpecial($("#oldPwd", $("#divMimalog_divform")).val());
    var newPwd1 = convertSpecial($("#newPwd", $("#divMimalog_divform")).val());
    var newPwd2 = convertSpecial($("#newaginPwd", $("#divMimalog_divform")).val());
    //    var oldPwd = $("#oldPwd", $("#divMimalog_divform")).val();
    //    var newPwd1 = $("#newPwd", $("#divMimalog_divform")).val();
    //    var newPwd2 = $("#newaginPwd", $("#divMimalog_divform")).val();
    if (oldPwd == null || oldPwd == "") {
        window.parent.cihoAlert(msg.ma.oldMa, null, function () { $("#oldPwd", $("#divMimalog_divform")).select(); });
        return;
    }
    else if (newPwd1 == null || newPwd1 == "") {
        window.parent.cihoAlert(msg.ma.newMa, null, function () { $("#newPwd", $("#divMimalog_divform")).select(); });
        return;
    }
    else if (newPwd2 == null || newPwd2 == "") {
        window.parent.cihoAlert(msg.ma.maforOk, null, function () { $("#newaginPwd", $("#divMimalog_divform")).select(); });
        return;
    }
    if (oldPwd == newPwd1) {
        window.parent.cihoAlert(msg.ma.newPewNOtEmoOld, null, function () {
            clearPwd();
        });
        return;
    }
    if (newPwd1 != newPwd2) {
        window.parent.cihoAlert(msg.ma.checkMa, null, function () {
            clearPwd();
        });
        return;
    } else {
        //        if (getLevelPwd(newPwd1) < 3) {
        //            window.parent.cihoAlert(msg.ma.pwdStrong, null, function () {
        //                clearPwd();
        //            });
        //            return;
        //        }
        //        else {
        var strItem = "{\"oldPwd\":\"" + oldPwd + "\",\"newPwd1\":\"" + newPwd1 + "\",\"newPwd2\":\"" + newPwd2 + "\"}";
        var s = ParseData(window.external.DoOptions("TXgmm", "Edit", strItem));
        if (s.MSG == "新密码不能与数据初始化密码相同！") {
            window.parent.cihoAlert(s.MSG, null, function () {
                clearPwd();
            });
            return;
        } else if (s.MSG == "旧密码错误！") {
            window.parent.cihoAlert(s.MSG, null, function () {
                $("#oldPwd", $("#divMimalog_divform")).val("");
                $("#oldPwd", $("#divMimalog_divform")).select();
            });
            return;
        }
        else {
            window.parent.cihoAlert(s.MSG);
            //$("#" + dlgId).dialog("close");
            cihoDlgClose(dlgId, true);
        }
        //}
    }
}

//密码错误，清空密码，设置焦点
function clearPwd() {
    $("#newPwd", $("#divMimalog_divform")).val("");
    $("#newaginPwd", $("#divMimalog_divform")).val("");
    $("#level", $("#divMimalog_divform")).text("");
    $("table.mima td").css({ "background-image": "url('../Content/Default/images/mima_02.gif')", "background-repeat": "no-repeat", "background-position": "bottom" });
    $("#newPwd", $("#divMimalog_divform")).focus();
}

//soft升级
function softUp() {
    window.external.DoOptions('TXtsz', 'DoOthers', '{"option":"12"}');
}
