
// Classes pour la récup des données  de mondoxmlview :
function mondoXmlClientClass()
{
	var Kommunikator;
	this.Kommunikator = Kommunikator;
	var OutFunction;
	this.OutFunction = OutFunction;
	var AttributeArray;
	this.AttributeArray = AttributeArray;
	
	// CONSTRUCTOR :
    function processXmlResponse(xml)
    {
		//alert( xml );
        // XML PARSER (Cross Platform) :
        // code for IE
        var doc;
        if (window.ActiveXObject)
        {
			doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = 'false';
            doc.loadXML(xml);
			//alert( xmlObj );			
        }
        // code for Mozilla, Firefox, Opera, etc.
        else
        {
            var parser=new DOMParser();
            doc = parser.parseFromString(xml,"text/xml");
        }

		//alert(xml);
		//alert( allobjects.length );
        if( doc.hasChildNodes() )
        {
            // Html collection
            var tabResult = Array();
            var allobjects = doc.getElementsByTagName('object');
			//alert(  allobjects.length );
            for(var o=0; o < allobjects.length; o++)
            {
                tabResult[o] = Array();
                
                var title = allobjects[o].getElementsByTagName('title');
                tabResult[o]['title'] = title[0].firstChild.nodeValue;
                
                var url = allobjects[o].getElementsByTagName('url');
                tabResult[o]['url'] = url[0].firstChild.nodeValue;
                
                var section_id = allobjects[o].getElementsByTagName('section_id');
                tabResult[o]['section_id'] = section_id[0].firstChild.nodeValue;
    
                var node_id = allobjects[o].getElementsByTagName('node_id');
                tabResult[o]['node_id'] = node_id[0].firstChild.nodeValue;
    
                var parent_node_id = allobjects[o].getElementsByTagName('parent_node_id');
                tabResult[o]['parent_node_id'] = parent_node_id[0].firstChild.nodeValue;

                // Attributes :
				var attributes = allobjects[o].getElementsByTagName('attribute');
				tabResult[o]['attributes'] = Array();
				if( this.AttributeArray.length > 0 )
				{
					for(var at=0; at < this.AttributeArray.length; at++)
					{
						var valuetemp = attributes[at].getElementsByTagName('value')[0].firstChild;
						//alert( valuetemp );
						if(valuetemp)
						{
							var key_temp = attributes[at].getElementsByTagName('name')[0].firstChild.nodeValue;
							tabResult[o]['attributes'][key_temp] = Array();
							tabResult[o]['attributes'][key_temp]['type'] = attributes[at].getElementsByTagName('type')[0].firstChild.nodeValue;
							tabResult[o]['attributes'][key_temp]['value'] = valuetemp.nodeValue;
						}
					}
				}
				else
				{
					//alert(attributes.length);
					for(var at=0; at < attributes.length; at++)
					{
						var valuetemp = attributes[at].getElementsByTagName('value')[0].firstChild;
						//alert( valuetemp );
						if(valuetemp)
						{
							var key_temp = attributes[at].getElementsByTagName('name')[0].firstChild.nodeValue;
							tabResult[o]['attributes'][key_temp] = Array();
							tabResult[o]['attributes'][key_temp]['type'] = attributes[at].getElementsByTagName('type')[0].firstChild.nodeValue;
							tabResult[o]['attributes'][key_temp]['value'] = valuetemp.nodeValue;
						}
					}
				}
            }

            //alert( tabResult.length );
			this.OutFunction(tabResult);
        }
        else
        {
            //alert('no response');
        }

		return false;
    }
	this.processXmlResponse = processXmlResponse;

	function LoadResult( req, nextfunc, attributeArray )
	{
		this.Kommunikator = null;
		//this.Kommunikator = this.getXMLHTTP();
		this.Kommunikator = getXMLHTTP();
		this.Kommunikator.onreadystatechange = function()
		{
			// document.ajax.dyn.value="Wait server...";
			if( mondoXmlClient.Kommunikator.readyState == 4 )
			{
				if( mondoXmlClient.Kommunikator.status == 200 )
				{
					// document.ajax.dyn.value="Received:" + Kommunikator.responseText;					
					mondoXmlClient.processXmlResponse( mondoXmlClient.Kommunikator.responseText );				
				}
				else 
				{
					//document.ajax.dyn.value="Error: returned status code " + Kommunikator.status + " " + Kommunikator.statusText;
				}
			} 
		}; 
		
		if(typeof (attributeArray) != "undefined" && attributeArray != null )
		{
			this.AttributeArray	= attributeArray;
		}
		else
		{
			this.AttributeArray = new Array();	
		}
		
		this.OutFunction = nextfunc;

		// Si windows
		//req = 'http://w2.iarc.fr' + req;
		
		//alert( req );

		this.Kommunikator.open("GET", req , true);
		this.Kommunikator.send(null);
	}
	this.LoadResult = LoadResult;
	
    function getXMLHTTP()
    {var xhr=null; if(window.XMLHttpRequest){xhr = new XMLHttpRequest();}else if(window.ActiveXObject){try{xhr = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xhr = new ActiveXObject("Microsoft.XMLHTTP");}catch (e1){xhr = null; alert('Error creating xmlhttp');}}}else{alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");}
	return xhr;}	
	this.getXMLHTTP = getXMLHTTP;

}

