/******************************************************************************
*
*
*
******************************************************************************/
//定义全局
var ieVersion = "7.0";
var arrIndex = null;            //报表链接
var _MaxPerBill = 100000000;    //单张单据允许录入的最大值
var _startDate = "";            //系统与许录入的最早，最晚时间
var _endDate = "";
_startDate = _endDate = getNowDate();

var strPricePlace = "2";    //单价小数位数（该项与金额相同）
var strCountPlace = "0";    //数量小数位数
var strMoneyPlace = "2";    //金额小数位数
var strPercentPlace = "2";  //百分比小数位数
var bShowMsgOfMoney = true; //单据金额和收付金额不一致时,true:提示用户； false:不提示用户。
var strOpratDate = _startDate; //系统启用时间
var iRowsPerPage = "15";    //每页默认显示多少行记录。

var objLoginInfo = null;    //操作员信息

var bSkZt = true;           //是否使用收款
var SoftID = '';            //
var strVer = '1.0';            //版本号

//存储除了基本资料变动引起的刷新
//对象数组[{page:"Tp0201",flag:""}]
var arrRefresh = [];

//基本资料是否有变动
var arrJbzl = [false, false, false, false, false, false, false, false];
var _arrCmbGys = [];    //供应商   0
var _arrCmbGys2 = [];    //供应商   0
var _arrCmbYp = [];     //药品     1
var _arrCmbYp2 = [];     //药品     1
var _arrCmbKh1 = [];     //客户     2
var _arrCmbKh2 = [];     //客户     2
var _arrCmbKh3 = [];     //客户     2
var _arrCmbBm = [];     //部门     3
var _arrCmbBm2 = [];     //部门     3
var _arrCmbZy = [];     //职员     4
var _arrCmbZy2 = [];     //职员     4
var _arrCmbKjkm = [];   //会计科目 5
var _arrCmbKjkm2 = [];   //会计科目 5
var _arrCmbKpdw = [];   //开票单位 6
var _arrCmbKpdw2 = [];   //开票单位 6
var _arrCmbCk = [];     //仓库     7
var _arrCmbCk2 = [];     //仓库     7

//调用AddTab方法，实现另一子窗口的OPen
function add(title, url, id) {
    AddTab(title, url, id);
}

//触发子画面的动作
function triggerSubPageAction(strTableName, strType) {
    //关闭
    if ("0" == strType || "10" == strType) {
        //验证数据是否有修改
        isDataChange(strTableName, strType);
    }
    else {//保存，保存新增
        //alert("idSubPage_" + strTableName);
        //alert(strType);

        //传递动作方式给子画面
        $(document.getElementById("idIframe").contentWindow.document.getElementById("idSubPage_" + strTableName)).val(strType);
        //触发子画面的保存动作
        $(document.getElementById("idIframe").contentWindow.document.getElementById("idSubPage_" + strTableName)).click();
    }
}

//创建添加修改窗口1
function openDlgAlter() {
    //清空弹出窗口层
    $("#alterRecord").empty();

    dialog("alterRecord", window.document.documentElement.clientHeight, "100%", true, "", false, true);
    //    $(".ui-dialog").css("height", "100%");

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

    //创建弹出窗口
    //        $("#alterRecord").dialog({
    //            autoOpen: true,
    //            height: 100,
    //            width: 100,
    //            modal: true,
    //            title: strTitle,
    //            zIndex: 10000
    //        });
    //alert(document.getElementById("idIframe").contentWindow.document.getElementById(strTableName).id);
    //var objTbl = document.getElementById("idIframe").contentWindow.document.getElementById(strTableName);
    //$(objTbl).jqGrid("setCell", strSelRowId, "xh", "序号test");
}

//创建弹出窗口2
function openDlg2() {
    //清空弹出窗口层
    $("#idPopDlg2").empty();

    dialog("idPopDlg2", window.document.documentElement.clientHeight, "100%", true, "", false, true);
}

//创建消息弹出窗口
function openDlgMsg() {
    //清空弹出窗口层
    $("#divMessage").empty();
    dialog("divMessage", window.document.documentElement.clientHeight, "100%", true, "", false, true);
}

