var strTableP020101 = "Tp020101";
var strDivP020101 = "divP020101";
var cbcols = ["spdm", "a1", "a2"];
var ytcols = ["spdm", "a1", "a2", "xsydm"];
var cbdata, ytdata;
var cellX, cellY;                                                                //选中单元格的行索引和列索引

//打开销售窗口
//state：1   新增
//state：2   修改
function openSales(state) {
    try {

        //声明变量
        var tbId = strTableP020101, dlgId = strDivP020101;

        //窗口对象
        var $dlgId = $("#" + dlgId);

        creatDiv_ShowDlgHaveTbl(dlgId, "销售单", false, true, tbId, 900);
        createElements(tbId, dlgId);
        initGridSet(tbId, dlgId);
        initInputsProp(tbId, dlgId);

        var $tbId = $("#" + tbId);

        //设置销售单的状态标识：1，修改；2，新增
        $dlgId.data("state", state);
        var mylist = [];

        //状态标识为修改时，去后台请求数据
        if (state == 2) {
            $("#idMust", $dlgId).text("NO:" + objSelRowP0201.djbh);
            var record = ParseData(window.external.DoOptions(tbId, "GetList", '{"djbh":"' + objSelRowP0201.djbh + '"}'));
            if (record && record.ReturnCode == 0) {

                //设置单据日期
                $("#djdate", $dlgId).attr("disabled", true).val(record.djdate);

                //加载combobox数据
                loadComboData(dlgId, tbId, record.xslx);

                //为客户combobox赋值
                var $kh = $("#khmc", $dlgId);
                $kh.combobox("setValue", record.khdm);
                $kh.combobox("setText", record.khmc);
                if ($kh.combobox("getText") == record.khdm) { $kh.combobox("setText", record.khmc); }

                //为职员combobox赋值
                var $zy = $("#zymc", $dlgId);
                $zy.combobox("setValue", record.zydm);
                if ($zy.combobox("getText") == record.zydm) { $zy.combobox("setText", record.zymc); }

                //为仓库combobox赋值
                var $ck = $("#ckmc", $dlgId);
                $ck.combobox("setValue", record.ckdm);
                if ($ck.combobox("getText") == record.ckdm) { $ck.combobox("setText", record.ckmc); }

                //为销售类型combobox赋值
                var $xslx = $("#xslx", $dlgId);
                $xslx.combobox("setValue", record.xslx);
                $xslx.combobox('disable');

                //为备注、运输单位、运输单号、制单人赋值
                $("#bz", $dlgId).val(escape(record.bz));
                $("#ysdw", $dlgId).val(escape(record.ysdw));
                $("#ysdh", $dlgId).val(escape(record.ysdh));
                $("#czydm", $dlgId).attr("disabled", true).val(record.czymc);

                //将grid数据保存到加载数组里面
                if (record.Records instanceof Array) { mylist = record.Records; }

                //将修改数据保存到grid里面
                $dlgId.data("data", { "djbh": objSelRowP0201.djbh, "khdm": record.khdm, "khmc": record.khmc, "zydm": record.zydm, "zymc": record.zymc, "ckdm": record.ckdm, "ckmc": record.ckmc, "czydm": record.czydm, "cyzmc": record.czymc });

                $kh = $zy = $ck = $xslx = null;
            } else {
                window.parent.cihoAlert(record.MSG, msgBox.dlgBtn.Ok);
            }
        } else {

            //新增，为表单元素设置默认值
            var ckdm = loadComboData(dlgId, tbId, "201");

            $dlgId.data("data", {});
            $("#djdate", $dlgId).val(getNowDate());
            $("#xslx", $dlgId).combobox("setValue", "201");
            $("#czydm", $dlgId).val(window.parent.objLoginInfo.name);
            $("#ckmc", $dlgId).combobox("setValue", ckdm);
        }

        //为grid加载
        loadGridData(tbId, mylist);

        //销售类型为配送模式时grid气泡提示
        var xslx = $("#xslx", $dlgId).combobox("getValue");
        if (xslx == "202") {
            var ids = $tbId.getDataIDs();
            for (var i in ids) {
                $tbId.setCell(ids[i], "xsy", "", "", { title: "配送模式销售，不需要选择销售员。" });
            }
        }

        if (state == 2) {
            cbdata = getCompareData(tbId, cbcols);
            ytdata = getCompareData(tbId, ytcols);
        }

        $("#" + dlgId + "_divform").data("change", false);
        setTimeout('$("#ysdw","#"+strDivP020101).focus();', 100);
        $(".layoutpanel").css("z-index", "50000009");

        $dlgId = $tbId = null;
    } catch (e) { }
}


//计算grid合计信息
function sumGridTotal(tbId) {
    var ypsl = 0, ypje = 0, kpsl = 0, kpje = 0, sl = 0;
    var $tbId = $("#" + tbId);

    var ids = $tbId.getDataIDs();
    for (var i = 0; i < ids.length; i++) {
        var item = $tbId.getRowData(ids[i]);
        ypsl += isNaN(item.a1) ? 0 : item.a1 * 1;
        ypje += isNaN(item.je) ? 0 : item.je * 1;
        sl += isNaN(item.glsl) ? 0 : item.glsl * 1;
        if (item.a3 == "true") {
            kpsl += isNaN(item.a4) ? 0 : item.a4 * 1;
            kpje += isNaN(item.kpje) ? 0 : item.kpje * 1;
        }
    }
    $tbId.footerData("set", { rn: "合计", a1: ypsl, je: ypje, a4: kpsl, kpje: kpje, glsl: sl }, true);
    $tbId = null;
}

