/* ------------------------------- */
/* FUNCTIONS FOR XML/HTML PARSING */
/* ------------------------------- */

function ParsingTools()
{
	this.ParseString = Parsing_ParseString;
	this.GetParameterName = Parsing_GetParameterName;
	this.GetParameterList = Parsing_GetParameterList;
	this.xmlTools = new XMLTools();
	
	/* 
	Parses html string parameters and replaces it with xml node values
	Exemple:

	htmlString = "<p>#title#</p>"
	xmlNode = <node><title>Parsing Exemple</title></node>

	returns:
	newString = "<p>Parsing Exemple</p>"
	*/
	function Parsing_ParseString(xmlNode, htmlString)
	{
		var newString = htmlString;
		
		var params = this.GetParameterList(htmlString);
		
		for(var i = 0; i < params.length; i++)
		{
			var val = this.xmlTools.GetNodeValue(this.xmlTools.SelectSingleNode(xmlNode, this.GetParameterName(params[i])));
			newString = newString.replace(params[i], val);
		}
		
		return (newString);
	}

	/*
	Gets the name of the parameter
	Exemple:

	parameter = "#title#";

	returns:
	name = "title";
	*/
	function Parsing_GetParameterName(parameter)
	{
		var name = parameter.replace("#", "");
		name = name.replace("#", "");
		
		return (name);
	}

	/*
	Gets and array with all the parameters contained on the text
	Exemple: 
	text = "<p>#title#</p><br><img src=\"#imgUrl#\">"

	returns:
	params = [#title#, #imgUrl#]
	*/
	function Parsing_GetParameterList(text)
	{
		var params = new Array(0);
		
		var index1 = 0;
		var index2 = 1;
		
		while(true) 
		{
			//finds first occurrence of # on the string
			index1 = text.indexOf("#", index1);
			
			//breaks loop when no # is found on the string
			if(index1 == -1)
				break;
			
			//finds second occurence of # on the string
			index2 = text.indexOf("#", index1 + 1);
			
			//adds the name of the parameter to the list
			params.push(text.substring(index1,index2+1));
			
			index1 = index2 + 1; 
		}
		
		return (params);
	}
}
