//Calculate absolute  position of an object
function calc_X(obj)
{
	X=obj.offsetLeft;
	while (obj.offsetParent != null )
	{
		obj = obj.offsetParent;
		X+=obj.offsetLeft;
	}
	return X;
}

//Calculate absolute  position of an object
function calc_Y(obj)
{
	Y=obj.offsetTop;
	while (obj.offsetParent != null )
	{
		obj = obj.offsetParent;
		Y+=obj.offsetTop;
	}
	return Y;
}

function menuUnHighlihgt()
{
	window.event.cancelBubble = true;
	a = window.event.srcElement;
	if (window.event.srcElement != null) 
	{
		while (a!=null && a.tagName !="A" )
			a = a.parentElement;
		if (a!= null) a.className="menuItem";
	}
}

function menuHighlihgt()
{
	window.event.cancelBubble = true;
	a = window.event.srcElement;
	if (window.event.srcElement != null) 
	{
		while (a!=null && a.tagName !="A" )
			a = a.parentElement;
		if (a!= null) a.className="menuSelectedItem";
	}
}

//Showes Drop down menu 
function show(id,direction,anchor,width,parent)
{
	window.event.cancelBubble = (parent == null);
	if (!MenuActivated) 
	{
		n=menuDIVs.length;
		for (i=0;i<n;i++)
			if (document.all['M'+menuDIVs[i]] != null) 
						document.all['M'+menuDIVs[i]].className="menu";
		anchor.className="menuActive";
		return
	}
	if (parent == null)
		anchor.className="menuActiveDown";
	obj=document.all[id];
	obj.style.left = calc_X(anchor);
	obj.style.top = calc_Y(anchor)+anchor.offsetHeight+1 ;
	obj.style.width = width;
	obj.style.visibility="visible";

}
// hides all DIVs specified in menuDIVs[]
function hideAll()
{
	n=menuDIVs.length;
	for (i=0;i<n;i++)
		if (document.all[menuDIVs[i]] != null)
		{
            if (document.all[menuDIVs[i]]!=null)
    			document.all[menuDIVs[i]].style.visibility="hidden";
			if (document.all['M'+menuDIVs[i]] != null)
				document.all['M'+menuDIVs[i]].className="menu";
			
		}
	MenuActivated = false;
}


function ActivateMenu(){
	MenuActivated = !MenuActivated;
	if (!MenuActivated)
		hideAll();
}

function DocMOver()
{
	if (!MenuActivated) hideAll();
}


// used to hide all menu panels but (anchor)
function hideRest(anchor)
{
        window.event.cancelBubble = true;
        n = menuDIVs.length;
		for (i=0;i<n;i++)
        {
            if (document.all[menuDIVs[i]] != null)
                document.all[menuDIVs[i]].style.visibility="hidden";
			if (document.all['M'+menuDIVs[i]] != null) 
						document.all['M'+menuDIVs[i]].className="menu";
        }
        if (anchor != null)
    		anchor.className="menuActive";
        MenuActivated = false;
}
// used to hide all menu panels
function hideMenus() {
        n = menuDIVs.length;
		for (i=0;i<n;i++)
        {
            if (document.all['M'+menuDIVs[i]] != null)
                if (document.all['M'+menuDIVs[i]].className == 'menuActive')
                    document.all['M'+menuDIVs[i]].className = 'menu'
        }
}
// switches gray icon filter off (when mouse moves over toolbar button)
function on(obj){
    try {
		obj.firstChild.className="normal";
	} catch(e) {};
}

// switches gray icon filter on (when mouse moves out of toolbar button)
function off(obj){
	try{
		obj.className = 'menu';
	    obj.firstChild.className="Gray";
	} catch(e) {};

}


function lastReadyStateReset(){
	lastReadyState = 0;
	return true;
}

// this function is called 5 times per second to update (if needed) the innerHTML of 
// a DIV that displays Article. The HTML of an Article is loaded into Frame1 - hidden 
// IFrame
function update(){
try{
	DivContent.style.height = document.getElementsByTagName("BODY")[0].offsetHeight-120;
	DivIndex.style.height = document.getElementsByTagName("BODY")[0].offsetHeight-110;
	DivSearch.style.height = document.getElementsByTagName("BODY")[0].offsetHeight-110;
} catch(e){}

	FrameContent.style.height = document.getElementsByTagName("BODY")[0].offsetHeight-55;
	if (Navigation.style.display != "none")
		FrameContent.style.width = document.getElementsByTagName("BODY")[0].offsetWidth-290;
	else
		FrameContent.style.width = document.getElementsByTagName("BODY")[0].offsetWidth-20;

	try{
		var fl = Frame1.document.getElementsByTagName("BODY")[0].innerText.length;
		var fc = FrameContent.innerText.length;
		var d = Math.abs(fc - fl);
		if (( d > 2) || (lastReadyState!=Frame1.document.readyState) )	
		{
			s = FrameContent.innerHTML = Frame1.document.getElementsByTagName("BODY")[0].innerHTML;
			lastReadyState = Frame1.document.readyState;
		}
	}
	catch (e) {}
	
			A = FrameContent.getElementsByTagName("A");
			for (i=0;i<A.length;i++){
				A[i].target = "Frame1";
				A[i].onclick = lastReadyStateReset;
			}

	try{
		checkXML()
	}
	catch(e){}
}

