
var DundasTree = {

	click : function( e, control, data )
	{
		 e = DundasTree.fixEvent(e);
		 var element = e.target;
		 if ( "IMG,SPAN,A".indexOf(element.tagName) != -1)
		 {
			var node = new DundasTreeNode( element);
			if ( node.isValid())
			{
		        if ( element == node.nodeLink)
		        {
			        DundasTree.selectNode( element, control);
		        }
		        else if ( element.tagName == "IMG")
		        {
		            if ( node.expandImage == element)
		            {
			            DundasTree.expandOrColapse(node, control, data)
		            }
		            else if ( node.checkBox == element )
		            {
			            DundasTree.checkBoxProcess(node, control, data)
		            }
		        }
		   }     
		 }
		 return false;
	},	
	
	updateBoldItems : function ( elementName, boldItems)
	{
	    var element = document.getElementById( elementName);
	    if ( element )
	    {
			for( var i = 0; i < element.childNodes.length; i++)
			{
				var node = element.childNodes.item(i);
				if ( node.tagName && node.tagName.toUpperCase() == "TABLE")
				{
					var treeNode = new DundasTreeNode( node);
					if ( treeNode.isValid())
					{
					    treeNode.makeNormalBold();
					    treeNode.testForBold(boldItems);
					}
				}
			}
                	    
	    }
	},
	
	fixEvent : function( e )
	{
			if (typeof(e) == 'undefined' || e == null) e = window.event;
			if (typeof(e.layerX) == 'undefined') e.layerX = e.offsetX;
			if (typeof(e.layerY) == 'undefined') e.layerY = e.offsetY;
			if (typeof(e.target) == 'undefined') e.target = e.srcElement;
			
			return e;
	},
	
	scroll : function( e, control )
	{
		 var input = DundasTree.getInputByName( "ScrollTo", control);
		 if ( input )
		 {
			input.value = control.scrollLeft + ";" + control.scrollTop;
		 }
	},
	

	getParentByTag : function ( o, tagName, lockupId)	
	{
		if ( o == null )
		{
			return null;
		}	
        var parent = o;
        var upperTagName = tagName.toUpperCase();
        if (lockupId)
        {
			while( true )
			{
				while (parent && parent.tagName && (parent.tagName.toUpperCase() != upperTagName)) {
					parent = parent.parentNode ? parent.parentNode : parent.parentElement;
				}
				if (parent && typeof(parent.id) == "string" && parent.id.length == 0)
				{
					parent = parent.parentNode ? parent.parentNode : parent.parentElement;
					continue;
				}
				break;
			}
        }
        else
        {
			while (parent && parent.tagName && (parent.tagName.toUpperCase() != upperTagName)) {
				parent = parent.parentNode ? parent.parentNode : parent.parentElement;
			}
        }
        return parent;
	},


	getElementByName : function ( nodeList, id)
	{
		for( i = 0; i < nodeList.length; i++)
		{
			if (nodeList.item(i).name == id)
			{
				return nodeList.item(i);
			}
		}
	},


	getSelectedNode : function ( nodePath, control ) 
	{
		if ( nodePath.length == 0 )
		{
			return null;
		}
		var result = null;
		var nodes = control.childNodes;
		for( i = 0; i < nodes.length; i++)
		{
			if ( nodes[i].tagName == "TABLE")
			{
				result = (new DundasTreeNode(nodes[i])).getByPath(nodePath);
				if ( result )
				{
					return result;
				}
			}
		}		
		return null;
	},

	getInputByName : function ( name, control)
	{
		var result = null;
		return document.getElementById(control.id + "_" + name);	
	},
	
	nodeResult   : "",
	
	selectNode : function ( item, control )
	{
		var input   = DundasTree.getInputByName("SelectedNode", control);
		var oldSelectedNode = DundasTree.getSelectedNode( input.value, control);
		var newSelectedNode = new DundasTreeNode( item);
		
		if ( oldSelectedNode )
		{
			oldSelectedNode.applySelectStyle( "s" + control.id);
			input.value = "";
		}		
		if ( newSelectedNode )
		{
			var pathNode = newSelectedNode.getPath();
			newSelectedNode.applySelectStyle( "ss" + control.id);
			input.value = newSelectedNode.getPath();
			DundasTree.nodeResult = input.value;
		}	
		return;
	},
		
	expandOrColapse : function (o, control, data)
	{	
		if ( o.isValid() && o.hasChildren())
		{
			var input = DundasTree.getInputByName( "ExpandedNodes", control);
			o.switchExpanded( control, data);
			if ( input )
			{
				if ( !o.isExpanded())
				{
					input.value = input.value.replace("{" +o.getPath() + "};", "");
				}		
				else
				{
					input.value = input.value + "{" + o.getPath() + "};"
				}
			}
			return true;
		}
		return false;
	},
	
	checkBoxProcess : function (o, control, data)
	{	
		if ( o.isValid() && o.isCheckBox())
		{
			var input = DundasTree.getInputByName( "CheckedNodes", control);
			o.switchCBox( control, input, data);
			return true;
		}
		return false;
	},
	
    alignCubeBrowser : function ( elementID )
    {
        //return;
        var client = document.getElementById(elementID);
        var toolbar = document.getElementById(elementID + "ChartToolbar");
        var report = document.getElementById(elementID + "ReportListFrame");
        var cube   = document.getElementById(elementID + "CubeSelectorFrame");
        var cubeBrowserFrame = document.getElementById(elementID + "CubeBrowserFrame");
        var cubeBrowser = document.getElementById(elementID + "CubeBrowserT");
        if ( client && cubeBrowser )
        {
            var height = client.clientHeight;
            if ( height == 0 )
            {
                height = client.offsetHeight;
            }
            if ( report )
            {
                height -= parseInt(report.offsetHeight);
            }
            if ( cube )
            {
                height -= parseInt(cube.offsetHeight);
            }
            if ( toolbar )
            {
				height -= parseInt(toolbar.offsetHeight);
            }
            cubeBrowser.style.height = "0px"
            cubeBrowser.style.display = "block";
            var border = cubeBrowser.offsetHeight - cubeBrowser.clientHeight;
            cubeBrowser.style.height = (height - 22 - 7 - border) + "px";
        }    
    },
    	
	fixScroll: function ( name, x, y ) 
	{ 

		if (document.getElementById) 
		{ 
			var el = document.getElementById(name);
			if ( el) 
			{ 
				if ( el.style.display == "none" ) 
				{ 
					var isGecko = window.navigator.userAgent.indexOf("Gecko") > -1;
					var p = el.offsetParent ? el.offsetParent : el.parentNode; 
					if (p) 
					{
						if ( isGecko )
						{
							if ( !el.style.width )
							{ 
							    el.style.width	= (p.clientWidth  - 4)+"px"; 
							}
							if ( !el.style.height )
							{
							    el.style.height = (p.clientHeight - 4)+"px";	
							}
						}
						el.style.display = "block";
					} 
				}
				el.scrollLeft=x; 
				el.scrollTop=y; 
				if ( el.scrollLeft!=x)  el.scrollLeft=x; 
				if ( el.scrollTop !=y)  el.scrollTop=y; 
			}
		}
	}
};

