function HTMLTreeview(sys, formID, code, posX, posY, width, height, value) {
	this.create(sys, formID, code, posX, posY, width, height, '', value);
	this.type = 1;
	this.autoCollapse = false;
	this.check = false;
	this.radio = false;
	this.maxCode = 0;
	this.id = value;
	this.color = "";
	this.border = true;
	this.selectedColor = "";
}

HTMLTreeview.inherits(HTMLElementBase);

HTMLTreeview.prototype.name = 'HTMLTreeview';
HTMLTreeview.prototype.tabable = false;
HTMLTreeview.prototype.zindex = 100000;

HTMLTreeview.prototype.clean = function() {
	this.context.innerHTML = "";
	this.designComponent(null);
	this.show();
}

HTMLTreeview.prototype.filter = function(value) {
  var url = 'treeViewFilter.do?sys='+this.sys+'&formID='+this.formID+'&comID='+this.code+'&filter='+URLEncode(value);
  getAndEvalSync2(url);
  this.refreshURL = url;
}

HTMLTreeview.prototype.designComponent = function(doc) {	
	this.tree = new Bs_Tree();
	this.otherDiv = document.createElement("div");
	this.otherDiv.id = "WFRTree" + this.code;
	this.otherDiv.style.overflow = "auto";
	
	if (this.border)
  	this.otherDiv.style.border = "1px solid";
	
	this.otherDiv.style.width = this.width + "px";
	this.otherDiv.style.height = this.height + "px";
	this.status = 'closed';
	
	/* INI DECORATION */
	this.setFontStyle ();
	
  if (this.bgColor) {
    this.otherDiv.style.backgroundColor = this.bgColor;
  }
  /* END DECORATION */
  
  this.context.appendChild(this.otherDiv);

  this.lockDiv = this.getDiv("WFRTreeLock" + this.code, 0, 0, this.width, this.height, this.zindex + 1, false);
  this.lockDiv.innerHTML = '<table width=100% height=100%><tr><td>&nbsp;</td></tr></table>';
  this.context.appendChild(this.lockDiv);
}

HTMLTreeview.prototype.setEnabled = function(enabled){
  this.enabled = enabled;
  visibleDiv(this.lockDiv, this.visible && !this.enabled);
}

HTMLTreeview.prototype.hideTree = function() {
	this.div.style.display = "none";
}

HTMLTreeview.prototype.showTree = function() {
	this.div.style.display = "block";
}

HTMLTreeview.prototype.setIcon = function(element, iconFile) {
	element.icon = iconFile;
}

HTMLTreeview.prototype.show = function() {
	// Propriedades
	this.tree.useCheckboxSystem = this.check;
	this.tree.useRadioButton = this.radio;
	this.tree.autoCollapse = this.autoCollapse;

	this.tree.drawInto(this.otherDiv.id);
}

HTMLTreeview.prototype.getRoot = function() {
	return this.tree.getElement(0);
}

HTMLTreeview.prototype.getTree = function() {
	return this.tree;
}

HTMLTreeview.prototype.setIconsHeight = function(height) {
	this.tree.imageHeight = height;
}

HTMLTreeview.prototype.setFontStyle = function() {
  var divStyle = "";
  
  divStyle += ("font-family: " + (this.font ? this.font : "Arial, Helvetica, sans-serif") + ";");
  divStyle += ("font-size: " + (this.size ? this.size : "8pt") + ";");
  divStyle += ("color: " + (this.color ? this.color : "#000000") + ";");

  if (this.weight) {
    divStyle += ("font-weight: bold;");
  }
  if (this.italic) {
    divStyle += ("font-style: italic;");
  }
  if (this.underline || this.strikeout) {
    divStyle += "text-decoration:";
    if (this.underline) {
      divStyle += " underline";
    }
    if (this.strikeout) {
      divStyle += " line-through";
    }
    divStyle += ";";
  }
  this.tree.divStyle = divStyle;
  
  if (parseInt(this.size) < 8 || parseInt(this.size) > 12) {
    if (parseInt(this.size) >= 10 && parseInt(this.size) <= 20) {
      this.setIconsHeight((parseInt(this.size)+10)+'px');
    } else if (parseInt(this.size) > 20) {
      this.setIconsHeight((parseInt(this.size)+ 20)+'px');
    } else {
      this.setIconsHeight((parseInt(this.size)+5)+'px');
    }
  }
}

HTMLTreeview.prototype.setElementDBInfo = function(element, arrInfo) {
	element.dataContainer = arrInfo;
}

HTMLTreeview.prototype.getElementDBInfo = function(element) {
	return element.dataContainer;
}

