// JavaScript Document
function equals(val1,val2){
	if(typeof(val1) == "string" && typeof(val2) == "string"){	
		if(val1.toLowerCase()==val2.toLowerCase())
			return true;
	}	
	
	return val1==val2;
}

String.prototype.equals = function(val2){
	var val1=this;
	if(typeof(val2) == "string"){	
		if(val1.toLowerCase()==val2.toLowerCase())
			return true;
	}
	return val1==val2;
}

function NoEvent(ev){	if(window.event){window.event.cancelBubble=true;window.event.returnValue=false;}else{ev.stopPropagation();} }

function getChildeElementsByTagName(elem,tagName){
 var arr= new Array();	
 var chs = elem.childNodes;
 for(var index=0;index<chs.length;index++){
	var e=chs[index];
	if(equals(e.tagName,tagName))
		arr[arr.length]=e;
 }
 return arr;
}

function getElementByTagName(elem,name){
	var arr=elem.getElementsByTagName(name);
	if(arr.length>0)
		return arr[0];
	return null;	
}
function getElementByClassName(elem,name,fclassName){
	var arr=elem.getElementsByTagName(name);
	if(arr.length>0)
	{
		for(var index=0;index<arr.length;index++){
		var elem=arr[index];
		if(equals(elem.className,fclassName))
			return elem;
		}
	}
	return null;	
}

////////////Class EventObject ////////
function EventObject(){}
EventObject.fnk_apply=function(a,fnk){return function(){return fnk.apply(a,arguments)}}
EventObject.fnk_call=function(elem,fnk){return function(e){if(!e)e=window.event;fnk.call(elem,e)}}
EventObject.prototype.addEvent=function (msgtype,elem,callback){
		var c=EventObject.fnk_apply(elem,callback);
		var d="ev__"+msgtype;
		if(this[d])
			this[d].push(c);
		else
			this[d]=[c];
};
EventObject.prototype.attachEvent=function(elem,msgtype,callback){
		if(elem.attachEvent){
			var e=EventObject.fnk_call(this,callback);
			var d=function(){return e.apply(elem,arguments)};
			elem.attachEvent(msgtype,d);
		}else if(elem.addEventListener){
			var e=EventObject.fnk_call(this,callback);
			var d=function(){return e.apply(elem,arguments)};
			if(equals(msgtype,"onclick"))
				msgtype="click";
			if(equals(msgtype,"onmouseover"))
				msgtype="mouseover";
			if(equals(msgtype,"onmouseout"))
				msgtype="mouseout";	
			elem.addEventListener(msgtype,d,false);//true);
			//alert(elem.id);
		}
	}
function CopyTo(arr,stindex,length){
	var narr=new Array();
	if(!length || length==-1 || length>arr.length)
		length=arr.length;
	for(var h=stindex;h<length;h++){
			narr.push(arr[h]);
	}
	return narr;
}
EventObject.prototype.fire=function(msgtype){
	var e="ev__"+msgtype;
	var f=this[e];
	if(f&&f.length>0)
		for(var h=0;h<f.length;h++){
			var i=f[h];
			var arr=CopyTo(arguments,1);
			if(i)try{i.apply(this,arr)}catch(j){}
		}
}	

function LoadViewer(elem){
	this.elem=elem;
	this.isStarted=false;
	this.tick=0;
}
LoadViewer.prototype.Start=function(){
	this.isStarted=true;
	this.elem.innerHTML="Loadind";
	this.elem.style.display="";
	this.elem.className="pLoading";
	//this.tick=0;
	this.RunStep();
}
LoadViewer.prototype.RunStep=function(){
	if(this.isStarted)
	{
		if(this.elem.innerHTML.length<10)
			this.elem.innerHTML=this.elem.innerHTML+".";
		else
			this.elem.innerHTML="Loadind";
		this.iTimerID=this.SetTimeout(this.RunStep,500);
	}
	else
		this.iTimerID=null;
}
LoadViewer.prototype.SetTimeout=function(b,c){
	var f=EventObject.fnk_apply(this,b);
	return window.setTimeout(f,c);
}
LoadViewer.prototype.Stop=function(){this.isStarted=false; this.elem.className="";}


