// Copyright Emerson Pinter @ 2007
function isXHRSupported() {
	return (window.XMLHttpRequest || window.ActiveXObject);
}
function xml_tool() {
	var xmlhttp=null;
	var req_contenttype=null;
	var loadindiv;
	var that=this;
	this.return_value_uninitialized=false;
	this.return_value_open=false;
	this.return_value_sent=false;
	this.return_value_receiving=false;
	this.return_value_loaded=false;
	this.reqinit = function () {
		// criar objeto xmlhttp
		if(window.XMLHttpRequest) {  //dom
			xmlhttp=new XMLHttpRequest();
		} else if (window.ActiveXObject) { // m$ internet exploder
			try {
			  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
				    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
				} catch (E) {
					xmlhttp=null;
				}
			}
		}
		if(xmlhttp==null) {
			alert('XmlHttp not supported');
			return(-100);
		} else {
			xmlhttp.onreadystatechange=this.xmlhttp_event_handler;
		}
	}
	this.setContentType=function(value) {
		req_contenttype=value;
	}
	this.setLoadingDiv=function(obj) {
			loadingdiv=obj;
	}
	this.user_event_handler_uninitialized=function(obj) {
	}
	this.user_event_handler_open=function(obj) {
	}
	this.user_event_handler_sent=function(obj) {
	}
	this.user_event_handler_receiving=function(obj) {
	}
	this.user_event_handler_loaded=function(obj) {
	}
	this.sendreq=function(xmlurl,method,data) {
		if(this.reqinit()==-100) {
				alert('Error initializing xmlhttp');
				return(-100);
		}
		if(typeof(loadingdiv)!="undefined" && loadingdiv!=null)
			loadingdiv.style.visibility='visible';
		xmlhttp.open(method || 'GET',xmlurl,true);
		xmlhttp.setRequestHeader("X-Pinter-Xml","0.5");
		// this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-Type", req_contenttype || "text/xml; charset=UTF-8");
		xmlhttp.send(data);
	}
	this.xmlhttp_event_handler=function() {
		switch(xmlhttp.readyState) {
			case 0:
				that.return_value_uninitialized=that.user_event_handler_uninitialized(xmlhttp);
				break;
			case 1:
				that.return_value_open=that.user_event_handler_open(xmlhttp);
				break;
			case 2:
				that.return_value_sent=that.user_event_handler_sent(xmlhttp);
				break;
			case 3:
				that.return_value_receiving=that.user_event_handler_receiving(xmlhttp);
				break;
			case 4:
				if(typeof(loadingdiv)!="undefined" && loadingdiv!=null)
					loadingdiv.style.visibility='hidden';
					that.return_value_loaded=that.user_event_handler_loaded(xmlhttp);
				break;
		} 
	}
	this.dom_walk_tree=function(element) {
		for(var i=0;i<element.childNodes.length;i++) {
			var nd=element.childNodes.item(i);
				if(nd.childNodes.length == 0) {
					switch(nd.nodeType) {
						case 1:
							document.write(nd.nodeName+'<br>');
							break;
						case 3:
							var testspace=/^\s+$/;
							var testtab=/^\t+$/;
							//var testnl=/^\n$/;
							if(! testspace.test(nd.nodeValue) && ! testtab.test(nd.nodeValue) ) {
								document.write(nd.parentNode.nodeName+': '+nd.nodeValue+'<br>');
							}
							break;
					}
				} else {
					if(nd.nodeType==1 && nd.childNodes.item(0).nodeType==1)
						document.write(nd.nodeName+'<br>');
					dom_walk_tree(nd);
				}
		}
	}
	this.dom_get_bytagname=function(xmlroot,tagname,stripwhitespaces) {
		if(xmlroot.nodeType!=1) {
			alert('I need a element!!');
			return;
		}
		var item=new Array;
		var mytag=xmlroot.getElementsByTagName(tagname);
		for(j=0;j<mytag.length;j++) {
			switch(mytag.item(j).nodeType) {
			   case 1:
			   	item[j]=mytag.item(j);
				break;
			   case 3:
				// fix me: only the first text
				item[j]=mytag.item(j).childNodes.item(0).nodeValue;
				if(stripwhitespaces) {
					item[j]=item[j].replace(/^(\s|\t)+/,""); // START
					item[j]=item[j].replace(/(\s|\t)+$/,""); // END
				}
				break;
			   default:
			   	item[j]='';
				break;
			}
		}
		if(item.length > 0)
			return item;
		return(false);
	}
	this.dom_get_bytagname_first=function(xmlobj,tagname,strip) {
		var getbytag=this.dom_get_bytagname(xmlobj,tagname,strip);
		if(getbytag && getbytag[0].childNodes && getbytag[0].childNodes.item(0)) {
			return this.dom_get_bytagname(xmlobj,tagname,strip)[0].childNodes.item(0).nodeValue;
		} else {
			return '';
		}
	}
}