/*-----------------------------------------------------------------------------
//功能：验证表单里的内容是否改变
//参数：
//   stype   0:退出//点击关闭按钮
//strTableName：表的名称
//url：      保存数据URL
-----------------------------------------------------------------------------*/
function isDataChange(strTableName, strType) {
    //数据有变化
    if ($("#divform").data("change")) {
        $("#divMessage").dialog({
            modal: true,
            title: "提示"
        });
        $("#divMessage").html("<div style='float:center;clear:both; margin-top:20px;margin-left:40px;>'>您已经修改记录，是否保存</div>");
        $("#divMessage").append("<div id='btndivmessage' style='float:left;clear:both; margin-top:70px;>'></div>");
        $("#btndivmessage").html("<a style='margin-left:100px;' href='#' onclick=\"clickYes('" + strType + "','" + strTableName + "');\">是</a><a style='margin-left:60px;' href='#' onclick=\"clickNo('" + strType + "','" + strTableName + "')\">否</a>");
    }
    else {//0：关闭。关闭编辑窗口
        if (strType == "0") {
            $('#alterRecord').dialog('close');
        }
        else if (strType == "10") {
            $('#idPopDlg2').dialog('close');
        }
    }
}

//数据有修改，保存数据。YES
function clickYes(strType, strTableName) {
    //关闭消息窗口
    $('#divMessage').dialog('close');

    //传递动作方式给子画面
    document.getElementById("idIframe").contentWindow.document.getElementById("idSubPage_" + strTableName).val(strType);
    //触发子画面的保存动作
    $(document.getElementById("idIframe").contentWindow.document.getElementById("idSubPage_" + strTableName)).click();
}

//数据有修改，不保存数据。NO
function clickNo(strType, strTableName) {
    //关闭消息窗口
    $('#divMessage').dialog('close');

    //0：关闭。关闭编辑窗口
    $('#alterRecord').dialog('close');
}

function popDlgDraggAble(strDlgId) {
    //增加窗口的拖动功能
    //$('#' + strDlgId).parent().parent().draggable();
}
function createkcGrid(strTableName, strJsonData, arrNames, arrModel, strTitle) {
    $("#" + strTableName).jqGrid({
        colNames: arrNames,
        colModel: arrModel,
        data: strJsonData,
        datatype: "local",
        width: 600,
        height: 320,
        rownumbers: true,
        "loadComplete": function () {
            //                    var selRows = jQuery("#" + strTableName).jqGrid("getDataIDs");
            //                    //格式化数据
            //                    var arrIds = $("#" + strTableName).jqGrid("getDataIDs");
            //                    var strState = "1";
            //                    var strClassCode = "";
            //                    for (var intI = 0; intI < arrIds.length; intI++) {
            //                        strButtonHtml = "";
            //                        strButtonHtml += "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"AddRows('TKcqc','2')\">修改</a>";
            //                        strButtonHtml += "|";
            //                        strButtonHtml += "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"deleteRows();\">删除</a>";
            //                        $("#" + strTableName + " tr:eq( " + intI + ") td:eq(15) input[type='button']").attr("disabled", false).bind('click', function () { });
            //                        $("#" + strTableName).jqGrid("setCell", arrIds[intI], "cz", strButtonHtml);
            //                    }

        },
        onSelectRow: function (id) {
            //createFloatDiv(strTableName, id);
        }
    });
    //增加表头‘序号’列
    //$("table.ui-jqgrid-htable th:first").text("序号");
}

function createShieldDiv(dlgId) {
    var strShieldDiv = "<div id='menu_" + dlgId + "' class='menu_dlg'></div>";
    $("body").append(strShieldDiv);
    $("#menu_" + dlgId).css("display", "block");
}
function closeShieldDiv(dlgId) {
    //$("#menu_" + dlgId).css("display", "none");
    $("#menu_" + dlgId).remove();
}

