{TSWIDGET.Xml={};} 
TSWIDGET.Xml.Xml2Object = function(){
	this.init.apply(this,arguments);
}

TSWIDGET.Xml.Object = function(){};

TSWIDGET.Xml.Xml2Object.prototype = {
	xml:null,
	xmlDoc:null,
	encodeNewLine:false,
	/**
	 * initialize the Xml2Object
	 * @param {Object} stgXml
	 **/
	init:function(){
		stgXml = arguments[0];
		if(arguments.length>1)
			if(arguments[1]==true){
				stgXml = arguments[0].replace(/(\n)/g,'-newLine-');
				this.encodeNewLine  = true;
			}
		this.xml = stgXml;
		this.loadXmlDoc();
	},
	/**
	 * load a xml doc object from the xml string
	 */
	loadXmlDoc:function(){
		if(this.xml){
			if (window.ActiveXObject){
				this.xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
				this.xmlDoc.loadXML(this.xml);
			}else if(document.implementation){
				var parser = new DOMParser();
				this.xmlDoc = parser.parseFromString(this.xml,"text/xml"); 
			} 
		}
	},
	/**
	 * get object from xml
	 */
	getObject:function(){
		var obj = null;
		if(this.xmlDoc){
			var child = this.xmlDoc.firstChild;
			while(child){
				if(child.nodeType == 1){
					obj = new TSWIDGET.Xml.Object();
					obj[child.nodeName] = this.node2Object(child);
					break;
				}
				child = child.nextSibling;
			}
		}
		return obj;
	},
	/**
	 * convert an xml node to object
	 * @param {Object} node
	 */
	node2Object:function(node){
		var child;
		if(!node)
			return null;
		var obj = new TSWIDGET.Xml.Object();
		for (var i = 0; i < node.attributes.length; i++){
			var attr = node.attributes[i];
			var attrName = "@" + attr.name;
			obj[attrName] = attr.value;
			if(this.encodeNewLine)
				obj[attrName] = attr.value.replace(/(-newLine-)/g,'\n');
		}
		if(this.nodeHasValue(node)){
			try{
				child = node.firstChild;
				if (child.nodeType == 3 /* TEXT_NODE */)
					obj[child.nodeName] = this.encode(child.data);
				else if (child.nodeType == 4 /* CDATA_SECTION_NODE */){
					obj[child.nodeName] = child.data;
					if(this.encodeNewLine)
						obj[child.nodeName] = child.data.replace(/(-newLine-)/g,'\n');
					
				}
					//obj[child.nodeName] = child.data;
			} catch (e) {}
		}else{
			child = node.firstChild;
			while (child){
				if (child.nodeType == 1 /* Node.ELEMENT_NODE */){
					var isArray = false;
					var tagName = child.nodeName;
					if (obj[tagName]){
						if (obj[tagName].constructor != Array){
							var curValue = obj[tagName];
							obj[tagName] = new Array;
							obj[tagName].push(curValue);
						}isArray = true;
					}
					var childObj = this.node2Object(child);
					if (isArray)
						obj[tagName].push(childObj);
					else
						obj[tagName] = childObj;
				}
				child = child.nextSibling;
			}
		}
		return obj;
	},
	nodeHasValue:function(node){
		if (node){
			var child = node.firstChild;
			if (child && child.nextSibling == null && (child.nodeType == 3 /* Node.TEXT_NODE */ || child.nodeType == 4 /* CDATA_SECTION_NODE */))
			return true;
		}
		return false;
	},
	encode:function(str){
		if (str && str.search(/[&<>"]/) != -1){
			str = str.replace(/&/g, "&amp;");
			str = str.replace(/</g, "&lt;");
			str = str.replace(/>/g, "&gt;");
			str = str.replace(/"/g, "&quot;");
			if(this.encodeNewLine)
				str = str.replace(/(-newLine-)/g,'\n');
		}
		return str;
	}
	
}