
//**************************** EVENT HANDLING ********************************************
//****************************************************************************************
//  re-directs to the proper event-driven functions.

window.onload= loadPage;
document.onclick= onclickTriage;
document.onmouseover= gettingHot;
document.onmouseout= gettingCold;

//************************  USER-DEFINED GLOBAL VARIABLES  **********************************
//*******************************************************************************************
//  The images listed below can all be changed by the user.


var sExpandTip= "Expand/collapse";

var moniker= "./media/";                         // moniker= ""; for flat files
var sSharedCHM= moniker+"";

var closed = sSharedCHM + "plusCold.gif";	//image used for collapsed item in callExpand()
var closedHot = sSharedCHM + "plusHot.gif";	//hot image used for collapsed item in callExpand()
var expand = sSharedCHM + "minusCold.gif";	//image used for expanded item in callExpand()
var expandHot = sSharedCHM + "minusHot.gif";	//hot image used for expanded item in callExpand()


//*************************  GLOBAL VARIABLES  *****************************************
//**************************************************************************************

var printing = "FALSE";
var isRTL= (document.dir=="rtl");

var isIE5= (navigator.appVersion.indexOf("MSIE 5")>0) || (navigator.appVersion.indexOf("MSIE")>0 && parseInt(navigator.appVersion)> 4);


//***************************  INITIALIZATION  *******************************************
//****************************************************************************************

//*** loadPage ***************************************************************************
//  Adds the default image tags and re-usable text to the HTML page.

function loadPage(){

  syncTOC();

  insertImages();

}


 //****** insertImages *****************************************************************
 //  Inserts shared images in User-Defined Variables section and thumbnails.

function insertImages(){



//indents for Navigation Tree

  for (var i=0; i < document.anchors.length; i++){
        var imgInsert="";
	var imgStyle= "";
	var imgSpace= "<span class='space'></span>";
	var oAnchor= document.anchors[i].id.toLowerCase();


// insert EXPAND icons
         if (oAnchor=="expand") {
	          document.anchors[i].title= sExpandTip;
              if (document.anchors[i].children.tags("IMG")(0) && document.anchors[i].children.tags("IMG")(0).className.toLowerCase() == "expand")
		  imgInsert= ""; 	// not to re-insert when persistent
              else{
		   if (document.anchors[i].parentElement.offsetLeft == document.anchors[i].offsetLeft) {
		   imgSpace= "<span class='space' style='width:0'></span>";
			if (isRTL){ document.anchors[i].parentElement.style.marginRight= "1.5em";  imgStyle=" style=margin-right:'-1.5em'";}
			else { document.anchors[i].parentElement.style.marginLeft= "1.5em";  imgStyle=" style=margin-left:'-1.5em'";}
			}
		   imgInsert= "<img class='expand' src='"+ closed +"' "+imgStyle+">" +imgSpace;
	          }
       }

 	   document.anchors[i].innerHTML = imgInsert + document.anchors[i].innerHTML;
	   if (isRTL) document.anchors[i].dir="rtl";
   }
}


//***** onclickTriage *******************************************************************
// redirects to the appropriate function based on the ID of the clicked <A> tag.

function onclickTriage(){
var e= window.event.srcElement;

//  if the innerHTML in the <a> tag is encapsulated by a style tag or hightlighted in the word seach,
//  the parentElement is called.

    for (var i=0; i < 5; i++)
           if (e.tagName!="A" && e.parentElement!=null) e= e.parentElement;
    eID= e.id.toLowerCase();


    if (eID=="expand")    callExpand(e);
    return;
}


//*** gettingHot **********************************************************************
// Makes all the required changes for mouseover.

function gettingHot() {
var e = window.event.srcElement;

  if (e.id.toLowerCase()=="cold")  e.id ="hot";

  else if ((e.className.toLowerCase()=="expand" && e.tagName=="IMG") ||( e.id.toLowerCase()=="expand")) expandGoesHot(e);

}

//*** gettingCold **********************************************************************
// Initial state for mouseout.

function gettingCold() {
var e = window.event.srcElement;

  if (e.id.toLowerCase()=="hot")  e.id ="cold";

  else if ((e.className.toLowerCase()=="expand" && e.tagName=="IMG") ||( e.id.toLowerCase()=="expand")) expandGoesCold(e);
}


