// some sourcecode from fckeditor

var MYXml = function()
{}

MYXml.prototype.initXMLHttpRequest = function()
{
	// Gecko / IE7
	if ( typeof(XMLHttpRequest) != 'undefined' )
		return new XMLHttpRequest() ;

	// IE6
	try { return new ActiveXObject( 'Msxml2.XMLHTTP' ) ; }
	catch(e) {}

	// IE5
	try { return new ActiveXObject( 'Microsoft.XMLHTTP' ) ; }
	catch(e) {}

	return null ;
}

MYXml.prototype.exeCommand = function( urlToCall, asyncFunctionPointer )
{
	// call always the connector.php with parameters
	// urlToCall = uri+mycms+root+pluginshome+"php/connector.php" + urlToCall + "&lang="+def_lang+"&demo="+demo;
	
	var oFCKXml = this ;

	var bAsync = ( typeof(asyncFunctionPointer) == 'function' ) ;

	var oXmlHttp = this.initXMLHttpRequest() ;

	oXmlHttp.open( "GET", urlToCall, bAsync ) ;

	if ( bAsync )
	{
		oXmlHttp.onreadystatechange = function()
		{
			if ( oXmlHttp.readyState == 4 )
			{
				var oXml ;
				try
				{
					// this is the same test for an FF2 bug as in fckxml_gecko.js
					// but we've moved the responseXML assignment into the try{}
					// so we don't even have to check the return status codes.
					var test = oXmlHttp.responseXML.firstChild ;
					oXml = oXmlHttp.responseXML ;
				}
				catch ( e )
				{
					try
					{
						oXml = (new DOMParser()).parseFromString( oXmlHttp.responseText, 'text/xml' ) ;
					}
					catch ( e ) {}
				}

				if ( !oXml || !oXml.firstChild || oXml.firstChild.nodeName == 'parsererror' )
				{
					alert( 'The server didn\'t send back a proper XML response. Please contact your system administrator.\n\n' +
							'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')\n\n' +
							'Requested URL:\n' + urlToCall + '\n\n' +
							'Response text:\n' + oXmlHttp.responseText ) ;
					return ;
				}
				
				//xml header is not supported (supported later)
				oFCKXml.DOMDocument = oXml ;
				oFCKXml.httpText = oXmlHttp.responseText;
				
				// check errornode 
				var oErrorNode = oFCKXml.SelectSingleNode( 'Connector/Error' ) ;
				// if errornode is set show errormessage and don't call callback function
				if (oErrorNode) {
					iErrorNumber = parseInt(oErrorNode.attributes.getNamedItem('number').value, 10);
				
					alert(oErrorNode.attributes.getNamedItem('text').value);

				}
				else {
					// call callback funtion
					asyncFunctionPointer(oFCKXml);
				}
			}
		}
	}

	oXmlHttp.send( null ) ;

	if ( ! bAsync )
	{
		if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 )
			this.DOMDocument = oXmlHttp.responseXML ;
		else
		{
			alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
		}
	}
}

MYXml.prototype.SelectNodes = function( xpath )
{
	//alert(this.htmlentities(this.httpText));
	if ( navigator.userAgent.indexOf('MSIE') >= 0 )		// IE
		return this.DOMDocument.selectNodes( xpath ) ;
	else					// Gecko
	{
		var aNodeArray = [];

		var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument,
				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
		if ( xPathResult )
		{
			var oNode = xPathResult.iterateNext() ;
 			while( oNode )
 			{
 				aNodeArray[aNodeArray.length] = oNode ;
 				oNode = xPathResult.iterateNext();
 			}
		}
		return aNodeArray ;
	}
}

MYXml.prototype.SelectSingleNode = function( xpath )
{
	if ( navigator.userAgent.indexOf('MSIE') >= 0 )		// IE
		return this.DOMDocument.selectSingleNode( xpath ) ;
	else					// Gecko
	{
		var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument,
				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), 9, null);

		if ( xPathResult && xPathResult.singleNodeValue )
			return xPathResult.singleNodeValue ;
		else
			return null ;
	}
}