function XmlHttp(url){
	this.Url=url;
	this.Request=null;
	this.openType="POST";
	// if(window.ActiveXObject)
	//	this.Request = new ActiveXObject("MSXML2.XMLHttp");
	// else
	//	this.Request= new XMLHttpRequest();
}

XmlHttp.prototype.onReadyStateChange=function(){
	
	if(this.Request.readyState==4)
	{
		data=this.Request.responseText;
		this.CallBack.call(this.objCaller,data);
	}
}

XmlHttp.prototype.send=function(data,objCaller,callback){
	this.objCaller=objCaller;
	this.CallBack=callback;
	this.Request.open(this.openType, this.Url, true);
	this.Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var e=EventObject.fnk_call(this,this.onReadyStateChange);
	var d=function(){return e.apply(this.Request,arguments)};
	if(this.Request.addEventListener)
		this.Request.addEventListener("load",d,false);
	else
		this.Request.onreadystatechange=d;
	this.Request.send(data);
}
////////////Class Tree ////////
function Tree(ctrl){
	this.control=ctrl;
	this.pTree=this;
	this.id=ctrl.id;
	this.Nodes=new Array();
	this.selectedible=false;
	this.selectedStyle="background:#000099;color:#FFFFFF;border: 1px dotted #FFFFFF;CURSOR:pointer;";
	//this.selectedStyle=selStyle==null?"background:#000099;color:#FFFFFF;border: 1px dotted #FFFFFF;CURSOR:pointer;":selStyle;
	var childelems =getChildeElementsByTagName( this.control,'div');
	//aa.aa=0;
	for(var index=0;index<childelems.length;index++){
		var elem=childelems[index];
		if(equals(elem.tagName,'div')){
			elem.id=this.control.id+"_"+index;
			this.Nodes[this.Nodes.length]=new TreeNode(this,elem);
		}
	}	
}

Tree.prototype=new EventObject();

Tree.prototype.select=function(node,flag){
	if(!this.selectedible)
		return;
	if(this.selectedNode!=null)
	{
		this.selectedNode.elemText.style.cssText="CURSOR:pointer;";
	}

	node.elemText.style.cssText=this.selectedStyle;
	//node.txt().scrollIntoView();
	this.selectedNode=node;
	this.fire("onselect",this.selectedNode);
}
var gcImg="images/icon-plus.gif";
var goImg="images/icon-minus.gif";
var geImg="images/icon-empty.gif";
////////////Class TreeNode ////////
function TreeNode(parent,ctrl){
	this.pTree=parent.pTree;
	this.control=ctrl;
	this.id=ctrl.id;
	this.Nodes=new Array();
	this.isexpand=false;
	this.nLoadSate="noload";
	this.attachEvent(this.control,"onclick",this.onClick);
	this.elemText=getElementByTagName(this.control,'span');
	this.img = document.createElement("img");
	this.img.style.width="9";
	this.img.style.height="17";
	this.img.src=geImg;
	this.control.insertBefore(this.img,this.elemText);
	//this.control.attachEvent("onclick",function(){});//bug
	//this.control.onclick=function(){Consule.value=event.srcElement.id;};
	this.elemText.style.marginTop="3px";
	this.elemText.style.marginLeft="3px";
	this.elemText.style.whiteSpace="nowrap";
	
	this.control.style.cursor="pointer";
	this.control.style.paddingLeft="3px";
	//this.control.className="node";
	//if(this.elemText!=null)
	//	this.elemText.className="node";
	//this.cbCmd=this.control.getAttribute("cbCmd");
	var ch = getElementByTagName(this.control,'div');
	if(ch!=null)
	{
		this.elemNodes=ch;
		this.elemNodes.onclick=NoEvent;
		//this.elemText.className="closed";
		//this.control.style.paddingLeft="0px";
		//this.control.style.paddingRight="16px";
		this.img.src=gcImg;

		//WIDTH: 100%;
		this.elemNodes.style.display="none";
		//this.elemNodes.style.marginLeft="5px";
		this.initItems();
	}else{
		this.cbCmd=this.control.getAttribute("callbck");
		if(this.cbCmd!=null)
		{
			var e=document.createElement('div');
			this.control.appendChild(e);
			this.elemNodes=e;
			
			//this.elemText.className="closed";
			//this.control.style.paddingLeft="0px";
			//this.control.style.paddingRight="16px";
			
			this.elemNodes.onclick=NoEvent;
			this.elemNodes.style.display="none";
		    //this.elemNodes.style.marginLeft="5px";
		}
	}
}