//*********************************  EXPAND FUNCTIONS ***********************************
//***************************************************************************************

//**  callExpand ************************************************************************
//  This expands & collapses (based on current state) "expandable" nodes as they are clicked.
//  Called by: <A ID="expand" href="#">@@Hot text@@</A>
//  Followed by:  <div class="expand">

function callExpand(eventSrc) {

var e= eventSrc;
    event.returnValue = false;					// prevents navigating for <A> tag

var oExpandable = getExpandable(e);
var oImg = getImage(e);


     if (oExpandable.style.display == "block")
	      doCollapse(oExpandable, oImg);
     else doExpand(oExpandable, oImg);
}

//** expandGoesHot ********************************************************************
// Returns expand image to hot.

function expandGoesHot(eventSrc){
var e= eventSrc;

var oExpandable = getExpandable(e);
var oImg = getImage(e);


    if (oExpandable.style.display == "block") oImg.src = expandHot;
    else oImg.src = closedHot;

}


//** expandGoesCold *******************************************************************
// Returns expand image to cold.

function expandGoesCold(eventSrc){
var e= eventSrc;

var oExpandable = getExpandable(e);
var oImg = getImage(e);

    if (oExpandable.style.display == "block") oImg.src = expand;
    else oImg.src = closed;
}


//** getExpandable **********[used by callExpand, expandGoesHot, expandGoesCold]*******
//  Determine if the element is an expandable node or a child of one.

function getExpandable(eventSrc){
var  e = eventSrc;
var iNextTag, oExpandable;

       for (var i=1;i<4; i++){
               iNextTag=    e.sourceIndex+e.children.length+i;
              oExpandable= document.all(iNextTag);
              if (oExpandable.className.toLowerCase()=="expand" || iNextTag == document.all.length)
                   break;
       }
       return oExpandable;
}

//**  getImage **************[used by callExpand, expandGoesHot, expandGoesCold]*******
//  Find the first image in the children of the current srcElement.
// (allows the  image to be placed anywhere inside the <A HREF> tag)

function getImage(header) {
var oImg = header;

       if(oImg.tagName != "IMG") oImg=oImg.children.tags("IMG")(0);
       return oImg;
}


//****  doExpand **********************************************************************
//  Expands expandable block & changes image

var redo = false;
function doExpand(oToExpand, oToChange) {
var oExpandable= oToExpand;
var oImg= oToChange;

	oImg.src = expand;
	oExpandable.style.display = "block";

	if (!redo && !isIE5) {
		redo = true;
		focus(oToExpand);
		doExpand(oToExpand, oToChange);
		}

}


//****  doCollapse ********************************************************************
//  Collapses expandable block & changes image

function doCollapse(oToCollapse, oToChange) {
if (printing == "TRUE") return;
var oExpandable= oToCollapse;
var oImg= oToChange;

    oExpandable.style.display = "none";
    oImg.src = closed;
}







/*****************************************************************************
*   Multiple Choice Question Secondary Window                                *
*****************************************************************************/

function opennewwindow(url) {

var tempX;
var tempY;


//tempX = 180 + event.clientX;
//tempY = 120 + event.clientY;

tempX = 10 + event.screenX;
tempY = 10 + event.screenY;

//alert(document.body.clientHeight);
//alert(event.clientY);
//alert(event.screenY);

//alert(window.screen.Height);

popText = url+"<br/><br/>"
myWidth = 340 + Math.round((popText.length - 100)/3);
myHeight = Math.round(myWidth * 1.5 / 4);
//alert(myHeight);

if (tempY + myHeight > window.screen.height - 20) {
tempY = tempY - myHeight - 100;
}


var head1='<html><head><title>Question & Answer</title><link rel="stylesheet" href="ie.css" type="text/css"/></head><body>';
var body1 = url;
var body2='<p align=right><a href="javascript://" onClick="self.close(); return false;">close</p></body></html>'

//popWin = window.open("","popupWindow","toolbar=no,location=no,width="+myWidth+",height="+myHeight+",left=200,top=50,status=no,menubar=no,scrollbars=yes,resizable=yes");

popWin = window.open("","popWin","toolbar=no,location=no,directories=no,width="+myWidth+",height="+myHeight+",left="+tempX+",top="+tempY+",status=no,menubar=no,scrollbars=yes,resizable=yes");


popWin.document.write(head1+body1+body2);
popWin.document.close();
popWin.focus();
  return false;

}





