var commonGridObj_settlement;
var comboCustomer; //委托单位

function $(id) {
    return document.getElementById(id);
}

function createGrid() {
    //header
    var headerArgs_settlement = new Array("<input type=\"checkbox\" id=\"ck_all\" onclick=\"checkAll('ck_all')\" />", "凭证号", "结算类型", "结算单号", "结算单状态", "结算类别", "结算单位", "结算方式", "USD", "RMB", "结算人", "结算日期", "备注");
    //width
    var widthArgs_settlement = new Array("40", "100", "70", "120", "70", "70", "100", "70", "80", "80", "150", "150", "200");
    //column align                       
    var colAlignArgs_settlement = new Array("center","center","center", "center", "center", "center", "center", "center", "center", "center", "center", "center", "center");
    //column sort               
    var colSortArgs_settlement = new Array("na", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str");
    //column type
    var colTypeArgs_settlement = new Array("ch", "ro", "ro", "ro", "ro", "ro", "ro", "ro", "ro", "ro", "ro", "ro", "ro");
    
    commonGridObj_settlement = new TGridCommon("mygrid_container_settlement", "form1", headerArgs_settlement, widthArgs_settlement, colAlignArgs_settlement, colSortArgs_settlement, colTypeArgs_settlement);
    commonGridObj_settlement.setResponseUrl("CwVouchersChFeeSettlementListGridSource.aspx");
    commonGridObj_settlement.setRequest("CwVouchersChFeeSettlementListGridSource.aspx?handle=settlelist&show_page=30&cur_page=1&search=" + escape($("h_searchJson").value.trim()) + "&uid=" + commonGridObj_settlement.newGuid());
    commonGridObj_settlement.setSourceType("json");
    commonGridObj_settlement.setSkin("xp");
    commonGridObj_settlement.setImageUrl("../images/");
    commonGridObj_settlement.initGrid();
    commonGridObj_settlement.bind();
    commonGridObj_settlement.setSearch($("h_searchJson").value.trim());
    commonGridObj_settlement.initPageByHandle("settlelist");
    commonGridObj_settlement.isDBClickEvent = true;
}

function initCombo() {
    createComboCustomer(); //结算单位
}

function refreshList() {
    createGrid();
}

//根据条件查询付费申请信息
function search() {
    var dg = new dialog();
    dg.html = "程序正在提交,请稍等!";
    //dg.html+="<input type=button onclick='new dialog().close();' value='确定'>";
    dg.width = 200;
    dg.height = 100;
    dg.title = "标题";
    dg.show();
    //
    var _begintime = $("txt_beginDate").value.trim();
    if (_begintime.trim() != "") {
        if (!validateDateTime(_begintime)) {
            alert("起始时间格式错误");
            return;
        }
    }
    var _endtime = $("txt_endDate").value.trim();
    if (_endtime.trim() != "") {
        if (!validateDateTime(_endtime)) {
            alert("结束时间格式错误");
            return;
        }
    }

    var _customer = $("h_customer").value.trim();
    var _checkno = $("txt_billno").value.trim();
    var _vouno = $("txt_vouno").value.trim();
    //var _blno = $("txt_blno").value.trim();

    var searchJson = "";
    //begin
    searchJson += "{";
    searchJson += "[";

    //如果查询条件设置了起始时间和结束时间则判断结束时间是否大于起始时间
    if (_begintime != "" && _endtime != "") {
        if (_begintime.length > 9 && _endtime.length > 9) {
            var status = DateTimeEqual(_begintime, _endtime);
            if (status == 0) {
                searchJson += "\"btime\":\"" + _begintime + "\","
                searchJson += "\"etime\":\"" + _endtime + "\","
            } else if (status == -1) {
                alert("时间设置错误,起始时间不能大于结束时间,请修改!");
                return;
            } 
//            else if (status == -2) {
//                alert("时间设置错误,起始时间不能等于结束时间,请修改!");
//                return;
//            }
        }
        else {
            alert("时间格式有问题,长度应大于9个字符,请修改!");
            return;
        }
    } else {
        searchJson += "\"btime\":\"" + _begintime + "\","
        searchJson += "\"etime\":\"" + _endtime + "\","
    }
    searchJson += "\"cus\":\"" + _customer + "\",";
    searchJson += "\"no\":\"" + _checkno + "\",";
    //searchJson += "\"blno\":\"" + _blno + "\",";
    searchJson += "\"vouno\":\"" + _vouno + "\",";
    searchJson += "\"cbisv\":\"" + $("cbISVOUCHERNO").checked + "\"";
    searchJson += "]";
    searchJson += "}";
    $("h_searchJson").value = searchJson;
    //
    setTimeout(function () {
        createGrid();
        dg.close();
    }, 5000);
}
//校验时间格式
function validateDateTime(dateTimeObj) {
    var isValid = false;

    var parseToObj = new Date(dateTimeObj.replace(/-/g, "/"));
    if (!isNaN(parseToObj)) {
        isValid = true;
    }

    return isValid;
}

function DateTimeEqual(beginObj, endObj) {
    var isEqual = 0;
    var begin = new Date(beginObj.replace(/-/g, "/"));
    var end = new Date(endObj.replace(/-/g, "/"));

    if (Date.parse(begin) - Date.parse(end) > 0) {
        isEqual = -1; //起始时间大于结束时间
    } else if (Date.parse(begin) - Date.parse(end) == 0) {
        isEqual = -2; //起始时间与结束时间相等
    }
    return isEqual;
}

String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }

function createComboCustomer() {//委托单位
    comboCustomer = dhtmlXComboFromSelect("sel_customer");
    comboCustomer.loadXML("../FeeCodes/WebCrmClientAdapter.aspx?mask=0&pos=0");

    comboCustomer.attachEvent("onChange", function() {
        comboCustomer.setComboText($("h_customer").value);
    });

    comboCustomer.attachEvent("onBlur", function() {
        var s1 = comboCustomer.getComboText().trim();
        var s2 = s1.indexOf("|");
        if (s2 > 0) {
            s2 = s2 + 2;
        }
        var s3 = s1.substring(s2);
        if (s2 > 0) {
            comboCustomer.setComboText(s3);
            $("h_customer").value = s3;
        }
        else {
            comboCustomer.setComboText($("h_customer").value);
        }
    });

    comboCustomer.setComboText($("h_customer").value);
    comboCustomer.attachEvent("onKeyPressed", function(keyCode) {
        if (keyCode != "8") {
            var arrayArg = new Array();
            setTimeout(function() {
                var filterCount = 0;
                var is = -1;
                var ishd = $("h_customer").value.trim();
                if ("" != comboCustomer.getComboText().trim()) {
                    while ("undefined" != typeof comboCustomer.getOptionByIndex(filterCount)) {
                        var comboValue = comboCustomer.getOptionByIndex(filterCount).text.toUpperCase(); //.value;
                        var enterValue = comboCustomer.getComboText().trim().toUpperCase();
                        $("h_customer").value = comboCustomer.getComboText().trim().toUpperCase();
                        if (comboValue.indexOf(enterValue) == 0) {
                            comboCustomer.selectOption(filterCount, true, true);
                            is = filterCount;
                            break;
                        }
                        ++filterCount;
                    }
                    if (is == -1) {
                        $("h_customer").value = ishd;
                        alert("无此内容,请重新选择!");
                    }
                }
            }, 10);
        }
    });

    comboCustomer.attachEvent("onSelectionChange", function() {
        $("h_customer").value = comboCustomer.getComboText().trim();
    });
}

//初始化界面
function initGrid() {
    initCombo();
    $("h_customer").value = "";
    comboCustomer.setComboText($("h_customer").value);
    $("txt_beginDate").value = "";
    $("txt_endDate").value = "";
    $("txt_billno").value = "";
    $("cbISVOUCHERNO").checked = false;
    $("txt_vouno").value = "";
    //$("txt_blno").value = "";
    //createGrid();
}

function checkAll(id) {
    if ($(id).checked) {
        for (var i = 0; i < commonGridObj_settlement.getGridObj().getRowsNum(); i++) {
            commonGridObj_settlement.getGridObj().cellByIndex(i, 0).setValue(1);
        }
    } else {
        for (var i = 0; i < commonGridObj_settlement.getGridObj().getRowsNum(); i++) {
            commonGridObj_settlement.getGridObj().cellByIndex(i, 0).setValue(0);
        }
    }
}

