/*
----------------------------------------------------------
Version:		0.1
Author:			RHH
Creation Date:	22/06/2005
----------------------------------------------------------
Description:
Functions to parse an XML data set.
*/

function bytXML()
{
	var xmlDocument = null;
	var xmlDataNodeList = null;
	var iDataNodeCount = 0;
	var iNodeCounter = 0;	
	
	this.iterateChildren = iterateChildren;
	this.loadXMLFromString = loadXMLFromString;
	this.getParseError = getParseError;
	this.printChildren = printChildren;
	this.getRecords = getRecords;
	this.getNextRecord = getNextRecord;
	this.getResult = getResult;
}

function printChildren(indentSpacing) 
{
/*
----------------------------------------------------------
Version:		0.1
Author:			RHH
Creation Date:	22/06/2005
----------------------------------------------------------
Description:
Debug function to print the entire dataset
*/
	return this.iterateChildren(this.xmlDocument,indentSpacing);
}

function getResult(sDataFieldName)
{
/*
----------------------------------------------------------
Version:		0.1
Author:			RHH
Creation Date:	22/06/2005
----------------------------------------------------------
Description:
Iterates around the current DATA element and return the text of the matching element.
*/
	var xmlDataNode = null;
	var sDataNodeValue = "";
	var iCounter = 0;
		
	if( this.xmlDataNodeList != null)
	{
		xmlDataNode = this.xmlDataNodeList.item(this.iNodeCounter);
		
		for(iCounter = 0; iCounter < xmlDataNode.childNodes.length; iCounter++)
		{
			if(xmlDataNode.childNodes[iCounter].tagName == sDataFieldName)
			{
					
				if (xmlDataNode.childNodes[iCounter].text != undefined)
					sDataNodeValue = xmlDataNode.childNodes[iCounter].text;	
				else if (xmlDataNode.childNodes[iCounter].textContent != undefined)
					sDataNodeValue = xmlDataNode.childNodes[iCounter].textContent;
				else
					sDataNodeValue = undefined;
					
			}
		}

	}
	
	return sDataNodeValue;
}

function getRecords()
{
/*
----------------------------------------------------------
Version:		0.1
Author:			RHH
Creation Date:	22/06/2005
----------------------------------------------------------
Description:
Obtains an XMLDOMNode List of DATA tags
*/
	this.iDataNodeCount = 0;
	this.iNodeCounter = 0;	
	
	if( this.xmlDataNodeList == null)
	{
		this.xmlDataNodeList = this.xmlDocument.getElementsByTagName("DATA");
		this.iDataNodeCount = this.xmlDataNodeList.length;
	}
		
	return this.iDataNodeCount;
}

function getNextRecord()
{
/*
----------------------------------------------------------
Version:		0.1
Author:			RHH
Creation Date:	22/06/2005
----------------------------------------------------------
Description:
Move the XMLNode
*/
	var bNextNodeFound = false;

	this.iNodeCounter++;	
	
	if(this.iNodeCounter < this.iDataNodeCount )
	{

		bNextNodeFound = true;
	}
	
	return bNextNodeFound;
}


function iterateChildren(theNode, indentSpacing) 
{
	var typeData;

	switch (theNode.nodeType) 
	{
		case 1:
			typeData = "element";
			break
		case 2:
			typeData = "attribute";
			break
		case 3:
			typeData = "text";
			break
		case 4:
			typeData = "CDATA section";
			break
		case 5:
			typeData = "entity reference";
			break
		case 6:
			typeData = "entity";
			break
		case 7:
			typeData = "processing instruction";
			break
		case 8:
			typeData = "comment";
			break
		case 9:
			typeData = "document";
			break
		case 10:
			typeData = "document type";
			break
		case 11:
			typeData = "document fragment";
			break
		case 12:
			typeData = "notation";
	}
	  var text

	  if (theNode.nodeValue != null)
	  {
		  text = indentSpacing + theNode.nodeName 
		  + " = " + theNode.nodeValue  
		  + " (Node type: " + typeData 	  + ")\n";
	  } 
	  else 
	  {
		  text = indentSpacing + theNode.nodeName 
		  + " (Node type: " + typeData   + ")\n";
	  }

	 if (theNode.childNodes.length > 0) 
	 {
		for (var loopIndex = 0; loopIndex < theNode.childNodes.length; loopIndex++) 
		{
			text += iterateChildren(theNode.childNodes(loopIndex), 	indentSpacing);
		}
	}
	return text;
}  


function loadXMLFromString (markup) 
{	
	if (typeof DOMParser != 'undefined') 
	{
		this.xmlDocument = new DOMParser().parseFromString(markup,'application/xml');
		var roottag = this.xmlDocument.documentElement;
		if ((roottag.tagName == "parserError") ||
			(roottag.namespaceURI == "http://www.mozilla.org/newlayout/xml/parsererror.xml"))
		{
			//alert("Parsing Error!");
			return false;
		}
		return true;
	}
	else if (typeof ActiveXObject != 'undefined') 
	{
		this.xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
		this.xmlDocument.loadXML(markup);
	}
	
	return this.xmlDocument.parseError;
}

function getParseError()
{
	return xmlDocument.parseError.reason;
}

function encodeCustomXMLString(sText)
{
/*
*	----------------------------------------------------------
*	Version:		0.1
*	Author:			AJM
*	Creation Date:	13/03/2008
*	----------------------------------------------------------
*	Description:	AJAX can't seem to handle £ & < > characters
*					Convert all occurrences to Hex
*
*/
	sText = sText.replace(/£/g, 'HA3');
	sText = sText.replace(/&/g, 'H26');
	sText = sText.replace(/</g, 'H3C');
	sText = sText.replace(/>/g, 'H3E');
	
	return sText;
}

function decodeCustomXMLString(sText)
{
	sText = sText.replace(/HA3/g, '£');
	sText = sText.replace(/H26/g, '&');
	sText = sText.replace(/H3C/g, '<');
	sText = sText.replace(/H3E/g, '>');
	
	return sText;
}
