function fTreemenu(name){
	this.name = name;
	this.nodes = [];
	this.root = new Node(-1,-1,'root');
	this.def = {
				iconClose : "../images/img-plus.gif",
				iconOpen : "../images/img-minus.gif"
				}
}

function Node(id,indent,text,target,url,isOpen){
	this.id = id;
	this.text = text;
	this.indent = indent;
	this.isOpen = isOpen;
	this.parent = null;
	this.childs = [];
	this.target = target;
	this.url = url;
	this.selected  = -1;
}

	fTreemenu.prototype.add = function (label,target,url,isOpen){
		id = this.nodes.length;	

		var indent = 0
		while (label.charAt(indent)==' ') indent++;	
	
		isOpen = (isOpen==0) ? false : true; 

		//create nodes
		var node = new Node(id,indent,label,target,url,isOpen);
		
		this.nodes[this.nodes.length] = node;
	
		//create/Store parents node
		for (var i=this.nodes.length-1; i>=0; i--){
			if (this.nodes[i].indent < indent){
				node.parent = this.nodes[i];
				break;
			}
		}
		if (!node.parent) node.parent = this.root;
	
		node.parent.childs[node.parent.childs.length] = node;
	}


	fTreemenu.prototype.toString = function(){
		var str = '<div class="bk_menubg">';
		var menu = '';
		var lastIndent = 0;
		var prevNodeId = 0
		for (var id=0; id<this.nodes.length; id++){
			var node =  this.nodes[id];
	
			if (lastIndent < node.indent) {
				lastIndent = node.indent;
				str += '<div id="'+ this.name + 'Subnode' + prevNodeId
					+ '" style="display:'
					+((node.isOpen) ? 'block' : 'none') +'">';
			}
			
			while (lastIndent > node.indent) {lastIndent--; str += '</div>';}	

			str += this.writeNode(node);		

			if (id == (this.nodes.length-1)) {
				for(var i=lastIndent; i>0; i--) str += '</div>';
			}
			
			prevNodeId = node.id;
		}
		str += '</div>';
	
		return str;
	}

	fTreemenu.prototype.writeNode = function (node){
		//set class or format font
		if (this.selected==-1) this.selected = node.id;
		vcls = this.SetNodeClass(node,"block",this.selected);
		vclass = ' class="' + vcls + '"';

		var str = '<div  '+vclass+'>' ; 
			str += this.writeNodeIndent(node) + this.writeNodeIcon(node) + this.writeNodeText(node); 
		str += '</div>';
		return str;
	}

	fTreemenu.prototype.writeNodeIndent = function(node){
		var str = '';
		var space = '';
		if (node.indent < 1) return '';
		for (var i=0; i<(node.indent * 2); i++) {
			space += '&nbsp';
		}
		str += space;
		return str;
	}

	fTreemenu.prototype.writeNodeIcon = function(node){
			var icon = '';
			if (node.childs.length > 0){
				if (node.isOpen == false) {
					icon = '<img id="'+this.name+'NodeIcon'+ node.id +'" src="'+this.def.iconClose+'" align="middle">';
				}else{
					icon = '<img id="'+this.name+'NodeIcon'+ node.id +'" src="'+this.def.iconOpen+'" align="middle">';
				}
			}else{
					icon = '<img id="'+this.name+'NodeIcon'+ node.id +'" src="'+this.def.iconOpen+'" align="middle">';
			}
			return icon;
	}

	fTreemenu.prototype.writeNodeText = function(node){
		var str = '';
	
		//chk if node has url
		var vhref = '';
		var vclass = '';
		var vcls = '';
		var name = '';
		var vname ='';

		var vtarget = 'target="'+ node.target +'"';

		if (node.url) vhref = 'href="' + node.url + '"';
		else vhref = 'href="javascript: '+this.name+'.toggleNode('+node.id+')"';
		
		if (this.selected==-1) this.selected = node.id;
		vcls = this.SetNodeClass(node,"text",this.selected);
		vclass = ' class="' + vcls + '"';
	
		name = new String(node.text);
		vname = name.substring(node.indent,name.length);

		str += '<a id="'+this.name+'Node'+node.id+'" '+ vhref + vtarget + vclass +'>' + vname + '</a>';
		return str;
	}

	fTreemenu.prototype.toggleNode = function(id){
		this.updateNode(this.nodes[(id)]);
		this.updateNodeIcon(this.nodes[(id)]);
	}
	
	fTreemenu.prototype.updateNode = function(node){
		var Subnode = document.getElementById(this.name+'Subnode'+ node.id);
		if (Subnode) Subnode.style.display = (node.isOpen==true) ? 'none' : 'block';
		if (node.isOpen==true) {
			node.isOpen=false;
		}else {
			node.isOpen=true;
		}
	}

	fTreemenu.prototype.updateNodeIcon = function(node){
		var image = document.getElementById(this.name+'NodeIcon'+node.id);

		icon = this.getNodeIcon(node);

		image.src = icon;
	}

	fTreemenu.prototype.getNodeIcon = function(node){
		if (node.childs.length > 0) {
			if (node.isOpen == true){
				return this.def.iconOpen;
			}else if (node.isOpen == false){
				return this.def.iconClose;
			}
		}else{
			return this.def.iconOpen;
		}
	}

	fTreemenu.prototype.SetNodeClass = function(node,type,selectedId){
		var vcls = '';

		if (type == "text") {
			if (node.indent == 0 && !node.url){
				vcls = 'lbl_root';
			}else if (node.indent > 0 && !node.url){
				vcls = 'lbl_menu';
			}else{
				vcls = 'lbl_item';		
			}
		}else if ("block") {
			if (node.indent == 0 && !node.url){
				vcls = 'bk_menubar';		
			}else{
				vcls = '';		
			}			
		}

			return vcls;
	}

	var message="Right click disabled!";
	function mclick() {
		if (document.layers){
			document.captureevents(event.MOUSEDOWN);
		}
		document.onmousedown=rclick;
	}
	
	function rclick(e) {
		if (document.all) {
			if (event.button == 2) {
				alert(message);
				return false;
			}
		}
	
		if (document.layers) {
			if (e.which == 3) {
				alert(message);
				return false;
			}
		}
	}
	
	
	function submitSelected() {
		frm_products.submit();
	}

	function fShowForm(vfForm,label){
		fHideForms(vfForm);
		
		fHighlightBorder(label);
		
		document.all(vfForm).style.position = 'absolute';		
//		document.all(vfForm).style.left = 20;
//		document.all(vfForm).style.top = vfy1;
		document.all(vfForm).style.visibility = 'visible';
	}