HTMLTreeview.prototype.setElementDBCodeInfo = function(element, code,
		codeParent) {
	var arrInfo = new Array;
	arrInfo['code'] = code;
	arrInfo['codeParent'] = codeParent;

	element.dataContainer = arrInfo;
}

HTMLTreeview.prototype.getElementByName = function(nameOfItem) {
	var myarr = new Array(nameOfItem);
	return this.tree.getElementByCaptionPath(myarr);

}

HTMLTreeview.prototype.getActiveElement = function() {
	return this.tree.getActiveElement();
}

HTMLTreeview.prototype.getElement = function(id) {
	return this.tree.getElement(id);
}

HTMLTreeview.prototype.focus = function() {
	return false;
}

HTMLTreeview.prototype.blur = function() {
	return false;
}

HTMLTreeview.prototype.addChild = function(parentElement, description, link, target, isOpen) {
	var elementParent = this.tree.getActiveElement();
	
	if (parentElement)
		elementParent = parentElement;

	var elementData = new Array;

	elementData.caption = description;
	if (link)
		elementData.url = link;
	if (target)
		elementData.target = target;
	if (isOpen)
		elementData.isOpen = isOpen;

	return elementParent.addChildByArray(elementData);
}

HTMLTreeview.prototype.associateRuleToElement = function(element, ruleName, ruleParams) {
	if (typeof (ruleParams) == 'undefined' || ruleParams == null) {
		ruleParams = '';
	}

	var _sys = this.sys;
	var _formID = this.formID;
	var _params = ruleParams;
	var _ruleName = ruleName;

	element.attachEvent('onClickCaption', function() {
		executeJSRuleNoField(_sys, _formID, _ruleName, _params);
	});

	// element.url = "javascript:false;"
	// element.target = "";
}

HTMLTreeview.prototype.addChildByElement = function(parentElement, element) {
	var elementParent = this.tree.getActiveElement();
	if (parentElement)
		elementParent = parentElement;

	return elementParent.addChild(element);
}

HTMLTreeview.prototype.createElement = function(description, link, target) {
	var elementData = new Array;
	elementData.caption = description;

	if (link)
		elementData.url = link;
	if (target)
		elementData.target = target;

	return elementData;
}

HTMLTreeview.prototype.getDataContainer = function(element) {
	if (element) {
		return element.dataContainer;
	}
}

HTMLTreeview.prototype.removeElement = function(element) {
	if (element) {
		this.tree.removeElement(element.id);
	}
}

HTMLTreeview.prototype.useRadioControl = function(bool) {
	this.radio = bool;
	if (bool)
		this.check = false;
}

HTMLTreeview.prototype.useCheckBox = function(bool) {
	this.check = bool;
	if (bool)
		this.radio = false;
}

HTMLTreeview.prototype.useAutoCollapse = function(bool) {
	this.autoCollapse = bool;
}

HTMLTreeview.prototype.removeAllChildsOf = function(element) {
	this.tree.removeAllChildsOf(element.id);
}

HTMLTreeview.prototype.getMaxCode = function() {
	return this.maxCode = this.findMaxCode(this.getRoot());
}

HTMLTreeview.prototype.findMaxCode = function(element) {
	var maxCode = 0;
	for (i = 0; i < element._children.length; i++) {
		if (element._children[i].id > maxCode) {
			maxCode = element._children[i].id;
		}

		if (element._children[i]._children.length > 0)
			if (this.findMaxCode(element._children[i]) > maxCode) {
				maxCode = element._children[i].id;
			}
	}
	return maxCode;
}

HTMLTreeview.prototype.loadScripts = function() {
	webrun.include("components/treeview/core/lang/Bs_Misc.lib.js");
	webrun.include("components/treeview/core/lang/Bs_Array.class.js");
	webrun.include("components/treeview/core/lang/Bs_Cookie.lib.js");
	webrun.include("components/treeview/core/util/Bs_Wddx.class.js");
	webrun.include("components/treeview/core/util/Bs_XmlParser.class.js");
	webrun.include("components/treeview/components/tree/Bs_Tree.class.js");
	webrun.include("components/treeview/components/tree/Bs_TreeElement.class.js");
	webrun.include("components/treeview/components/checkbox/Bs_Checkbox.class.js");
}

HTMLTreeview.prototype.addLoadActionEvent = function(element) {
	var key = this.getElementDBInfo(element).chave;
	var id = element.id;
	var idParent = element.parent.id;

	var func = this.onbeforeopen;

	element.attachEvent('onBeforeOpen', loadActionEventHandle(this.sys, this.formID, this.code, key, id, idParent, func));
}

