
/*
  init
    input: null
    output: null
    desc:
	this is to initiate CP Viewer
	
*/
function init()
{
	document.body.innerHTML = "";
	resourcesHash = initResources(xmlDoc);	
	displayNodes(xmlDoc,"0",0,resourcesHash);
	TREE_ITEMS = getItems(pre_items);
	document.body.innerHTML+="<ul>";
}

/*
  close
   input: null
   output: null
   desc:
	this is to finalize cp viewer runtime
	
*/
function close()
{
	document.body.innerHTML+="<\/ul>";
	document.body.innerHTML+="<span id=\"nextPrev\"></span>";
	document.body.innerHTML+="<span id=\"itemTitle\"></span>";
	document.body.innerHTML+="<input type=\"hidden\" id=\"currentItem\" value=\"0\">";
	updateCurrentItem(0,0);

}

/*
  removeLastDigit
    input: string, total digits
    output: string without n last digits
    desc:
	this is to remove n digits from string s
	
*/
function removeLastDigit(s,n)
{
	for (var ii=0;ii<n;ii++)
	{
		s=Left(s,s.lastIndexOf('.'));
	}
	return s;
}

/*
  getLastDigit
    input: string
    output: last digit of string s
    desc:
	this is to get last digit from string s
	
*/
function getLastDigit(s)
{
	return Right(s,s.length-s.lastIndexOf('.')-1);
}

/*
  Left
    input: string, total characters
    output: string contains first n characters of s
    desc:
	this is to get first n characters of s
	
*/
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

/*
  Right
    input: string, total characters
    output: string contains last n characters of s
    desc:
	this is to get last n characters of s
	
*/
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

/*
  findLastChar
    input: character to find, string
    output: position where character lies
    desc:
	find last position of character in str
	
*/
function findLastChar(ch, str)
{
	var res=-1;
	var zz;
	for (zz=0;zz<str.length;zz++)
	{
		if (str[zz].contains(ch)) res=zz;
	}
	return res;
}

/*
  mf_LoadXMLData
    input: xml document, xml file
    output: null
    desc:
	core function to load xml file
	
*/

function mf_LoadXMLData(oXMLDOM, sXML)
{

	if (oXMLDOM )
	{
	
		oXMLDOM.async = false;
		oXMLDOM.validateOnParse = false;
		
		if ((getBrowser()=="SAFARI")||(getBrowser()=="OTHER")){
			var oXML=new XMLHttpRequest();
			oXML.open("GET","../"+sXML,false);
			oXML.send(null);
			xmlDoc= oXML.responseXML;			
			display();
			
		}else{
			if (false ==  oXMLDOM.load("../"+sXML) )
			{
			
				alert(oXMLDOM.parseError.reason + " " + oXMLDOM.parseError.line);
			}
		}
		
		
	}
}

/*
  importXML
    input: null
    output: null
    desc:
	load xml file then display result
	
*/
function importXML()
{
	
	if (document.implementation && document.implementation.createDocument)
	{	
	
		xmlDoc   = document.implementation.createDocument("", "", null);
		xmlDoc.onload = function(){display();}
		res="FF";
		
	}
		// For IE.
		else if (window.ActiveXObject)
		{
		
			//xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

			xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) display();
			res="IE";
		};
			
	}		
	mf_LoadXMLData(xmlDoc, manifest_file);
	
	return res;
}

