/*设备编码组件,根据系统参数生成
*使用GetString方法可以得到设备编码(不足的填?,用于模糊查询或校验输入合法性)
*使用Edits.Item[i]可以得到每一个编辑框,Edits是Items类型
*使用Open方法打开数据
*Active属性得到是否处于打开状态
*/
function QueryEquipmentClass(name,parent)
{
  this.sender = new Div(name,parent);
  var obj=this.sender;
  obj.style.position='absolute';
  obj.style.backgroundColor=clBlue0;
  obj.LevelNames=new Strings();
  obj.Table=new Table(null,obj);
  obj.Edits=new Items();
  setFont(obj.Table);
  obj.Open=QEPCLASS_init;
  obj.getlevelnames=QEPCLASS_getlevelnames;
  obj.GetString=QEPCLASS_getstring;//得到选择的编码
  obj.Validate=QEPCLASS_validate;//校验输入合法性
  obj.Active=false;
//  obj.init();
  return obj;
}
//初始化,从参数表中读取数据,并创建界面对象
function QEPCLASS_init(){
  this.Active=true;
  this.getlevelnames();//得到层级分段信息
  var query=new Query();
  query.OpenUrl='/eam?classname=eqclass_bas_levels&operate=open';
  query.MaxRows=-1;
  query.Open();
  this.Edits.Clear();
  var cell,treeview,title,button,cell1,cell2,cell3,edit;
  var headrow1=new TR();
  this.Table.tHead.appendChild(headrow1);

  var bodyrow1=new TR();
  this.Table.tbody.appendChild(bodyrow1);
  var bodyrow2=new TR();
  this.Table.tbody.appendChild(bodyrow2);
  var bodyrow3=new TR();
  this.Table.tbody.appendChild(bodyrow3);
  var w,k=0;
  for(var i=0;i<this.LevelNames.Count;i++){
	cell = new TD();
	cell.ItemNames=new Strings();//位名称
	cell.ItemLengths=new Strings();//位长
	cell.ItemSeps=new Strings();//位分隔符
	cell.Items=new Items();
	query.First();
	while(!query.Eof){
		if(query.Locate('CATEGORY',this.LevelNames.Strings[i])){
			cell.ItemNames.Add(query.Fields.Field['LEVELNAME'].Value);
			cell.ItemLengths.Add(query.Fields.Field['LENGTH'].Value);
			cell.ItemSeps.Add(query.Fields.Field['SEPARATOR'].Value);
			if(k==0)this.sortno=query.Fields.Field['SORTNO'].Value;//得到起始位置
			k++;
			query.Next();
		}
		else break;
	}
	cell.colSpan=cell.ItemNames.Count;

	cell3 = new TD();
	cell3.colSpan=cell.ItemNames.Count;

	treeview=new TreeView(null,cell3);
	treeview.style.width = 300;
	treeview.style.height = 300;
	treeview.style.display='none';
	treeview.style.backgroundColor=clWhite;
	treeview.CATEGORY=this.LevelNames.Strings[i];
	treeview.Edits=new Items();

	for(var j=0;j<cell.ItemNames.Count;j++){
		cell1 = new TD();
		bodyrow1.appendChild(cell1);
		cell1.innerText=cell.ItemNames.Strings[j];

		cell2 = new TD();
		bodyrow2.appendChild(cell2);
		edit = new Edit(null,cell2);
	    edit.style.position = 'relative';
		edit.maxLength=parseInt(cell.ItemLengths.Strings[j]);
		edit.iscode=true;
		edit.width=12*edit.maxLength;
		edit.style.ezIndex=1;
		this.Edits.Add(edit);
		treeview.Edits.Add(edit);

		edit = new Edit(null,cell2);
		edit.innerText=cell.ItemSeps.Strings[j];
	    edit.style.position = 'relative';
		edit.style.ezIndex=1;
		edit.width=cell.ItemSeps.Strings[j].length*12;
		edit.disabled = true;
		edit.style.border=0;
	    edit.style.backgroundColor=clBlue0;
		this.Edits.Add(edit);
	}    
	
	headrow1.appendChild(cell);
	bodyrow3.appendChild(cell3);

	button=new Button(null,cell);

//	button.style.position = 'relative';
	button.value=this.LevelNames.Strings[i];

	button.treeview=treeview;
	treeview.close=button;
	button.Expanded=false;
	button.onclick=QEPCLASS_buttononclick;
	button.filltree = QEPCLASS_buttonfilltree;
	button.owner=this;
	button.index=i;
	button.edit=edit;
	treeview.style.zIndex =2 ;
  }
}
    //按钮点击,拉出树由用户选择
