//ORGTree.prototype.ORGTreeQuery=null;
//
ORGTree.prototype.ORGT_Name=null;
ORGTree.prototype.ORGT_DataSet=null;//数据源
ORGTree.prototype.ORGT_TreeView=null;//
ORGTree.prototype.ORGT_GetUserInfo=null;
ORGTree.prototype.ORGT_GetOrgizeNum=null;
ORGTree.prototype.ORGT_Grid;//网格对象

function ORGTree(parent)//树型控件的构造函数
{	 
  var pdiv;if(parent){pdiv=parent;}else pdiv=document.body;//获取父块 
  var obj=new Div(pdiv);
//  if(name){var s='window.'+name+'=obj';eval(s);};  
  
  obj.ORGT_Name=name;
  obj.style.overflow = 'auto';
  obj.ORGT_InitDataSet=OT_InitDataSet;    
  obj.ORGT_InitTree=OT_InitTreeView;
  obj.ORGT_InitTreeNode=OT_InitTreeNode;
  obj.ORGT_InitTreeMenu=OT_InitTreeMenu;
  obj.ORGT_DatasetToTree=OT_DatasetToTree;  
  obj.ORGT_GetUserInfo=OT_GetUserInfo;
  obj.ORGT_Travel=OT_Travel;
  obj.ORGT_OrgizeUser=OT_OrgizeUser;
  obj.ORGT_GetOrgizeNum=OT_GetOrgizeNum;
  obj.ORGT_EmployeeNum=OT_EmployeeNum;
  //
  obj.ORGT_InitTree(); 
  obj.ORGT_InitTreeMenu(); 
  obj.ORGT_UserInfo=null;
  obj.ORGT_OrgizeInfo=null;//机构编号
  obj.ORGT_UserEmployeeNum=null;//用户编号
  obj.ORGT_DefaultValue=null;//
  obj.ORGT_GetTreeGrade=null;//返回树节点的等级
  obj.ORGT_SetIsVisibleGrid=true;//是否显示Grid
  obj.ORGT_OrgizeName;//机构名称
  obj.ORGT_OrgizeID;//机构ID
  //网格对象
  obj.ORGT_Grid=null;
  return obj;
}
//获取数据源
function OT_InitDataSet(params,dao){  	  
  this.ORGT_DataSet=new DataSet();
  this.ORGT_DataSet.DataObject=dao;
  this.ORGT_DataSet.Params.Clear();
  if(dao=='Query_Orgize') 
    this.ORGT_DataSet.Params.Add(new Param('ORGANGRADE',params,'String'));      
  else
    this.ORGT_DataSet.Params.Add(new Param('ORGANID',params,'String'));      

  this.ORGT_DataSet.MaxRows=-1;
  this.ORGT_DataSet.AutoControl = false;
  this.ORGT_DataSet.Open();
}

//初始化结构树
function OT_InitTreeView(){
  this.ORGT_TreeView=new TreeView();
  this.ORGT_TreeView.parent=this;
  this.ORGT_TreeView.style.backgroundColor = clNone;
  this.ORGT_InitTreeNode();
  this.ORGT_TreeView.OnChange=function(sender,node){
    this.parent.ORGT_UserInfo=OT_OrgizeUser(node.NodePro[0]);//用户姓名
    this.parent.ORGT_OrgizeID=node.NodePro[0];//机构ID
    this.parent.ORGT_OrgizeInfo=node.NodePro[1];    //机构编号
    this.parent.ORGT_UserEmployeeNum=OT_EmployeeNum(node.NodePro[0]);
    this.parent.ORGT_GetTreeGrade=node.ORGT_DefaultValue;//树节点的等级
    this.parent.ORGT_OrgizeName=node.Text.innerHTML;
    //数据    
    if(this.parent.ORGT_SetIsVisibleGrid){
      if(this.parent.ORGT_Grid.Grid_RowCount!=0) this.parent.ORGT_Grid.Grid_ClearRows();
      for(var i=0;i<this.parent.ORGT_UserInfo.length;i++){
		var record=new Array();
		record[0]='';
        record[1]=this.parent.ORGT_UserEmployeeNum[i];//node.NodePro[1];
        record[2]=this.parent.ORGT_UserInfo[i];
		this.parent.ORGT_Grid.Grid_Insert(record,false);     
      }      
      this.parent.ORGT_Grid.Grid_ChangeSelRecord();
    }
  };
}