HTMLTreeview.prototype.addAfterCloseActionEvent = function(element) {
	var key = this.getElementDBInfo(element).chave;
	var id = element.id;
	var idParent = element.parent.id;

	var func = this.onafterclose;

	element.attachEvent('onAfterClose', unLoadActionEventHandle(this.sys, this.formID, this.code, id, idParent, func));
}


HTMLTreeview.prototype.setOnClickEvt = function(element) {
	var func = this.onclick;
	
	if (element && func)
		element.attachEvent('onClickCaption', funcHandle(func, element.dataContainer.chave, (element.parent&&element.parent.dataContainer?element.parent.dataContainer.chave:null)));
}

HTMLTreeview.prototype.setOnDblClickEvt = function(element) {	
	var func = this.ondblclick;
	
	if (element && func)
		element.attachEvent('onDblClickCaption', funcHandle(func, element.dataContainer.chave, (element.parent&&element.parent.dataContainer?element.parent.dataContainer.chave:null)));
}

function funcHandle(func, id, parentID){
	return function(){
		func(id, parentID);
	}
}

HTMLTreeview.prototype.setOnBeforeOpenEvt = function(element) {
	var func = this.onbeforeopen;
	if (element && func)
		element.attachEvent('onBeforeOpen', funcHandle(func, element.dataContainer.chave, (element.parent&&element.parent.dataContainer?element.parent.dataContainer.chave:null)));
}

HTMLTreeview.prototype.setOnAfterOpenEvt = function(element) {
	var func = this.onafteropen;
	if (element && func)
		element.attachEvent('onAfterOpen', funcHandle(func, element.dataContainer.chave, (element.parent&&element.parent.dataContainer?element.parent.dataContainer.chave:null)));
}

HTMLTreeview.prototype.setOnBeforeCloseEvt = function(element) {
	var func = this.onbeforeclose;
	if (element && func)
		element.attachEvent('onBeforeClose', funcHandle(func, element.dataContainer.chave, (element.parent&&element.parent.dataContainer?element.parent.dataContainer.chave:null)));
}

HTMLTreeview.prototype.setOnAfterCloseEvt = function(element) {
	var func = this.onafterclose;
	if (element && func)
		element.attachEvent('onAfterClose', funcHandle(func, element.dataContainer.chave, (element.parent&&element.parent.dataContainer?element.parent.dataContainer.chave:null)));
}

HTMLTreeview.prototype.setCaptionBgColor = function(color) {	
	if(color && color != 'undefined')
    this.tree.captionBgColor = color;
}

loadActionEventHandle = function(sys, formID, code, key, id, idParent, func) {
	return function() {
		var url = "treeViewLoad.do?sys=" + sys + "&formID=" + formID
				+ "&comID=" + code + "&nodeKey=" + key + "&nodeId=" + id;
		getAndEvalSync(url);

		if (func){
			var nodeSelected = treeView.getElement(id);
			func = funcHandle(func, nodeSelected.dataContainer.chave, (nodeSelected.parent&&nodeSelected.parent.dataContainer?nodeSelected.parent.dataContainer.chave:null)); // chamada da regra antes de expandir
			func();
		}
	}
}

unLoadActionEventHandle = function(sys, formID, code, id, idParent, func) {  
	return function() {
		var treeView = eval("d.c_" + code);
		var nodeSelected = treeView.getElement(id);
		treeView.removeAllChildsOf(nodeSelected);
		var nodeLoading = treeView.addChild(nodeSelected, getLocaleMessage("LABEL.LOADING"));
		treeView.setIcon(nodeLoading,
				'components/treeview/components/tree/img/win98/loading.gif');

		if (func){
			func = funcHandle(func, nodeSelected.dataContainer.chave, (nodeSelected.parent&&nodeSelected.parent.dataContainer?nodeSelected.parent.dataContainer.chave:null)); // chamada da regra antes de expandir
			func();
		}
	}
	
}

HTMLTreeview.prototype.setHint = function(hint) {
  this.callMethod(HTMLElementBase, "setHint", [hint]);
 
  if (!isNullable(hint)) {
    if (this.otherDiv) {
      this.otherDiv.alt = hint;
      this.otherDiv.title = hint;
    }
    
    if (this.lockDiv) {
      this.lockDiv.alt = hint;
      this.lockDiv.title = hint;
    }
  }
}

HTMLTreeview.prototype.getPermissionDescription = function() {
  if (!isNullable(this.permissionDescription)) {
    return this.permissionDescription;
  }
  return this.callMethod(HTMLElementBase, "getPermissionDescription");
}