//改变对话框或数据行的状态
//如果只提供表格id，则只改变窗口的状态标志
function changeZt(tbId, rowId) {
    //修改销售单的状态
    if (tbId == strTableP020101) {
        $("#" + strDivP020101 + "_divform").data("change", true);
    } else {
        $("#" + strDivP020101 + "_divform").data("change", true);
    }

    //修改行的状态
    if (rowId) {
        var $tbId = $("#" + tbId);
        var item = $tbId.getRowData(rowId);
        if (item.mxguid) {
            $tbId.setCell(rowId, "flag", 2);                               //2代表更新
        } else {
            $tbId.setCell(rowId, "flag", 1);                               //1代表新增
        }
        $tbId = null;
    }
}
//创建表单元素
function createInputs(arr) {
    var strForm = "<ul>";
    for (i = 0; i < arr.length; i++) {
        var Item = arr[i];
        if (!Item.hidden) {
            if (Item.edittype == "text") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "' ";
                if (!Item.editable) { strForm += " disabled='disabled'"; }
                if (Item.index != "bz") {
                    strForm += " maxlength='50' ";
                }
                strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
                strForm += "</li>";
            } else if (Item.edittype == "select") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<select name='" + Item.index + "' id='" + Item.index + "'";
                strForm += " class='easyui-combobox' >";
                strForm += "</select>";
                strForm += "</li>";
            }
        }
    }
    strForm += "</ul>";
    return strForm;
}
//创建单据grid
function createDjGrid(tbId, cNames, cModel) {
    $("#" + tbId).jqGrid({
        datatype: "local",
        //height: "100%",
        autowidth: true,
        colNames: cNames,
        colModel: cModel,
        rownumbers: true,
        footerrow: true,
        sortable: false,
        rownumWidth: 30,
        cellEdit: true,
        cellsubmit: "clientArray"
    });
}
//创建窗口input元素和grid
function createElements(tbId, dlgId) {

    // 向后台请求配置信息
    var config = ParseData(window.external.DoOptions(tbId, "GetTableConfig", "{}"));

    // 根据头配置 生成表单元素 
    var header = createInputs(config.pageHeader);

    //根据脚信息   生成表单元素
    var footer = createInputs(config.pageFooter);

    //将表单元素添加到销售单
    $("#" + dlgId + "_divform").append(header + '<table id="' + tbId + '"></table>' + footer);
    config.colNames.push("操作");
    config.colModel.push({ "sortable": false, "edittype": "text", "editable": false, "index": "cz", "hidden": false, "width": 80, "editrules": { "required": false }, "name": "cz", "Formatter": {}, "align": "center" });

    //创建grid
    createDjGrid(tbId, config.colNames, config.colModel);

    //收集可见、可编辑列信息
    var vCols = [];
    for (var i in config.colModel) {
        if (config.colModel[i].editable && !config.colModel[i].hidden) {
            vCols.push(config.colModel[i].index);
        }
    }
    $("#" + tbId).data("vCols", vCols);

    var $dlgId = $("#" + dlgId);
    $("#" + tbId).setLabel("rn", "序号");
    $(".ui-jqgrid-htable th:visible:last", $dlgId).html('<img id="btnAdd"  style="cursor:pointer"  title="添加行" src="../../Content/Default/images/edit_add.gif" onclick="addNewRow(\'' + tbId + '\');return false; "/>');
    $dlgId = null;
}

//加载窗口grid数据，并计算合计
function loadGridData(tbId, mylist) {
    var count = mylist.length > 5 ? mylist.length : 5;
    var ypsl = 0, ypje = 0, kpsl = 0, kpje = 0, glsl = 0, $tbId = $("#" + tbId);
    for (var i = 0; i < count; i++) {
        if (mylist[i]) {                             //添加后台数据行
            ypsl += mylist[i].a1 * 1;
            ypje += mylist[i].je * 1;
            glsl += mylist[i].glsl * 1;
            if (mylist[i].a3 == "true") {           //计算带票合计
                kpsl += mylist[i].a4 * 1;
                kpje += mylist[i].kpje * 1;
            }
            $tbId.addRowData(i + 1, mylist[i]);      //添加后台数据行
        }
        else {                                     //添加空行
            $tbId.addRowData(i + 1, {});
        }
    }
    $tbId.footerData("set", { rn: "合计", a1: ypsl, je: ypje, a4: kpsl, kpje: kpje, glsl: glsl }, true);
    $tbId.data("nextNum", count + 1);
    resizeGridScroll(tbId);
    $tbId = null;
}
//添加空行
function addNewRow(tbId) {
    try {
        $tbId = $("#" + tbId);
        var nextNum = $tbId.data("nextNum") + 1;
        $tbId.addRowData(nextNum, {});
        $tbId.data("nextNum", nextNum);
        resizeGridScroll(tbId);
        $tbId = null;
    } catch (e) { }
}

//为操作按钮注册事件
function bindDelEvent(tbId) {
    $("#" + tbId).delegate("tr[id]:visible", "hover", function (e) {
        if (e.type == "mouseenter") {
            var btn = '<a style="color:red;cursor:pointer;text-decoration:none" onclick="delDjRow(\'' + tbId + '\',' + this.id + ');return false;">删除</a>';
            $("#" + tbId).setCell(this.id, "cz", btn);
        }
        if (e.type == "mouseleave") {
            $("#" + tbId).setCell(this.id, "cz", null);
        }
    });
}

//删除窗口中的grid数据
function delDjRow(tbId, rowId) {
    try {
        //删除前先保存处于编辑状态的单元格
        var $tbId = $("#" + tbId);
        $tbId.saveCell(cellX, cellY);

        var delItem = $tbId.getRowData(rowId);
        var vCols = $tbId.data("vCols");

        //如果改行有修改标示，并且不为空
        if (delItem.flag && !isRowEmpty(delItem, vCols)) {              //如果有操作标示并且行不为空，就提示是否要删除
            window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (strRet) {
                if (msgBox.dlgRst.Yes == strRet) {
                    //存在关联数量不允许删除
                    if (delItem.glsl * 1 > 0) {
                        window.parent.cihoAlert(msg.del.delForbid, msgBox.dlgBtn.Ok);
                    } else {
                        //不存在关联数量
                        var $grid = $("#" + tbId);
                        var row = $grid.getRowData(rowId);

                        //如果存在mxguid，则去后台判断是否可以删除
                        if (row.mxguid) {
                            var record = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"mxguid":"' + row.mxguid + '","flag":"2"}'));

                            //如果可以删除，则将该条数据保存到表格中
                            if (record == 0) {
                                var records = $grid.data("records");
                                records.push('{"mxguid":"' + row.mxguid + '","flag":"3"}');
                                $grid.data("records", records);

                                //改变窗口的状态，表示删除、修改过数据
                                changeZt(tbId);
                            } else {
                                window.parent.cihoAlert("请先删除收款单信息！", msgBox.dlgBtn.Ok); return;
                            }
                        }

                        //删除表格中的数据，计算合计行
                        $grid.delRowData(rowId);
                        sumGridTotal(tbId);
                        resizeGridScroll(tbId);
                    }
                }
            });
        } else {

            //该行为空行，有修改标示或者没有修改标示
            $tbId.delRowData(rowId);

            //如果存在mxguid则保持到records数组里
            if (delItem.mxguid) {
                var records = $tbId.data("records");
                records.push('{"mxguid":"' + delItem.mxguid + '","flag":"3"}');
                $tbId.data("records", records);

                //改变窗口的状态
                changeZt(tbId);
            }
            sumGridTotal(tbId);
            resizeGridScroll(tbId);
        }

    } catch (e) { }
}