var cbState_checked    = "c";
var cbState_unchecked  = "u";
var cbState_multiple   = "m";

function DundasTreeNode( element)
{
	this.expandImage = null;
	this.nodeLink = null;
	this.childContainer = null;
	this.nodeContainer = null;
	this.checkBox  = null;
    this.dataElement = null;
	this.dataText = null;
    this.value = null;
	this.control = null;
	this.data = null;
	if ( element )
	{
		if ( element.tagName != "TABLE")
		{
			this.nodeContainer = DundasTree.getParentByTag(element,"TABLE");
		}
		else
		{
			this.nodeContainer = element;
		}
		
		
		if ( this.nodeContainer)
		{

			this.control = DundasTree.getParentByTag(element,"DIV",true);
			if ( this.control && this.control.id )
			{
				this.data = eval(this.control.id + "_Info");
			}
			elms = this.nodeContainer.getElementsByTagName("IMG");
			if ( elms.length > 0 )
			{
				this.checkBox  = elms[elms.length-1];
				if ( this.checkBox )
				{
					var cbsrc = this.checkBox.src;
					if ( this.data && ( ( cbsrc.indexOf(this.data.imgcbc) + cbsrc.indexOf(this.data.imgcbu) + cbsrc.indexOf(this.data.imgcbm))) == -3)
					{
						this.checkBox = null;
					}
				}
				this.expandImage  = elms[0];
			}


			elms = this.nodeContainer.getElementsByTagName("A")
			if ( elms.length > 0 )
			{
				this.nodeLink = elms[elms.length-1];	
			}
			else
			{
				elms = this.nodeContainer.getElementsByTagName("SPAN");
				if ( elms.length > 0 )
				{
					this.nodeLink = elms[elms.length-1];	
				}
			}
			
			this.childContainer = this.getSiblingByTag( this.nodeContainer, "DIV", false);
			
			elms = this.nodeContainer.getElementsByTagName("SPAN");
			if ( elms.length > 0 )
			{
				this.dataElement = elms[0];
				this.dataText   = this.dataElement.innerHTML;
				if ( this.dataText)
				{
					var data = this.dataText.split("//");
					this.value = unescape(data[0]).split("&nbsp;").join(" ")
					if ( data.length > 1 && this.checkBox && !this.checkBox.getAttribute("dnd_checked"))
					{
						this.checkBox.setAttribute("dnd_checked", data[1]);
					}
				}
			}
			
		}
	}
}	
		