TreeNode.prototype=new EventObject();

TreeNode.prototype.initItems=function(){
	var ex=this.elemNodes.getAttribute("exp");
	
	var childelems=getChildeElementsByTagName(this.elemNodes,'div');
	for(var index=0;index<childelems.length;index++){
		var elem=childelems[index];
			elem.id=this.control.id+"_"+index;
			this.Nodes[this.Nodes.length]=new TreeNode(this,elem);
	}
	this.nLoadSate=(childelems.length>0)?"loaded":"noload";
	if(ex!=null)this.expand();
}

TreeNode.prototype.CallBack=function(data){
	if(this.elemNodes!=null){
		if(this.LoadViewer)  this.LoadViewer.Stop();
		this.elemNodes.innerHTML=data;
		this.callups();
		this.initItems();
		this.expand();
	    //Consule.value=this.elemNodes.outerHTML;
	}
	this.nLoadSate="loaded";
}

TreeNode.prototype.processLoad=function(){

	params=this.control.getAttribute("callbck");
	if(params!=null)
	{
		if(this.elemNodes!=null){
			if(this.LoadViewer==null)this.LoadViewer=new LoadViewer(this.elemNodes);
			this.LoadViewer.Start();
		}
		this.nLoadSate="loading";
		
		if(this.XmlHttp==null)this.XmlHttp=new XmlHttp(gXmlUrl);
		//alert(params);
		this.XmlHttp.send(params,this,this.CallBack);
	}else
		this.nLoadSate="loaded";
}

TreeNode.prototype.isExpand=function(){
	if(this.elemNodes!=null && this.elemNodes.style.display=="")
		return true;
	return false;
}

TreeNode.prototype.select=function(flag){
	this.pTree.select(this,flag);
}

TreeNode.prototype.expand=function(){
	//if(this.disabled )return;
	//this.initChild();
	if(this.elemNodes!=null)
	{
		//this.elemText.className="open";
		this.img.src=goImg;
		//this.control.style.paddingLeft="0px";
		//this.control.style.paddingRight="0px";
		if(this.elemNodes.innerHTML.length>0)
			this.elemNodes.style.display="";
	}
}

TreeNode.prototype.callups=function(node){
	if(this.elemNodes!=null)
	    //this.elemText.className="closed";
		this.img.src=gcImg;
		//this.control.style.paddingLeft="0px";
		//this.control.style.paddingRight="16px";
		this.elemNodes.style.display="none";
	}

TreeNode.prototype.onClick=function(ev){
	this.select(true);
	if(this.nLoadSate=="noload" || this.nLoadSate=="reload")
		this.processLoad();
	if(this.nLoadSate=="loading")
		return;
	if(this.nLoadSate=="loaded")
	{
		if(this.isExpand())
			this.callups();
		else
			this.expand();
	}
	if(window.event){
		ev.cancelBubble=true;
		ev.returnValue=false
	}else{
		ev.stopPropagation();
	}
	return false;
}
////////////Class Tree ////////
function Menu(ctrl){
	this.control=ctrl;
	this.pMenu=this;
	this.id=ctrl.id;
	this.Items=new Array();
	this.elemNodes=ctrl;
	this.lock=false;
	var childelems =getChildeElementsByTagName( this.control,'div');
	for(var index=0;index<childelems.length;index++){
		var elem=childelems[index];
		if(equals(elem.tagName,'div')){
			elem.id=this.control.id+"_"+index;
			this.Items[this.Items.length]=new MenuItem(this,elem);
		}
	}	
}
function MenuItem(parent,ctrl){
	this.pMenu=parent.pMenu;
	this.parent=parent;
	this.control=ctrl;
	this.id=ctrl.id;
	this.Items=new Array();
	this.elemNodes=getElementByClassName(this.control,'div','manu');
	if(this.elemNodes!=null){
		this.elemNodes.style.position="absolute";
		this.elemNodes.style.visibility="hidden";
		//this.elemNodes.onclick=NoEvent;
		
		this.attachEvent(this.control,"onmouseover",this.onShow);
		this.attachEvent(this.control,"onmouseout",this.onHide);
		var childelems =getChildeElementsByTagName( this.elemNodes,'div');
		for(var index=0;index<childelems.length;index++){
			var elem=childelems[index];
			elem.id=this.control.id+"_"+index;
			this.Items[this.Items.length]=new MenuItem(this,elem);
		}	
	}
}
MenuItem.prototype=new EventObject();