//保存销售单
function saveMyDataTp020101(dlgId, afterSave) {
    try {
        var tbId = strTableP020101;
        var $tbId = $("#" + tbId);
        var $dlgId = $("#" + dlgId);


        //保存在表格中的数据
        var vCols = $tbId.data("vCols");
        var records = $tbId.data("records");

        //保存在窗口中的数据
        var state = $dlgId.data("state");
        var data = $dlgId.data("data");

        var rows = [];

        var count = 0;
        var ids = $tbId.getDataIDs();
        for (var i = 0; i < ids.length; i++) {
            var item = $tbId.getRowData(ids[i]);
            if (item.flag) {                                  //只有修改过或新增的数据保存到数据库    1：新增，2：修改，3：删除
                var rowEmpty = isRowEmpty(item, vCols);
                if (!rowEmpty) {
                    if (item.a3 == "false") {                 //如果不带票
                        item.a4 = item.a5 = item.a6 = item.kpdwdm = "";
                    }
                    record = '{"spdm":"' + item.spdm + '","a1":"' + item.a1 + '","a2":"' + item.a2 + '","a3":"' + item.a3 + '","a4":"' + item.a4 + '","a5":"' + item.a5 + '","xsydm":"' + item.xsydm + '","kpdwdm":"' + item.kpdwdm + '","a6":"' + item.a6 + '","flag":"' + item.flag + '","mxguid":"' + item.mxguid + '"}';
                    rows.push(record);
                    count++;
                } else if (item.mxguid) {
                    rows.push('{"mxguid":"' + item.mxguid + '","flag":"3"}');
                }
            }
        }

        if (count == 0) {
            window.parent.cihoAlert(msg.xs.xsdjNotEmp, msgBox.dlgBtn.Ok);
            $tbId = $dlgId = null;
            return false;
        }

        //组织表单元素

        var inputs = [];
        var $kh = $("#khmc", $dlgId);
        var $zy = $("#zymc", $dlgId);
        var $ck = $("#ckmc", $dlgId);

        //获取客户信息
        var khdm = $kh.combobox("getValue");
        var khmc = $kh.combobox("getText");
        if (khdm) {
            inputs.push('"khdm":"' + khdm + '"');
        } else if (khmc == data.khmc) {
            inputs.push('"khdm":"' + data.khdm + '"');
        }

        //获取职员信息
        var zydm = $zy.combobox("getValue");
        var zymc = $zy.combobox("getText");
        if (zydm) {
            inputs.push('"zydm":"' + zydm + '"');
        } else if (zymc == data.zymc) {
            inputs.push('"zydm":"' + data.zydm + '"');
        }

        //获取销售类型信息
        var xslx = $("#xslx", $dlgId).combobox("getValue");
        inputs.push('"xslx":"' + xslx + '"');

        //获取仓库信息
        var ckdm = $ck.combobox("getValue");
        var ckmc = $ck.combobox("getText");
        if (ckdm) {
            inputs.push('"ckdm":"' + ckdm + '"');
        } else if (ckmc == data.ckmc) {
            inputs.push('"ckdm":"' + data.ckdm + '"');
        }

        //获取单据时间、运输单位、运输单号、备注信息
        var djdate = $("#djdate", $dlgId).val();
        inputs.push('"djdate":"' + djdate + '"');
        inputs.push('"ysdw":"' + trim($("#ysdw", $dlgId).val()) + '"');
        inputs.push('"ysdh":"' + trim($("#ysdh", $dlgId).val()) + '"');
        inputs.push('"bz":"' + trim($("#bz", $dlgId).val()) + '"');            //备注

        //获取操作员和单据编号信息
        if (state == 1) {                                                      //新增
            inputs.push('"czydm":"' + window.parent.objLoginInfo.code + '"');
        } else if (state == 2) {
            inputs.push('"czydm":"' + data.czydm + '"');              //修改
            inputs.push('"djbh":"' + objSelRowP0201.djbh + '"');
        }

        //组成json字符串
        var jsonStr = '{' + inputs + ',"Records":[' + rows.concat(records) + ']}';

        //执行保存
        var s;
        if (state == 1) {
            s = ParseData(window.external.DoOptions(tbId, "Add", jsonStr));
        } else if (state == 2) {
            s = ParseData(window.external.DoOptions(tbId, "Edit", jsonStr));
        }

        //保存成功
        if (s.ReturnCode == 0) {
            if (state == 2) {
                if (cbdata != getCompareData(tbId, cbcols)) {
                    var docb = '{"flag":"1","djdate":"' + djdate + '"}';
                    window.external.DoOptions(tbId, "DoOthers", docb);
                }
                if (khmc != data.khmc || ytdata != getCompareData(tbId, ytcols)) {
                    var doyt = '{"xslx":"' + xslx + '","djchange":true,"djbh":"' + objSelRowP0201.djbh + '"}';
                    window.external.DoOptions(tbId, "DoOthers", doyt);
                }
            }

            //aftersave：2为保存新增，否则为保存退出
            if (afterSave == 2) { changeGridState(tbId, dlgId); $("#" + dlgId + "_pop_box_top").hide(); } else { cihoDlgClose(dlgId); }
            searchDataClickP0201("djdate", "desc");
        }
        else {
            window.parent.cihoAlert(s.MSG, msgBox.dlgBtn.Ok);
        }
        // $tbId = $dlgId = ids = footerData = records = data = inputs = $kh = $zy = $ck = s = null;
    } catch (e) { }
}

