NlUtil = {
	findElement : function(_campo, _idSrc, _idFind) {
		var _idValue = _campo.id;
		var _idx = _idValue.indexOf(_idSrc);
		var _prefix = _idValue.substring(0, _idx);
		return document.getElementById(_prefix + _idFind);
	},

	/**
	 * O elemento HTML onde foi executado o evento
	 */
	getElementEvent : function(evt) {
		var ie_var = "srcElement";
		var moz_var = "target";

		evt[moz_var] ? _element = evt[moz_var] : _element = evt[ie_var];
		return _element;
	},

	setFocus : function(_foco) {
		var _focus = function(_comp) {
			try {
				if (_comp) {
					if (_comp.select) {
						_comp.select();
					}
					_comp.focus();
				}
			} catch (err) {
			}
		}
		var _component;
		var _byId = false;
		if (typeof _foco == 'string') {
			_component = document.getElementById(_foco);
			_byId = true;
		} else {
			_component = _foco;
		}

		if (_component) {
			_focus(_component);
		} else if (_byId) {
			if (typeof NlLov != "undefined") {
				var _lov = NlLov.__LOVS[_foco];
				if (_lov) {
					var _txtDesc = document.getElementById(_foco + '_txtDesc');
					var _txtCod = document.getElementById(_foco + '_txtCod');
					if (_lov['tipLov'] == 1) {
						if (_txtCod) {
							_focus(_txtCod);
						} else {
							_focus(_txtDesc);
						}
					} else {
						if (_txtDesc) {
							_focus(_txtDesc);
						} else {
							_focus(_txtCod);
						}
					}
				}
			}
		}
	},

	trim : function(_str) {
		try {
			return _str.replace(/^\s+|\s+$/g, "");
		} catch (e) {
			return _str;
		}
	},

	lpad : function(str, pSize, pCharPad) {
		var dif = pSize - str.length;
		var ch = String(pCharPad).charAt(0);
		for (; dif > 0; dif--) {
			str = ch + str;
		}
		return (str);
	},

	rpad : function(str, pSize, pCharPad) {
		var dif = pSize - str.length;
		var ch = String(pCharPad).charAt(0);
		for (; dif > 0; dif--) {
			str = str + ch;
		}
		return (str);
	},

	importScript : function(_src) {
		var head = document.getElementsByTagName("head")[0];
		script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = getContexto() + _src;
		head.appendChild(script);
	},

	log : function(_msg) {
		if (typeof (console) == "undefined") {
			console = {
				log : function(_msg) {
					if (typeof (_msg) == "undefined") {
						_msg = "undefined";
					}
					this.d.innerHTML += _msg + '<br/>';
				}
			}
			console.d = document.createElement('div');
			console.d.className = 'nl_console_log';
			document.body.appendChild(console.d);

			console.log(_msg);
		} else {
			console.log(_msg);
		}
	}
}

function addListener(comp, type, callback) {
	if (comp.addEventListener) {
		comp.addEventListener(type, callback, false);
	} else if (comp.attachEvent) {
		comp.attachEvent("on" + type, callback, false);
	}
}

function removeListener(comp, type, callback) {
	if (comp.removeEventListener) {
		comp.removeEventListener(type, callback, false);
	} else if (comp.detachEvent) {
		comp.detachEvent("on" + type, callback, false);
	}
}

function fireClick(_element, evt) {
	if (_element.fireEvent) {
		_element.fireEvent("onclick");
	} else if (document.createEvent) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initMouseEvent('click', true, true, window, 1, evt.screenY,
				evt.screenY, evt.clientX, evt.clientY, evt.ctrlKey, evt.altKey,
				evt.shiftKey, evt.metaKey, 0, null);
		_element.dispatchEvent(evObj);
	}
}