function load(href)
{
lastReadyState = -1;
Frame1.location.href = href;
}

// Here we search for any level parent element with tagName=pTagName
function getParent(el, pTagName) {
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}

// onClick event handler for the tree
function handleClick()
{
	window.event.cancelBubble = false;

	obj = event.srcElement;
	tr = getParent(obj,"TR");
	if (tr==null) 
		return;
	if (isExpandable(tr))
		if (isOpen(tr))
			close_(tr);
		else
			open_(tr);
}

// closes a book - hides lower level articles in tree.
function close_(obj){
	obj.cells[0].firstChild.style.display="block";
	obj.cells[0].childNodes[1].style.display="none";
	obj.className="closed";
	nextTR = obj.parentElement.rows[obj.rowIndex+1];
	nextTR.style.display="none";
}
// openes a book - showes lower level articles in tree.
function open_(obj){
	obj.cells[0].firstChild.style.display="none";
	obj.cells[0].childNodes[1].style.display="block";
	obj.className="open";
	nextTR = obj.parentElement.rows[obj.rowIndex+1];
	nextTR.style.display="block";
}

function isOpen(obj){
	return (obj.className=="open");
}
function isExpandable(obj){
	return (obj.className=="closed" || obj.className=="open");
}



// this function has to be called when XML is (re)loaded
function xmlLoaded()
{
	btn = "";
	try{
		btn = adminBars;
	} catch(e){}

	try {
	DivContent.innerHTML = btn + buildTreeXML(XMLDoc.firstChild.childNodes,false);
	} catch(e){}
	
	try{
		insertAdminControls();
	} catch(e){}

		generateIndex();
		isXMLLoaded = true;

}		

// This one calls xmlLoaded when needed
function checkXML(){
	if ((!isXMLLoaded) && (XMLDoc.readyState==4)) 
	{
		xmlLoaded();
	}
}


		
// here we build Help Tree using loaded XML
// it returns HTML to be places into a DIV that displays the tree.
function buildTreeXML(XMLDoc,inner)
{
	var str = ""
	if (!inner)
		str = "<table border=0 bordercolor=Blue cellpadding=0 cellspacing=0 onclick=\"handleClick()\" ondblclick=\"handleClick()\">"
	else
		str = "<table border=0 cellpadding=0 cellspacing=0>";
	var key=0;
	for (key=0; key<XMLDoc.length; key++){
				text = XMLDoc[key].attributes.getNamedItem("text").text;
				try {
				imgSrc1 = XMLDoc[key].attributes.getNamedItem("imgSrc1").text;
				} catch(e)
				{
				imgSrc1 = "";
				}
				try {
				imgSrc2 = XMLDoc[key].attributes.getNamedItem("imgSrc2").text;
				} catch(e)
				{
				imgSrc2 = "";
				}
				try {
				html = XMLDoc[key].attributes.getNamedItem("html").text;
				} catch(e)
				{
				html = "";
				}
				try {
				href = XMLDoc[key].attributes.getNamedItem("href").text;
				} catch(e)
				{
				href = "";
				}
				parentText = (XMLDoc[key].parentNode.attributes.getNamedItem("text") != null)
							?XMLDoc[key].parentNode.attributes.getNamedItem("text").text
							:"null"
		if (XMLDoc[key].tagName == "HLP")
		{
		if (imgSrc1 == "") 	imgSrc1 = "images/closedbook.gif" ;
				if ( imgSrc2== "") 	imgSrc2 = "images/openbook.gif" ;
	
				str += "<tr class=\"closed\"><td valign=top><img style=\"display:block\" src=\"" 
					+imgSrc1
					+"\" width=16 height=16 border=0 align=left><img style=\"display:none\" src=\""
					+imgSrc2
					+"\" width=16 height=16 border=0 align=left class=HelpTreeIMG>"
					+ html
					+"</td>"
					+ "<td><a onclick=\"return false;\" href='#'"
					+ "oncontextmenu = 'try {return adminMenu(\""+text+"\",\""+text+"\",\""
						+parentText
 						+ "\",true,this)} catch(e){}'" 
					+">" 
					+ text 
					+ "</a></td></tr>"
					+ "<tr style=\"{display:none}\"><td>&nbsp;</td><td>";
					str += buildTreeXML(XMLDoc[key].childNodes,true);
					str += "</td></tr>";
		} else if (XMLDoc[key].tagName == "ART")
		{
		
				
				if (imgSrc1 == "") 	imgSrc1 = "images/article.gif" ;

				tip =text +"\n\nIndexes:";

	// 		Index list to ToolTip remove if you don't need it.
				var children = XMLDoc[key].childNodes;
				for (i=0;i<children.length;i++)
				{
					tip += "\n - "+children[i].text;
				}
				
				str += "<tr><td valign=top><img src=\""+imgSrc1+"\" width=16 height=16 border=0 class=HelpTreeIMG align=left></td>"
					+"<td><a TITLE='"+tip+"' href='"+href+"' Target=Frame1 "
					+"oncontextmenu='try{return adminMenu(\""+text+"\",\""+text+"\",\""+parentText+"\",false,this)} catch(e){}'"
					+" onclick='lastReadyState = 0; return true;'>"+text+"</a>";

				str += "</td></tr>";
		}		
	}
	str += "</table>";
	return str;
}