/*****************************************************************************
*   Open Table of Contents Page                                              *
*****************************************************************************/

function openpage()
{

        top.left.location.href = "twistytoc.htm"
}

/*****************************************************************************
*   Open Table of Contents Page                                              *
*****************************************************************************/

function openindex()
{

        top.left.location.href = "twistyindex.htm"

}

/*****************************************************************************
*   Open Search Page                                                         *
*****************************************************************************/

function synsearch()
{
	var address = String(window.location.href);

	if(address.substring(0,5)=='file:')
	{
		top.left.location.href = "search_tmp.htm"
	}
	else
	{
		//top.left.location.href = "search.asp"
	}
        //top.left.location.href = "search_tmp.htm"

}

/*****************************************************************************
*   Function to toggle between style sheets                                  *
*****************************************************************************/
function ChangeStyle()
{
    if (is_ie)
    {
        if (document.styleSheets(0).href != null)
        {
            if (document.styleSheets(0).href == "newstyle.css")
            {
                document.styleSheets(0).href = "ie5.css";
            }
            else
            {
                document.styleSheets(0).href = "newstyle.css";
            }
        }
    }
}

/*****************************************************************************
*   Function to make collapsable TOC (twisties)                              *
*****************************************************************************/
function expandIt(whichEl,srcgif,url)
{
    document.cookie = url;


    whichIm = event.srcElement;
    if (whichEl.style.display == "none")
    {
        whichEl.style.display = "";
        srcgif.src = "media/collapse.gif";
        //top.main.location.href = url;
    }
    else
    {
        whichEl.style.display = "none";
        srcgif.src = "media/expand.gif";
        //top.main.location.href = url;
    }


}

/*****************************************************************************
*   Function to make collapsable TOC (twisties)                              *
*****************************************************************************/
function testIt(url)
{

    //document.cookie = url;

    whichIm = event.srcElement;

    //top.main.location.href = url;

    countanchors = document.anchors.length;


}


/*****************************************************************************
*   Synchronise Table of Contents                                            *
*****************************************************************************/

