function Select(parent)//构造函数
{
 var pdiv,obj=document.createElement('SELECT');//创建一个控制节点
 if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
 pdiv.appendChild(obj);
 obj.style.position='absolute';
 obj.options.Add=SELE_add;
 obj.options.Clear=SELE_clear;
 obj.oncontextmenu=div_oncontextmenu;
 setStyle(obj);
 return obj;
}
function SELE_clear()
{
  var l=this.length;
  for (var i=0; i<l; i++){
    this.remove(0);
  }
}
function SELE_add(value,index)
{
  var oOption = document.createElement("OPTION");
  this.add(oOption);
  oOption.innerText = value;
  oOption.Value = value;
}
function Button(parent)
{
  var obj=new Input(parent,'button');
  return obj;
}
function Image(parent)
{
  var obj=new Input(parent,'image');
  return obj;
}
function SpeedButton(parent){
  var obj=new Div(parent);
  obj.Text = new Span(obj);
  obj.Text.style.position = 'relative';
  obj.align = "center";
  obj.Text.style.top=3;
  obj.Text.style.cursor = 'pointer';
  obj.style.cursor = 'pointer';
  obj.style.width=75;
  obj.style.height=21;
  obj.onmouseover=SpeedButton_onmouseover;
  obj.onmouseout=SpeedButton_onmouseleave;
  obj.onblur=SpeedButton_onmouseleave;
  obj.SetUp=function(){this.style.backgroundColor=this.Parent.style.backgroundColor;this.style.borderColor=this.style.backgroundColor;}
  obj.style.borderWidth=1;
  obj.style.borderStyle='solid';
  obj.SetFocus=function(){this.style.borderColor=clShadow;this.style.backgroundColor=clButton;}
  obj.SetUp();

  return obj;
}
function SpeedButton_onmouseover(){
	this.SetFocus();
}
function SpeedButton_onmouseleave(){
	this.SetUp();
}

function Submit(parent)
{
  return new Input(parent,'submit');
}
function Reset(parent)
{
  return new Input(parent,'reset');
}
function FileUpload(parent)
{
  return new Input(parent,'file');
}
function CheckBox(parent)
{
  return new Input(parent,'checkbox');
}
function Radio(parent)
{
  return new Input(parent,'radio');
}
function PassWord(parent){
  return new Input(parent,'password');
}
function Edit(parent)
{
  var obj=new Input(parent,'text');
  obj.onmouseover=EDIT_onmouseover;
  return obj;
}
function EDIT_onmouseover(){
  this.focus();
}

function Input(parent,type)//构造函数
{
 var pdiv,obj=document.createElement('input');//创建一个控制节点
 if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
 obj.setAttribute('type',type);
 pdiv.appendChild(obj);
// obj.style.position='absolute';
 obj.Parent=pdiv;
 obj.oncontextmenu=div_oncontextmenu;
 setStyle(obj);
 return obj;
}
function Span(parent)//构造函数
{
 var pdiv,obj=document.createElement('span');//创建一个控制节点
 if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
 pdiv.appendChild(obj);
 obj.oncontextmenu=div_oncontextmenu;
 setFont(obj);
 if(isMoz)
	addEventTimerListener(obj,SPAN_InnerText_onpropertychange);
 return obj;
}
//SPAN兼容INNERTEXT
function SPAN_InnerText_onpropertychange(sender){
	if(sender.oldInnerText!=sender.innerText){
		sender.innerHTML=sender.innerText;
		sender.oldInnerText=sender.innerText;
	}		
}
function Link(parent)//构造函数
{
 var pdiv,obj=document.createElement('a');//创建一个控制节点
 if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
 pdiv.appendChild(obj);
 obj.oncontextmenu=div_oncontextmenu;
 setFont(obj);
 return obj;
}
function TextArea(parent)//构造函数
{
 var pdiv,obj=document.createElement('textarea');//创建一个控制节点
 if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
 pdiv.appendChild(obj);
 obj.oncontextmenu=div_oncontextmenu;
 setFont(obj);
 return obj;
}

