/**
* Provides basic XML-functionality
*
* Ensures cross-browser-compatibility for IE6/7 and Gecko
*
* @package Ceasy_Core
* @author Ralf Glaser
*
*
*/

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
if (typeof(XMLSerializer)=='undefined') {  // implements XMLSerializer for IE

	XMLSerializer=function() {};

	XMLSerializer.prototype.serializeToString=function(obj) {

		if (obj.nodeType==3) { // textNode

			return obj.nodeValue;

		} else {

			return obj.xml;

		}

	};

}


// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
if (typeof(DOMParser)!='undefined') { // implements missing methods and properties of "XMLDocument" and "Node" for gecko
	
	try { // from  Firefox 3.6 on readyState is supported, setting this properties throws an error
	
		XMLDocument.prototype.readyState = 0;
		XMLDocument.prototype.onreadystatechange = null;
		XMLDocument.prototype._changeReadyState= function(readyState) {

			this.readyState = readyState;

			if (typeof(this.onreadystatechange) == 'function') {
				this.onreadystatechange();
			}

		};
	} catch (e) {
		XMLDocument.prototype._changeReadyState= function(readyState) {
			// do nothing
		};
	}
		
		
	XMLDocument.prototype.parseErrorContainer= {
		errorCode: 0,
		filepos: -1,
		line: -1,
		reason: null,
		srcText: null,
		url: null,
		valueOf: function () { return this.errorCode; },
		toString: function () { return this.errorCode.toString(); }
	};

	XMLDocument.prototype._checkForErrors= function () {

		if (this.documentElement && this.documentElement.tagName == 'parsererror') {

			var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;

			reError.test(this.xml);

			this.parseErrorContainer.errorCode = -999999;
			//this.parseErrorContainer.reason = RegExp.$1;
			this.parseErrorContainer.reason = this.xml;
			this.parseErrorContainer.url = RegExp.$2;
			this.parseErrorContainer.line = parseInt(RegExp.$3);
			this.parseErrorContainer.linepos = parseInt(RegExp.$4);
			this.parseErrorContainer.srcText = RegExp.$5;

		}

	};

	XMLDocument.prototype.loadXML= function (xmlString) {

		this._changeReadyState(1);

		var parser = new DOMParser();
		var newXmlDom = parser.parseFromString(xmlString, 'text/xml');

		while (this.firstChild) {

			this.removeChild(this.firstChild);

		}

		for (var i=0; i < newXmlDom.childNodes.length; i++) {

			var newNode = this.importNode(newXmlDom.childNodes[i], true);
			this.appendChild(newNode);

		}

		this._checkForErrors();
		this._changeReadyState(4);

	};

	XMLDocument.prototype._load= XMLDocument.prototype.load;

	XMLDocument.prototype.load= function (url) {

		this._changeReadyState(1);
		this._load(url);

	};

	XMLDocument.prototype.__defineGetter__('parseError', function () {

		this._checkForErrors();

		return this.parseErrorContainer;

	});

	Node.prototype.__defineGetter__('xml', function () {

		var oSerializer = new XMLSerializer();

		var xmlText=oSerializer.serializeToString(this, 'text/xml');

		if (this.fixDisableOutputEscaping) {

			xmlText=xmlText.replace(/&amp;/g, '&');
			xmlText=xmlText.replace(/&gt;/g, '>');
			xmlText=xmlText.replace(/&lt;/g, '<');

		}

		return xmlText;

	});

	Node.prototype.__defineGetter__('text', function () {

		var text= '';

		for (var i= 0; i < this.childNodes.length; i++) {

			if (this.childNodes[i].hasChildNodes()) {

				text+= this.childNodes[i].text;

			} else {

				text+= this.childNodes[i].nodeValue;

			}

		}

		return text;

	});

}

// -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
XMLArrayizer=function() {

};

XMLArrayizer.xmlToArray=function(xmlObj) {

	var newArray={};
	var useNumericIndex=false;

	for (var i=0; i<xmlObj.childNodes.length; i++) {

		var childNodeName=xmlObj.childNodes[i].nodeName;

		if (childNodeName+'s'==xmlObj.nodeName) {

			if (!useNumericIndex) {

				useNumericIndex=true;
				newArray=[];

			}

		}

		var nodeValue=false;

		if (xmlObj.childNodes[i].childNodes.length==1 && xmlObj.childNodes[i].childNodes[0].nodeType==3) { // contains only a text-node

			nodeValue=xmlObj.childNodes[i].childNodes[0].nodeValue;

		}	else if (xmlObj.childNodes[i].childNodes.length==1 && xmlObj.childNodes[i].childNodes[0].nodeType==4) { // contains only an cdata-node

			nodeValue=xmlObj.childNodes[i].childNodes[0].nodeValue;

		}	else if (xmlObj.childNodes[i].nodeType==1) { // element-node

			nodeValue=this.xmlToArray(xmlObj.childNodes[i]);

		}

		if (nodeValue!==false) {

			if (useNumericIndex) {

				newArray[newArray.length]=nodeValue;

			} else {

				newArray[childNodeName]=nodeValue;

			}

		}

	}

	return newArray;

};

