/**Attempt to make some of the AJAX calls simpler and perhaps protect us from changes as thte API matures.*/

/**Given an XML Text returned from the HTTPRequestObject (or otherwise) this will parse it and return a google XML Document.
* @return google XML Document
*/
function GoogleXpathHelper(xmlTextStringOrXmlNode)
{
		//the node
		this.xml = null;
		if(xmlTextStringOrXmlNode.nodeType == null)
		{
			this.xml = xmlParse(xmlTextStringOrXmlNode);
		}
		else
		{
			this.xml = xmlTextStringOrXmlNode;
		}
	
	/**Given the xpath this will return the google value object that can provide the more specific type of value by calling one of the following methods:
	*  nodeSetValue - an array of XMLNodes
	* stringValue - if the result is a string
	* numberValue - if the result is an integer
	* booleanValue - if the result is true or false
	* 
	* @return a google value object */
	this.evaluate = function(xpathString)
	{
		return this.evaluateRelativeTo(xpathString,this.xml); 
	}
	
	/**evalutes a relative XPath relative to the node given (rootNode).
	  * The root Node must be part of the document managed by this object given during construction.
	  * @see this.evaluate(xpathString)
	  * @return same as this.evalute(xpathString)
	  */
	this.evaluateRelativeTo = function(xpathString, rootNode)
	{
		  var xpath = xpathParse(xpathString);
          var exprContext = new ExprContext(rootNode);
		  return  xpath.evaluate(exprContext);
	}

	/**@return an array of Nodes matching the xpath given.  returns an empty array rather than null*/
	this.evaluateNodeSet = function(xpathString)
	{
		return this.evaluate(xpathString).nodeSetValue();
	}		
	
	/**@return the node matching the xpath or null if none found*/
	this.evaluateSingleNode = function(xpathString)
	{
		return this.evaluateNodeSet(xpathString)[0];
	}
	
	this.evaluateNumber = function(xpathString)
	{
		return this.evaluate(xpathString).numberValue();
	}
	
	this.evaluateString = function(xpathString)
	{
		return this.evaluate(xpathString).stringValue();
	}
	
	this.evaluateBoolean = function(xpathString)
	{
	//note: .booleanValue was return true when the value was actually false.
		var booleanString = this.evaluateString(xpathString);
		var result;
		if(booleanString != null)
		{
			result = booleanString.toLowerCase().indexOf("true") > -1;
		}
		else
		{
			result = false;
		}
		return result;
	}
	
	/**searches the document for the only element to contain an attribute named 'id'.
	 * @return the google XML node with the id attribute matching the given id string or null if not found.
	 */
	this.getElementById = function(id)
	{
		var nodeSet = this.evaluateNodeSet("//*[@id='" + id + "']");
		var result;
		if(nodeSet.length == 0)
		{
			result = null;
		}
		else
		{
			result = nodeSet[0];
		}
		return result;
	}
	
	/**@return the Google XML document built from the xpath string provided during construction */
	this.getXml = function() { return this.xml; }
}