function TreeView (id, clickHandler)
{
    this.tid = id;
    this.OnClickHandler = clickHandler;
}

TreeView.prototype.Initialize = function()
{
	document.getElementById(this.tid+'View').innerHTML += this.GetTreeNodes(null,this.tid);
}

TreeView.prototype.Show = function()
{
	if(document.getElementById(this.tid+'View').children.length==1)
	{
		this.Initialize();
	}
	
	document.getElementById(this.tid+'View').style.display = 'block';
}

TreeView.prototype.Hide = function()
{
	document.getElementById(this.tid+'View').style.display = 'none';
}

TreeView.prototype.OnCollapseClick = function(id)
{
	event.cancelBubble = true;
	
	var node = document.getElementById(this.tid+'_tn_'+id.toString());
	var subnodes = node.children[1];
	var img = node.children[0].children[0];
	
	if(node.children.length>1 && subnodes.style.display=='block')
	{
		subnodes.style.display = 'none';
		img.src='images/TreeView/plus.gif'
	}
	else if(node.children.length>1 && subnodes.style.display=='none')
	{
		subnodes.style.display = 'block';
		img.src='images/TreeView/minus.gif'
	}
	else
	{
		var loading = document.createElement("div");
		loading.innerText=loadingStr;
		loading.style.paddingLeft = '31';
		loading.style.paddingBottom = '5';
		loading.id = 'loadingStr';
		node.appendChild(loading);

		window.setTimeout(this.tid + ".AppendData(" + id.toString() + ")", 1);
	}
}

TreeView.prototype.AppendData = function(id)
{
	var node = document.getElementById(this.tid+'_tn_'+id);
	var img = node.children[0].children[0];

	var innerHtml = this.GetTreeNodes(id);

	node.removeChild(document.getElementById('loadingStr'));
	
	if(innerHtml != '<table></table>')
	{
		var sn = document.createElement("span");
		sn.style.display = 'block';
		sn.innerHTML = innerHtml;
		node.appendChild(sn);
		img.src='images/TreeView/minus.gif'
		node.className = 'TreeNode TreeFont';
	}

}

TreeView.prototype.OnNodeClick = function(id)
{
	event.cancelBubble = true;
	eval(this.OnClickHandler+'('+id+',"'+this.tid+'")');
}

TreeView.prototype.GetTreeNodes = function(id)
{
	var params = '';

	if(id!=null && id!='')
	{
		params = '&id=' + id;		
	}
	
    var strPost = "Tree.aspx?tid=" + this.tid + params;
    var objHTTP = new ActiveXObject("Microsoft.XMLHTTP");
    objHTTP.open("GET", strPost, false);
    objHTTP.send();
    return objHTTP.responseText;
}

TreeView.prototype.OnMouseOver = function()
{
	event.cancelBubble = true;
	event.srcElement.className = 'SelectedTreeNode TreeFont';
}

TreeView.prototype.OnMouseOut = function()
{
	event.cancelBubble = true;
	event.srcElement.className = 'TreeNode TreeFont';
}