function Generate_TOC_XML(htmldoc)
{
	var returnXML;
	var TOC_config;
	try
	{  
		var xmlUtil=new XMLDOM();
		//Load ContentExplorer Configuration File
		TOC_config=xmlUtil.load("TOC/ContentExplorer.xml");
		//Select TOC node
		var tmpNode=xmlUtil.doXPath('//toc',TOC_config)[0];		
		//create a new xml document
		var xmldoc=xmlUtil.newDocument("tree");
		//Add TOC header node
		var TOC=xmldoc.createElement("item");
		TOC.setAttribute("text",tmpNode.getAttribute("text"));
		TOC.setAttribute("id","toc");
		TOC.setAttribute("class","toc");
		TOC.setAttribute("im0",tmpNode.getAttribute("image"));
		TOC.setAttribute("im1",tmpNode.getAttribute("image"));
		TOC.setAttribute("im2",tmpNode.getAttribute("image"));
		TOC.setAttribute("tooltip",tmpNode.getAttribute("tooltip"));
		xmldoc.documentElement.appendChild(TOC);

		//Search all td node in loaded document
		var tds=htmldoc.getElementsByTagName("td");
		var counter=0;
		while(counter<tds.length)
		{
			var tdNode,parNode,image;
			var nodeTxt="";
			tdNode=tds[counter];
			counter++;
										
			var td_class=tdNode.className.toLowerCase();
			tmpNode=xmlUtil.doXPath('//toc/item[translate(@class,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="'+ 								td_class +'"]',TOC_config);
			//If class not defined in TOC configuration
			if(tmpNode.length==0)
				continue;
			
			parNode=getTOCParentHead(td_class,xmldoc,TOC_config);
			image=tmpNode[0].getAttribute("image");
			
			if(navigator.appName.lastIndexOf("Microsoft")!=-1)
				nodeTxt=tdNode.innerText;
			else
				nodeTxt=tdNode.textContent;
				
			var nodeHtml=tdNode.innerHTML;
				
			var newele=xmldoc.createElement("item");
			nodeTxt = nodeTxt.replace(new RegExp(/\n*\s*/)," ");

			newele.setAttribute("text",nodeHtml);
			
			if(tdNode.id)
				newele.setAttribute("id",tdNode.id)
			else
				newele.setAttribute("id",getTOCTempId());
				
			newele.setAttribute("class",td_class);
			newele.setAttribute("im0",image);
			newele.setAttribute("im1",image);
			newele.setAttribute("im2",image);
			newele.setAttribute("tooltip",nodeTxt);
			parNode.appendChild(newele);				
		}
		
			//Code Used For ACKNOWLEDGMENTS
			var ack_head=SearchElements("p","ack_head",htmldoc);
			if(ack_head.length>0)
			{
				var newele=xmldoc.createElement("item");
				var nodeTxt = "ACKNOWLEDGMENTS";
				
				newele.setAttribute("text",nodeTxt);
				newele.setAttribute("id","ack_head")
				newele.setAttribute("class","ack_head");
				newele.setAttribute("im0","toc_h1.gif");
				newele.setAttribute("im1","toc_h1.gif");
				newele.setAttribute("im2","toc_h1.gif");
				newele.setAttribute("tooltip",nodeTxt);
				
				var parNode=getTOCParentHead("h1",xmldoc,TOC_config);
				parNode.appendChild(newele);	
			}
			// End ACKNOWLEDGMENTS

			// Used For REFERENCES
			var refID="";
			var objs=htmldoc.getElementsByTagName("div");
			for(var i=0;i<objs.length;i++)
			{
				if(objs[i].className.toLowerCase()=="bibliography")
				{
					var paras=objs[i].getElementsByTagName("p");
					for(var i=0;i<paras.length;i++)
					{
						if(paras[i].className.toLowerCase()=="bib_entry")	
						{
							refID=paras[i].id;
							break;
						}
					}
					break;
				}
			}
			
			if(refID!="")
			{
				var newele=xmldoc.createElement("item");
				var nodeTxt = "REFERENCES";
			
				newele.setAttribute("text",nodeTxt);
				newele.setAttribute("id","bib_head")
				newele.setAttribute("class","Bib_entry");
				newele.setAttribute("im0","toc_h1.gif");
				newele.setAttribute("im1","toc_h1.gif");
				newele.setAttribute("im2","toc_h1.gif");
				newele.setAttribute("tooltip",nodeTxt);
			
				var parNode=getTOCParentHead("h1",xmldoc,TOC_config);
				parNode.appendChild(newele);	
			}
			//End  REFERENCES
		
		var itemNodes=xmlUtil.doXPath('//item',xmldoc);
		if(itemNodes.length==1)
			return "";
		returnXML=xmlUtil.getXml(xmldoc.documentElement.firstChild);
	}
	catch(e)
	{
		var msg=e.message;
		returnXML="";
	}
	return returnXML;
}	