DundasTreeNode.prototype.isValid = function () { return this.value != null }
	
	
DundasTreeNode.prototype.isExpanded = function() 
{ 
		if (this.childContainer && this.childContainer.style)
		{
			return this.childContainer.style.display == "block";
		}
		return false;
}

DundasTreeNode.prototype.isCheckBox = function() { return this.checkBox != null }
	
DundasTreeNode.prototype.switchCBox = function ( control, input, data)
{
	var state = this.getCBoxState();
	if ( data.single )
	{
		if (state != cbState_checked)
		{
			this.uncheckAll( control, input, data);
			this.setCBoxState( cbState_checked, control, input, null, data );
		}
	}
	else 
	{		
		if ( !this.hasChildren() )
		{
			if ( state == cbState_checked )
			{
				this.setCBoxState( cbState_unchecked, control, input, null, data );
			}
			else if ( state == cbState_unchecked )
			{
				this.setCBoxState( cbState_checked, control, input, null, data);
			}
		}
		else
		{
			if ( state == cbState_checked )
			{
				this.setCBoxState( cbState_multiple, control, input, null,  data);
			}
			else if ( state == cbState_unchecked )
			{
				this.setCBoxState( cbState_checked, control, input, null,  data );
			}
			else if ( state == cbState_multiple )
			{
				this.setCBoxState( cbState_unchecked, control, input, null,  data );
			}
		}
	}
}
    
DundasTreeNode.prototype.makeNormalBold = function()
    {
        if ( this.nodeLink )
        {
            this.nodeLink.style.fontWeight = "normal";
            var childTreeNodes = this.getChildren();
            for( var childIndex = 0; childIndex < childTreeNodes.length; childIndex++)
            {
                childTreeNodes[childIndex].makeNormalBold(); 
            }
        }
    },
        
DundasTreeNode.prototype.testForBold = function( pathList )
    {
        if ( this.nodeLink )
        {
            for( var pathListIndex = 0; pathListIndex < pathList.length; pathListIndex++)
            {
                var pathListList = pathList[pathListIndex].split("/");
                if ( pathListList[0] == this.getValue())
                {
                    this.nodeLink.style.fontWeight = "bold";
                    pathListList.shift();
                    if ( pathListList.length > 0 )
                    {
                        var childTreeNodes = this.getChildren();
                        for( var childIndex = 0; childIndex < childTreeNodes.length; childIndex++)
                        {
                            childTreeNodes[childIndex].makeBold(pathListList.join("/")); 
                        }
                    }
                }
            }
        }
    }
    
DundasTreeNode.prototype.makeBold = function( pathPath )
    {
        if ( this.nodeLink )
        {
            var pathListList = pathPath.split("/");
            if ( pathListList[0] == this.getValue())
            {
                this.nodeLink.style.fontWeight = "bold";
                pathListList.shift();
                var childTreeNodes = this.getChildren();
                for( var childIndex = 0; childIndex < childTreeNodes.length; childIndex++)
                {
                    if ( pathListList.length > 0 )
                    {
                        childTreeNodes[childIndex].makeBold(pathListList.join("/")); 
                    }
                }
            }
        }
    }
    
DundasTreeNode.prototype.uncheckRecs = function(control, input, data)
	{
		this.setCBoxState( cbState_unchecked, control, input, true, data );
		if ( this.hasChildren())
		{
			var list = this.getChildren();
			for( var i = 0; i < list.length; i++)
			{
				list[i].uncheckRecs(control, input, data);
			}
		}
	}
	
DundasTreeNode.prototype.uncheckAll = function( control, input, data)
	{
		var elms = control.getElementsByTagName("TABLE");
		for(var i = 0; i < elms.length; i++)
		{
			var node = new DundasTreeNode( elms[i]);
			if ( node.isValid() && node.getCBoxState() == cbState_checked)
			{
			    if ( node.checkBox )
			    {
			        node.checkBox.setAttribute("dnd_checked", cbState_unchecked);
			        node.checkBox.src = eval("data.imgcb" + cbState_unchecked);
                }
			}
		}
		if ( input )
		{
		    input.value = "";
		}
	}
			
DundasTreeNode.prototype.getCBoxState = function () {
		if ( this.checkBox != null )
		{
			return this.checkBox.getAttribute("dnd_checked");
		}		
		return "0";
	}