var mondoXmlClient = new mondoXmlClientClass();
    



// Classes pour la récup des données  de mondoxmlview :
function mfeZAjaxClientClass()
{
	var DivID;
	this.DivID = DivID;
	var Kommunikator;
	this.Kommunikator = Kommunikator;
	var OutFunction;
	this.OutFunction = OutFunction;
	
	// CONSTRUCTOR :
	this.Kommunikator = getXMLHTTP();
	this.Kommunikator.onreadystatechange = function()
	{
		//document.ajaxdebug.innerHtml = "Wait server...";
		if(mfeZAjaxManager.Kommunikator.readyState == 4)
		{
			//alert( 'Ajax Client : Status 200' );
			if(mfeZAjaxManager.Kommunikator.status == 200)
			{
				//alert( 'Ajax Client : Status 200' );
				// document.ajax.dyn.value="Received:" + Kommunikator.responseText;
				//mfeZAjaxManager.processXmlResponse( mfeZAjaxManager.Kommunikator.responseText );
				mfeZAjaxManager.OutFunction( mfeZAjaxManager.Kommunikator.responseText );
			}
			else 
			{
				//document.ajax.dyn.value="Error: returned status code " + Kommunikator.status + " " + Kommunikator.statusText;
			}
		} 
	};
	
	function LoadResult(req, nextfunc, divid)
	{
		alert( req );
		
		this.DivID = divid;
		this.OutFunction = nextfunc;
		this.Kommunikator.open("GET", req , true);
		this.Kommunikator.send(null);
	}
	this.LoadResult = LoadResult;
	
    function getXMLHTTP()
    {var xhr=null; if(window.XMLHttpRequest){xhr = new XMLHttpRequest();}else if(window.ActiveXObject){try{xhr = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xhr = new ActiveXObject("Microsoft.XMLHTTP");}catch (e1){xhr = null; alert('Error creating xmlhttp');}}}else{alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");}
	return xhr;}	
	this.getXMLHTTP = getXMLHTTP;


}
var mfeZAjaxManager = new mfeZAjaxClientClass();
    










/* XML PARSER */
function mfXMLParserClass()
{
	/* SEARCH */
	function searchById( thexml, theid )
	{
		var searchResult = parseSearch( thexml, theid );
		return searchResult;
		
	}
	this.searchById = searchById;

	function parseSearch( node, theid )
	{
		// GOOD ID ?
		//alert( node );
		if( node.attributes != null )
		{
			var map = node.attributes;
			for(var i = 0; i < map.length; i++)
			{
				//alert( map[i].nodeValue );
				if( map[i].nodeName == 'id' )
				{
					//if( map[i].nodeValue == theid)
					if( map[i].nodeValue == 'columns')
					{
						//alert( 'Good : ' + map[i].nodeValue );
						//alert( map.innerHTML );
						//alert( node.data.toString() );
						alert( node.firstChild.data.toString() );
						alert( node.nodeValue.toString() );
						return node.nodeValue;
					}
				}
			}			
		}
		if (node.hasChildNodes) 
		{
			var child = node.firstChild;
			while (child) 
			{
				result = parseSearch( child, theid );
				if( result != false )
				{
					return result;	
				}
				child = child.nextSibling;
			}
		}
		return false;
	}
	this.parseSearch = parseSearch;


	/* DISPLAYING CONTENT */
	function inspectNamedNodeMap(map)
	{
		if (null == map)
		{
			return 'null';
		}
	
		var result = '[';
		for(var i = 0; i < map.length; i++)
		{
			result += map[i].nodeName + '="' + map[i].nodeValue + '" ';
		}
		return result + ']';
	}
	this.inspectNamedNodeMap = inspectNamedNodeMap;	

	function printNode(node) 
	{
		var types = [	'', 'ELEMENT_NODE', 'ATTRIBUTE_NODE', 'TEXT_NODE', 'CDATA_SECTION_NODE', 'ENTITY_REFERENCE_NODE', 'ENTITY_NODE',
				'PROCESSING_INSTRUCTION_NODE', 'COMMENT_NODE', 'DOCUMENT_NODE', 'DOCUMENT_TYPE_NODE', 'DOCUMENT_FRAGMENT_NODE', 'NOTATION_NODE']
		return('<li>			Type: <strong>'		+ types[node.nodeType] +
					'</strong> - 	Name: <strong>'		+ node.nodeName +
					'</strong> - 	Value: <strong>'	+ node.nodeValue +
					'</strong> - 	Attributes: <strong>'	+ inspectNamedNodeMap(node.attributes) +
					'</strong></li>')
	}
	this.printNode = printNode;

	function preOrderPrint(node) 
	{
		var result = printNode(node)
		if (node.hasChildNodes) 
		{
			var child = node.firstChild
			result += ('<ul>')
			while (child) 
			{
				result += preOrderPrint(child)
				child = child.nextSibling
			}
			result += ('</ul>')
		}
		return result
	}
	this.preOrderPrint = preOrderPrint;

	/* ENTRY POINT */
	function affiche( thexml )
	{
		//var dom = openDocument('XMLInvoice/invoiceSimple.xml')
		document.getElementById("urlContent").innerHTML = '<ul>' + preOrderPrint(thexml)+ '</ul>';
		makeAjaxLinks();
	}
	this.affiche = affiche;
}
var mfXMLParser = new mfXMLParserClass();


