/*
 * Authors: Giuseppe, Matt
 
 This file is to implement all functionality of the document that IE browsers are lacking.
 One of the function is the Xpath document.evaluate() function.
 Here we are implementing a narrow-down version of Xpath iterator to supply for IE lack of this feature
 */
try 
{	var test = (NETJOHNHENRY.name == 'NETJOHNHENRY'); } 
catch (e) 
{	NETJOHNHENRY = {}; }

NETJOHNHENRY.Document = new Object();

NETJOHNHENRY.Document.evaluate = function(contentPattern, xmlobj, par3, XPathResult__Type, par5)
{
	//This object implements the contentIterator that in standard compliand browsers is 
	// returned by the document.evaluate(...) function
	// the body of the function is at the bottom of the function body
	var contentIterator = function(contentPattern, xmlobj, par3, XPathResult__Type, par5)
	{
		//we want the parameters signature to be the same of the one of the evaluate function
		//so that we can implement other features if required.
		this.parameters = {
			contentPattern: null,
			xmlobj: null,
			par3: null,
			XPathResult__Type: null,
			par5: null
		};
		this.parameters.contentPattern = contentPattern;
		this.parameters.xmlobj = xmlobj;
		this.parameters.par3 = par3;
		this.parameters.XPathResult__Type = XPathResult__Type;
		this.parameters.par5 = par5;
		//this is the recursive engine. it works by appending childNodes to myDocFrag which is 
		// created down the bottom of contentIterator body and is of type xmlobj.createDocumentFragment()		
		this.getMatchedNodes = function(pattern, thisNode)
		{
			if (thisNode.tagName == pattern[0]) 
			{
				if (pattern.length == 1) 
				{
					myDocFrag.appendChild(thisNode);
				}
				else 
				{
					var newPattern = [];
					//the new pattern passed to the recursive function is shifted the first 
					//element from the previous pattern array. What we want is to traverse the nodes 
					//up to the point where the pattern.length = 1, if the tagName matched we add the
					//node to the myDocFrag (see above)
					for (var i = 1; i < pattern.length; i++) 
					{
						newPattern.push(pattern[i]);
					}
					for (var n = 0; n < thisNode.childNodes.length; n++) 
					{
						this.getMatchedNodes(newPattern, thisNode.childNodes[n].cloneNode(true));
					}
				}
			}
		}
		
		this.returnNodes = function(parameters)
		{
			var pattern = getPatternArray(parameters.contentPattern);
			var thisNode = parameters.xmlobj.documentElement;
			this.getMatchedNodes(pattern, thisNode);
			
			return myDocFrag;
		}
		
		var getPatternArray = function(contentPattern)
		{
			var a = new Array();
			var c = contentPattern.replace(/\/\//, '');
			a = c.split('/');
			return a;
		}
		
		var myDocFrag = xmlobj.createDocumentFragment();
		//the cursor object is what holds the matched nodes in a document fragment.
		//it also keeps the position of the of the current iterated node.
		//this is used directly by the iterateNext function.
		var cursor = new Object();
		cursor.nodes = this.returnNodes(this.parameters);
		//cursor.selectedNodeIndex = -1;
		cursor.position = -1;
		
		//this is the return object of the iterateNext function
		//it implements two attributes: textContent which returns the 
		// nodeValue and the attributes object accessed like if(node.attributes['type'].value =='b'){}
		var xpathNode = new Object();
		xpathNode.textContent;
		xpathNode.attributes = new Object();
		xpathNode.initialize = function(node)
		{
			xpathNode.textContent = node.childNodes[0].nodeValue;
			for (var i = 0; i < node.attributes.length; i++) 
			{
			
				xpathNode.attributes[node.attributes[i].name] = {
					value: node.attributes[i].value
				};
			}
			return {
				textContent: this.textContent,
				attributes: this.attributes
			};
		}
		
		this.iterateNext = function()
		{
			try 
			{
				cursor.selectedNode = cursor.nodes.childNodes[++cursor.position];
				return xpathNode.initialize(cursor.selectedNode);
			} 
			catch (e) 
			{
				return false;
			}
		}
	}//end of contentIterator function body
	try 
	{
		//if the Xpath is implemented in the browser
		return document.evaluate(contentPattern, xmlobj, par3, XPathResult__Type, par5);
	} 
	catch (e) 
	{		
		//if the Xpath is not implemented in the browser we return a scale down implementation
		// of the contentIterator that otherwise the document.evaluate would have returned		
		var c = new contentIterator(contentPattern, xmlobj, par3, XPathResult__Type, par5);
		return c;
	}
	
}
