/*
 Retorna uma nova instancia do objeto XMLHttpRequest.
 */
function xmlHttp() {
	var http = false;
	try {
		http = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try	{
			http = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (ex) {
			http = false;
		}
	}
	
	if(!http && typeof XMLHttpRequest!="undefined")
		http = new XMLHttpRequest();
	
	return http;
}

/*
 Executa uma requisição em uma url.
 */
function ajax(url, callBack) {
	try{
		var xml = xmlHttp();
		if(xml)	{
			xml.open("GET", url, true);
			xml.onreadystatechange = 
				function() {
					if(xml.readyState==4)
						if(callBack!="")
						{
							eval(callBack + "(xml);");
						}
							
				};
			xml.send(null);
		}
	}
	catch(e){
		alert("O sistema não conseguiu executar esta operação. Aguarde alguns instantes e tente novamente.");
	}
}

/*
 Define o innerHTML de um determinado objeto html.
 */
function setHtml(idObjeto, node, nomeNode, valorDefault) {
	document.getElementById(idObjeto).innerHTML = getNodeValue(node.getElementsByTagName(nomeNode).item(0), valorDefault) ;
}

/*
 Retorna o valor de um node
 */
function getNodeValue(node, valorDefault) {
	if (node && node.firstChild && node.firstChild.data)
		return node.firstChild.data;
	else
		return valorDefault;
}

/*
 Retorna um objeto html de acordo com o id dele.
 */
function getObj(id) {
	try {
		if (document.getElementById(id))
			return document.getElementById(id);
		else if(document.all)
			return document.all[id];
		else if(document.layers)
			return document.layers[id];
	}
	catch (e) {
	}
	
	return null;
}

/*
 adiciona um handler para o evento.
*/
function addEvent(obj, evType, fn){
    try {
		if (obj.addEventListener)
	        obj.addEventListener(evType, fn, true)
	    if (obj.attachEvent)
	        obj.attachEvent("on"+evType, fn)
	}
	catch(ex) {
		alert(ex.message);
	}
}