/*
* Utility Functions
* 
* @author       Seisan Consulting   2-16-2006 
*/


function XMLFNCT(){
	
	this.getHTTPRequest = function(){
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		else {
			alert("You browser doesn't support xml transfers, you cannot view this!");
		}
	}

	/*
	* Function to retrieve an XML Document. 
	* Post-Condition: The XML document is returned. 
	*
	* @return		xmlDoc - an XML document 
	* @author       Seisan Consulting   2-16-2006 
	*/
	this.getXMLDocument = function() {
		var xmlDoc;
		// code for IE
		if (window.ActiveXObject){
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument){
			xmlDoc=document.implementation.createDocument("","",null);
		}
		else{
			alert('Your browser cannot handle this script');
		}
		return xmlDoc;
	}
}

var XMLFunctions = new XMLFNCT();

function XMLParser(xmlDoc){
	this.xmlDoc = xmlDoc;
	
	/*
	* Function to retrieve a node from an XML document. 
	* Pre-Condition: The XMl node must exist.
	* Post-Condition: An array of values are returned from the node specified. 
	*
	* @param        xmlDoc - the XML document the node is retrieved from.
	* @param		str - the node requested.
	* @return		An Array of the node values. 
	* @author       Seisan Consulting   2-16-2006 
	*/
	this.getNode = function(str){
		//gets node from xmlDoc, node tree is split by '/'
		try{
			var arr = str.split('/');
			return this.getNodeViaArray(this.xmlDoc.responseXML.documentElement, 0, arr);
		}
		catch(exc){
			return null;
		}
	}
	
	/*
	* Function that recursively finds an XML node at the end of an array. 
	* Pre-Condition: The XML node exists.
	* Post-Condition: If found an XML node is returned.
	*
	* @param        node - element currently at
	* @param		num - number of the element in the current array
	* @param 		arr - array containing xml node names
	* @return		If found an XML node, if not null is returned. 
	* @author       Seisan Consulting   2-16-2006 
	*/
	this.getNodeViaArray = function(node, num, arr){
		//recursive function finds node at the end of the arr array
		if(num == arr.length){
			return node;
		}
		else if(arr[num] == ""){
			return this.getNodeViaArray(node, num+1, arr);
		}
		else if(arr[num] == node.nodeName){
			return this.getNodeViaArray(node, num+1, arr);
		}
		else { 	
			var children = node.childNodes;
			for(i=0; i < children.length;i++){
				
				if(children[i].nodeName == arr[num]){
					return this.getNodeViaArray(children[i], num+1, arr);
				}
			}
			return null;
		}
	}

}
