/*-------------------------------文档信息-----------------------------------
///功能：数据维护
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strDivEmail = "divEmail";       //绑定邮箱div
var strDivEmpty = "divEmptymm";       //修改初始密码div
var objYzsz = 0;                //初始化数据是输入密码的次数

//硬盘备份
function StartSave() {
    var objResult = ParseData(window.external.DoOptions('TXtsz', 'DoOthers', '{"option":"3","filename":""}'));
    if (objResult.ReturnCode == "0") {
        window.parent.cihoAlert(objResult.MSG);
        objResult = {};
    }
}

//邮箱备份
function startBindEmData() {
    var objRet = ParseData(window.external.DoOptions('TXtsz', 'GetList', '{}'));
    var strData = objRet.Records;
    if (strData != "{}" && strData.length != 0) {
        for (var intI = 0; intI < strData.length; intI++) {
            if (strData[intI].a2 == "IsBindMail") {
                if (strData[intI].a3 != "" && strData[intI].a3 == "true") {
                    $("#btnToEm", "#idDiv_p0703").attr("disabled", true);
                    var objResult = ParseData(window.external.DoOptions('TXtsz', 'DoOthers', '{"option":"6"}'));
                    if (objResult.ReturnCode == "0") {
                        window.parent.cihoAlert(objResult.MSG);
                        objRet = {};
                        strData = {};
                        objResult = {};
                    }
                    else {
                        window.parent.cihoAlert(objResult.MSG);
                        $("#btnToEm", "#idDiv_p0703").attr("disabled", false);
                    }
                }
                else {
                    window.parent.cihoAlert(msg.other.emailNotBind);
                    return;
                }
            }
        }
    }
}

//开始恢复数据
function startCopy() {
    var fileName = trim($("#exportPath", "#idDiv_p0703").val());
    fileName = fileName.replace(/\\/g, "\\\\");
    if (fileName) {
        var objResult = ParseData(window.external.DoOptions('TXtsz', 'DoOthers', '{"option":"4","filename":"' + fileName + '"}'));
        if (objResult.ReturnCode == "0") {
            window.parent.cihoAlert(objResult.MSG + "<br/>" + msg.other.pleaseActivate);
            fileName = null;
            objResult = {};
        }
        else {
            window.parent.cihoAlert(objResult.MSG);
        }
    }
    else {
        window.parent.cihoAlert(msg.other.InputLoadUrl);
    }
}

/*-----------------------------------初始化-------------------------------------
//strBhjb:只输入登录密码，传1，这时只删除单据数据。
//          输入密码，checked删除档案信息，传2，这时删除单据数据和基本信息
//          三个控件都录入和选择上，传3，这时恢复到出厂时的状态            
-------------------------------------------------------------------------------*/
function startLoad() {
    var strBhjb = 1;
    var strPwd = convertSpecial($("#postPwd", $("#" + strDivEmpty)).val());
    if ($("#initOld", $("#" + strDivEmpty)).attr("checked")) {  //恢复出厂状态
        strBhjb = 3;
    }
    else if ($("#initOld", $("#" + strDivEmpty)).attr("checked") == false && $("#bhJbzl", $("#" + strDivEmpty)).attr("checked")) {  //删除档案信息
        strBhjb = 2;
    }
    else {      //删除单据数据
        strBhjb = 1;
    }
    if (strPwd) {
        window.parent.cihoAlert(msg.other.isIfStartData, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                if (objYzsz >= "3") {
                    window.parent.cihoAlert(msg.ma.codeIsError);
                    return;
                }
                var objRet = ParseData(window.external.DoOptions('TXtsz', 'DoOthers', '{"option":"5","param":"' + strBhjb + '","pwd":"' + strPwd + '"}'));
                if (objRet.ReturnCode == "0") {
                    window.parent.cihoAlert(objRet.MSG);
                    //$("#" + strDivEmpty).dialog("close");
                    cihoDlgClose(strDivEmpty);
                    strBhjb = 1;
                    strPwd = null;
                    objRet = {};
                }
                else {
                    window.parent.cihoAlert(objRet.MSG, null, function () {
                        $("#postPwd", $("#" + strDivEmpty)).val("");
                        $("#postPwd", $("#" + strDivEmpty)).select();
                    });
                    objYzsz++;
                }
            }
        });
    }
    else {
        window.parent.cihoAlert(msg.ma.pleaseInpLogmin, msgBox.dlgBtn.Ok, function (result) { $("#postPwd", $("#" + strDivEmpty)).select() });
    }
}
/*------------------------------------------绑定邮箱--------------------------------*/
function BindEmail() {
    if ($("#bingEmailBtn", "#idDiv_p0703").val() == "取消绑定") {
        var objResult = ParseData(window.external.DoOptions('TXtsz', 'DoOthers', '{"option":"8"}'));
        if (objResult.ReturnCode == "0") {
            window.parent.cihoAlert(msg.other.emailIsNone);
            $("#bingEmailBtn", "#idDiv_p0703").val("绑定");
            $("#BindLable", "#idDiv_p0703").html("<font color='red'>未绑定</font>");
            $("#btnToEm", "#idDiv_p0703").attr("disabled", false);
        }
        return;

    } else if ($("#bingEmailBtn", "#idDiv_p0703").val() == "绑定") {
        creatDiv_ShowDialog(strDivEmail, "绑定邮箱", false, false, "", "400");
        var elements = createInputs_P0703();
        $("#" + strDivEmail + "_divform").append(elements);
        var strForm = "";
        strForm = "<div class=\"clr\"></div>";
        strForm += "<div class=\"box_btn_width\">";
        strForm += "    <div class=\"box_btn\">";
        strForm += "        <div class=\"btn02\"><a onclick=\"saveMyEmail('" + strDivEmail + "')\">确定</a></div>";
        strForm += "        <div class=\"btn03\"><a onclick=\"closeMyDlg('" + strDivEmail + "',0,'')\">取消</a></div>";
        strForm += "    </div>";
        strForm += "</div>";
        $("#" + strDivEmail + "_divform").append(strForm);
        $("#cmbComData", "#" + strDivEmail).combobox();
        $("#cmbComData", "#" + strDivEmail).combobox({
           width: _iWidth190,
            listWidth: _iWidth190,
            textField: "mc",
            valueField: "dm",
            editable: false
        });
        var strEmailData = [{ "dm": "@163.com", "mc": "@163.com" },
            { "dm": "@126.com", "mc": "@126.com" }, { "dm": "@qq.com", "mc": "@qq.com" },
            { "dm": "@sohu.com", "mc": "@sohu.com" }, { "dm": "@21cn.com", "mc": "@21cn.com"}];
        $("#cmbComData", "#" + strDivEmail).combobox("loadData", strEmailData);
        $(".layoutpanel").css("z-index", "50000009");
        setTimeout(function () { $("#zhname", "#" + strDivEmail).focus(); }, 100);
        $("#" + strDivEmail).dialog("option", "position", "center");
        strEmailData = null;
        elements = null;
        strForm = null;
    }
}