//点击保存按钮后执行验证和保存销售单
//afterSave：1   保存退出
//afterSave：2   保存新增
function afterSaveClick(dlgId, afterSave) {
    try {
        var $tbId = $("#" + strTableP020101);
        var isChange = $("#" + dlgId + "_divform").data("change");
        $tbId.saveCell(cellX, cellY);
        if ($("#" + dlgId).data("state") == 2 && isChange) {
            var ids = $tbId.getDataIDs();
            for (var i = 0; i < ids.length; i++) {
                var item = $tbId.getRowData(ids[i]);
                if (item.glsl * 1 && (item.a1 * 1 != item.glsl * 1)) {
                    window.parent.cihoAlert(msg.other.pihaoNoSame, msgBox.dlgBtn.Ok, function () {
                        if (!authMyBillsP020101(dlgId, afterSave)) { return false; }
                        saveMyDataTp020101(dlgId, afterSave);
                        $tbId = null;
                    });
                    return;
                }
            }
            if (!authMyBillsP020101(dlgId, afterSave)) { return false; }
            saveMyDataTp020101(dlgId, afterSave);
        } else {
            if (!authMyBillsP020101(dlgId, afterSave)) { return false; }
            saveMyDataTp020101(dlgId, afterSave);
        }
        //$tbId = null;
    } catch (e) { }
}

//验证销售单
function authMyBillsP020101(dlgId, afterSave) {
    try {
        var tbId = strTableP020101;
        var $tbId = $("#" + tbId);
        var $dlgId = $("#" + dlgId);

        var djdate = $("#djdate", $dlgId).val();

        if (!djdate || !checkDatePattern(djdate)) {
            window.parent.cihoAlert(msg.xs.inputXsDt, msgBox.dlgBtn.Ok, function () {
                $("#djdate", "#" + dlgId).focus();
                $tbId = $dlgId = null;
            });
            return false;
        } else {
            var djdt = djdate.replace(/\-/g, "/");
            if (djdt < window.top._startDate) {
                window.parent.cihoAlert(msg.other.xsrqExtent, function () {
                    $("#djdate", "#" + dlgId).focus();
                    $tbId = $dlgId = null;
                });
                return false;
            }
        }

        var data = $("#" + dlgId).data("data");

        //验证客户
        var $kh = $("#khmc", $dlgId);
        var khdm = $kh.combobox("getValue");
        var khmc = $kh.combobox("getText");
        if (!khdm && khmc != data.khmc) {
            window.parent.cihoAlert(msg.sz.kh.empOrNotExit, msgBox.dlgBtn.Ok, function () {
                $kh.combobox("focus");
                $tbId = $dlgId = data = $kh = null;
            });
            return false;
        }

        //验证经手人
        var $zy = $("#zymc", $dlgId);
        var zydm = $zy.combobox("getValue");
        var zymc = $zy.combobox("getText");
        if (!zydm && zymc != data.zymc) {
            window.parent.cihoAlert(msg.sz.jsr.empOrNotExit, msgBox.dlgBtn.Ok, function () {
                $zy.combobox("focus");
                $tbId = $dlgId = data = $kh = $zy = null;
            });
            return false;
        }

        //验证仓库
        var $ck = $("#ckmc", $dlgId);
        var ckdm = $ck.combobox("getValue");
        var ckmc = $ck.combobox("getText");
        if (!ckdm && ckmc != data.ckmc) {
            window.parent.cihoAlert(msg.sz.ck.empOrNotExit, msgBox.dlgBtn.Ok, function () {
                $ck.combobox("focus");
                $tbId = $dlgId = data = $kh = $ck = null;
            });
            return false;
        }


        //验证运输单位和运输单号
        var $ysdw = $("#ysdw", $dlgId);
        var ysdh = $("#ysdh", $dlgId).val();
        var ysdw = $ysdw.val();

        //如果输入运输单号，则必须输入运输单位
        if (ysdh && !ysdw) {
            window.parent.cihoAlert(msg.other.inputYsdw, msgBox.dlgBtn.Ok, function () {
                $ysdw.focus();
                $tbId = $dlgId = data = $kh = $ck = $ysdw = null;
            });
            return false;
        }

        var je = $tbId.footerData("get").je * 1;
        var kpje = $tbId.footerData("get").kpje * 1;
        var maxAmount = window.top._MaxPerBill;
        if (je > maxAmount || kpje > maxAmount) {
            window.parent.cihoAlert(msg.other.djhjExtent, msgBox.dlgBtn.Ok);
            return false;
        }

        var vCols = $tbId.data("vCols");
        var ids = $tbId.getDataIDs();
        for (var i = 0; i < ids.length; i++) {
            var item = $tbId.getRowData(ids[i]);
            if (item.flag && !isRowEmpty(item, vCols)) { //只遍历有修改的行
                if (!item.spdm) {
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.yp.lnEmpOrNotExit, msgBox.dlgBtn.Ok);
                    return false;
                }
                if (item.a1 * 1 <= 0) {//药品数量
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.yp.lnAmountEmp, msgBox.dlgBtn.Ok);
                    return false;
                }
                if (item.a2 * 1 <= 0) {//药品单价
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.sz.yp.lnPriceEmp, msgBox.dlgBtn.Ok);
                    return false;
                }

                //验证最低销售单价
                if (item.a2 * 1 < item.djflag * 1) {
                    window.parent.cihoAlert(msg.other.dnh + (i + 1) + "】行的药品单价不能低于最低销售单价！", msgBox.dlgBtn.Ok);
                    return false;
                }
                if (item.a3 == "true") {
                    if (item.a4 * 1 <= 0) { //开票数量
                        window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.fp.lnAmountEmp, msgBox.dlgBtn.Ok);
                        return false;
                    }
                    if (item.a5 * 1 <= 0) { //开票单价
                        window.parent.cihoAlert(msg.other.dnh + (i + 1) + msg.fp.lnPriceEmp, msgBox.dlgBtn.Ok);
                        return false;
                    }
                    if (!item.kpdwdm) {
                        window.parent.cihoAlert(msg.other.dnh + (i + 1) + "】行的开票单位为空或不存在！", msgBox.dlgBtn.Ok);
                        return false;
                    }
                }
            }
        }
        return true;
    } catch (e) { }
}

