
  /////////
 // CNode
//
function CNode(id, text, value)
{
	this.id = id;
	this.text = text;
	this.value = null;
	this.child = [];
	this.parent = [];
	return this;
}

// CNode.addParent
//
function CNode_addParent(node)
{
	this.parent[this.parent.length] = node;
	node.child[node.child.length] = this;
	return node;
}
CNode.prototype.addParent = CNode_addParent;

//
// CNode.addChild
//
function CNode_addChild(node)
{
	this.child[this.child.length] = node;
	node.parent[node.parent.length] = this;
	return node;
}
CNode.prototype.addChild = CNode_addChild;

//
// CNode.findChild
//
function CNode_findChild(text)
{
	for (var i=0; i<this.child.length; i++)
		if ( this.child[i].text == text )
			return this.child[i];
	return null;
}
CNode.prototype.findChild = CNode_findChild;


  /////////////
 // CTreeView
//
function CTreeView(id)
{
	this.id = id;
	this.cls = "CTreeView";

	// root 
	this.root = new CNode(0, "root");
	this.all = [this.root];

	// selected
	this.selected = null;

	// event
	this.onclick = function(id){};
	this.ondblclick = function(id){};
}

//
// CTreeView.clear
//
function CTreeView_clear()
{
	for (var i=0; i<this.all.length; i++)
		delete this.all[i];
	this.root = new CNode(0, "root");
	this.all = [this.root];
}
CTreeView.prototype.clear = CTreeView_clear;

//
// CTreeView.addNode
//
function CTreeView_addNode(parent, text, value, cls, custom, title)
{
	if ( IsUndefined(value) ) value = null;
	if ( IsEmpty(cls) )	cls = this.cls;
	if ( IsEmpty(custom) ) custom = "";
	if ( IsEmpty(title) ) title = "";
	
	var i = this.all.length;
	var node = new CNode(i, text, value);
	node.cls = cls;
	node.custom = custom;
	node.title = title;
	this.all[i] = node;
	
	if ( parent == null )
		parent = this.root;
	return parent.addChild(node);
}  
CTreeView.prototype.addNode = CTreeView_addNode;



//
// CTreeView.nodeHTML
//
function CTreeView_nodeHTML(node)
{
	var i;
	var HTML = "";
	var baseId = this.id + "_" + node.id;

	if ( node.child.length > 0 )
	{
		HTML += '\n<DD ID="' + baseId + '" CLASS="' + node.cls + '-clsDD" NOWRAP>';
		HTML += '<TABLE><TR TITLE="'+ node.title +'">';
		HTML += '<TD NOWRAP ID="' + baseId + '_sign" CLASS="' + node.cls + '-clsTD-signPlus" NAME="+" onclick="' + this.id + '.expand(\'' + node.id + '\', event);" >&nbsp;</TD>';
		HTML += '<TD NOWRAP ID="' + baseId + '_icon" CLASS="' + node.cls + '-clsTD-iconClose" onmousedown="' + this.id + '.select(\'' + node.id + '\');" ondblclick="' + this.id + '.expand(\'' + node.id + '\', event);">&nbsp;</TD>';
		HTML += '<TD NOWRAP ID="' + baseId + '_text" CLASS="' + node.cls + '-clsTD-text" onmousedown="' + this.id + '.select(\'' + node.id + '\');" ondblclick="' + this.id + '.expand(\'' + node.id + '\', event);">' + node.text + '</TD>';
		HTML += '<TD NOWRAP ID="' + baseId + '_custom" CLASS="' + node.cls + '-clsTD-custom">'+ node.custom +'</TD>';
		HTML += '</TR></TABLE></DD>';
		
		// sub folder
		HTML += '\n<DL ID="' + baseId + '_sub" CLASS="CTreeView-clsDL" STYLE="display: none;">';

		for (i=0; i<node.child.length; i++)
		{
			HTML += this.nodeHTML(node.child[i]);
		}

		HTML += '</DL>';
	}
	else
	{
		HTML += '\n<DD ID="' + baseId + '" CLASS="' + node.cls + '-clsDD" onmousedown="' + this.id + '.select(\'' + node.id + '\', event);" NOWRAP>';
		HTML += '<TABLE><TR TITLE="'+ node.title +'">';
		HTML += '<TD NOWRAP CLASS="' + node.cls + '-clsTD-iconItem">&nbsp;</TD>';
		HTML += '<TD NOWRAP ID="' + baseId + '_text" CLASS="' + node.cls + '-clsTD-text" ondblclick="' + this.id + '.ondblclick(\'' + node.id + '\');">' + node.text + '</TD>';	
		HTML += '<TD NOWRAP ID="' + baseId + '_custom" CLASS="' + node.cls + '-clsTD-custom">'+ node.custom +'</TD>';
		HTML += '</TR></TABLE></DD>';
	}
	return HTML;
}
CTreeView.prototype.nodeHTML = CTreeView_nodeHTML;