function LTrimtest(str)
	{	
		try
		{			
			var returnValue="";
			if (str.length>0)
			{				
				for(var i=0;str.charAt(i)==" ";i++);
				{
					returnValue= str.substring(i,str.length);
				}
			}			
			return returnValue;
		}
		catch(e)
		{
		}
	}
	
function trimtest(s) 
	{
		try
		{

			while (s.substring(0,1) == ' ') 
			{
				s = s.substring(1,s.length);
			}
			while (s.substring(s.length-1,s.length) == ' ')
			{
				s = s.substring(0,s.length-1);
			}
			return s;
		}
		catch(e)
		{}
}
	
function getTOCParentHead(childClassName,docObj,configObj)
{
	if(!docObj)
		return null;
	
	if(!configObj)
		return null;
	
	try
	{	
		var xmlUtil=new XMLDOM();
		var tmpNode;
		childClassName=childClassName.toLowerCase();
		//get current node from Configuration
		tmpNode=xmlUtil.doXPath('//*[translate(@class,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="' + childClassName +'"]',configObj)[0];
		while(true)
		{
			var parClass=tmpNode.getAttribute("parent_class").toLowerCase();
			//get parent node from generated TOC				
			var nods=xmlUtil.doXPath('//*[translate(@class,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="' + parClass + '"]',docObj);		
			if(nods && nods.length>0)
			{
				return nods[nods.length-1];
			}
			//get parent node from Configuration
			tmpNode=xmlUtil.doXPath('//*[translate(@class,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="'+ parClass +'"]',configObj)[0];
		}
		return null;	
	}
	catch(ex)
	{
		return null;
	}
}

function getTOCTempId()
{
	return Math.floor(Math.random()*1111);
}
//////////////////////////////////Generate Figure XML///////////////////////////////////////////////	

function Generate_Figure_XML(htmldoc,configNode)
{
	
	var objectList=getChapterFigureList(htmldoc);
	var image=configNode.getAttribute("image");
	if(objectList.length<=0)
		return;
	var xmlString='<item text="' + configNode.getAttribute("text") +'" tooltip="'+ configNode.getAttribute("tooltip") +'" id="Figure" im0="'+ image + '" im1="'+ image +'" im2="'+ image + '">';
	
	for(var i=0;i<objectList.length;i++)
	{	
		if(typeof(objectList[i])!="undefined")
		{
			var newid=objectList[i].Id;
			var CaptionText=objectList[i].Caption;
			var itemText=getToolTip(CaptionText);	
			var Text=getItemText(CaptionText);
			//Text=Text.substring(Text.indexOf(" ")+1);
			xmlString=xmlString+'<item text="'+Text+'" id="'+newid+'" im0="iconarrs.gif" im1="iconarrs.gif" im2="iconarrs.gif" tooltip="'+itemText+'"></item>';
		}
	}
	xmlString=xmlString+"</item>";
	return xmlString;
}

function getChapterFigureList(htmldoc)
{
	
	try
	{
		var FigureList=[];
		var figure_list=[];
		//find no. of figure where <table class=Figure >
		figure_list=SearchElements("div","divFigure",htmldoc);
		for ( var i=0; i <figure_list.length ;i++)
		{
			var tableCaption=[];
			var figNum=[];
			//Find a Figure caption for each figure where <p class=Figure_legend >
			tableCaption=SearchElements("P","Figure_legend",figure_list[i]);

			if (tableCaption.length==1)
			{
				var strTableCaption="";
				var strFigNum="";
				figNum=SearchElements("span","number",tableCaption[0]);
				if(figNum.length==1 && figNum.length!=null)
				{
					if(navigator.appName.lastIndexOf("Microsoft")!=-1)
						strFigNum=figNum[0].innerText;
					else
						strFigNum=figNum[0].textContent;
				}

				if(navigator.appName.lastIndexOf("Microsoft")!=-1)
					strTableCaption=tableCaption[0].innerText;														
				else
					strTableCaption=tableCaption[0].textContent;
					
				var repl_strFigNum="";
				if(strFigNum!="")
					repl_strFigNum=strFigNum.substring(strFigNum.indexOf(" ")+1);

				var obj={};
				obj.Id=trim(figure_list[i].id);
				
				if(strFigNum=="")
					obj.Caption=strTableCaption;
				else
					obj.Caption=strTableCaption.replace(strFigNum,repl_strFigNum+" ");

				FigureList[i]=obj;
			}
		}
		
		return FigureList;
	}
	catch(ex)
	{}
}