//#Hide form
	function fHideForms(vfForm) {
		if (vfForm == "view_desc" ) {
			document.all("view_spec").style.visibility = 'hidden';
			document.all("view_othr").style.visibility = 'hidden';
		}else if (vfForm == "view_spec" ) {
			document.all("view_desc").style.visibility = 'hidden';		
			document.all("view_othr").style.visibility = 'hidden';
		}else if (vfForm == "view_othr" ) {
			document.all("view_desc").style.visibility = 'hidden';
			document.all("view_spec").style.visibility = 'hidden';		
		}
	}

//#Highlight label border color
	function fHighlightBorder(label) {
		document.all(label).style.borderColor = '#CCCCCC';

		//Unhighlight label border color
		fUnHighlightBorder(label);
	}

//#Unhighlight label border color
	function fUnHighlightBorder(label) {
		if (label == "lblSpec") {
			document.all("lblDesc").style.borderColor = '#B00000';
			document.all("lblOthr").style.borderColor = '#B00000';
		}else if (label == "lblDesc") {
			document.all("lblSpec").style.borderColor = '#B00000';
			document.all("lblOthr").style.borderColor = '#B00000';
		}else if (label == "lblOthr") {
			document.all("lblDesc").style.borderColor = '#B00000';
			document.all("lblSpec").style.borderColor = '#B00000';
		}
	}


//#Highlight label background color
	function fHighlightBackground(label) {
		document.all(label).style.backgroundColor = '#CCCCCC';
	}
//#Unhighlight label background color
	function fUnHighlightBackground(label) {
		document.all(label).style.backgroundColor = '#FFFFFF';
	}	
