var HideElementTemp = new Array();
//点击菜单时，调用此的函数,菜单对象
function cal_hideElementAll(obj){ 
       // cal_HideElement("IMG",obj);
        cal_HideElement("SELECT",obj);
        cal_HideElement("OBJECT",obj);
        cal_HideElement("IFRAME",obj);
}
function cal_HideElement(strElementTagName,obj){
try{
    var showDivElement = obj;
    var calendarDiv = obj;
    var intDivLeft = cal_GetOffsetLeft(showDivElement);
    var intDivTop = cal_GetOffsetTop(showDivElement);//+showDivElement.offsetHeight;
    //HideElementTemp=new Array()
    for(i=0;i<window.document.all.tags(strElementTagName).length; i++){
 var objTemp = window.document.all.tags(strElementTagName)[i];
 if(!objTemp||!objTemp.offsetParent)
     continue;
 var intObjLeft=cal_GetOffsetLeft(objTemp);
 var intObjTop=cal_GetOffsetTop(objTemp);
 if(((intObjLeft+objTemp.clientWidth)>=intDivLeft)&&
    (intObjLeft<=intDivLeft+calendarDiv.offsetWidth)&&
    (intObjTop+objTemp.clientHeight>=intDivTop)&&
    (intObjTop<=intDivTop+calendarDiv.offsetHeight)){
     //var intTempIndex=HideElementTemp.length;//已经有的长度
  //save elementTagName is stutas
     //HideElementTemp[intTempIndex]=new Array(objTemp,objTemp.style.visibility);
     HideElementTemp[HideElementTemp.length]=objTemp
     objTemp.style.visibility="hidden";
        }
    }
}catch(e){alert(e.message)
}
}

function cal_ShowElement(){
    var i;
    for(i=0;i<HideElementTemp.length; i++){
 var objTemp = HideElementTemp[i]
 if(!objTemp||!objTemp.offsetParent)
     continue;
 objTemp.style.visibility=''
    }
    HideElementTemp=new Array();
}
function cal_GetOffsetLeft(src){
    var set=0;
    if(src && src.name!="divMain"){
        if (src.offsetParent){
           set+=src.offsetLeft+cal_GetOffsetLeft(src.offsetParent);
 }
 if(src.tagName.toUpperCase()!="BODY"){
     var x=parseInt(src.scrollLeft,10);
     if(!isNaN(x))
            set-=x;
 }
    }
    return set;
}

function cal_GetOffsetTop(src){
    var set=0;
    if(src && src.name!="divMain"){
        if (src.offsetParent){
            set+=src.offsetTop+cal_GetOffsetTop(src.offsetParent);
   }
 if(src.tagName.toUpperCase()!="BODY"){
     var y=parseInt(src.scrollTop,10);
     if(!isNaN(y))
  set-=y;
 }
    }
    return set;
}