//初始化节点
function OT_InitTreeNode(){
  //设置根节点
  var gradeArray=new Array();//机构等级
  gradeArray[0]='03';
  gradeArray[1]='04';
  gradeArray[2]='05';

  //生成根节点
  
  this.ORGT_InitDataSet('01','Query_Orgize');  
  var rootNode =null;   
  for(var i=0;i<this.ORGT_DataSet.RecordCount;i++){      
    rootNode=new TreeNode();	
    rootNode.ORGT_DefaultValue='01';//等级
    this.ORGT_DatasetToTree(rootNode);
    this.ORGT_TreeView.Items.AddChild(rootNode);
    this.ORGT_DataSet.Next();	
  }   
  //生成二级节点
  this.ORGT_InitDataSet('02','Query_Orgize');     
   for(var i=0;i<this.ORGT_DataSet.RecordCount;i++){     
    var childNode=new TreeNode();
    childNode.ORGT_DefaultValue='02';
    this.ORGT_DatasetToTree(childNode);
    rootNode.Items.AddChild(childNode);
    this.ORGT_DataSet.Next();	
  }
  //
  var start=2;var end=4;    
  for(var i=0;i<gradeArray.length;i++){
    var nNodeCount=this.ORGT_TreeView.ChildNodes.length; //节点总数    
    var childNodes=this.ORGT_TreeView.ChildNodes;//节点集      
    this.ORGT_InitDataSet(gradeArray[i],'Query_Orgize'); 
 
    for(var j=0;j<this.ORGT_DataSet.RecordCount;j++){ 
      var orgnum=this.ORGT_DataSet.Fields.Field["ORGANNUM"].Value;//机构编号    
      this.ORGT_Travel(childNodes,nNodeCount,orgnum,start,end,gradeArray[i]);
      this.ORGT_DataSet.Next();	
    }  
    end=end+3;
  }
}
//遍历机构树
function OT_Travel(nodes,count,orgnum,start,end,grade){
  var dept=orgnum.substring(start,end);
  for(var i=0;i<count;i++){
    var node=nodes[i];  	
    var num=node.NodePro[1];//机构编号 
    var dept1=num.substring(start,end);
    if(dept1==dept){
      var childNode=new TreeNode();		
      childNode.ORGT_DefaultValue=grade;	    
      node.Items.AddChild(childNode);	 
      this.ORGT_DatasetToTree(childNode);
      break;  	
    }    
  }
}
//树节点赋值
function OT_DatasetToTree(node){
  node.NodePro[0]=this.ORGT_DataSet.Fields.Field["ORGANID"].Value;//机构ID
  node.NodePro[1]=this.ORGT_DataSet.Fields.Field["ORGANNUM"].Value;//机构编号	
  node.Text.innerHTML=this.ORGT_DataSet.Fields.Field["ORGANNAME"].Value;  
}
//建立二维阵列
function Array2D(x,y){
  var arr=new Array(x);
  for(var i=0;i<x;i++) arr[i]=new Array(y);
  return arr;
}
//获取机构下所有用户信息
function OT_OrgizeUser(organid){
  OT_InitDataSet(organid,'Query_OrgizeUser'); 
  var userInfo=new Array();
  for(var i=0;i<this.ORGT_DataSet.RecordCount;i++){     	 	
    userInfo[i]=this.ORGT_DataSet.Fields.Field["NAME"].Value;//    
    this.ORGT_DataSet.Next();
  }
  return userInfo;
}
//
function OT_GetUserInfo(){
  return this.ORGT_UserInfo;
}
//
function OT_GetOrgizeNum(){
  return this.ORGT_OrgizeInfo;
}
//
function OT_EmployeeNum(organid){
  OT_InitDataSet(organid,'Query_OrgizeUser'); 	
  var userNum=new Array();
  for(var i=0;i<this.ORGT_DataSet.RecordCount;i++){     	 	
    userNum[i]=this.ORGT_DataSet.Fields.Field["EMPLOYEENUM"].Value;//    
    this.ORGT_DataSet.Next();
  }
  return userNum;
}
//初始化Tree的菜单
function OT_InitTreeMenu(){
  var ORGT_Menu=new PopupMenu();

  var ORGT_Expand_Stair_MenuItem=new MenuItem();
  ORGT_Menu.Items.Add(ORGT_Expand_Stair_MenuItem);
  ORGT_Expand_Stair_MenuItem.Text.innerHTML='展开下一级';
  ORGT_Expand_Stair_MenuItem.OnClick=function(sender,event){this.ORGT_TreeView.Selected.Expand(false);};

  var ORGT_Expand_All_MenuItem=new MenuItem();
  ORGT_Menu.Items.Add(ORGT_Expand_All_MenuItem);
  ORGT_Expand_All_MenuItem.Text.innerHTML='下级全部展开';
  ORGT_Expand_All_MenuItem.OnClick=function(sender,event){this.ORGT_TreeView.Selected.Expand(true);};

  var ORGT_Shrink_Stair_MenuItem=new MenuItem();
  ORGT_Menu.Items.Add(ORGT_Shrink_Stair_MenuItem);
  ORGT_Shrink_Stair_MenuItem.Text.innerHTML='收缩下一级';
  ORGT_Shrink_Stair_MenuItem.OnClick=function(sender,event){this.ORGT_TreeView.Selected.Collapse(false);};

  var ORGT_Shrink_All_MenuItem=new MenuItem();
  ORGT_Menu.Items.Add(ORGT_Shrink_All_MenuItem);
  ORGT_Shrink_All_MenuItem.Text.innerHTML='收缩下级所有';
  ORGT_Shrink_All_MenuItem.OnClick=function(sender,event){this.ORGT_TreeView.Selected.Collapse(true);};

  this.ORGT_TreeView.PopupMenu=ORGT_Menu;
  this.ORGT_TreeView.FullCollapse(true); 
}