function TreeNode()
{
  var pdiv,obj = document.createElement('div');
  obj.style.position='relative';
  obj.style.borderStyle='solid';
  obj.style.borderWidth=0;
//  obj.style.backgroundColor=clBlue;
//  obj.style.backgroundRepeat='repeat-y';
//  obj.style.backgroundImage='url('+jcl_ResourceURL+'treedownline.gif)';
  obj.Recurse=false;
  obj.Expanded=false;
  obj.SelectImage;
  obj.DefaultImage;

  obj.texttable=document.createElement('table');
  obj.appendChild(obj.texttable);
  obj.texttable.style.position='relative';
  obj.texttable.style.backgroundRepeat='no-repeat';
  obj.texttable.style.backgroundImage='url('+jcl_ResourceURL+'treerightline.gif)';
//  obj.texttable.style.backgroundColor=clBlue;

//  obj.texttable.border='1';
  obj.texttable.cellSpacing="0";
  obj.texttable.cellPanding='0';

  obj.headthead = document.createElement('thead');
  obj.texttable.appendChild(obj.headthead);

  obj.row1=document.createElement('tr');
  obj.headthead.appendChild(obj.row1);

  obj.col1=document.createElement('td');
  obj.row1.appendChild(obj.col1);
  obj.col2=document.createElement('td');
  obj.row1.appendChild(obj.col2);
  obj.col3=document.createElement('td');
  obj.row1.appendChild(obj.col3);
  obj.col4=document.createElement('td');
  obj.row1.appendChild(obj.col4);

  obj.nodediv = new Rect(obj.col1);
  obj.nodediv.style.backgroundColor = clWhite;
  obj.nodediv.borderStyle='solid';
  obj.nodediv.style.borderColor=clGray;
  obj.nodediv.style.borderWidth=1;
  obj.nodediv.style.position='relative';
  obj.nodediv.style.width = 11;
//  obj.nodediv.style.top = 4;
  obj.nodediv.style.height = 11;
  obj.nodediv.style.zIndex = 3;
  obj.nodediv.style.visibility='hidden';
  obj.nodediv.treenode=obj;
  obj.nodediv.onmousedown=TreeNode_onmousedown;

  obj.nodediv1=new Div(obj.nodediv);
  obj.nodediv1.style.position='absolute';
  obj.nodediv1.style.height=13;
  obj.nodediv1.style.width=14;
  obj.nodediv1.style.left=1;
  obj.nodediv1.style.top=-2;
  obj.nodediv1.style.fontSize=10;
  obj.nodediv1.style.cursor = 'default';
  obj.nodediv1.innerHTML = '+';

  obj.nodediv1.treenode=obj;
  obj.nodediv1.onmousedown=TreeNode_onmousedown;

  obj.Image=document.createElement('IMG');
  obj.col3.appendChild(obj.Image);
  obj.Image.treenode=obj;

  obj.Text=document.createElement('span');
  obj.col4.appendChild(obj.Text);
  obj.Text.treenode=obj;
  obj.Text.style.fontSize = 12;
  obj.Text.style.cursor = 'default';

  obj.childtable=new Div(obj);
  obj.childtable.style.position='relative';
  obj.childtable.style.left=20;
  obj.childtable.style.backgroundRepeat='no-repeat';
  obj.childtable.style.backgroundImage='url('+jcl_ResourceURL+'treerightline.gif)';
//  obj.childtable.style.backgroundColor=clRed;

  obj.Items=new TreeNodes(obj,obj.childtable);//子控件集
  obj.childdiv= obj.Items;

  obj.treenode=obj;

  if(isIE)
	obj.onpropertychange=TreeNode_onpropertychange
  else
	addEventTimerListener(obj,TreeNode_onpropertychange);

  obj.Drawplus=TreeNode_Drawplus;
  obj.Text.onclick=TreeNode_onclick;
  obj.Text.ondblclick=TreeNode_onmousedown;
  obj.Image.onclick=TreeNode_onclick;
  obj.Image.ondblclick=TreeNode_onmousedown;
  obj.oncontextmenu =TreeNode_oncontextmenu ;
  obj.Focus=TreeNode_Focus;
  obj.Focused=false;
  obj.TreeView;
  obj.Expand=TreeNode_Expand;
  obj.Collapse=TreeNode_Collapse;
  obj.NodePro=new Array();//dengming 2005.8.25
/*		AbsoluteIndex
		Count

		Expanded
		Focused
		HasChildren
		ImageIndex
		Index
		IsVisible
		Item
		Level
		OverlayIndex
		Parent
		Selected
		SelectedIndex
		StateIndex
		Text
		TreeView*/
  return obj;
}
function TreeNode_oncontextmenu (event)
{
	if(!event)event=window.event;
    this.Focus();
	var popupmenu=this.TreeView.PopupMenu;
	if(popupmenu)if(popupmenu.AutoPopup)this.TreeView.PopupMenu.Popup(event.clientX,event.clientY);
    event.returnValue = false;
    event.cancelBubble=true;
}
function TreeNode_onmousedown(event)
{
  if(!event)event=window.event;
  var parent=this.treenode;
  if(event.button<=1){
	if(parent.Expanded==true) parent.Collapse(false);
    else if(parent.Expanded==false) parent.Expand(false);
	if(event.button==0){
	  if(parent.TreeView.Selected){
		var text=parent.TreeView.Selected.Text;
		var caption=text.innerHTML;
		text.innerHTML='';
		text.innerHTML=caption;
      }
	}
  }
  event.cancelBubble=true;
}
function TreeNode_onclick(event)
{
  if(!event)event=window.event;
  this.treenode.Focus();
  event.cancelBubble=true;
}
function TreeNode_Focus()
{
  if(this.TreeView.PopupMenu)this.TreeView.PopupMenu.Hide();
  var treeview=this.TreeView;
  treeview.Selected=this;
}
function TreeNode_Drawplus()
{
  if(this.Items.Item.length>0)this.nodediv.style.visibility='visible';
  else this.nodediv.style.visibility='hidden';
}
function TreeNode_onpropertychange(sender)
{
  var n;
  if(isIE) {
	  n=event.propertyName;
	  sender=this;
  }
  if(isMoz)
	  n='Expanded';
  var treeview=sender.TreeView;
  switch(n){
    case 'Expanded':
	  if(sender.OldExpanded==sender.Expanded)return;
  	  sender.OldExpanded=sender.Expanded;
	  if(sender.Expanded==true){
        if(treeview)if(treeview.OnExpand)treeview.OnExpand(treeview,sender);
		if(sender.Items.Item.length>0){
		  sender.childtable.style.display='block';
		  if(sender.SelectImage)
			sender.Image.src=sender.SelectImage;
		  else
			sender.Image.src=treeview.SelectImage;
		  var lastnode=sender.Items.Item[sender.Items.Item.length-1];
		  lastnode.style.backgroundRepeat='no-repeat';
		  lastnode.style.backgroundImage='url('+jcl_ResourceURL+'treedownshortline.gif)';
		}
		sender.nodediv1.innerHTML='-';
		if(treeview)if(treeview.OnExpanded)treeview.OnExpanded(treeview,sender);
		if(sender.Recurse){
			for(var i=0;i<sender.Items.Item.length;i++){
			  sender.Items.Item[i].Expanded = true;
		    }
		}
  	  }
	  else if(sender.Expanded==false){
		if(sender.Items.Item.length>0){
		  sender.childtable.style.display='none';
		}
		sender.nodediv1.innerHTML='+';
		  if(sender.DefaultImage)
			sender.Image.src=sender.DefaultImage;
		  else
			sender.Image.src=treeview.Image;
		if(treeview)if(treeview.OnCollapsed)treeview.OnCollapsed(treeview,sender);
  	    if(sender.Recurse){
			for(var i=0;i<sender.Items.Item.length;i++){
			  sender.Items.Item[i].Expanded=false;
			}
		}
	  }
//	  treeview.applyElement(treeview);
    break;
  }
}
function TreeNode_Expand(Recurse)//boolean
{
  this.Recurse=Recurse;
  this.Expanded=true;
}
function TreeNode_Collapse(Recurse)//boolean
{
  this.Recurse=Recurse;
  this.Expanded=false;
}
//TreeNodes