function nlTable(_tableDivId, _rowClickId) {
	var _tableDiv = document.getElementById(_tableDivId);
	if (_tableDiv) {
		var _table = _tableDiv.parentNode;
		while (true) {
			if (_table == null || _table.className == "af_table_content") {
				break;
			} else {
				_table = _table.parentNode;
			}
		}

		if (_table != null) {
			var _fnMouseover = function(evt) {
				var _element = nlTableFindComp(evt);
				var _click = _element.getAttribute("nlRowClick");
				if (_element.className.search("selectedRow") < 0) {
					_element.className = "hoverRow"
							+ (_click ? " clickRow" : "");
				} else {
					_element.className = "selectedRow"
							+ (_click ? " clickRow" : "");
				}
			}

			var _fnMouseout = function(evt) {
				var _element = nlTableFindComp(evt);
				if (_element.className.search("selectedRow") < 0) {
					_element.className = "";
				}
			}

			var _fnClick = function(evt) {
				var _element = NlUtil.getElementEvent(evt);
				if (_element) {
					if (_element.tagName == "A") {
						return;
					}
					if (_element.tagName == "INPUT") {
						return;
					}
					if (_element.tagName == "BUTTON") {
						return;
					}
					if (_element.tagName == "IMG"
							&& _element.className.search("nlPanelPopup") >= 0) {
						return;
					}
					if (_element.tagName == "DIV"
							&& _element.id.search("nlSelectCommand") >= 0) {
						return;
					}

					var _tr = nlTableFindComp(evt);

					var _clickId = _tr.getAttribute("nlRowClickId");

					// Busca por nl:selectCommand
					var _divs = _tr.getElementsByTagName("DIV");
					for (i = 0; i < _divs.length; i++) {
						var _found = false;
						if (_clickId) {
							if (_divs[i].id
									&& _divs[i].id
											.search("nlSelectCommand_group_"
													+ _clickId) >= 0) {
								_found = true;
							}
						} else {
							if (_divs[i].className
									&& _divs[i].className
											.search("nlSelectCommand\\w*_group") >= 0) {
								_found = true;
							}
						}
						if (_found) {
							for (j = 0; j < _divs[i].childNodes.length; j++) {
								if (_divs[i].childNodes[j].tagName == "DIV"
										&& _divs[i].childNodes[j].id
												.search("nlSelectCommand\\w*_btnShow_") >= 0
										&& _divs[i].childNodes[j].style.display != "none") {
									// Cancela a propagacao do evento
									evt.cancelBubble = true;
									fireClick(_divs[i].childNodes[j], evt);
									return;
								}
							}
							return;
						}
					}

					var _links = _tr.getElementsByTagName("A");
					for (i = 0; i < _links.length; i++) {
						if (_clickId) {
							if (_links[i].id
									&& _links[i].id
											.search(":" + _clickId + "$") >= 0) {
								fireClick(_links[i], evt);
								return;
							}
						} else {
							fireClick(_links[i], evt);
							return;
						}
					}
				}
			}

			for (i = 0; i < _table.rows.length; i++) {
				var _trow = _table.rows[i];
				if (_trow.cells[0].tagName != 'TH') {
					// verifica se nao é uma linha de detalhe
					var _tds = _trow.getElementsByTagName("TD");
					if (!(_tds.length > 0 && _tds[0].className && _tds[0].className
							.search("af_table_detail") >= 0)) {
						addListener(_trow, "mouseover", _fnMouseover);
						addListener(_trow, "mouseout", _fnMouseout);
						addListener(_trow, "click", _fnClick);
						_trow.setAttribute("nlRowAnimation", true);

						var _links = _trow.getElementsByTagName("A");
						if (_links.length > 0) {
							_trow.setAttribute("nlRowClick", true);
							if (_rowClickId) {
								_trow.setAttribute("nlRowClickId", _rowClickId);
							}
						}
					}
				}
			}
		}
	}
}

function nlTableFindComp(evt) {
	var _element = NlUtil.getElementEvent(evt);
	while (!_element.getAttribute("nlRowAnimation")) {
		_element = _element.parentNode;
	}
	return _element;
}

function nlTableRowSelect(_rowSelectedId) {
	var _div = document.getElementById(_rowSelectedId);
	if (_div)
		_div.parentNode.parentNode.className = 'selectedRow';
}

function elementPosition(element) {
	var parent = element;

	selectedPosX = 0;
	selectedPosY = 0;

	while (parent != null) {
		if (parent.currentStyle) {
			var _position = parent.currentStyle['position'];
		} else if (window.getComputedStyle) {
			var _position = document.defaultView.getComputedStyle(parent, null)
					.getPropertyValue('position');
		}

		if (_position == 'absolute') {
			break;
		}
		selectedPosX += parent.offsetLeft;
		selectedPosY += parent.offsetTop;
		parent = parent.offsetParent;
	}

	return {
		x : selectedPosX,
		y : selectedPosY
	};
}

