var datatableObj=null;
var AllString=new StringBuffer();
var PGroup="";
var ReadCountGroup="";
var TopNodeIDGroup="";
var ParentIDGroup="";
var UserTime="";
var TopNodeID=0;
var PubOtherAtt="";
//获取树的生成字符串
function GetTree(txtSql,tableName,_TopNodeID,OtherAtt)
{
    var returnValue="";
    try
    {
    //    UserTime+="查询数据使用时间：\r\n开始时间："+(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString();
        datatableObj=DBReader.selectSql(txtSql,tableName);
    //     UserTime+="\r\n"+"结束时间："+(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString();
         
    //     alert(UserTime);
        if(_TopNodeID!=undefined)
        {
            TopNodeID=_TopNodeID;
        }
       
	    if(DBReader.DObjectIsNull(datatableObj)==false && parseInt(datatableObj.RecordCount)>0)
	    {
            datatableObj.MoveFirst();
            //获取顶级ID集合
        //    UserTime+="获取上级节点集合使用时间：\r\n开始时间："+(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString();
             getTopIDGroup();
        //     UserTime+="\r\n"+"结束时间："+(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString();
        //     alert(UserTime);
        //    alert(TopNodeIDGroup);
            AllString.clear();
            AllString.appendLine("[");
            var RowCount=datatableObj.RecordCount;
            PubOtherAtt=OtherAtt;
            
            var arrTopNodeID=TopNodeIDGroup.split(',');
            TopNodeIDGroup="";
            for(x=0;x<arrTopNodeID.length;x++)
            {
                if(AllString.toString()!="[")
                {
                    AllString.appendLine(",");
                }
                var strTopNodeID=arrTopNodeID[x];
                datatableObj.Filter=" ParentID="+strTopNodeID;
                var FNodeList=datatableObj;
                if(FNodeList.RecordCount>0)
                {
                    datatableObj.Filter=" ID="+strTopNodeID;
                    var FParentNodeList=datatableObj;
                    var IsParentNodeHaved=false;
                    var ParentNodeValue="";
                    if(FParentNodeList.RecordCount>0)
                    {
                        IsParentNodeHaved=true;
                        FParentNodeList.MoveFirst();
                        var IDValue=FParentNodeList.Fields("ID").Value;
                        var NameValue=FParentNodeList.Fields("NAME").Value;
                        var ParentIDValue=0;
                        var EndFlag=0;//2010-11-17添加
                        try{EndFlag=FParentNodeList.Fields("EndFlag").Value;}catch(err){};
                        var strOthArr="";
                        if(PubOtherAtt!=undefined && PubOtherAtt!="")
                        {
                            var tempOtherAtt=PubOtherAtt.replace(/@Data@/g,'FParentNodeList');
                            try{strOthArr=eval(tempOtherAtt);}catch(err){};
                        }
                        NodeValue='{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":'; 
                        AllString.appendLine(NodeValue);
                        AllString.appendLine("[");
                    }
                    datatableObj.Filter=" ParentID="+strTopNodeID;
                    FNodeList=datatableObj;
                    var FNodeCount=FNodeList.RecordCount;
                    if(FNodeCount>0)
                    {
                        FNodeList.MoveFirst();
                        var ReadCount=0;
                        for(i=0;i<FNodeCount;i++)
                        {
                            var IDValue=FNodeList.Fields("ID").Value;
                            var NameValue=FNodeList.Fields("NAME").Value;
                            var ParentIDValue=FNodeList.Fields("ParentID").Value;
                            
                            var EndFlag=0;//2010-11-17添加
                            try{EndFlag=FNodeList.Fields("EndFlag").Value;}catch(err){};
                            
                            var strOthArr="";
                            if(PubOtherAtt!=undefined && PubOtherAtt!="")
                            {
                                var tempOtherAtt=PubOtherAtt.replace(/@Data@/g,'FNodeList');
                                try{strOthArr=eval(tempOtherAtt);}catch(err){};
                            }
                            ReadCount=i;
                            datatableObj.Filter=" ParentID= "+IDValue;
                            var ChileNodeItemList=datatableObj;
                            
                            var NodeValue="";
                            if(ChileNodeItemList.RecordCount==0)
                            {
                                if(i==0)
                                {
                                    NodeValue='{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":null}'; 
                                }
                                else
                                {
                                    NodeValue=',{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":null}';
                                }
                                AllString.appendLine(NodeValue);
                            }
                            else
                            {
                                if(i==0)
                                {
                                    NodeValue='{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":'; 
                                }
                                else
                                {
                                    NodeValue=',{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":';
                                }
                                AllString.appendLine(NodeValue);
                                AllString.appendLine("[");
                                ReadCountGroup="";
                                PGroup="";
                                GetChildValue(IDValue);
                                AllString.appendLine("]");
                                AllString.appendLine("}");
                            }
                            
                            datatableObj.Filter=" ParentID="+strTopNodeID;
                            FNodeList=datatableObj;
                            i=ReadCount;
                            if(i>0)
                            {
                                for(tt=0;tt<parseInt(i)+1;tt++)
                                {
                                    FNodeList.MoveNext();
                                }
                            }
                            else
                            {
                                FNodeList.MoveNext();
                            }
                        }
                    }
                    if(IsParentNodeHaved==true)
                    {
                        AllString.appendLine("]");
                        AllString.appendLine("}");
                    }
                    //}
                }
                else
                {
                    datatableObj.Filter=" ID="+strTopNodeID;
                    var FNodeList=datatableObj;
                    if(FNodeList.RecordCount>0)
                    {
                        FNodeList.MoveFirst();
                    
                        var IDValue=FNodeList.Fields("ID").Value;
                        var NameValue=FNodeList.Fields("NAME").Value;
                        var ParentIDValue=0;
                        var EndFlag=0;//2010-11-17添加
                        try{EndFlag=FNodeList.Fields("EndFlag").Value;}catch(err){};
                        var strOthArr="";
                        if(PubOtherAtt!=undefined && PubOtherAtt!="")
                        {
                            var tempOtherAtt=PubOtherAtt.replace(/@Data@/g,'FNodeList');
                            try{strOthArr=eval(tempOtherAtt);}catch(err){};
                        }
                        var ParentNodeValue="";
                        ParentNodeValue='{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":null}';             
                        AllString.appendLine(ParentNodeValue);
                        FNodeList.MoveNext();
                    }
                }
            }
            DBReader.Dispose(datatableObj);
            
            AllString.appendLine("]");
            if(AllString.toString()=="[]")
            {
                returnValue="";
            }
            returnValue=AllString.toString();
            AllString.clear();
       }
//        alert(returnValue);
//        window.clipboardData.setData('Text',returnValue);
    }
    catch(e)
    {
        alert(e.message);
        DBReader.Dispose(datatableObj);
    }
    return returnValue;
}
//递归绑定下级树节点
var inum=0;
function GetChildValue(PID)
{
    try{
        inum++;
        if(PID!="")
        {
            if(PGroup=="")
            {
                PGroup+=PID;
            }
            else
            {
                PGroup+=","+PID;
            }
            datatableObj.Filter=" ParentID= "+PID;
            var ChileNodeList=datatableObj;
            var ReadChildCount=0;
            var ChildLen=ChileNodeList.RecordCount;
            
            var strOthArr="";
            if(PubOtherAtt!=undefined && PubOtherAtt!="")
            {
                strOthArr=PubOtherAtt;
            }
            for(m=0;m<ChildLen;m++)
            {
                var IDValue=ChileNodeList.Fields("ID").Value;
                var NameValue=ChileNodeList.Fields("NAME").Value;
                var EndFlag=0;//2010-11-17添加
                try{EndFlag=ChileNodeList.Fields("EndFlag").Value;}catch(err){}
                var strOthArr="";
                if(PubOtherAtt!=undefined && PubOtherAtt!="")
                {
                    var tempOtherAtt=PubOtherAtt.replace(/@Data@/g,'ChileNodeList');
                    try{strOthArr=eval(tempOtherAtt);}catch(err){};
                }
                
                ReadChildCount=m;
                datatableObj.Filter=" ParentID= "+IDValue;
                var ChileNodeItemList=datatableObj;
                
                var NodeValue="";
                if(ChileNodeItemList.RecordCount==0)
                {
                    if(m==0)
                    {
                        NodeValue='{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":false,"isexpand":true,"complete":true,"ChildNodes":null}'; 
                    }
                    else
                    {
                        NodeValue=',{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":false,"isexpand":true,"complete":true,"ChildNodes":null}';
                    }
                    AllString.appendLine(NodeValue);
                }
                else
                {                    
                    if(m==0)
                    {
                        NodeValue='{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":'; 
                    }
                    else
                    {
                        NodeValue=',{"id":"'+IDValue+'","text":"'+NameValue+'",'+strOthArr+'"EndFlag":"'+EndFlag+'","hasChildren":true,"isexpand":true,"complete":true,"ChildNodes":';
                    }
                    AllString.appendLine(NodeValue);
                    
                    AllString.appendLine("[");
                    GetChildValue(IDValue);
                    AllString.appendLine("]");
                    AllString.appendLine("}");
                    
                }
                try{
                    var arrPG=PGroup.split(',');
                    
                    PID=arrPG[arrPG.length-1];
                    if(PID=="")
                    {
                        PID=arrPG[0];
                    }
                    if(PID!="")
                    {
                        datatableObj.Filter=" ParentID= "+PID;
                        ChileNodeList=datatableObj;
                        
                        m=ReadChildCount;
                        if(m>0)
                        {
                            for(tt=0;tt<parseInt(m)+1;tt++)
                            {
                                ChileNodeList.MoveNext();
                            }
                        }
                        else
                        {
                            ChileNodeList.MoveNext();
                        }
                    }
                }
                catch(e)
                {
//                    alert(arrPG);
                    alert(e.message);
                }
            }
//            PGroup=PGroup.substring(0,PGroup.length-2);

            var tempArr=Array();
            tempArr=PGroup.split(',');
            tempArr[tempArr.length-1]=null;
            tempArr.length=tempArr.length-1;
            
            PGroup=tempArr.join(',');
        }
    }
    catch(e)
    {
//        alert(inum);
        alert(e.message);
    }
}
//获取顶级节点ID集合
function getTopIDGroup()
{
    if(TopNodeID==0)
    {
        ParentIDGroup="0";
    }
    else
    {
        getParentIDGroup();
    }
    var arrParentID=ParentIDGroup.split(',');
    if(TopNodeID!=0 && arrParentID.length>0)
    {
    arrParentID=arrParentID.sort( function(x, y) { 
return (x[0]==y[0])?(y.localeCompare(x)):(y[0].localeCompare(x[0])) 
}); 
    }
    ParentIDGroup="";
    for(x=0;x<arrParentID.length;x++)
    {
        var strParentID=arrParentID[x];
        if(TopNodeID!=0)
        {
            datatableObj.Filter=" ID="+strParentID;
            if(datatableObj.RecordCount==0 || strParentID<=0)
            {
                if(TopNodeIDGroup=="")
                {
                    TopNodeIDGroup+=strParentID;
                }
                else
                {
                    TopNodeIDGroup+=","+strParentID;
                }
            }
        }
        else
        {
            if(TopNodeIDGroup=="")
            {
                TopNodeIDGroup+=strParentID;
            }
            else
            {
                TopNodeIDGroup+=","+strParentID;
            }
        }
        datatableObj.Filter="";
    }
}
//获取所有父级节点ID集合
function getParentIDGroup()
{
    var RowCount=datatableObj.RecordCount;
    var SelectedIDGroup="";
    var DonotSelectedIDGroup="";
    var execCount=0;
    if(TopNodeID==0)
    {
        datatableObj.Filter="ParentID=NULL OR ParentID=0";
        var NullParentCount=datatableObj.RecordCount;
        //先找到ParentID是null或0的节点为顶级，如果找不到再找ParentID是大于0的
        for(m=0;m<NullParentCount;m++)
        {
            if(ParentIDGroup=="")
            {
                ParentIDGroup+=datatableObj.Fields("ID").Value;
            }
            else
            {
                ParentIDGroup+=","+datatableObj.Fields("ID").Value;
            }
            datatableObj.MoveNext();
            execCount++;
        }
    }
    else
    {
        datatableObj.Filter="";//ParentID="+TopNodeID;
    }
    if(ParentIDGroup=="")
    {
        SelectedIDGroup="";
        datatableObj.Filter="";
        if(datatableObj.RecordCount>0)
        {
            datatableObj.MoveFirst();
        }
        for(m=0;m<RowCount;m++)
        {
            var TempParentID=datatableObj.Fields("ParentID").Value;
            if(parseInt(TempParentID)<0)
            {
                var NewParentID=datatableObj.Fields("ID").Value;
                if(SelectedIDGroup.indexOf("["+NewParentID+"]")==-1)
                {
                    if(SelectedIDGroup=="")
                    {
                        SelectedIDGroup+="["+NewParentID+"]";
                    }
                    else
                    {
                        SelectedIDGroup+=","+"["+NewParentID+"]";
                    }
                    if(ParentIDGroup=="")
                    {
                        ParentIDGroup+=NewParentID;
                    }
                    else
                    {
                        ParentIDGroup+=","+NewParentID;
                    }
                }
                datatableObj.MoveNext();
            }
            else
            {
                if(SelectedIDGroup.indexOf("["+TempParentID+"]")==-1)
                {
                    if(SelectedIDGroup=="")
                    {
                        SelectedIDGroup+="["+TempParentID+"]";
                    }
                    else
                    {
                        SelectedIDGroup+=","+"["+TempParentID+"]";
                    }
                    if(ParentIDGroup=="")
                    {
                        ParentIDGroup+=TempParentID;
                    }
                    else
                    {
                        ParentIDGroup+=","+TempParentID;
                    }
                    if(DonotSelectedIDGroup=="")
                    {
                        DonotSelectedIDGroup+=" ParentID<>"+TempParentID +" AND ID<>"+TempParentID;
                    }
                    else
                    {
                        DonotSelectedIDGroup+=" AND ParentID<>"+TempParentID+" AND ID<>"+TempParentID;
                    }
                    datatableObj.Filter=DonotSelectedIDGroup;
                    RowCount=datatableObj.RecordCount;
                    if(RowCount>0)
                    {
                        m=0;
                        datatableObj.MoveFirst();
                    }
                    else
                    {
                        datatableObj.Filter="";
                        RowCount=datatableObj.RecordCount;
                        datatableObj.MoveNext();
                    }
                }
                else
                {
                    datatableObj.MoveNext();
                }
            }
            execCount++;
        }
    }
//    alert(execCount);
    datatableObj.Filter="";
}