function TreeNodes(parent,pdiv)
{
  var obj = new Div(pdiv);
  obj.Owner=parent;
  obj.TreeView=parent.TreeView;

//  obj.style.borderStyle='solid';
//  obj.style.borderWidth=1;
  obj.Level=parent.Level;
  obj.style.position='relative';

  obj.Item=new Array();
  obj.AddChild=TreeNodes_AddChild;
  obj.Clear=TreeNodes_Clear;
  obj.Delete=TreeNodesDelete;
  return obj;
/*  Item
  Count
	Add
		AddChild
		AddChildFirst
		AddChildObject
		AddChildObjectFirst
		AddFirst
		AddObject
		AddObjectFirst
		Assign
		Clear
		Delete
		GetFirstNode
		GetNode
		Insert
		InsertObject*/
}
function TreeNodes_AddChild(node)
{
  if(this.Item.length){
    var lastnode=this.Item[this.Item.length-1];
    lastnode.style.backgroundRepeat='repeat-y';
    lastnode.style.backgroundImage='url('+jcl_ResourceURL+'treedownline.gif)';
  }
  this.appendChild(node);
  this.Item[this.Item.length]=node;
  this.TreeView.ChildNodes[this.TreeView.ChildNodes.length]=node;
  node.Index=this.Item.length-1;
  if(this.Owner!=this.TreeView) node.Parent=this.Owner;
  node.Owner=this;
  node.Level=this.Level+1;
  node.Items.Level=node.Level;
  if(this.Owner.Drawplus)this.Owner.Drawplus();
  node.TreeView=this.TreeView;
  node.Items.TreeView=this.TreeView;
  if(node.DefaultImage)
    node.Image.src=node.DefaultImage;
  else
	node.Image.src= this.TreeView.Image;
  var bgcolor=node.TreeView.style.backgroundColor;
  if(!bgcolor)bgcolor=clWhite;
  node.style.borderColor=bgcolor;
  if(node.Text.innerText){
	  var w=parseInt(node.Text.style.fontSize);
	  node.style.width = node.Text.innerText.length*w+50;
  }
  node.style.backgroundRepeat='no-repeat';
  node.style.backgroundImage='url('+jcl_ResourceURL+'treedownshortline.gif)';
//  this.TreeView.applyElement(this.TreeView);
  return node;
}

