var commonGridObj_Chequeitem;

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

function createGrid()
{
    //header
    var headerArgs_Chequeitem = new Array("","支票号码","是否锁定","是否废除","是否已开出","创建人","创建时间","开出人","开出时间");
    //width
    var widthArgs_Chequeitem = new Array("40","150","70","70","70","120","120","120","120");
    //column align                       
    var colAlignArgs_Chequeitem = new Array("center","center","center","center","center","center","center","center","center");
    //column sort               
    var colSortArgs_Chequeitem = new Array("na","str","str","str","str","str","str","str","str");
    //column type
    var colTypeArgs_Chequeitem = new Array("ch","ro","ch","ch","ch","ro","ro","ro","ro");
 
    
    commonGridObj_Chequeitem = new TGridCommon("mygrid_container_Chequeitem","form1",headerArgs_Chequeitem,widthArgs_Chequeitem,colAlignArgs_Chequeitem,colSortArgs_Chequeitem,colTypeArgs_Chequeitem);
    
    commonGridObj_Chequeitem.setResponseUrl("ChequeBooksGridSource.aspx");
    
    var bookID = $("h_bookid").value;
    commonGridObj_Chequeitem.setRequest("ChequeBooksGridSource.aspx?handle=itemlist&show_page=15&cur_page=1&bookid="+bookID+"&uid="+commonGridObj_Chequeitem.newGuid());
    commonGridObj_Chequeitem.setSourceType("json");
    commonGridObj_Chequeitem.setSkin("xp");
    commonGridObj_Chequeitem.setImageUrl("../images/");
    commonGridObj_Chequeitem.initGrid();
    commonGridObj_Chequeitem.bind();
    
    commonGridObj_Chequeitem.initPageByGID("itemlist","&bookid="+bookID);
    
    commonGridObj_Chequeitem.getGridObj().attachEvent("onEditCell",function(stage,rId,cInd,nValue,oValue){
        if(stage == 0){
            if(cInd != 0){
                return false;
            }else{
                return true;
            }
        }
    });
    commonGridObj_Chequeitem.onEditEvent = function(){
        commonGridObj_Chequebooks.getGridObj().attachEvent("onEditCell",function(stage,rId,cInd,nValue,oValue){
            if(stage == 0){
                if(cInd != 0){
                    return false;
                }else{
                    return true;
                }
            }
        });
    }
}


function createCheque(){
    if(validateChequeNum() == 1){
        if(window.confirm("确定要生成")){
            var _name = $("txt_name").value;
            var _description = $("txt_description").value;
            var _type = $("sel_type").value;
            var _code = $("txt_code").value;
            var _start = $("txt_startnum").value;
            var _end = $("txt_endnum").value;
            var _comp = $("sel_company").value;
            var _total = $("txt_total").value;
            var _creator = $("h_userid").value;
            //
            var _CURRENCY = $("sel_CURRENCY").value;
            //
            
            var postJson = "";
            //begin
            postJson +="{";
            postJson += "[";
            postJson += "\"name\":\""+_name+"\",";
            postJson += "\"desp\":\""+_description+"\",";
            postJson += "\"type\":\""+_type+"\",";
            postJson += "\"code\":\""+_code+"\",";
            postJson += "\"start\":\""+_start+"\",";
            postJson += "\"end\":\""+_end+"\",";
            postJson += "\"comp\":\""+_comp+"\",";
            postJson += "\"total\":\""+_total+"\",";
            postJson += "\"creator\":\""+_creator+"\",";
            postJson += "\"CURRENCY\":\""+_CURRENCY+"\"";
            postJson += "]";
            postJson +="}";
            
            
            var url = "";
            url = "ChequeBooksGridSource.aspx?handle=createCheque&post="+escape(postJson)+"&uid="+commonGridObj_Chequeitem.newGuid();
            var loader = dhtmlxAjax.getSync(url);
	        var cacheTotal = "";
	        var cacheTotalObj = null;
		    var cacheVal = loader.xmlDoc.responseText;

		    if(cacheVal.trim().length > 1)
		    {
		        cacheTotalObj = eval('(' + cacheVal.trim() + ')');		        
		        if(cacheTotalObj.result.length > 0)
		        {
		            createGrid();
		            $("btn_create").disable = 1;
		        }
		    }
        }else{}
    }
}

function cancelCheque()
{

}

function updateCheque()
{
    
}

//校验支票起始和结束值
function validateChequeNum(){
    var result = "";
    var validate = 0;
    var startVal = $("txt_startnum").value;
    var endVal = $("txt_endnum").value;
    var totalVal = $("txt_total").value;
    var status = 0;
    if(startVal != ""){
        if(endVal != ""){
            //异步执行操作
            var url = "ChequeBooksGridSource.aspx?handle=validCheque&startnum="+startVal+"&endnum="+endVal+"&uid="+commonGridObj_Chequeitem.newGuid();
            var loader = dhtmlxAjax.getSync(url);
            var postStatus = loader.xmlDoc.responseText;
            
            status = parseInt(postStatus);
            
            
        }else if(totalVal != ""){
            //异步执行操作
            var url = "ChequeBooksGridSource.aspx?handle=validCheque&startnum="+startVal+"&endnum="+endVal+"&totalnum="+totalVal+"&uid="+commonGridObj_Chequeitem.newGuid();
            var loader = dhtmlxAjax.getSync(url);
            var postStatus = loader.xmlDoc.responseText;
            
            status = parseInt(postStatus);
        }else if(endVal == "" && totalVal == ""){
            validate = -1;
            result = "未填写支票结束号码和支票总数,生成支票时请填写支票结束号码或者支票总数";
        }
    }else{
        validate = -1;
        result = "起始值不能为空";
    }
    
    switch(status){
        case 1:
            result = "";
            validate = 1;
            break;
        case -1:
            result = "支票号码长度错误";
            validate = -1;
            break;
        case -2:
            result = "支票结束号码不能大于等于支票起始号码";
            validate = -1;
            break;
        case -3:
            result = "支票总数与起始结束号码不相符";
            validate = -1;
            break;
        case -4:
            result = "支票起始和结束间值超过支票册最大支票数值";
            validate = -1;
            break;
    }
    
    if(validate < 0){
        alert(result);
    }
    
    return validate;
}

function refreshList()
{
    createGrid();
}