function initGridSet(tbId, dlgId) {
    var $tbId = $("#" + tbId);
    if (tbId == strTableP020101) {
        $tbId.setGridParam({
            afterEditCell: function (rowId, cellname, value, iRow, iCol) {
                try {
                    cellX = iRow; cellY = iCol;
                    var item = $("#" + rowId + " :input[name='" + cellname + "']", this);
                    switch (cellname) {

                        //=======================================药品名称====================================                                                                                                                                                                                                                                                                            

                        case "spmc":
                            item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 });

                            //为药品combobox加载数据
                            var yplist = getComboboxData("Tspxx");
                            yplist.push({ "mc": "<font color='red'>添加药品</font>", "dm": "-1" });
                            item.combobox("loadData", yplist);

                            //为药品combobox设置值
                            var ypdm = $(this).getCell(rowId, "spdm");
                            item.combobox("setValue", ypdm);
                            if (item.combobox("getText") == ypdm) {
                                item.combobox("setText", value);
                            }

                            item.combobox('focus');
                            item.combobox({
                                onChange: function (newValue, oldValue) {
                                    var $tbId = $("#" + tbId);
                                    if (!newValue) { $tbId.setCell(rowId, "spdm", null); }
                                    else {
                                        var $dlgId = $("#" + dlgId);
                                        //修改时保存的控件值信息
                                        var data = $dlgId.data("data");
                                        $tbId.setCell(rowId, "spdm", newValue);

                                        //=========================设置和获取带票信息=========================

                                        var dp = $tbId.getCell(rowId, "a3");
                                        if (!dp) { $tbId.setCell(rowId, "dp", "否"); $tbId.setCell(rowId, "a3", "false"); }         //如果没有带票信息就赋默认值为否
                                        dp = $tbId.getCell(rowId, "a3") == "true" ? true : false;                                   //获取最终的是否带票

                                        //-------------------------------------获取和设置客户信息------------------------------
                                        var $kh = $("#khmc", "#" + dlgId);
                                        var khdm = $kh.combobox("getValue");
                                        var khmc = $kh.combobox("getText");
                                        if (khmc == data.khmc) { khdm = data.khdm; }

                                        var yplist = getComboboxData("Tspxx");

                                        //是否成功请求到跟踪信息
                                        var request = false;
                                        if (khdm) {
                                            var xslx = $("#xslx", "#" + dlgId).combobox("getValue");

                                            //--------------------------------向后台请求------------------------------
                                            var s = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"khdm":"' + khdm + '","spdm":"' + newValue + '","xslx":"' + xslx + '"}'));
                                            if (s && s.ReturnCode == 0 && s.Records instanceof Array && s.Records.length > 0) {
                                                request = true;

                                                //不管是否带票都要给药品单价赋值
                                                $tbId.setCell(rowId, "a2", s.Records[0].a1 ? s.Records[0].a1 : null);

                                                //当是否带票为是时，为开票单位信息赋值
                                                if (dp) {
                                                    $tbId.setCell(rowId, "a5", s.Records[0].a2 ? s.Records[0].a2 : null);            //开票单价
                                                    var ss = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", "")); //开票单位信息
                                                    if (ss && ss.ReturnCode == 0 && ss.Records instanceof Array) {
                                                        for (var i in ss.Records) {
                                                            if (ss.Records[i].dm == s.Records[0].kpdwdm) {
                                                                $tbId.setCell(rowId, "kpdwdm", ss.Records[i].dm);                    //开票单位代码
                                                                $tbId.setCell(rowId, "kpdwmc", ss.Records[i].mc);                    //开票单位名称
                                                                $tbId.setCell(rowId, "a6", ss.Records[i].kpsl);                      //开票税率
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        //为最低销售单价那列赋值
                                        for (var i in yplist) {
                                            if (newValue == yplist[i].dm) {
                                                $tbId.setCell(rowId, "djflag", yplist[i].a14 ? yplist[i].a14 : 0);              //最低销售单价
                                                //                                                //没有请求到跟踪信息
                                                //                                                if (!request) {
                                                //                                                    $tbId.setCell(rowId, "a2", yplist[i].a14 ? yplist[i].a14 : null);              //药品单价
                                                //                                                    if (dp) { $tbId.setCell(rowId, "a5", yplist[i].a14 ? yplist[i].a14 : null); }  //开票单价
                                                //                                                }
                                                break;
                                            }
                                        }

                                        //计算药品金额
                                        var ypNum = $tbId.getCell(rowId, "a1");
                                        var ypPrice = $tbId.getCell(rowId, "a2");
                                        if (/^\d+(\.\d+)?$/.test(ypNum) && /^\d+(\.\d+)?$/.test(ypPrice)) {
                                            $tbId.setCell(rowId, "je", ypNum * ypPrice);
                                        } else {
                                            $tbId.setCell(rowId, "je", null);
                                        }

                                        //计算开票金额
                                        if (dp) {
                                            var kpNum = $tbId.getCell(rowId, "a4");
                                            var kpPrice = $tbId.getCell(rowId, "a5");
                                            if (!kpPrice) { $tbId.setCell(rowId, "a5", ypPrice); }
                                            kpPrice = $tbId.getCell(rowId, "a5");
                                            if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                                $tbId.setCell(rowId, "kpje", kpNum * kpPrice);
                                            } else {
                                                $tbId.setCell(rowId, "kpje", null);
                                            }
                                        }
                                    }
                                    sumGridTotal(tbId);
                                    changeZt(tbId, rowId);
                                },
                                onSelect: function (item) {
                                    if (item.dm == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { model: "Tspxx", jquery: $(this) });
                                        $(this).combobox("setValue", "");
                                    }
                                },
                                afterChange: function (newValue, newText) {
                                    if (newValue == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "药品", table: "Tflxx" }, { model: "Tspxx", jquery: $(this) });
                                        ciho.showTypeDialog();
                                        $(this).combobox("setValue", "");
                                    }
                                }
                            });
                            break;
                        case "xsy":
                            var xslx = $("#xslx", "#" + dlgId).combobox("getValue");
                            if (xslx == "202") {                                                                                //当为配送销售时，销售员一列不可编辑
                                $(this).restoreCell(iRow, iCol); return;
                            }

                            //-----------------------------初始化销售员combobox------------------------------

                            item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 });
                            var zylist = getComboboxData("TZyxx");
                            zylist.push({ "mc": "<font color='red'>添加销售员</font>", "dm": "-1" });
                            item.combobox("loadData", zylist);
                            var zydm = $(this).getCell(rowId, "xsydm");
                            item.combobox("setValue", zydm);
                            if (item.combobox("getText") == zydm) { item.combobox("setText", value); }

                            item.combobox('focus');
                            item.combobox({
                                onChange: function (newValue, oldValue) {
                                    if (newValue) {
                                        $("#" + tbId).setCell(rowId, "xsydm", newValue);
                                        changeZt(tbId, rowId);
                                    }
                                    else {
                                        $("#" + tbId).setCell(rowId, "xsydm", null);
                                    }
                                    changeZt(tbId, rowId);
                                },
                                onSelect: function (item) {
                                    if (item.dm == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "销售员", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                                        $(this).combobox("setValue", "");
                                    }
                                },
                                afterChange: function (newValue, newText) {
                                    if (newValue == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "销售员", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                                        ciho.showTypeDialog();
                                        $(this).combobox("setValue", "");
                                    }
                                }
                            });
                            break;
                        case "kpdwmc":
                            var a3 = $(this).getCell(rowId, "a3");
                            if (a3 == "false" || !a3) {
                                $(this).restoreCell(iRow, iCol); return;
                            }

                            item.combobox({ valueField: "dm", textField: "mc", width: _iWidth80 });

                            //为开票单位信息加载数据
                            var kpdwlist = getComboboxData("Tkpdwxx");
                            kpdwlist.push({ "mc": "<font color='red'>添加开票单位</font>", "dm": "-1" });
                            item.combobox("loadData", kpdwlist);

                            //获取和设置开票单位
                            var kpdwdm = $(this).getCell(rowId, "kpdwdm");
                            item.combobox("setValue", kpdwdm);
                            if (item.combobox("getText") == kpdwdm) { item.combobox("setText", value); }

                            item.combobox('focus');
                            item.combobox({
                                onChange: function (newValue, oldValue) {
                                    if (newValue) {
                                        $("#" + tbId).setCell(rowId, "kpdwdm", newValue);
                                        var s = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", ""));
                                        if (s && s.ReturnCode == 0 && s.Records instanceof Array) {
                                            for (var i in s.Records) {
                                                if (s.Records[i].dm == newValue) {
                                                    $("#" + tbId).setCell(rowId, "a6", s.Records[i].kpsl);                                           //开票税率
                                                    break;
                                                }
                                            }
                                        }
                                    } else {
                                        $("#" + tbId).setCell(rowId, "kpdwdm", null);
                                        $("#" + tbId).setCell(rowId, "a6", null);
                                    }
                                    changeZt(tbId, rowId);
                                },
                                onSelect: function (item) {
                                    if (item.dm == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "开票单位", table: "Tflxx" }, { model: "Tkpdwxx", jquery: $(this) });
                                        $(this).combobox("setValue", "");
                                    }
                                },
                                afterChange: function (newValue, newText) {
                                    if (newValue == "-1") {
                                        ciho.IniData({ id: "divAddType", title: "开票单位", table: "Tflxx" }, { model: "Tkpdwxx", jquery: $(this) });
                                        ciho.showTypeDialog();
                                        $(this).combobox("setValue", "");
                                    }
                                }
                            });
                            break;
                        case "dp":            //是否带票下拉框
                            item.combobox({
                                valueField: "v",
                                textField: "txt",
                                width: _iWidth65,
                                panelHeight: 42,
                                editable: false,
                                onSelect: function (option) {
                                    var $tbId = $("#" + tbId);
                                    var $dlgId = $("#" + dlgId);
                                    //当选择带票为是时
                                    if (option.v == "true") {
                                        var data = $("#" + dlgId).data("data");

                                        //获取药品代码
                                        var ypdm = $tbId.getCell(rowId, "spdm");

                                        //获取客户信息
                                        var $kh = $("#khmc", $dlgId);
                                        var khdm = $kh.combobox("getValue");
                                        var khmc = $kh.combobox("getText");
                                        if (khmc == data.khmc) { khdm = data.khdm; }

                                        var yplist = getComboboxData("Tspxx");


                                        //用来标示是否成功请求到跟踪信息
                                        var request = false;
                                        //去后台请求跟踪信息
                                        if (ypdm && khdm) {                                                                         //去后台请求开票单价，税率，开票单位名称，开票单位代码
                                            var xslx = $("#xslx", $dlgId).combobox("getValue");

                                            //成功获取到跟踪信息
                                            var s = ParseData(window.external.DoOptions(tbId, "DoOthers", '{"khdm":"' + khdm + '","spdm":"' + ypdm + '","xslx":"' + xslx + '"}'));
                                            if (s && s.ReturnCode == 0 && s.Records instanceof Array && s.Records.length > 0) {
                                                request = true;

                                                //跟踪信息中的开票单价
                                                $tbId.setCell(rowId, "a5", s.Records[0].a2 ? s.Records[0].a2 : null);                    //开票单价

                                                //获取开票税率和开票单位名称
                                                var ss = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", ""));
                                                if (ss && ss.ReturnCode == 0 && ss.Records instanceof Array) {
                                                    for (var i = 0; i < ss.Records.length; i++) {

                                                        //根据开票单位代码获取开票税率和开票单位名称
                                                        if (ss.Records[i].dm == s.Records[0].kpdwdm) {
                                                            $tbId.setCell(rowId, "kpdwdm", ss.Records[i].dm);
                                                            $tbId.setCell(rowId, "a6", ss.Records[i].kpsl);
                                                            $tbId.setCell(rowId, "kpdwmc", ss.Records[i].mc);
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        var ypNum = $tbId.getCell(rowId, "a1");                      //药品数量       
                                        var ypPrice = $tbId.getCell(rowId, "a2");                    //药品价格

                                        $tbId.setCell(rowId, "a4", ypNum);                       //设置开票数量为药品数量

                                        if (!request) { $tbId.setCell(rowId, "a5", ypPrice); }   //如果没有获取到跟踪的开票单价，把药品单价赋给开票单价

                                        var kpNum = $tbId.getCell(rowId, "a4");                  //开票数量
                                        var kpPrice = $tbId.getCell(rowId, "a5");                //开票价格
                                        if (!kpPrice) { $tbId.setCell(rowId, "a5", ypPrice); }
                                        kpPrice = $tbId.getCell(rowId, "a5");
                                        if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                            $tbId.setCell(rowId, "kpje", kpNum * kpPrice);          //开票价格
                                        } else {
                                            $tbId.setCell(rowId, "kpje", null);
                                        }
                                    } else {

                                        //当选择带票为否时
                                        $tbId.setCell(rowId, "a4", null);                            //开票数量
                                        $tbId.setCell(rowId, "a5", null);                            //开票单价
                                        $tbId.setCell(rowId, "a6", null);                            //税率
                                        $tbId.setCell(rowId, "kpje", null);
                                        $tbId.setCell(rowId, "kpdwmc", null);
                                        $tbId.setCell(rowId, "kpdwdm", null);
                                    }

                                    //设置是否带票隐藏列
                                    $tbId.setCell(rowId, "a3", option.v);                           //是否带票

                                    sumGridTotal(tbId);
                                    changeZt(tbId, rowId);                              //改变状态该行的状态
                                }
                            });
                            var dplist = [{ v: "true", txt: "是" }, { v: "false", txt: "否"}];
                            item.combobox("loadData", dplist);
                            item.combobox("setValue", value);
                            item.combobox('focus');
                            break;
                        case "a1":                   //药品数量文本框
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                        case "a4":                   //开票数量文本框
                            var a3 = $(this).getCell(rowId, "a3");
                            if (a3 == "false" || !a3) { $(this).restoreCell(iRow, iCol); return; }
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strCountPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                        case "a2":                  //药品价格文本框
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                        case "a5":                   //开票价格文本框
                            var a3 = $(this).getCell(rowId, "a3");
                            if (a3 == "false" || !a3) {
                                $(this).restoreCell(iRow, iCol); return;
                            }
                            item.val(value);
                            item.autoNumeric({ mDec: window.parent.strPricePlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
                            item.change(function () { changeZt(tbId, rowId); });
                            break;
                    }
                    $(".layoutpanel").css("z-index", "50000009");
                } catch (e) { }
            },
            afterSaveCell: function (rowId, cellname, value, iRow, iCol) {
                try {
                    switch (cellname) {
                        case "a1":                   //药品数量文本框
                        case "a2":                  //药品价格文本框
                            var ypNum = $(this).getCell(rowId, "a1");
                            var ypPrice = $(this).getCell(rowId, "a2");
                            if (/^\d+(\.\d+)?$/.test(ypNum) && /^\d+(\.\d+)?$/.test(ypPrice)) {
                                $(this).setCell(rowId, "je", ypNum * ypPrice);
                            } else {
                                $(this).setCell(rowId, "je", null);
                            }

                            var dp = $(this).getCell(rowId, "a3") == "true" ? true : false;
                            if (dp) {
                                var kpNum = $(this).getCell(rowId, "a4");
                                var kpPrice = $(this).getCell(rowId, "a5");

                                $(this).setCell(rowId, "a4", kpNum ? kpNum : ypNum);

                                $(this).setCell(rowId, "a5", kpPrice ? kpPrice : ypPrice);

                                var kpNum = $(this).getCell(rowId, "a4");
                                var kpPrice = $(this).getCell(rowId, "a5");
                                if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                    $(this).setCell(rowId, "kpje", kpNum * kpPrice);
                                } else {
                                    $(this).setCell(rowId, "kpje", null);
                                }
                            }
                            sumGridTotal(tbId);
                            break;
                        case "a4":                   //开票数量文本框
                        case "a5":                   //开票价格文本框
                            var kpNum = $(this).getCell(rowId, "a4");
                            var kpPrice = $(this).getCell(rowId, "a5");
                            if (/^\d+(\.\d+)?$/.test(kpNum) && /^\d+(\.\d+)?$/.test(kpPrice)) {
                                $(this).setCell(rowId, "kpje", kpNum * kpPrice);
                            } else {
                                $(this).setCell(rowId, "kpje", null);
                            }
                            sumGridTotal(tbId);
                            break;
                    }
                } catch (e) { }
            }
        });
    }
    addColFormater(tbId);
    $tbId.data("records", []);
    bindDelEvent(tbId);
    $("#" + tbId).setGridHeight(141);
}