function TreeNodesDelete(node)
{
  _arrayremove(this.Item,node.Index);
  _arrayremove(this.TreeView.ChildNodes,node.Index);
  for(var i=0;i<this.Item.length;i++){
	  this.Item[i].Index=i;
  }
  this.removeChild(node);
  node.removeNode(true);
}
function TreeNodes_Clear()
{
   var count=this.Item.length;
   var node;
   var childs=this.TreeView.ChildNodes;
   for(var i=0;i<count;i++){
	 node = this.Item[i];
	 for(var j=0;j<childs.length;j++){
		 if(childs[j]==node){
			 _arrayremove(childs,j);
		 }
	 }
     this.removeChild(node);
     node.removeNode(true);
   }
   this.Item.length=0;
}

function TreeView(parent)
{
  var pdiv,obj = document.createElement('div');
  if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
  pdiv.appendChild(obj);

//  obj.style.borderStyle='solid';
//  obj.style.borderWidth=1;

  if(name){var s='window.'+name+'=obj';eval(s);};
  obj.style.position='absolute';
  obj.style.overflow='auto';
  obj.style.width = 200;
  obj.style.height = 300;
  obj.Level=0;
  obj.TreeView=obj;

  obj.Items=new TreeNodes(obj,obj);//子控件集
  obj.ChildNodes=new Array();
  obj.appendChild(obj.Items);
  obj.OnChange;
  obj.SelectImage=jcl_ResourceURL+'openfoldericon.gif';;
  obj.Image=jcl_ResourceURL+'foldericon.gif';
  obj.PopupMenu;
//  obj.Refresh=TreeView_refresh;
  obj.FullExpand=TreeViewFullExpand;
  obj.FullCollapse=TreeViewFullCollapse;
  if(isIE)
	obj.onpropertychange=TreeView_onpropertychange
  else
	addEventTimerListener(obj,TreeView_onpropertychange);
  setStyle(obj);
  return obj;
/*AutoExpand
		BorderStyle
		Canvas
		ChangeDelay
		DropTarget
		HideSelection
		HotTrack
		Images
		Indent
		Items
		MultiSelect
		MultiSelectStyle
		ReadOnly
		RightClickSelect

RowSelect
		Selected
		SelectionCount
		Selections
		ShowButtons
		ShowLines
		ShowRoot
		SortType
		StateImages
		ToolTips
		TopItem*/
}
function TreeView_onpropertychange(sender)
{
  var n;
  if(isIE) {
	  n=event.propertyName;
	  sender=this;
  }
  if(isMoz)
	  n='Selected';
  switch(n){
    case 'Selected':
      if(sender.OldSelected==sender.Selected)return;
	  sender.Selected.Focused=true;
	  if(sender.OldSelected){
		sender.OldSelected.Text.style.backgroundColor=clWhite;
		sender.OldSelected.Text.style.color=clBlack;
		if(sender.OnDeletion)sender.OnDeletion(sender,sender.OldSelected);
	  }
      if(sender.Selected!=null){
    	sender.Selected.Text.style.backgroundColor = clMaroon;
		sender.Selected.Text.style.color = clWhite;
		if(sender.OnChange)sender.OnChange(sender,sender.Selected);
      }
      sender.OldSelected=sender.Selected;
    break;
  }
}