//
// CTreeView.HTML
//
function CTreeView_HTML()
{
	var i;
	var HTML = '<DIV onselectstart="return false;" onmousedown="return false;" ID="' + this.id + '_idDIV" CLASS="' + this.cls + '-clsDIV"><DL CLASS="' + this.cls + '-clsDL-root" NOWRAP ID="' + this.id + '">';
	// Per gestire il mouse sul primo elemento in IE
	HTML += '\n<DD><TABLE><TR><TD CLASS="' + this.cls + '-clsTD-root"></TD></TR></TABLE></DD>';
	
	for (i=0; i<this.root.child.length; i++)
		HTML += this.nodeHTML(this.root.child[i]);

	HTML += '\n</DL></DIV>';

	return HTML;
}  
CTreeView.prototype.HTML = CTreeView_HTML;

//
// CTreeView_writeHTML
//
function CTreeView_writeHTML()
{
	document.write(this.HTML());
}  
CTreeView.prototype.writeHTML = CTreeView_writeHTML;

//
// CTreeView.expand
//
function CTreeView_expand(id, e)
{
	if ( IE ) e = window.event;
	
	if ( e.altKey || e.ctrlKey || e.shiftKey )
		return this.expandAll(id);
		
	var prefix = this.id + "_" + id;
	var sign = document.getElementById(prefix + "_sign");
	if ( sign == null )
		return;
	
	var ico = document.getElementById(prefix + "_icon");
	var sub = document.getElementById(prefix + "_sub");
	if ( sign.className.search(/-signPlus/) > 0 )
	{
		sub.style.display = "block";
		sign.className = sign.className.replace(/-signPlus/, "-signMinus"); 
		ico.className = ico.className.replace(/-iconClose/, "-iconOpen");
	}
	else
	{
		sub.style.display = "none";
		sign.className = sign.className.replace(/-signMinus/, "-signPlus"); 
		ico.className = ico.className.replace(/-iconOpen/, "-iconClose");
	}
}
CTreeView.prototype.expand = CTreeView_expand;

//
// CTreeView.expandAll
//
function CTreeView_expandAll(id, bExpand)
{
	if ( IsUndefined(bExpand) )
	{
		var sign = document.getElementById(this.id + "_" + id + "_sign");
		if ( sign == null )
			return;
		bExpand = (sign.className.search(/-signPlus/) > 0) ? true : false;
	}
	this.expandAllChild(id, bExpand);
}
CTreeView.prototype.expandAll = CTreeView_expandAll;

//
// CTreeView.expandAllChild
//
function CTreeView_expandAllChild(id, bExpand)
{
	var node = this.all[id];
	for (var i=0; i<node.child.length; i++)
		if ( node.child[i].child.length )
			this.expandAllChild(node.child[i].id, bExpand);
	
	var prefix = this.id + "_" + id;
	var sign = document.getElementById(prefix + "_sign");
	if ( sign == null )
		return;
	var ico = document.getElementById(prefix + "_icon");
	var sub = document.getElementById(prefix + "_sub");
	
	if ( bExpand )
	{
		sub.style.display = "block";
		sign.className = sign.className.replace(/-signPlus/, "-signMinus"); 
		ico.className = ico.className.replace(/-iconClose/, "-iconOpen");
	}
	else
	{
		sub.style.display = "none";
		sign.className = sign.className.replace(/-signMinus/, "-signPlus"); 
		ico.className = ico.className.replace(/-iconOpen/, "-iconClose");
	}
}
CTreeView.prototype.expandAllChild = CTreeView_expandAllChild;

//
// CTreeView.select
//
function CTreeView_select(id)
{
	var prefix; 
	
	if ( this.selected != null )
	{
		prefix = this.id + "_" + this.selected.id;	
		document.getElementById(prefix + "_text").className = this.selected.cls + "-clsTD-text";
		document.getElementById(prefix + "_custom").className = this.selected.cls + "-clsTD-custom";
	}
	prefix = this.id + "_" + id;
	document.getElementById(prefix + "_text").className = this.all[id].cls + "-clsTD-text-selected";
	document.getElementById(prefix + "_custom").className = this.all[id].cls + "-clsTD-custom-selected";
	this.selected = this.all[id];
	
	this.onclick(this.selected);
}
CTreeView.prototype.select = CTreeView_select;