//格式化grid数据
function addColFormater(tbId) {
    var $tbId = $("#" + tbId);
    if (tbId == strTableP020101) {
        $tbId.setColProp("a1", { formatter: formatCountPlace });
        $tbId.setColProp("a2", { formatter: formatPricePlace });
        $tbId.setColProp("a4", { formatter: formatCountPlace });
        $tbId.setColProp("a5", { formatter: formatPricePlace });
        $tbId.setColProp("je", { formatter: formatMoneyPlace });
        $tbId.setColProp("dp", { editoptions: { custom_element: my_input, custom_value: my_value} });
        $tbId.setColProp("xsy", { editoptions: { custom_element: my_input, custom_value: my_value} });
        $tbId.setColProp("kpje", { formatter: formatMoneyPlace });
        $tbId.setColProp("spmc", { editoptions: { custom_element: my_input, custom_value: my_value} });
        $tbId.setColProp("kpdwmc", { editoptions: { custom_element: my_input, custom_value: my_value} });
    }
    //$tbId = null;
}

//初始化表单元素
function initInputsProp(tbId, dlgId) {
    inputAndBindData("djdate", false, "#" + dlgId, function () { changeZt(tbId); });
    $("#bz", "#" + dlgId).parent().addClass("bz");
    $("#djdate,#zymc", "#" + dlgId).parent().addClass("inp_70");
    $("#djdate,#ysdw,#ysdh,#czydm,#bz", "#" + dlgId).change(function () { changeZt(tbId); }).focus(function () { $(this).select(); });
    $("#ckmc", "#" + dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth100,
        onSelect: function (item) {
            if (item.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "仓库", table: "Tflxx" }, { model: "TCkxx", jquery: $(this) });
                $(this).combobox("setValue", "");
            }
            changeZt(tbId);
            $("#" + tbId).saveCell(cellX, cellY);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "仓库", table: "Tflxx" }, { model: "TCkxx", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            }
        }
    });
    $("#zymc", "#" + dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth100,
        onSelect: function (item) {
            if (item.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "经手人", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                $(this).combobox("setValue", "");
            }
            changeZt(tbId);
            $("#" + tbId).saveCell(cellX, cellY);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "经手人", table: "Tflxx" }, { model: "TZyxx", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            }
        }
    });
    $("#xslx", "#" + dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth100,
        editable: false,
        panelHeight: 42,
        onSelect: function (option) {
            var cklist = getComboboxData("TCkxx");
            var $ck = $("#ckmc", "#" + dlgId);
            var $tbId = $("#" + tbId);
            $tbId.saveCell(cellX, cellY);
            if (option.dm == "202") {                                                                       //配送销售时，可以选择仓库和客户
                var s = ParseData(window.external.DoOptions("Tkhxx", "GetComboBoxData", '{"comtype":"3"}'));
                if (s && s.ReturnCode == 0 && s.Records instanceof Array) {
                    var list = cklist.concat(s.Records);
                    list.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
                    $ck.combobox("loadData", list);
                    if (list.length == 2) {
                        $ck.combobox("setValue", list[0].dm);
                    }
                }
                var ids = $tbId.getDataIDs();
                for (var i in ids) {
                    var item = $tbId.getCell(ids[i], "flag");
                    if (item) {
                        $tbId.setCell(ids[i], "xsy", null, "", { title: "配送模式销售，不需要选择销售员。" });
                        $tbId.setCell(ids[i], "xsydm", null);
                    }
                }
            } else {
                cklist.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
                $ck.combobox("loadData", cklist);
                if (cklist.length == 2) {
                    $ck.combobox("setValue", cklist[0].dm);
                }
                var ids = $tbId.getDataIDs();
                for (var i in ids) {
                    $tbId.setCell(ids[i], "xsy", "", "", { title: "" });
                }
            }
            changeZt(tbId);
            $("#" + tbId).saveCell(cellX, cellY);
        }
    });
    $("#khmc", "#" + dlgId).combobox({
        valueField: "dm",
        textField: "mc",
        width: _iWidth100,
        onSelect: function (item) {
            if (item.dm == "-1") {
                ciho.IniData({ id: "divAddType", title: "客户", table: "Tflxx" }, { model: "Tkhxx", jquery: $(this) });
                $(this).combobox("setValue", "");
            } else {
                var $ysdw = $("#ysdw", "#" + dlgId);
                var ysdw = $.trim($ysdw.val());
                if (!ysdw) {
                    var ysdwmc = window.external.DoOptions(tbId, "DoOthers", '{"khdm":"' + item.dm + '","flag":"0"}');
                    $ysdw.val(ysdwmc);
                }
                //$ysdw = null;
            }
            changeZt(tbId);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "客户", table: "Tflxx" }, { model: "Tkhxx", jquery: $(this) });
                ciho.showTypeDialog();
                $(this).combobox("setValue", "");
            } else {
                $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
            }
        }
    });
    $("#czydm", "#" + dlgId).attr("disabled", true);
}