function TreeViewFullExpand()
{
  for(var i=0;i<this.ChildNodes.length;i++){
    this.ChildNodes[i].Expanded = true;
  }
}
function TreeViewFullCollapse()
{
  for(var i=0;i<this.ChildNodes.length;i++){
    this.ChildNodes[i].Expanded = false;
  }
}
function MenuItem(name)
{
  var pdiv,obj = document.createElement('div');
  obj.table=new Table(obj);
  obj.table.border=0;
  obj.row=obj.table.AppendRow();
  obj.cell1=obj.table.AppendCell(obj.row);
  obj.cell1.innerHTML='&nbsp;';
  obj.cell2=obj.table.AppendCell(obj.row);
  obj.cell3=obj.table.AppendCell(obj.row);
//  obj.table.border='1';
  obj.Text=document.createElement('span');
  obj.Text.style.fontSize='10pt';
  obj.Text.style.cursor = 'default';
  obj.cell2.appendChild(obj.Text);
  obj.style.width = '100%';

  obj.SubMenu;

  obj.onclick=MenuItem_onclick;
  obj.OnClick;
  obj.onmouseover =MenuItem_onmouseover;
  setFont(obj);
  return obj;
}
function MenuItem_onclick(event)
{
  if(!event)event=window.event;
  var menu=this.Parent;
  if(this.OnClick){this.OnClick(this,event);menu.Hide();}
  if(this.SubMenu){
	var x= menu.style.posLeft+menu.style.posWidth-20;
	if(this.SubMenu.style.visibility=='hidden')this.SubMenu.Popup(x,event.clientY);
  }
  event.cancelBubble=true;
}
function MenuItem_onmouseover(event)
{
  if(!event)event=window.event;
  var menu=this.Parent;
  menu.style.visibility='visible';
  if(menu.Selected){
    if(menu.Selected!=this){
      menu.Selected.style.backgroundColor=clDefault;
      menu.Selected.Text.style.color=clBlack;
      if(menu.Selected.SubMenu){
    	  menu.Selected.SubMenu.Hide();
      }
    }
  }
  this.style.backgroundColor = clMaroon;
  this.Text.style.color = clWhite;
  menu.Selected=this;
  event.cancelBubble=true;
}
function PopupMenu(name)
{
  var pdiv,obj = document.createElement('div');
  pdiv=document.body;//获取父块
  pdiv.appendChild(obj);
  obj.Parent=pdiv;

  obj.style.borderStyle='outset';
  obj.style.borderWidth=1;
  obj.style.backgroundColor = clDefault;
  obj.style.zIndex=10;

  obj.style.position='absolute';
  obj.style.visibility='hidden';
  obj.style.width = 120;
//  obj.style.height = 150;

  obj.Items=new Items(obj);//子控件集
  obj.AutoPopup=true;

  obj.Popup=PopupMenu_Popup;
  obj.Hide=PopupMenu_Hide;
  obj.onmouseout=PopupMenu_onmouseleave;
  setFont(obj);
  return obj;
}
function PopupMenu_Popup(x,y)
{
  this.style.top=y;
  this.style.left=x;
  this.style.visibility='visible';
  if(this.OnPopup)this.OnPopup(this);
}
function PopupMenu_Hide()
{
  this.style.visibility='hidden';
//  this.Selected=null;
  var submenu;
  for(var i=0;i<this.Items.Item.length;i++){
    submenu=this.Items.Item[i].SubMenu;
	if(submenu)submenu.Hide();
  }
}
function PopupMenu_onmouseleave(event)
{
  if(!event)event=window.event;
  var hassub=false;
  for(var i=0;i<this.Items.Item.length;i++){
    if(this.Items.Item[i].SubMenu)
      if(this.Items.Item[i].SubMenu.style.visibility=='visible'){hassub=true;break;}
  }
  if(hassub==false){
	  var rect=new Rectangle(this.offsetLeft,this.offsetTop,this.offsetWidth,this.offsetHeight);
	  if(!rect.isContainXY(event.x,event.y))this.Hide();
  }
  if(this.Selected){
    this.Selected.style.backgroundColor=clDefault;
    this.Selected.Text.style.color=clBlack;
  }
  event.cancelBubble=true;
}