function ShowDetailAdd() {
    var checked = commonGridObj_settlement.getGridObj().getCheckedRows(0); //所有选中的grid的id编号
    if (checked == null || checked == "") {
        alert("请先选择要操作的数据!");
    }
    else {
        var url = "../CW/CwVouchersChFeeSettlementListGridSource.aspx?handle=isvouchers&gids=" + checked + "&uid=" + newGuid();
        var loader = dhtmlxAjax.getSync(url);
        var jsVal = loader.xmlDoc.responseText;
        if (jsVal.trim() != "") {
            alert(jsVal); //"有已生成的凭证,不能重复生成!"
            return;
        }
        //
        var feature = "height=200, width=350, toolbar=no, menubar=no,scrollbars=no, resizable=0,location=no, status=no,Top= " + (screen.height / 2 - 100) + ",Left= " + (screen.width / 2 - 200);
        var URLs = "CwVouchersAddingDo.aspx?gids=" + checked;
        window.open(URLs, "生成凭证信息", feature);
    }
}

function newGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid.toUpperCase();
}


function dialog() {
    this.width = 450;
    this.height = 360;
    this.title_height = 20;
    this.html = '';
    this.title = '';
    var self = this;
    var bgObj, msgObj, titleObj;
    this.close = function () {
        document.body.removeChild(document.getElementById("bgDiv"));
        document.getElementById("msgDiv").removeChild(document.getElementById("msgTitle"));
        document.body.removeChild(document.getElementById("msgDiv"));
    }

    this.show = function () {
        var msgw, msgh, bordercolor;
        msgw = self.width;
        msgh = self.height;
        bordercolor = "#336699";
        titlecolor = "#99CCFF";

        var sWidth, sHeight;
        sWidth = document.body.offsetWidth;
        sHeight = screen.height;
        bgObj = document.createElement("div");
        bgObj.setAttribute('id', 'bgDiv');
        bgObj.style.position = "absolute";
        bgObj.style.top = "0";
        bgObj.style.background = "#777";
        bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
        bgObj.style.opacity = "0.6";
        bgObj.style.left = "0";
        bgObj.style.width = sWidth + "px";
        bgObj.style.height = sHeight + "px";
        bgObj.style.zIndex = "10000";
        document.body.appendChild(bgObj);

        msgObj = document.createElement("div")
        msgObj.setAttribute("id", "msgDiv");
        msgObj.setAttribute("align", "center");
        msgObj.style.background = "white";
        msgObj.style.border = "1px solid " + bordercolor;
        msgObj.style.position = "absolute";
        msgObj.style.left = "50%";
        msgObj.style.top = "30%";
        msgObj.style.font = "12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
        msgObj.style.marginLeft = "-225px";
        msgObj.style.marginTop = -75 + document.documentElement.scrollTop + "px";
        msgObj.style.width = msgw + "px";
        msgObj.style.height = msgh + "px";
        msgObj.style.textAlign = "center";
        msgObj.style.lineHeight = "25px";
        msgObj.style.zIndex = "10001";

        titleObj = document.createElement("h4");
        titleObj.setAttribute("id", "msgTitle");
        titleObj.setAttribute("align", "center");
        titleObj.style.margin = "0";
        titleObj.style.padding = "3px";
        titleObj.style.background = bordercolor;
        titleObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
        titleObj.style.opacity = "0.75";
        titleObj.style.border = "1px solid " + bordercolor;
        titleObj.style.height = self.title_height + "px";
        titleObj.style.font = "12px Verdana, Geneva, Arial, Helvetica, sans-serif";
        titleObj.style.color = "white";
        titleObj.style.cursor = "pointer";
        titleObj.innerHTML = self.title;
        //titleObj.onclick=function(){self.close();} 
        document.body.appendChild(msgObj);
        document.getElementById("msgDiv").appendChild(titleObj);
        var txt = document.createElement("div");
        txt.style.margin = "1em 0"
        txt.setAttribute("id", "msgTxt");
        txt.innerHTML = self.html;
        document.getElementById("msgDiv").appendChild(txt);
    }
}
function msgBox() {
    var dg = new dialog();
    dg.html = "程序正在提交,请稍等!";
    //dg.html+="<input type=button onclick='new dialog().close();' value='确定'>";
    dg.width = 200;
    dg.height = 100;
    dg.title = "标题";
    dg.show();
}