//加载combobox数据
function loadComboData(dlgId, tbId, xslx) {
    var khlist = getComboboxData("Tkhxx1");
    var zylist = getComboboxData("TZyxx");
    var cklist = getComboboxData("TCkxx");
    var xslxlist = [{ "dm": "202", "mc": "配送销售" }, { "dm": "201", "mc": "终端销售"}];

    zylist.push({ "mc": "<font color='red'>添加经手人</font>", "dm": "-1" });
    khlist.push({ "mc": "<font color='red'>添加客户</font>", "dm": "-1" });

    var $kh = $("#khmc", "#" + dlgId);
    var $zy = $("#zymc", "#" + dlgId);
    var $ck = $("#ckmc", "#" + dlgId);
    var $xslx = $("#xslx", "#" + dlgId);

    $kh.combobox("loadData", khlist);
    $zy.combobox("loadData", zylist);
    $xslx.combobox("loadData", xslxlist);

    if (xslx == "202") {
        var s = ParseData(window.external.DoOptions("Tkhxx", "GetComboBoxData", '{"comtype":"3"}'));
        if (s && s.ReturnCode == 0 && s.Records instanceof Array) {
            var list = cklist.concat(s.Records);
            list.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
            $ck.combobox("loadData", list);
            return list.length == 2 ? list[0].dm : "";
            //list = null;
        }
        s = null;
    } else if (xslx == "201") {
        cklist.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
        $ck.combobox("loadData", cklist);
        return cklist.length == 2 ? cklist[0].dm : "";
    }
    // khlist = zylist = cklist = xslxlist = $kh = $zy = $ck = $xslx = null;
}