function Table(parent)
{
  var pdiv,obj=document.createElement('table');
  if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
  pdiv.appendChild(obj);
  if(name){var s='window.'+name+'=obj';eval(s);};
  obj.border=1;
  obj.cellSpacing=0;
  obj.cellPanding=0;
  obj.thead = document.createElement('thead');
  obj.appendChild(obj.thead);
  obj.tbody = document.createElement('tbody');
  obj.appendChild(obj.tbody);
  obj.AppendRow=Table_AppendRow;
  obj.AppendCell=Table_AppendCell;
  return obj;
}
function Table_AppendRow(obj)
{
  var row=new TR();
  if(!obj)
	obj = this.tbody;
  obj.appendChild(row);
  return row;
}
function Table_AppendCell(row)
{
  var cell = new TD();
  row.appendChild(cell);
  return cell;
}
function TR(){
  var row=document.createElement('tr');
  return row;
}
function TD(){
  var cell=document.createElement('td');
  return cell;
}
function Rect(parent)
{
  var obj=new Div(parent);
  obj.style.borderStyle='solid';
  obj.style.borderWidth=1;
//  obj.style.position='absolute';
  return obj;
}
function Div(parent)
{
 var pdiv,obj=document.createElement('Div');//创建一个控制节点
 if(parent){pdiv=parent;}else pdiv=document.body;//获取父块
 pdiv.appendChild(obj);
 obj.Parent=pdiv;

 obj.oncontextmenu=div_oncontextmenu;
 setFont(obj);
 return obj;
}
function div_oncontextmenu(event)
{
  if(!event)event=window.event;
	var popupmenu=this.PopupMenu;
	if(popupmenu)if(popupmenu.AutoPopup)this.PopupMenu.Popup(event.clientX,event.clientY);
    event.returnValue = false;
    event.cancelBubble=true;
}