var dropDownList=null;
	function DropDownList()
	{
		//下拉框容器
	 	this.dropDownContainer=null; 
		//下拉框的输入框
	 	this.source=null;
		//放置各个选项的表格
	 	this.items=null;
		//是否可以多选
	 	this.multiple=null;
	 	this.MultipleValueSeparator=";";
		//该下拉列表框的数据源
	 	this.xmlDoc=null;
	 	this.DropDownStyle=null;
	 	this.DropDownCanInput=null;
		//下拉列表框值
	 	this.valueField=null;
		this.init=function()
		{
				var sender=event.srcElement;
				if(arguments[0].length>0)
						this.source=arguments[0][0];		
				if(this.source==null||document.all(this.source)==null)
				{
					this.source=sender.getAttribute('source');
					if(this.source!=null)
						this.source=document.all(this.source);
					if(this.source==null)
						this.source=sender;
				}
				else
					this.source=document.all(this.source);
				this.DropDownCanInput=this.source.getAttribute('DropDownCanInput');
				if(this.DropDownCanInput==null)
					this.DropDownCanInput=false;
				else
					this.DropDownCanInput=this.DropDownCanInput.parseBool();
				this.multiple=this.source.getAttribute('AllowMultipleSelected');
				if(this.multiple!=null)
					this.multiple=this.multiple.parseBool();
				else
					this.multiple=false;

				var valueField_ID=this.source.getAttribute('ValueList_ID');
				this.valueField=document.getElementsByName(valueField_ID)[0];
				this.MultipleValueSeparator=this.source.getAttribute('MultipleValueSeparator');
				if(this.MultipleValueSeparator==null)
					this.MultipleValueSeparator="\r\n";
				this.DropDownStyle=this.source.getAttribute('DropDownStyle');
				if(this.DropDownStyle==null)
					this.DropDownStyle="Flat";
				this.xmlDoc=this.source.getAttribute('DropDownContentID');
				if(this.xmlDoc!=null)
				{
					this.xmlDoc=document.all(this.xmlDoc);
				}
		};
	}
	function rebindDropdownList(textboxID,xmlDocString)
	{
        if(xmlDocString&&xmlDocString!=null&&xmlDocString.trim()!="")
        {
	      var textbox=document.all(textboxID);
	      if(textbox!=null)
	      {
	          var xmlDoc=textbox.getAttribute('DropDownContentID');
	          if(xmlDoc!=null)
	          {
		        xmlDoc=document.all(xmlDoc);
		        if(xmlDoc==null)
		       {
		         var xmlDoc=document.createElement("xml");
		         document.body.insertAdjacentElement('afterBegin',xmlDoc);
		         var dropDownContentID="DropDownContentID_"+textboxID;
		         textbox.setAttribute('DropDownContentID',dropDownContentID);
		         xmlDoc.id=dropDownContentID;
		       }  
		       xmlDoc.loadXML(xmlDocString);
	          }
	      }
	  }
	}
    function filterItemByInput()
    {
        showDropList();
    }
	function showDropList()
		{
		
			hideDropDownList();
			dropDownList=new DropDownList();
			dropDownList.init(arguments);
			if(dropDownList.xmlDoc!=null)
			{
				//alert(dropDownList.dropDownWin.document.body.outerHTML);
				dropDownList.dropDownContainer=document.createElement('div');
				dropDownList.items=document.createElement('table');
				dropDownList.dropDownContainer.className="dropdown";
				document.body.insertAdjacentElement('beforeEnd',dropDownList.dropDownContainer);
				dropDownList.dropDownContainer.insertAdjacentElement('beforeEnd',dropDownList.items);
				
				thkAttachEvent(document,'click',hideDropDownList,false);

				dropDownList.items.className='dropdownList';
				dropDownList.items.border="0px";
				dropDownList.items.cellSpacing=0;
				dropDownList.items.cellPadding=0;
				dropDownList.xmlDoc=dropDownList.xmlDoc.XMLDocument.documentElement;
				switch(dropDownList.DropDownStyle)
				{
					case 'Flat':
					case 'Table':
					showAsNormal(dropDownList)
					break;
					case 'Tree':
					showAsTree(dropDownList)
					break;
				}
				calcPosition(dropDownList.dropDownContainer,dropDownList.source,true);
				cal_hideElementAll(dropDownList.dropDownContainer)

			}
			event.cancelBubble=true;
		}

	function showAsNormal(dropdown)
	{
		var xmlDoc=dropdown.xmlDoc,items=dropdown.items;
		var scrollIntoViewCell=null;
		if(xmlDoc)
		{
		//var 0']");
		//		alert(getAttributes(xmlDoc.childNodes[0]).length);
				for(var i=0;i<xmlDoc.childNodes.length;i++)
				{
					var xmlNode=xmlDoc.childNodes[i];
					if(xmlNode.nodeType==1)//NodeElement
					{
					    //按输入的关键词过滤
					    if(event.srcElement.tagName!='IMG'&&dropdown.source.value!=""&&xmlNode.text.indexOf(dropdown.source.value)==-1)
					    continue;
						var row=items.insertRow();
						row.className='dropdownItem';
						//row.height=dropDownList.source.offsetHeight;
						row.onmouseover=dropdownListItem_Mouseover;
						row.onmouseout=dropdownListItem_Mouseout;
						var cell =null;						
						if(dropDownList.multiple)
						{
							cell =row.insertCell();
							var checkbox=dropDownList.dropDownWin.document.createElement('input');
							checkbox.type='checkbox';
							checkbox.onclick=function(){event.cancelBubble=true;};
							cell.insertAdjacentElement('beforeEnd',checkbox);
						}
						cell =row.insertCell();
						cell.innerText=xmlNode.text.trim();
						if(cell.innerText==dropdown.source.value)
						    scrollIntoViewCell=cell;
						cell.noWrap =true;
						if(!dropDownList.DropDownCanInput)//不允许在列表框中输入值时,才注册值
						{
							cell.setAttribute("value",xmlNode.getAttribute("id"));
							if(dropDownList.multiple)
							{
								if(dropDownList.valueField.value.indexOf(xmlNode.getAttribute("id").trim()+dropDownList.MultipleValueSeparator)>-1)
								{
									checkbox.checked=true;
								}

							}
						}
						else if(dropDownList.multiple)
						{
							//alert('sf');
							 if(dropDownList.source.value.indexOf(xmlNode.text.trim()+dropDownList.MultipleValueSeparator)>-1)
							{
								checkbox.checked=true;
							}
						}
						thkAttachEvent(cell,'click',returnData);
							//添加其它属性
						if(dropDownList.DropDownStyle=="Table")
						{
							for(var j=0;j<xmlNode.attributes.length;j++)
							{
								var attribute=xmlNode.attributes[j];
								var attributeName=attribute.nodeName.toLowerCase();
								if(attributeName!='id'&&attributeName!='parentid')
								{
									cell=row.insertCell();
									cell.onclick=function(){event.cancelBubble=true;};
									cell.innerText=attribute.text;
								}
							}
						}
					}
					

				}//xmlDoc节点遍历完成
				if(scrollIntoViewCell!=null)
				{
				    //scrollIntoViewCell.scrollIntoView();
				    scrollIntoViewCell.parentNode.className='dropdownItem_MouserOver';
				}
				var row=items.insertRow();
				var cell=row.insertCell();
				cell.height='100%';
			}
		}
		function hideDropDownList()
		{
			if(dropDownList!=null&&dropDownList.dropDownContainer!=null)
			{
				dropDownList.dropDownContainer.removeNode(true);
				dropDownList=null;
			}
			cal_ShowElement();
		}
		function dropdownListItem_Mouseover()
		{
			var row=event.srcElement;
			if(row.tagName.toLowerCase()=='input')
				row=row.parentElement.parentElement;
			else	if(row.tagName.toLowerCase()=='td')
				row=row.parentElement;
			row.className='dropdownItem_MouserOver';
		}
		function dropdownListItem_Mouseout()
		{
		var row=event.srcElement;
		if(row.tagName.toLowerCase()=='input')
				row=row.parentElement.parentElement;
		else	if(row.tagName.toLowerCase()=='td')
				row=row.parentElement;
			row.className='dropdownItem';
		}	 
		function returnData()
		{
			if(dropDownList.valueField)
				dropDownList.valueField.value="";
			if(dropDownList.source)
				dropDownList.source.value="";
			if(!dropDownList.multiple)
			{
			    var isChange=(dropDownList.source.value!=event.srcElement.innerText);
				dropDownList.source.value=event.srcElement.innerText;
				if(!dropDownList.DropDownCanInput&&dropDownList.valueField!=null)
				{
					dropDownList.valueField.value=event.srcElement.getAttribute('value');
				}
				if(isChange&&dropDownList.source.onchange)
				    dropDownList.source.fireEvent('onchange',event);
			}
			else
			{
				for(var i=0;i<dropDownList.items.rows.length-1;i++)
				{
					if(dropDownList.items.rows[i].cells[0].childNodes[0].checked||dropDownList.items.rows[i].cells[1]==event.srcElement)
					{
						dropDownList.source.value+=dropDownList.items.rows[i].cells[1].innerText+dropDownList.MultipleValueSeparator;
						if(!dropDownList.DropDownCanInput&&dropDownList.valueField!=null)
						{
							dropDownList.valueField.value+=dropDownList.items.rows[i].cells[1].getAttribute('value')+dropDownList.MultipleValueSeparator;
						}
					}
				}
			    if(dropDownList.source)
			        dropDownList.source.fireEvent('onchange',event);
			}
		}


	//****************
	//treeHandle
	//***********************
	function getAttributes(xmlNode)
	{
			var nodes=[];
			for(var i=0;i<xmlNode.attributes.length;i++)
			{
				nodes[nodes.length]=xmlNode.attributes[i].nodeName.toLowerCase();
			}
			return nodes;
	}
	function hasAttribute(xmlNode,attributeName)
	{
		if(xmlNode.nodeType==1)
		{
			attributeName=attributeName.toLowerCase();
			for(var i=0;i<xmlNode.attributes.length;i++)
			{
				if(xmlNode.attributes[i].nodeName.toLowerCase()==attributeName)
					return true;
			}
		}
		return false;
	}
		//删除已经选择的项
	function clearSelectedItems()
	{
	    if(dropDownList.valueField)
			dropDownList.valueField.value="";
		if(dropDownList.source)
			dropDownList.source.value="";

	}
