/*	*	Xml.js	v1.0
	*	Xml Object
	*
	*	Copyright (c) 2007, Sebastian Gieseler info@my-byte.de
	*	All rights reserverd.
*/

var Xml = new Object;
Xml.construct = function () {
	
	var thisObj = this;
	thisObj.doc = null;
	thisObj.xml = null;
	thisObj.array = new Array();
		
	try {
		
		thisObj.parseString = function(string){
			thisObj.doc = string ? string : thisObj.doc;
			if (window.ActiveXObject){
				thisObj.xml = new ActiveXObject("Microsoft.XMLDOM");
				thisObj.xml.async = "false";
				thisObj.xml.loadXML(thisObj.doc);
			}
			else {
				var parser=new DOMParser();
				thisObj.xml = parser.parseFromString(thisObj.doc,"text/xml");
			}
			return thisObj.xml;
		}
		
		thisObj.mkArray = function(xml){
			thisObj.xml = xml ? xml : thisObj.xml;
			var root = thisObj.xml.documentElement;
			for (var x=0;x<root.childNodes.length;x++){
				var child = root.childNodes[x] ? root.childNodes[x] : false;
				if(child){
					var childName = child.nodeName ? child.nodeName : false;
					if((typeof(child) == "object") && !child.firstChild.nodeValue){
						thisObj.array[childName] = thisObj.getRecursive(child, childName);
					}
					else{
						thisObj.array[childName] = child.firstChild.nodeValue;
					}
				}
			}
			return thisObj.array;
		}
		
		thisObj.getRecursive = function(child, key) {
			if((typeof(child) == "object") && !child.firstChild.nodeValue){
				var retArray = new Array();
				if(child.childNodes){
					for (var x = 0; x < child.childNodes.length; x++) {
						var recChild = child.childNodes[x] ? child.childNodes[x] : false;
						if(recChild){
							var childName = recChild.nodeName ? recChild.nodeName : false;
							if((typeof(recChild) == "object") && recChild.firstChild && !recChild.firstChild.nodeValue){
								retArray[childName] = thisObj.getRecursive(recChild, childName);
							}
							else{
								retArray[childName] = recChild.firstChild ? recChild.firstChild.nodeValue : null;
							}
						}
					}
				}
				return retArray;
			}
			else{
				return child.firstChild ? child.firstChild.nodeValue : null;
			}
		}

	}
	catch (e) {
		thisObj.error(e.message, "Xmlp object error");
	}
}

Xml.construct.prototype = {
	error:function (message, usrmsg) {
		alert(usrmsg+": "+message);
	}
}