//--------------------------------------------------------------------------------------------------------------
// FILE: imXml.js
// Author: Ben Siroshton
//--------------------------------------------------------------------------------------------------------------

var imXml = {};
{
	imXml.parseXML = function(xml){
	
		if( xml==null || xml.length==0 ){
			return null;
		}
	
		var doc = null;
		
		if (window.ActiveXObject){
			// code for IE
			doc=new ActiveXObject("Microsoft.XMLDOM");	
			doc.async="false";
			doc.loadXML(xml);
		}else{
			// code for Mozilla, Firefox, Opera, etc.
			var parser=new DOMParser();
		
		  	doc=parser.parseFromString(xml,"text/xml");
		  }
	
		return doc.documentElement;
	};
	
	imXml.findNode = function(node, nodeId){
	
		if(node==null)
			return null;
	
		var A = new String(node.nodeName);
		var B = new String(nodeId);
	
		if( A.toLowerCase()==B.toLowerCase() )
			return node;
	
		for(var i=0;i<node.childNodes.length;i++){
			A = new String(node.childNodes[i].nodeName);
			B = new String(nodeId);
		
			if( A.toLowerCase()==B.toLowerCase() )
				return node.childNodes[i];
		}
		
		return null;
	
	};
	
	imXml.findNodeRecursive = function(node, nodeId){
	
		if(node==null)
			return null;
	
		var A = new String(node.nodeName).toLowerCase();
		var B = new String(nodeId).toLowerCase();
		
		if( A==B ){		
			return node;
		}
		
		// Check Immediate Node's First
		for(var i=0;i<node.childNodes.length;i++){
			A = new String(node.childNodes[i].nodeName).toLowerCase();
	
			if( A==B ){
				return node.childNodes[i];
			}
		}
		
		// Check Immediate Node's Children
		for(var i=0;i<node.childNodes.length;i++){
			var Result = imXml.findNodeRecursive(node.childNodes[i], nodeId);
			if( Result!=null ){
				return Result;
			}
		}
	
		return null;
	
	};
	
	imXml.findNodeValue = function(node, nodeId){
	
		return imXml.nodeTextValue(imXml.findNode(node,nodeId));
	
	};
	
	imXml.findNodeValueRecursive = function(node, nodeId){
	
		return imXml.nodeTextValue(imXml.findNodeRecursive(node,nodeId));
	
	};
	
	imXml.nodeTextValue = function(node){
	
		if( node==null )
			return null;
	
		if( node.nodeType==3 )
			return node.nodeValue;	
		else if( node.nodeType==1 && node.childNodes.length>0 )
			return imXml.nodeTextValue(node.childNodes[0]);
		
		return null;
	};
	
	
	imXml.printNodes = function(node, tab){
	
		if( node==null )
			return;
		
		for(var i=0;i<tab;i++ )
			document.write( "&nbsp;&nbsp;&nbsp;&nbsp;" );
	
		document.write( "Name [ " + node.nodeName + " ] Type [ " + node.nodeType + " ] Value [ " + node.nodeValue + " ] ChildNode.Length [ " + node.childNodes.length + " ]<BR>" );
	
		for(var i=0;i<node.childNodes.length;i++ )
			printNodes( node.childNodes[i], tab+1 );
	
	};
}