function QEPCLASS_buttononclick(){
	this.Expanded=!this.Expanded;
	if(this.Expanded){
		this.disibled=true;
		this.treeview.style.top=this.edit.offsetTop-2;
		this.treeview.style.display='block';
		this.filltree();
		this.disibled=false;
	}
	else{
		this.treeview.style.display='none';
	}
}
//初始树及填充根节点
function QEPCLASS_buttonfilltree(){
  var TreeView1=this.treeview;
  TreeView1.SelectImage='../jcl/image/openfoldericon.gif';;
  TreeView1.Image='../jcl/image/foldericon.gif';
  TreeView1.Items.Clear();

  var n = new TreeNode();
  TreeView1.Items.AddChild(n);
  n.Text.innerHTML=this.value;

  TreeView1.OnExpand=QEPCLASS_TreeViewOnExpand;
  n.Expanded = true;
}
//树的展开前事件 
function QEPCLASS_TreeViewOnExpand(sender,node){
  var Query1=new Query();
  Query1.MaxRows=-1;
  Query1.OpenUrl="/eam?classname=query_equipmentcodes&operate=open";
//  Query1.Close();	
  Query1.Params.Clear();
  Query1.Params.Add(new Param('CATEGORY',sender.CATEGORY,'String'));
  Query1.Params.Add(new Param('LEVEL',node.Level-1,'Integer'));
  var parent;
  if(node.Level!=1)
	  parent = node.CODEID;
  Query1.Params.Add(new Param('PARENT',parent,'String'));
  Query1.MaxRows=-1;
  Query1.Open();
  var n1;
  Query1.AutoControl = false;
  node.Items.Clear();
  var value;
  for(var i=0;i<Query1.RecordCount;i++){
	n1 = new TreeNode();
	value = Query1.Fields.Field["CODE"].Value;
    n1.Text.innerHTML = value+":"+Query1.Fields.Field["CODENAME"].Value;
    node.Items.AddChild(n1);
	n1.Text.ondblclick=QEPCLASS_treenodeondblclick;
	if(n1.Parent.fullcode)
	    n1.fullcode = value;  
	else
	    n1.fullcode = value;  
    n1.CODEID = Query1.Fields.Field["CODEID"].Value;
//  if(node.Level<TreeView1.maxlevel) 
	n1.nodediv.style.visibility='visible';
	Query1.Next();
  }
  Query1.AutoControl = true;
}
//双击后选定返回
function QEPCLASS_treenodeondblclick(){
	var node=this.treenode;
	var level=node.Level;
	var treeview=node.TreeView;
	var parentnode;
	for(var i=0;i<treeview.Edits.Count;i++){
		treeview.Edits.Item[i].value='';
	}
	for(var i=level;i>1;i--){
		treeview.Edits.Item[i-2].value=node.fullcode;
		node=node.Parent;
		if(!node)break;
	}
	treeview.close.click();
}
  //得到层级分段参数
function QEPCLASS_getlevelnames(){
	var query=new Query();
	query.MaxRows=-1;
	query.OpenUrl="/eam?classname=eqclass_levelcode&operate=open";
	query.Open();
	this.LevelNames.Clear();
	for(var i=0;i<query.RecordCount;i++){
		this.LevelNames.Add(query.Fields.Field['CONTENT'].Value);
		query.Next();
	}
}
//得到选择的编码
function QEPCLASS_getstring(){
	var result,s,l,max,edit;
	result='';
	for(var i=0;i<this.sortno;i++){
		result+='_';
	}
	for(var i=0;i<this.Edits.Count;i++){
		edit=this.Edits.Item[i];
		s=edit.value;
		if(edit.iscode){
			l=s.length;
			max=edit.maxLength;
			for(var j=l;j<max;j++){
				s+='_';
			}
		}
		result+=s;
	}
	return result;
}
//校验输入合法性,返回为错误的位数,正确返回-1
function QEPCLASS_validate(){
	var s=this.GetString();
	var x=s.indexOf('_');
	return -1;
}