function Point(a,b){
	this.x=a;
	this.y=b
}
Point.ORIGIN=new Point(0,0);
Point.prototype.toString=function(){return"("+this.x+", "+this.y+")"};
Point.prototype.equals=function(p){
	if(!p)return false;
	return p.x==this.x&&p.y==this.y
};
////////////Class Unit ////////
function Unit(){}
Unit.Pixsel=function(a){return Math.round(a)+"px"};
Unit.Point=function(a){return Math.round(a)+"pt"};
Unit.Pica=function(a){return Math.round(a)+"pc"};
Unit.Inch=function(a){return Math.round(a)+"in"};
Unit.Mm=function(a){return Math.round(a)+"mm"};
Unit.Cm=function(a){return Math.round(a)+"cm"};
Unit.Percentage=function(a){return Math.round(a)+"%"};           
Unit.Em=function(a){return Math.round(a)+"em"};           
Unit.Ex=function(a){return Math.round(a)+"ex"};           
Unit.toInt=function(a){parseInt(a)}; 

function getElementPosition(elm){
	var p=new Point(elm.offsetLeft,elm.offsetTop);//&& elm.tagName!="BODY"
	elm=elm.offsetParent;
	while(elm && elm.tagName!="BODY" ){
		
		p.x+=elm.offsetLeft-elm.scrollLeft;
		p.y+=elm.offsetTop-elm.scrollTop;
		if(elm.style.position=="absolute"){
			p.x-=elm.style.posLeft;
			p.y-=elm.style.posTop;	
		}
		
		elm=elm.offsetParent;
		
	}
	return p;
}

//document.onmousemove=function(){ window.status="x="+event.x+" y="+event.y;}
MenuItem.prototype.onShow=function(ev){
	//aa.zz=0;
	//t=document.getElementById('txt');
	//t.value+="onMouseOver "+ this.id+"\r\n";
	//alert(this);
	if(this.parent.onShow)
		this.parent.onShow(ev);
		
	if(this.parent.AtivePopup && this.parent.AtivePopup!=this){
		this.parent.ClearTimeout();
		this.parent.AtivePopup.Hide();
	}
	this.parent.AtivePopup=this;
	
	this.Show(ev);
	this.ClearTimeout();
}

MenuItem.prototype.Show=function(ev){
	
	var p=getElementPosition(this.control);
	this.elemNodes.style.visibility="visible";
	this.elemNodes.style.left=p.x+this.control.offsetWidth+1;//Unit.Pixsel();
	this.elemNodes.style.top=p.y-1;//Unit.Pixsel();
}

MenuItem.prototype.SetTimeout=function(b,c){
	var f=EventObject.fnk_apply(this,b);
	this.timeID=window.setTimeout(f,c);
	return this.timeID;
}
MenuItem.prototype.ClearTimeout=function(){
	if(this.timeID!=null)
		clearTimeout(this.timeID); 
	this.timeID=null;
}

MenuItem.prototype.Hide=function(ev){
	this.elemNodes.style.visibility="hidden";
	this.timeID=null;
}

MenuItem.prototype.onHide=function(ev){
	
	//t=document.getElementById('txt');
	//t.value+="onMouseOut "+ this.id+"\r\n";
	
	this.ClearTimeout();
	this.timeID=this.SetTimeout(this.Hide,500);
}