var JadeXML = {
	toXML: function(value) {
		var dom = null;
		if (window.DOMParser) {
			try { 
				dom = (new DOMParser()).parseFromString(value, "text/xml"); 
			} 
			catch (e) { dom = null; }
		}
		else if (window.ActiveXObject) {
			try {
				dom = new ActiveXObject('Microsoft.XMLDOM');
				dom.async = false;
				if (!dom.loadXML(value))
					window.alert(dom.parseError.reason + dom.parseError.srcText);
				} 
			catch (e) { dom = null; }
		}
		else{ alert("cannot parse xml string!"); }
		return dom;
	},
	toJson: function(value) {
		var obj = {};
		return Jade.XML._toObject(value);
	},
	_toObject: function(xml,obj) {
		if(typeof(xml.firstChild)=="undefined" || xml.firstChild==null){ return ""; }
		switch(xml.firstChild.nodeType) {
			case 1: case 9:
				if(typeof(obj)=="undefined"){ obj={} }
				for(var i=0;i<xml.childNodes.length;i++){
					var child = xml.childNodes[i];
					obj[child.nodeName] = Jade.XML._toObject(xml.childNodes[i]);
				}
				break;
			case 3:
				obj=xml.firstChild.nodeValue;
				break;
		}
		return obj;
	},
	parseToJson: function(value){
		var xml = Jade.XML.toXML(value);
		if(xml!=null) { return Jade.XML.toJson(xml); }
		else{ return null; }
	}
}
Jade.LoadLibrary('XML',JadeXML);