DundasTreeNode.prototype.setCBoxState = function ( state, control, input, stopProp, data) {
		if ( this.checkBox != null )
		{
			this.checkBox.setAttribute("dnd_checked", state);
			this.checkBox.src = eval("data.imgcb" + state);

			if ( input )
			{
				if ( state == cbState_checked)
				{
					input.value = input.value + "{" + this.getPath() + "};"
				}		
				else
				{
					input.value = input.value.replace(new RegExp("\\{" + this.getPath() + "\\};","i"), "");
				}
			}			

			
			if ( stopProp == null )
			{
				if ( this.hasChildren() && state != cbState_checked)
				{
					var list = this.getChildren();
					for( var i = 0; i < list.length; i++)
					{
						if ( state == cbState_multiple )
						{
							list[i].setCBoxState( cbState_checked, control, input, true, data );
						}
						else if ( state == cbState_unchecked )
						{
							if ( list[i].getCBoxState() != cbState_unchecked )
							{
								list[i].setCBoxState( cbState_unchecked, control, input, null, data);
							}
						}
					}
				}
				
				var p = this.getParent();
				while ( p != null )
				{
					if ( state != cbState_unchecked )
					{
						p.setCBoxState( cbState_multiple, control, input, true, data );
					} 
					else if ( p.getCBoxState() == cbState_multiple)
					{
						list = p.getChildren();
						var hasChecked = false;
						for ( var i = 0; i < list.length; i++)
						{
							if ( list[i].getCBoxState() != cbState_unchecked)
							{	
								hasChecked = true;
								break;
							}
						}
						if ( !hasChecked )
						{	
							p.setCBoxState( cbState_unchecked, control, input, true, data );
						}
					}
					p = p.getParent();
				}
			}
			return true;
		}		
		return false;
	}
	
DundasTreeNode.prototype.switchExpanded = function( control, data) { 
		if (this.childContainer && this.childContainer.style)
		{
			if ( this.isExpanded() )
			{
				this.childContainer.style.display = "none";
				this.expandImage.src = data.colapsed;
			}
			else
			{
				this.childContainer.style.display = "block";
				this.expandImage.src = data.expanded;
			}
		}
	}

DundasTreeNode.prototype.getParent = function (){
		if ( this.nodeContainer )
		{
			var parentSpan = DundasTree.getParentByTag(this.nodeContainer,"DIV");
			if ( parentSpan && !parentSpan.id)
			{
			    var parentElement = this.getSiblingByTag(parentSpan, "TABLE", true);
				return parentElement ? new DundasTreeNode( parentElement) : null;
			}
		}		
		return null;
	}
	
DundasTreeNode.prototype.applySelectStyle = function ( sname ) 
{
    var classNames = "";
    if (this.nodeLink.className)
    {
        classNames = this.nodeLink.className;
        if ( classNames.indexOf(" ") != -1 )
        {
            classNames = classNames.substring(classNames.indexOf(" "));
        }
        else
        {
            classNames = "";
        }
    }
    this.nodeLink.className = sname + classNames;  
}

	
DundasTreeNode.prototype.hasChildren = function ()	{ return this.childContainer != null; }
	
DundasTreeNode.prototype.getChildren = function ()	{
		var list = new Array();
		if ( this.childContainer )
		{
			for( var i = 0; i < this.childContainer.childNodes.length; i++)
			{
				var node = this.childContainer.childNodes.item(i);
				if ( node.tagName && node.tagName.toUpperCase() == "TABLE")
				{
					list[list.length] = new DundasTreeNode( node);
				}
			}
		}	
		return list;
	}
	
DundasTreeNode.prototype.getByPath = function ( path ) {
		var pathList = path.split("/");
		if ( pathList.length > 0 )
		{
			if ( this.getValue() == pathList[0] )
			{
				if ( pathList.length > 1 )
				{
					pathList.shift();
					var path1 =  pathList.join("/");
					var c = this.getChildren();
					for( i = 0; i < c.length; i ++)
					{
						var res = c[i].getByPath( path1 );
						if ( res != null )
						{
							return res;
						}
					}
				}
				else
				{
					return this;
				}
			}
		}
		return null;	
	}
	
DundasTreeNode.prototype.getValue = function () { return this.value }

DundasTreeNode.prototype.getText  = function () 
{ 
	if (this.nodeLink )
	{
		return this.nodeLink.innerHTML;
	}
	return ""
}
	
DundasTreeNode.prototype.getPath = function() {
		var p = this.getParent();
		if ( p != null )
		{
			return p.getPath() + "/" + this.getValue();	
		}
		return this.getValue();
	}

DundasTreeNode.prototype.getSiblingByTag = function( element, tag, previous)
{
	var result = null;
	if ( element)
	{
	    var probes = 2;
	    result = previous ? element.previousSibling : element.nextSibling;
	    if (result )
	    {
	        while( result && probes > 0)
	        {
                if ( result.tagName )
                {
                    if ( result.tagName.toUpperCase() == tag )
                    {
                        return result;
                    }
                    return null;
                }
	            result = previous ? element.previousSibling : element.nextSibling;
	            probes--;
	        }
	    }
	}
    return result;
}