function showAsTree(dropdown)
	{
		var xmlDoc=dropdown.xmlDoc,items=dropdown.items;
		if(!xmlDoc)
		return;
		//webFXTreeConfig.setPath(dropdown.source.getAttribute('ResourcePath')+'images/tree/');
		if (document.getElementById&&xmlDoc.childNodes.length>0) 
		{
			var id="root";
			var text=xmlDoc.getAttribute('title');
			var tree = new WebFXTree(id,text);
			tree.onSelect=function(sender)
			{
			clearSelectedItems();
			//event.cancelBubble=true;
			}
			var rootNodes=getRootNode(xmlDoc);
			for(var i=0;i<rootNodes.length;i++)
			{
				id=rootNodes[i].getAttribute("id");
				if(id==null)
					id=webFXTreeHandler.getId();
				text=rootNodes[i].text;
				var node =null;
				if(dropdown.multiple)
				{
					node =new WebFXCheckBoxTreeItem(id,text);
					node._checked=(dropDownList.valueField.value.indexOf(id+dropDownList.MultipleValueSeparator)>-1);
				}
				else
					node =new WebFXTreeItem(id,text);
				node.onSelect=onNodeSelected;
				tree.add(node);
				loadChildNods(dropdown.multiple,rootNodes[i],id,node);
			}
			var row=items.insertRow();
			var cell=row.insertCell();
			cell.vAlign ='top'
			cell.innerHTML=tree.toString();
		}
	}
	function loadChildNods(checkable,xmlNode,parentID,treeNode)
	{
		//"+xmlNode.nodeName+"
			var childNodes=xmlNode.selectNodes("//*[@parentID='"+parentID+"']");//  or (@parentID = '')]");=\"''\")
			for(var i=0;i<childNodes.length;i++)
			{
				var id=childNodes[i].getAttribute("id");
				if(id==null)
					id=webFXTreeHandler.getId();

				var text=childNodes[i].text;
				var node =null;

				if(checkable)
				{
					node =new WebFXCheckBoxTreeItem(id,text);
					node._checked=(dropDownList.valueField.value.indexOf(id+dropDownList.MultipleValueSeparator)>-1);
				}
				else
					node =new WebFXTreeItem(id,text);
				node.onSelect=onNodeSelected;
				treeNode.add(node);
				loadChildNods(checkable,childNodes[i],id,node)
			}
	}
	function onNodeSelected(sender)
	{
		if(dropDownList.valueField)
			dropDownList.valueField.value="";
		if(dropDownList.source)
			dropDownList.source.value="";
		if(!dropDownList.multiple)
		{
			dropDownList.source.value=sender.getText();
			if(!dropDownList.DropDownCanInput&&dropDownList.valueField!=null)
			{
				dropDownList.valueField.value=sender.id;
			}
		}
		else
		{
			var checkNodes=[];
			sender.TreeView.getCheckedNodes(checkNodes);
			
			var flag=false;
			for(var i=0;i<checkNodes.length;i++)
			{
				if(checkNodes[i].id==sender.id)
				{
					flag=true;
				}
				dropDownList.source.value+=checkNodes[i].getText()+dropDownList.MultipleValueSeparator;
				if(!dropDownList.DropDownCanInput&&dropDownList.valueField!=null)
				{
					dropDownList.valueField.value+=checkNodes[i].id+dropDownList.MultipleValueSeparator;
				}

			}
			if(!flag)
			{
				dropDownList.source.value+=sender.getText()+dropDownList.MultipleValueSeparator;
				if(!dropDownList.DropDownCanInput&&dropDownList.valueField!=null)
				{
					dropDownList.valueField.value+=sender.id+dropDownList.MultipleValueSeparator;
				}					
			}
		}
}