//加载comgbobox的值
function getAllComboData() {
    _arrCmbGys = ParseData(window.external.DoOptions("Tgysxx", "GetComboBoxData", "{}"));
    _arrCmbGys2 = ParseData(window.external.DoOptions("Tgysxx", "GetComboBoxData", "{\"comtype\":\"4\"}"));
    _arrCmbYp = ParseData(window.external.DoOptions("Tspxx", "GetComboBoxData", "{}"));
    _arrCmbYp2 = ParseData(window.external.DoOptions("Tspxx", "GetComboBoxData", "{\"comtype\":\"4\"}"));
    _arrCmbKh1 = ParseData(window.external.DoOptions("Tkhxx", "GetComboBoxData", "{\"comtype\":\"1\"}"));
    _arrCmbKh2 = ParseData(window.external.DoOptions("Tkhxx", "GetComboBoxData", "{\"comtype\":\"2\"}"));
    _arrCmbKh3 = ParseData(window.external.DoOptions("Tkhxx", "GetComboBoxData", "{\"comtype\":\"3\"}"));
    _arrCmbBm = ParseData(window.external.DoOptions("Tbmxx", "GetComboBoxData", "{}"));
    _arrCmbBm2 = ParseData(window.external.DoOptions("Tbmxx", "GetComboBoxData", "{\"comtype\":\"4\"}"));
    _arrCmbZy = ParseData(window.external.DoOptions("TZyxx", "GetComboBoxData", "{}"));
    _arrCmbZy2 = ParseData(window.external.DoOptions("TZyxx", "GetComboBoxData", "{\"comtype\":\"4\"}"));
    _arrCmbKjkm = ParseData(window.external.DoOptions("TKjkm", "GetComboBoxData", "{}"));
    _arrCmbKjkm2 = ParseData(window.external.DoOptions("TKjkm", "GetComboBoxData", "{\"comtype\":\"4\"}"));
    _arrCmbKpdw = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", "{}"));
    _arrCmbKpdw2 = ParseData(window.external.DoOptions("Tkpdwxx", "GetComboBoxData", "{\"comtype\":\"4\"}"));
    _arrCmbCk = ParseData(window.external.DoOptions("TCkxx", "GetComboBoxData", "{}"));
    _arrCmbCk2 = ParseData(window.external.DoOptions("TCkxx", "GetComboBoxData", "{\"comtype\":\"4\"}"));
}

//页面数据变化，引起其他页面刷新
function refreshInfo() {

    var i = 0;
    var arrTemp = arrRefresh.concat();

    // 刷新其他单据
    for (i = 0; i < arrTemp.length; i++) {
        if (arrTemp[i].flag && arrTemp[i].page) {
            $(".tabs>li>a>span").each(function () {

                //获取打开的窗口的信息
                var id = $(this).attr("id");
                var title = $(this).text();
                var arrId = id.split('_');
                if (arrId.length > 1) {
                    id = arrId[1];
                }

                //所有页面
                if (window.frames['idIframe-' + id].bLoadComplete != undefined && true == window.frames['idIframe-' + id].bLoadComplete) {
                    if ($(window.parent.document).find("#idIframe-" + id).length > 0 && window.frames["idIframe-" + id].refreshInfos) {
                        window.frames["idIframe-" + id].refreshInfos(arrTemp[i]);
                    }
                }
            });

            //把传递完消息的对象删除
            arrRefresh.splice(i, 1);
        }
    }

    arrTemp = null;
}