//生成表单，修改时，添加数据
function createInputs_P0703() {
    var eleArray = [];
    eleArray.push('<ul>');
    eleArray.push('<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱：<select class="easyui-combobox" name="cmbComData" id="cmbComData"></select></li>');
    eleArray.push('<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;帐&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;户：<input type="text" maxlength="50" name="zhname" id="zhname"  onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：<input type="password" maxlength="50" name="newPwd" id="newPwd" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('</ul>');    
    return eleArray.join("");
}

//保存邮箱的方法
function saveMyEmail(dlgId) {
    if ($("#cmbComData", "#" + dlgId).combobox("getText") == "") {
        window.parent.cihoAlert(msg.other.selEmialafter, null, function () { $("#cmbComData", "#" + dlgId).combobox('focus'); });
        return;
    }
    if ($("#zhname", "#" + dlgId).val() == "") {
        window.parent.cihoAlert(msg.other.intputEmailName, null, function () { $("#zhname", "#" + dlgId).focus(); });
        return;
    }
    if ($("#newPwd", "#" + dlgId).val() == "") {
        window.parent.cihoAlert(msg.other.intputEmailCode, null, function () { $("#newPwd", "#" + dlgId).focus(); });
        return;
    }
    var strEmailName = trim($("#zhname", "#" + dlgId).val() + $("#cmbComData", "#" + dlgId).combobox("getValue"));
    var strPassword = convertSpecial($("#newPwd", "#" + dlgId).val());
    var strItem = "{\"option\":\"7\",\"MailUser\":\"" + trim($("#zhname", "#" + dlgId).val()) + "\",\"MailPassWord\":\"" + strPassword + "\",\"MailAddress\":\"" + strEmailName + "\"}";
    var objResult = ParseData(window.external.DoOptions("TXtsz", "DoOthers", strItem));
    if (objResult.ReturnCode == "0") {
        //window.parent.cihoAlert(msg.other.emailSucess);
        $("#BindLable", "#idDiv_p0703").html("<font color='green'>已绑定</font>");
        $("#bingEmailBtn", "#idDiv_p0703").val("取消绑定");
        //closeMyDlg(dlgId, "0", "");
        cihoDlgClose(dlgId);
        strEmailName = null;
        strPassword = null;
        strItem = null;
        objResult = {};
    }
    else {
        window.parent.cihoAlert(objResult.MSG);
        return;
    }
}

/*-------------------------------------绑定邮箱结束----------------------------------------*/

/*------------------------------初始化密码----------------------------------------------*/
//初始密码
function updateEmpty() {
    creatDiv_ShowDialog(strDivEmpty, "系统初始化", false, false, "TXtxz", "400");
    var elemengts = createInputs_empty();
    $("#" + strDivEmpty + "_divform").append(elemengts);
    var strForm = "";
    strForm = "<div class=\"clr\"></div>";
    strForm += "<div class=\"box_btn_width\">";
    strForm += "    <div class=\"box_btn\">";
    strForm += "        <div class=\"btn01\"><a onclick=\"startLoad()\">确定</a></div>";
    strForm += "    </div>";
    strForm += "</div>";
    $("#" + strDivEmpty + "_divform").append(strForm);
    $("#" + strDivEmpty).bind('keypress', function (e) {
        if (13 == e.keyCode || 8 == e.keyCode) {
            //停止响应回车事件。
            return false;
        }
    });
    $("#initOld", $("#" + strDivEmpty + "_divform")).click(function () {
        if ($("#initOld", $("#" + strDivEmpty + "_divform")).is(":checked")) {      //checked恢复出厂状态，也checked删除档案，并且不可编辑
            if ($("#bhJbzl", $("#" + strDivEmpty + "_divform")).attr("checked")==false) {
                $("#bhJbzl", $("#" + strDivEmpty + "_divform")).attr('checked', true);
            }
            $("#bhJbzl",$("#" + strDivEmpty + "_divform")).attr('disabled',true);
        }
        else {     //取消checked恢复出厂状态，也取消checked删除档案，并且可编辑
            $("#bhJbzl",$("#" + strDivEmpty + "_divform")).attr('disabled',false);
            $("#bhJbzl",$("#" + strDivEmpty + "_divform")).attr('checked',false);
        }
    });
    $("#" + strDivEmpty).dialog("option", "position", "center");
    $("#postPwd", "#" + strDivEmpty).focus();
    elemengts = null;
    strForm = null;
}

//生成表单，修改时，添加数据
function createInputs_empty() {
    var eleArray = [];
    eleArray.push('<ul>');
    eleArray.push('<li><font color="red">*</font>登陆密码：<input id="postPwd" name="postPwd" type="password"  maxlength="50" onkeypress="return checkValidate(event)" onpaste="return validatePaste()"/></li>');
    eleArray.push('<li class="unline"><input type="checkbox"  value=""  name="bhJbzl" id="bhJbzl" style="float:left;width:18px; border:0;margin:0 0 0 25px;"/><label for="bhJbzl" style="float:left; text-align:left; margin-right:5px;">删除档案信息</label></li>');
    eleArray.push('<li class="unline"><input type="checkbox"  value=""  name="initOld" id="initOld" style="float:left;width:18px; border:0;margin:0 0 0 25px;"/><label for="initOld" style="float:left; text-align:left; margin-right:5px;">恢复出厂状态</label></li>');
    eleArray.push('</ul>');
    return eleArray.join("");
}