function syncTOC()
{
	var MyUrl,  MyPage, TocPage, LeftUrl, LeftPage;
	var n, m, l, x, y;
	var scrXpos=0, scrYpos=0, scrYbottom=0;
	var count, curcount, countanchors, countid, curvalue, curlevel, maxcount, oldcount;
	var beginindex, endindex, result, url;
	var id = new Array(1000);


	top.left.document.focus();

	MyUrl = top.main.location.href;


	//set begin index to 1st letter of value ("W")

	if (document.cookie.indexOf("file") == "14") {
	    beginindex=document.cookie.indexOf("file");
	    endindex=beginindex;
	}
	else if (document.cookie.indexOf("http") == "14") {
	    beginindex=document.cookie.indexOf("http");
	    endindex=beginindex;
	}


	//while we haven't hit ";" and it's not end of cookie
	while (document.cookie.charAt(endindex)!=";"&&
	endindex<=document.cookie.length)
	endindex++


	//result contains "JavaScript Kit"
	var result=document.cookie.substring(beginindex,endindex)

	x = MyUrl.lastIndexOf("#") + 1;
	y = MyUrl.lastIndexOf("-") + 1;


       	n = MyUrl.lastIndexOf("/") + 1;
      	l = MyUrl.length;
      	MyPage = MyUrl.substring(n, l);
	m = MyPage.lastIndexOf("htm") - 1;
	TocPage = MyPage.substring(0, m) + ".htm#TopOfPage";
//TocPage = MyPage.substring(0, m);

        LeftUrl = top.left.location.href;
        m = LeftUrl.lastIndexOf("/") + 1;
        l = LeftUrl.length;
        LeftPage = LeftUrl.substring(m, l);

	if (LeftPage != "twistytoc.htm") {
		return false;
	}

	count = 0;

	countanchors = top.left.document.anchors.length;

	countlevel = 0;

        pass = false;


//exit if left hand panel url equal right hand panel

//	if (result==MyUrl) {

//		return false;
//	}


// get current position of the page and highlight first page

	for(i=1;i<=countanchors;i++){


		if (top.left.document.anchors(i-1).name == TocPage) {
			top.left.document.anchors[i-1].style.background = "lightgrey";
			url = top.left.document.anchors(i-1);
			count = count + 1;
			oldcount = count;
			pass = true;

		}
		else {
			top.left.document.anchors[i-1].style.background = "white";
			count = count + 1;
		}
	}


// don't do anything if pass = false

	if (pass == false) {
		return;
	}

// get accesslevel

	for(k=0;k<=oldcount-1;k++){


		if (top.left.document.anchors(k).level == "1") {
			accesslevel = top.left.document.anchors(k).level;
			id1 = top.left.document.anchors(k).value;

		}

		else if (top.left.document.anchors(k).level == "2") {
			accesslevel = top.left.document.anchors(k).level;
			id2 = top.left.document.anchors(k).value;

		}

		else if (top.left.document.anchors(k).level == "3") {
			accesslevel = top.left.document.anchors(k).level;
			id3 = top.left.document.anchors(k).value;

		}

		else if (top.left.document.anchors(k).level == "4") {
			accesslevel = top.left.document.anchors(k).level;
			id4 = top.left.document.anchors(k).value;

		}

		else if (top.left.document.anchors(k).level == "5") {
			accesslevel = top.left.document.anchors(k).level;
			id5 = top.left.document.anchors(k).value;

		}

	}


// get the expand id

	if (accesslevel == "2") {
		top.left.document.all['ID' + id1].style.display = "block";
		top.left.document.all['ar' + id1].src = "media/collapse.gif";


	}

	else if (accesslevel == "3") {
		top.left.document.all['ID' + id1].style.display = "block";
		top.left.document.all['ar' + id1].src = "media/collapse.gif";

		top.left.document.all['ID' + id2].style.display = "block";
		top.left.document.all['ar' + id2].src = "media/collapse.gif";

		top.left.document.all['ID' + id3].style.display = "block";
		top.left.document.all['ar' + id3].src = "media/collapse.gif";

	}

	else if (accesslevel == "4") {
		top.left.document.all['ID' + id1].style.display = "block";
		top.left.document.all['ar' + id1].src = "media/collapse.gif";

		top.left.document.all['ID' + id2].style.display = "block";
		top.left.document.all['ar' + id2].src = "media/collapse.gif";

		top.left.document.all['ID' + id3].style.display = "block";
		top.left.document.all['ar' + id3].src = "media/collapse.gif";

	}

	else if (accesslevel == "5") {
		top.left.document.all['ID' + id1].style.display = "block";
		top.left.document.all['ar' + id1].src = "media/collapse.gif";

		top.left.document.all['ID' + id2].style.display = "block";
		top.left.document.all['ar' + id2].src = "media/collapse.gif";

		top.left.document.all['ID' + id3].style.display = "block";
		top.left.document.all['ar' + id3].src = "media/collapse.gif";

		top.left.document.all['ID' + id4].style.display = "block";
		top.left.document.all['ar' + id4].src = "media/collapse.gif";

	}


	//top.left.scrollTo(scrXpos,'10000');
	scrYpos = top.left.document.body.scrollTop;
	//top.left.location.hash = "#" + TocPage;
	top.left.scrollTo(scrXpos,scrYpos);
	//scrYbottom = top.left.document.body.scrollTop;
	//top.left.location.hash = "#" + TocPage;

	//scrYpos = top.left.document.body.scrollTop;

}




/*****************************************************************************
*   Pop Up Window                                                            *
*****************************************************************************/

function showMenu(linkObj, menuID) {
  var popupObj = window.createPopup();
  var popupBodyObj = popupObj.document.body;
  popupBodyObj.style.border = "1px gray solid";
  popupBodyObj.style.background = "#f2f2f2";
  popupBodyObj.style.fontFamily = "Arial";
  popupBodyObj.innerHTML = menuID.outerHTML;
  for (var i = 0; i < popupBodyObj.all.length; i++) {
    if (popupBodyObj.all[i].tagName == "A")
      popupBodyObj.all[i].onclick = doClick;
  }
  popupObj.show(0, linkObj.offsetHeight, menuID.offsetWidth, menuID.offsetHeight * 1.5, linkObj);
}

function doClick() {
  top.main.location = this.href;
  return false;
}