//如果基本资料数据有变动，刷新combobox的数据
function refreshCombobox(selPanelId) {
    var strModel;
    var arrChangeModel = [];                //存放改变的model

    var i = 0;
    for (i = 0; i < arrJbzl.length; i++) {
        if (true == arrJbzl[i]) {
            //如果数据有变动，取得该数据的值
            switch (i) {
                case 0: //供应商
                    strModel = "Tgysxx";
                    _arrCmbGys = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbGys2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
                case 1: //药品  
                    strModel = "Tspxx";
                    _arrCmbYp = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbYp2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
                case 2: //客户  
                    strModel = "Tkhxx";
                    _arrCmbKh1 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"1\"}"));
                    _arrCmbKh2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"2\"}"));
                    _arrCmbKh3 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"3\"}"));
                    break;
                case 3: //部门 
                    strModel = "Tbmxx";
                    _arrCmbBm = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbBm2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
                case 4: //职员
                    strModel = "TZyxx";
                    _arrCmbZy = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbZy2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
                case 5: //会计科目
                    strModel = "TKjkm";
                    _arrCmbKjkm = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbKjkm2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
                case 6: //开票单位
                    strModel = "Tkpdwxx";
                    _arrCmbKpdw = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbKpdw2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
                case 7: //仓库
                    strModel = "TCkxx";
                    _arrCmbCk = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{}"));
                    _arrCmbCk2 = ParseData(window.external.DoOptions(strModel, "GetComboBoxData", "{\"comtype\":\"4\"}"));
                    break;
            }
            //循环tabs，加载给所有要更新数据的tab页更新数据
            $(".tabs>li>a>span").each(function () {
                //$('#tabs>ul>li>a[href]').each(function () {
                var id = $(this).attr("id");
                var title = $(this).text();
                var arrId = id.split('_');
                if (arrId.length > 1) {
                    id = arrId[1];
                }

                //采购：1，销售：2，纯销：8，库存：3，发票：4，财务：5,档案：6
                var strFilterData = "1234586";
                //var test = strFilterData.indexOf(id.substring(0, 1));
                //                if ((id.indexOf("p01") ||  //采购
                //                      id.indexOf("p02") ||  //销售
                //                      id.indexOf("p03") ||  //纯销
                //                      id.indexOf("p04") ||  //库存
                //                      id.indexOf("p05"))   //发票    //财务
                //                 && true == window.frames['idIframe-' + id].bLoadComplete) {
                if ("-1" != strFilterData.indexOf(id.substring(0, 1)) && window.frames['idIframe-' + id].bLoadComplete != undefined && true == window.frames['idIframe-' + id].bLoadComplete) {
                    if ($(window.parent.document).find("#idIframe-" + id).length > 0 && window.frames["idIframe-" + id].refreshMyCombobox) {
                        window.frames["idIframe-" + id].refreshMyCombobox([strModel]);
                    }
                }
            });

            //恢复标志为
            arrJbzl[i] = false;
            break;
        } //End if
    }
}

//关于我们
function openP0902() {
    var dlgId = "divP0902";
    var iWidth = 350;
    var iHeight = "230";
    creatDiv_ShowDialog(dlgId, "关于我们", false, false, "", iWidth + 2, true);
    $('#' + dlgId + "_content").css("margin-top", "50px");
    $('#' + dlgId + "_divform").height(iHeight);
    //调整弹出窗口遮罩层的高度
    $('.menu_dlg_shield').height(window.parent.document.documentElement.clientHeight);
    $('.menu_dlg_bg').css("height", window.parent.document.documentElement.clientHeight); //调整弹出所属窗口层的高度

    //添加内容
    var strAbout = "";
    strAbout += '<iframe scrolling=\"no\" id=\"idIframe-p0902" frameborder=\"0\" align=\"center\"  src=\"\"';
    strAbout += ' width=\"' + iWidth.toString() + 'px\" height=\"' + iHeight + 'px\"></iframe>';

    $("#" + dlgId + "_divform").append(strAbout);
    $("#idIframe-p0902").attr("src", "09/p0902.htm");
}

//用户反馈
function openP0906() {
    var dlgId = "divP0906";
    var iWidth = "450";
    var iHeight = "330";
    creatDiv_ShowDialog(dlgId, "用户反馈", false, false, "", parseInt(iWidth) + 2, true);
    $('#' + dlgId + "_content").css("margin-top", "50px");
    $('#' + dlgId + "_divform").height(iHeight);
    //调整弹出窗口遮罩层的高度
    $('.menu_dlg_shield').height(window.parent.document.documentElement.clientHeight);
    $('.menu_dlg_bg').css("height", window.parent.document.documentElement.clientHeight); //调整弹出所属窗口层的高度

    //添加内容
    var strAbout = "";
    strAbout += '<iframe scrolling=\"no\" id=\"idIframe-p0906" frameborder=\"0\" align=\"center\"  src=\"\"';
    strAbout += ' width=\"' + iWidth + 'px\" height=\"' + iHeight + 'px\"></iframe>';

    $("#" + dlgId + "_divform").append(strAbout);
    $("#idIframe-p0906").attr("src", "09/p0906.htm");
}

//关闭弹出窗口：about
function closeMyDlg(strDialog, strType, strTable) {
    cihoDlgClose(strDialog);
}