//改变grid状态，使grid由修改状态转换为新增状态
function changeGridState(tbId, dlgId) {
    try {
        var $tbId = $("#" + tbId);
        var $dlgId = $("#" + dlgId);
        $tbId.clearGridData();
        $tbId.data("records", []);
        $dlgId.data("data", {});
        $dlgId.data("state", 1);
        $("#" + dlgId + "_divform").data("change", false);
        $("#idMust", "#" + dlgId).text("");
        $("#djdate", "#" + dlgId).attr("disabled", false).val(getNowDate());
        $("#czydm", "#" + dlgId).val(window.parent.objLoginInfo.name);
        $("#zymc,#khmc", "#" + dlgId).combobox("setValue", "");
        $("#xslx", "#" + dlgId).combobox("setValue", "201");
        $("#xslx", "#" + dlgId).combobox("enable");

        var cklist = getComboboxData("TCkxx");
        cklist.push({ "mc": "<font color='red'>添加仓库</font>", "dm": "-1" });
        $("#ckmc", "#" + dlgId).combobox("loadData", cklist);
        if (cklist.length == 2) {
            $("#ckmc", "#" + dlgId).combobox("setValue", cklist[0].dm);
        }

        $("#ysdw,#ysdh,#bz", "#" + dlgId).val("");
        loadGridData(tbId, []);

        setTimeout('$("#ysdw","#"+strDivP020101).focus();', 100);
        //$tbId = $dlgId = cklist = null;
    } catch (e) { }
}

//格式化数量
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化价格
function formatPricePlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strPricePlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化金额
function formatMoneyPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化grid中的combobox
function my_input(value, options) {
    return $('<select name="' + options.name + '" id="' + options.id + '" class="easyui-combobox"></select>');
}
function my_value(ele) {
    return ele.combobox('getText');
}

//判断grid中的行是否为空行
function isRowEmpty(row, cols) {
    for (var i = 0; i < cols.length; i++) {
        if (!/^\s*$/.test(row[cols[i]])) {
            return false;
        }
    }
    return true;
}