// here we go through XML and get data for Index tab.
function generateIndex()
{
try{
	indexKeywordsList.options.length=0;
	arr = XMLDoc.selectNodes("//INDEX");
	a = new Array();
	for (i=0;i<arr.length;i++)
		a[a.length] = arr[i].text;

	function compare2(n1,n2)
	{
		if (n1.toLowerCase() < n2.toLowerCase()) return -1;
		else if (n1.toLowerCase() == n2.toLowerCase()) return 0;
		else return 1;
	}
	a.sort(compare2);
	
	var j=0;
	a[a.length]="";
	for (i=0;i<arr.length-1;i++) 
		if (a[i] != a[i+1])	
			indexKeywordsList.options[j++] = new Option(a[i],a[i]);
 } catch(e){}
}

// this function is called when text in textBox (in Index tab) is changed.
// So, we need to find the text in list of indexes and select the row
function findIndex()
{
	i=0;
	while ( i < indexKeywordsList.options.length-1 &&
		indexKeywords.value.toUpperCase() > indexKeywordsList.options[i].text.toUpperCase()) 
			i++;		
	indexKeywordsList.value = indexKeywordsList.options[i].value;
	listArticles();
} 

// is called when an Index item is selected. It displays refered article names.
function listArticles()
{
	var html = "<table cellpadding=0 cellspacing=0>";
	arr = XMLDoc.selectNodes("//INDEX[. = \""+indexKeywordsList.value+"\"]");
	for (i=0;i<arr.length;i++)
	{
		html = html + "<tr><td height=10><a href='"+arr[i].parentNode.attributes.getNamedItem("href").text+"' target=Frame1>" + arr[i].parentNode.attributes.getNamedItem("text").text+"</a></td></tr>";
	}
	html = html +"</table>";
	indexHrefs.innerHTML = html;
}

// What will happen if we DoubleClick on a index in a list? Rihgt, the referef article
// will be loaded and displayd.
function indexDblClick(){
	node = XMLDoc.selectSingleNode("//INDEX[. = \""+indexKeywordsList.value+"\"]");
	if (node != null)
	{
		lastReadyState = -1;
		Frame1.location.href = node.parentNode.attributes.getNamedItem("href").text;		
	}
}

// when Search tab is selected and "List topics" button is pressed
// the script has to find the index that contains all the words entered into the 
// textbox
function searchHelp()
{
	text = searchText.value;
	SearchSelect.options.length = 0;
	var str="";
	words = text.split(" ");
				
	arr = XMLDoc.selectNodes("//INDEX");

	for (n=0;n<arr.length;n++)
	{
		fits = true;	

		for (i=0;i<words.length;i++)
			if ((arr[n].text.toLowerCase().indexOf(words[i].toLowerCase()) < 0) && (arr[n].text != "") )
				fits = false;
		if (fits)
		{
			options = SearchSelect.options;
			exists = false;
			text = arr[n].parentNode.attributes.getNamedItem("text").text;
			href = arr[n].parentNode.attributes.getNamedItem("href").text;
			for (z=0;z<options.length;z++)
				if (options[z].text == text)
					exists = true;
			if (!exists)					
				options[options.length] = 
					new Option(text,href);
		}
	}
	SearchSelect.click();
}

// Shows refered article in responce to dowbleclick on the search results list
function SearchDblClick(){
try{
	 if (SearchSelect.options.length >0 )
	 {	 	
	 	lastReadyState = -1;
		Frame1.location.href = SearchSelect.value;
	 }
} catch(e){}
}

// some variables initialization
lastReadyState = 0;
MenuActivated = false;
navHidden = false;
var menuDIVs = new Array("Left","Back","Forward","Parameters","Help","AdminItemMenu","AdminContainerMenu");
var submenuDIVs = new Array();
isXMLLoaded = false;
XMLDoc = HelpXML.XMLDocument;

// some events has to be processed too
window.onresize=update;
document.onmouseover = hideMenus;
document.onclick = hideAll;
XMLDoc.onreadystatechange = xmlLoaded;


