/***********************************************************************************************************************
	
											------------------------------------
											| JavaScript XML parser class V1.0 |
											------------------------------------
	
	Parameters:
		- String file url
		- String parentNode (Main Array)
	
	Attributes:
		- String useragent;
			Determines which browser is used.
		- Boolean IE;
			If the browser is IE, it is set TRUE.
		- Object XMLdoc;
			The ActiveX XML object.
		- String url;
			The path of the XML document.
		- String parent;
			The first node from which the Array should start.
		- Array output;
			The XML document transformed into an array.
	
	Methods:
		- init;
			Initializes the XML document and calls the handle method.
		- handle;
			This method puts the data from the XML document in the output attribute using the node2array method.
		- node2array;
			This method transforms the nodes into array.
			If the length of the node is more than 1, he calls itself again with this node as parameter.
	
***********************************************************************************************************************/

var parse_XML = function(url, parent) { // 
	var useragent	= navigator.userAgent.toLowerCase();
	this.IE			= useragent.indexOf('msie') > 0 ? true : false;
	this.XMLdoc		= null;
	this.url		= url;
	this.parent		= parent;
	this.output		= new Array();
	
	this.init();
}

parse_XML.prototype = {
	/****************************************************/
	init: function() {
		if(this.IE) {
			this.XMLdoc = new ActiveXObject("Microsoft.XMLHTTP");
			
			if(this.XMLdoc) {
				var tmpObj = this;
				this.XMLdoc.onreadystatechange = function() {tmpObj.handle();}//this.handle;
				this.XMLdoc.open('GET', this.url, false);
				this.XMLdoc.send();
			}
		}
		else {
			this.XMLdoc = new XMLHttpRequest();
			this.XMLdoc.open("GET", this.url, false);
			this.XMLdoc.send(null);
			this.XMLdoc.onreadystatechange = this.handle();
		}
	},
	
	/****************************************************/
	handle: function() {
		if(this.XMLdoc && this.XMLdoc.readyState == 4) {
			if(this.XMLdoc.status == 200) {
				if(this.XMLdoc.responseXML.getElementsByTagName(this.parent).length > 0) {
					this.output = this.node2array(this.XMLdoc.responseXML.getElementsByTagName(this.parent)[0]);
				}
			}
		}
	},
	
	/****************************************************/
	node2array: function(node) {
		var output = new Array();
		var j = 0;
		for(var i = 0; i < node.childNodes.length; i++) {
			if(node.childNodes[i].nodeType != 3) {
				if(node.childNodes[i].childNodes.length > 0) {
					output[j] = this.node2array(node.childNodes[i]);
				}
				else {
					output[j] = node.childNodes[0].nodeValue;
				}
				j++
			}
		}
		return output;
	},
	
	/****************************************************/
	data: function() {
		return this.output;
	}
}

function printArray(arr, deep) {
	if(typeof(deep) == "undefined") {
		deep = 0;
	}
	else {
		deep++;
	}
	
	if(typeof(arr) == "object") {
		for(a in arr) {
			var space = "";
			for(i = 0; i < deep; i++) {
				space +="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			}
			document.write(space + "[" + (!isNaN(parseInt(a)) ? a : "\"" + a +"\"") + "]");
			
			if(typeof(arr[a]) == "object") {
				document.write("&nbsp;&nbsp;=>&nbsp;&nbsp;Array ( <br/>");
				printArray(arr[a], deep);
				document.write(space + ") <br/>");
			}
			else if(typeof(arr[a]) == "string") {
				document.write("&nbsp;&nbsp;=>&nbsp;&nbsp;\"" + arr[a] + "\"<br/>");
			}
		}
	}
}