////////////////////////////////////////////////////////////////////////////////////////////////////	
function Generate_Table_XML(htmldoc,configNode)
{
	var objectList=getChapterNumberedTableList(htmldoc);
	var image=configNode.getAttribute("image");
	if(objectList.length<=0)
		return;
	var xmlString='<item text="' + configNode.getAttribute("text") +'" tooltip="'+ configNode.getAttribute("tooltip") +'" id="Table" im0="'+ image + '" im1="'+ image +'" im2="'+ image + '">';
	for(var i=0;i<objectList.length;i++)
	{
		if(typeof(objectList[i])!="undefined")
		{
			var newid=objectList[i].Id;
			var CaptionText=objectList[i].Caption;
			var itemText=getToolTip(CaptionText);	
			var Text= getItemText(CaptionText);
			//Text=Text.substring(Text.indexOf(" ")+1);
			xmlString=xmlString+'<item text="'+Text+'" id="'+newid+'" im0="iconarrs.gif" im1="iconarrs.gif" im2="iconarrs.gif" tooltip="'+itemText+'"></item>';
		}
	}
	xmlString=xmlString+"</item>";
	return xmlString;
}

function getChapterNumberedTableList(htmldoc)
{
	try
	{
		var TableList=[];
		var table_list=[];
		//find no. of table where class=numtable
		table_list=SearchElements("table","numtable",htmldoc);
		for ( var i=0; i <table_list.length ;i++)
		{
			var tableCaption=[];
			var tableNumber=[];
			//Find a table caption for each table 
			tableCaption=SearchElements("caption","Table_caption",table_list[i]);
			if (tableCaption.length==1)
			{
				var strTableCaption="";
				var strTableNum="";
				tableNumber=SearchElements("span","number",tableCaption[0]);
				if(tableNumber.length==1 && tableNumber.length!=null)
				{
					if(navigator.appName.lastIndexOf("Microsoft")!=-1)
						strTableNum=tableNumber[0].innerText;
					else
						strTableNum=tableNumber[0].textContent;
				}

				
				if(navigator.appName.lastIndexOf("Microsoft")!=-1)
					strTableCaption=tableCaption[0].innerText;														
				else
					strTableCaption=tableCaption[0].textContent;
				
				var repl_strTableNum="";
				if(strTableNum!="")
					repl_strTableNum=strTableNum.substring(strTableNum.indexOf(" ")+1);

				var obj={};
				obj.Id=trim(table_list[i].id);
		
				if(strTableNum=="")
					obj.Caption=strTableCaption;
				else
					obj.Caption=strTableCaption.replace(strTableNum,repl_strTableNum+" ");

				TableList[i]=obj;
			}
		}
		return TableList;
	}
	catch(ex)
	{}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

function getToolTip(itemText)
{
	itemText=itemText.replace(/&/ig,"&amp;");
	itemText=itemText.replace(/</ig,"&lt;");
	itemText=itemText.replace(/"/ig,"&quot;");
	itemText=itemText.replace(/\(/ig,"&#x0028;");//&lpar;
	itemText=itemText.replace(/\)/ig,"&#x0029;");//&rpar;
	itemText=itemText.replace(/\?/ig,"&#x003f;");//&quest;
	if(itemText.length <= 1)
		itemText="This head is missing in the content";
	return itemText;
}
function getItemText(itemText)
{
	
	if(itemText.length >60)
	{
		itemText=(itemText.substring(0,60))+"...";
	}
	
	itemText=itemText.replace(/&/ig,"&amp;");
	itemText=itemText.replace(/</ig,"&lt;");
	itemText=itemText.replace(/"/ig,"&quot;");
	itemText=itemText.replace(/\(/ig,"&#x0028;");//&lpar;
	itemText=itemText.replace(/\)/ig,"&#x0029;");//&rpar;
	itemText=itemText.replace(/\?/ig,"&#x003f;");//&quest;
	return itemText;
}
var counter=0;//Global variable
function getTempNodeId()//This function generate a unique id for tree item node
{
	counter++;
	var tempId="tmpNode"+counter;
	return tempId;
}

////////////////////////////////////////////////////////////////////////////////////////	





