/*
11/21/00
Made two modifications to accomodate NS6: 

1) Because of bad NS6 history.go behavior, declared a variable in goNextPage that holds name of
   previous topic and put in conditional goPreviousPage routine for NS6

2) Because NS6 doesn't seem to recognize object references of the form
   parent.framename.object put in NS6 specific getElementByID references
   for visibility properties of navigation controls in setstuff.
*/

/* 
If some of these functions appear unnecessarily complex, that's
because they're designed to work on multiple, similar (but not identical) 
pages. There are only a few rules/assumptions:

1.	Each page contains one or more sentences that discuss some topic.

2. 	Blocks of text are contained in spans with ID values that are
	prefixed by "step" followed by a sequential number.
	These spans can contain a single sentence, multiple sentences, 
	or entire paragraphs. 

3.	There may or may not be one or more objects (e.g. tables, charts)
	that the text is discussing. 

4.	There may or may not be transparent layers (actually divs) with visible borders
	associated with each block of text that highlight relevant portions
	of the object being discussed. These layers have ID values that
	consist of the ID of the associated text, followed by "graphic."

5.	In the absence of the aformentioned highlights, there may or may not
	be some other type of annotation, such as an arrow pointing from one
	thingy to another thingy.

So what these scripts basically do is: show and hide blocks of text sequentially
until there's no more text to display and show highlights or other annotations 
associated with each block of text if such annotations exist.

Netscape 4.x note: NS 4.x is a squirrely little bugger:
 
It won't display empty layers at the specified height; 
it insists on dynamically resizing layers to fit the contents, 
and in the absence of any content this makes for a mighty short layer. 
So in NS 4.x, we display a pointer at the  midpoint of the highlight instead. 
(The person writing the source XML doesn't have to worry about this, 
since the script does all the work: it checks for the existence of the highlight 
layer and then dynamically moves an invisible layer containing a pointer graphic 
to the midpoint of that layer and then makes the pointer visible.)

It can't handle references to non-existent top-level layers,
even in IF statements, making it necessary to hard-code some variable in the 
HTML as an object checker (so we can use code that says something like
"If this highlight object exists, show it"). While this may look
cumbersome in the HTML, it is actually auto-generated by the XSL that
creates the HTML from the source XML. 

Finally, there is a MAJOR BUG in
NS 4.x that essentially prevents you from using more than one top-level
layer with nested layers inside it, and only then if it's the LAST layer 
in the HTML -- and since NS treats spans and divs as aliases for layers, 
spans within divs are considered "nested" layers. So this entire model 
only works in NS 4.x if the text layer is the only top-level layer that
contains nested layers (spans in this case) inside it, and that layer is 
specified last in the HTML.
*/  

function setstuff()
{
//sticking this stuff in a function is necessary because
//Netscape won't execute script commands when the page loads
//unless explicitly told to do so in an onload call.


vernumber=parseInt(navigator.appVersion.charAt(0));
//alert(vernumber);
ns4=0;
ie4=0; 
mzx=0;

//browser sniffer, checks name and version
//idholders are to switch between document.all for IE and
//getElementById(),Mozilla, and Netscape 5+. The latter will also work
//in IE 5 but not IE 4.

if ((navigator.appName.indexOf("Explorer")!=-1)
		& (vernumber >=4))//then it's IE 4+
	{
		ie4=1;
		idholder1="document.all[";
		idholder2="]";
	}

else if ((navigator.appName.indexOf("Netscape")!=-1)
		& (vernumber<=4))//then it's Netscape 4 or lower
	{ns4=1;}

else if (((navigator.appName.indexOf("Netscape")!=-1)
		& (vernumber >4)) | (navigator.appName.indexOf("Mozilla")!=-1))
	{
		mzx=1;
		idholder1="document.getElementById(";
		idholder2=")";
	}

else
	{alert("Sorry! I only work with Netscape 4+ and IE 4+.")}

currstepnum=0;
x=0;
y=0;
obj2exists=0; //for stupid ol' Netscape
obj1exists=0; //for stupid ol' Netscape

	setTimeout("showNextStep();",100);
}

function showNextStep()
{
	
	id="step";
	x=x*1+1; 
	id2=id+y;//previous step, starts with step0 (which doesn't exist)
	id=id+x;//current step, starts with step1


	if (ie4==1 | mzx==1) //if IE or Mozilla
	{
		//Check to see if we need to swap any graphics.
  	//The array position dicates the step at which the swap occurs.
  	//The array value dictates the graphic being swapped.
		//Prefix for old graphics is "graphic";
		//Prefix for new graphics is "replace" to avoid conflict.

		//loop through arrays for all images
		//imagenum is set in page script to the number of 
		//initial images displayed (generated from XSL)
		//so the script will first initial image replacements (replace1[]),
		//second initial image replacements (replace2[]), etc.
		//The array position indicates the step at which swap occurs.

		for(myloop=1;myloop<=imagenum;myloop++)
		{
		pic1=eval("replaceexists" + myloop + "[" + x + "]");
		
		if (pic1)
		{
			replaceval = eval(idholder1 + "'graphic" + myloop + "'" + idholder2);
			replaceval.style.visibility="hidden";
			
			//now check to see if we need to replace a replacement
			//instead of original
			//And since it may have been replaced several steps back
			//we need to recursively find and hide up to previous step
			z=x*1-1;
			for (zcount=0;zcount<=z;zcount++)
			{
			pic2=eval("replaceexists" + myloop + "[" + zcount + "]");
			if (pic2)
			{
				replaceval = eval(idholder1 + "'replace" + zcount + "'" + idholder2);
				replaceval.style.visibility="hidden";
			}
			}
		
			newval = eval(idholder1 + "'replace" + x + "'" + idholder2);
			newval.style.visibility="visible";
		}
		}

		
		
		idobj1=eval(idholder1 + "id" + idholder2);
		idobj2=eval(idholder1 + "id2" + idholder2);

		if (idobj1)
		{
			if (idobj2)
			{
				idobj2.style.visibility="hidden";
				graph2=id2 + "graphic";
				objgraph2=eval(idholder1 + "graph2" + idholder2);
				if (objgraph2)
				{
					objgraph2.style.visibility="hidden";
				}
			}
			idobj1.style.visibility="visible";
			graph1=id + "graphic";
			objgraph1=eval(idholder1 + "graph1" + idholder2);
			
			if (objgraph1)
			{
				if (mzx==1)
				{	
					//adjust for discrepancy in way MZ/NS6 determine height/width
					objgraph1.style.height= parseInt(objgraph1.style.height)*1-13;
					objgraph1.style.width=parseInt(objgraph1.style.width)*1-13;
				}
				objgraph1.style.visibility="visible";
				growme(graph1);
			}
		}
		else
		{
		goNextPage();
		}
	}

	if (ns4==1) 
	{
		//first hide the pointer, since if it needs to be shown for
		//a particular step that's dealt with later
		document.pointer.visibility="hidden";

		if(graphexists[x]==2)
			{
				//if there is picture hilite (ghilite) for step, show it
				graph1=id + "graphic";
				document[graph1].visibility="visible";
			}		
			
			if(graphexists[x*1-1]==2)
			{
				//if there is picture hilite (ghilite) for previous step, hide it
				graph2=id2 + "graphic";
				//alert(graph2);
				document[graph2].visibility="hidden";
			}			
		
		for (myloop=1;myloop<=imagenum;myloop++)
		{
		pic1=eval("replaceexists" + myloop + "[" + x + "]");
		
		if (pic1)
		{
			replaceval = eval("document.graphic" + myloop);
			replaceval.visibility="hidden";
		
			z=x*1-1;
			for (zcount=0;zcount<=z;zcount++)
			{
			pic2=eval("replaceexists" + myloop + "[" + zcount + "]");
			if (pic2)
			{
				replaceval = eval("document.replace" + zcount);
				replaceval.visibility="hidden";
				
			}
			}
			newval = eval("document.replace" + x);
			newval.visibility="visible";

		}
		}

		for (counter=0;counter<document.layers.length;counter++)
		{
			//document.layers.length turns out to be highly
			//unreliable in ns 4.x, since it appears to stop counting
			//once it encounters a layer embedded within another layer.
			//but we can't use some arbitrarily high value for the counter
			//because ns 4.0 barfs on references to non-existent layers even
			//in IF statements.

			if (document.layers[counter].document[id])
			{ 
				obj1=document.layers[counter].document[id];
				obj1exists=1; //to check later without ns complaining
				thing1=obj1.id; //the id value of the layer
				thing2=parseInt(thing1.charAt(4));//the number after "step"
				if (thing1.length>4) //then it's a 2-digit number
				{
					thing2=parseInt(thing2+(thing1.charAt(5)));
				}
				//the result of the above is the integer portion of step
				//based on the assumption that step IDs are step1, step2, etc.
				
				
				if(document.layers[counter].document[id2])
				{
					obj2=document.layers[counter].document[id2];
					obj2exists=1; //as above
				}
				
			}
		}
		if(obj2exists==1)
		{
			obj2.visibility="hide"; //if there is a previous step, hide it
		}

		if(obj1exists==1)
		{
			obj1.visibility="show"; // if there is a next step, show it

			//then see if there's a graphic associated with it using
			//the graphexists array which is hard-coded in the HTML page
			//to 1 if a graphic exists for the step and null if not as in
			//graphexists(,1,1,,1,,,). The HTML looks truly ugly but since
			//it's auto-generated by xsl from the xml source, it's not really
			//as it looks.

			if(graphexists[thing2]==1)
			{
				//if there is an associated border hilite (used by the IE version)
				showpointer();
			}
			
		}
		else
		{
			goNextPage();
		}
	}
	
	obj1exists=0; //reset at end of each step
	y=y*1+1; //increment the id2 counter
}


function showPreviousStep()
{	

	if (ie4==1 | mzx==1)
	{
		if (x > 1)
		{
			id2 = "step" + (x-1);
			id = "step" + x;
			idobj1=eval(idholder1 + "id" + idholder2);
			idobj2=eval(idholder1 + "id2" + idholder2);
			if(idobj1)
			{
				idobj1.style.visibility="hidden";
				graph1=id + "graphic";
				objgraph1=eval(idholder1 + "graph1" + idholder2);
				if (objgraph1)
				{
					objgraph1.style.visibility="hidden";
					if (mzx==1)
					{
						//reset adjusted height and width and don't ask me why we
						objgraph1.style.height=parseInt(objgraph1.style.height)*1+13;
						objgraph1.style.width=parseInt(objgraph1.style.width)*1+13;
					}
				}
				
				for (myloop=1;myloop<=imagenum;myloop++)
				{
				pic1=eval("replaceexists" + myloop + "[" + x + "]");
				if (pic1)
				{
					//check backwards until you find a previous replacement
					//or show original if no replacement found
					z=x*1-1;
					for (zcounter=z;zcounter>=0;zcounter--)
					{
						foundreplace=0;
						pic2=eval("replaceexists" + myloop + "[" + zcounter + "]");
						if (pic2)
						{
							replaceval = eval(idholder1 + "'replace" + zcounter + "'" + idholder2);
							replaceval.style.visibility="visible";
							foundreplace=1;
							break;//stop after you find the first one previous
						}
					}
					
					if (foundreplace==0)//then put in original 
					{
						replaceval = eval(idholder1 + "'graphic" + myloop + "'" + idholder2);
						replaceval.style.visibility="visible";
					}
					newval = eval(idholder1 + "'replace" + x + "'" + idholder2);
					newval.style.visibility="hidden";
				}
				}

				if (idobj2)
				{
					idobj2.style.visibility="visible";
					graph2=id2 + "graphic";
					objgraph2=eval(idholder1 + "graph2" + idholder2);
					if (objgraph2)
					{
						objgraph2.style.visibility="visible";
					}
				}
			}
			x -= 1; 
			y -= 1;
		}
			
		else
		{
			goPreviousPage();
		}
		
	}

	if (ns4==1) 
	{
		document.pointer.visibility="hidden";
		if (x > 1)
		{
			id2 = "step" + (x-1);
			id = "step" + x;
			
			if(graphexists[x]==2)
			{
				//if there is picture hilite (ghilite) for step, hide it
				graph1=id + "graphic";
				document[graph1].visibility="hidden";
			}		

			if(graphexists[x*1-1]==2)
			{
				//if there is picture hilite (ghilite) for previous step, show it
				graph2=id2 + "graphic";
				document[graph2].visibility="visible";
			}			


			for (counter=0;counter<document.layers.length;counter++)
			{
				if (document.layers[counter].document[id])
				{
					obj1=document.layers[counter].document[id];
					obj1exists=1; //to check later without ns complaining
					if(document.layers[counter].document[id2])
					{
						obj2=document.layers[counter].document[id2];
						obj2exists=1; //as above
					}	
				}
			}
			//alert(id + " " + id2);
			
			if(obj1exists==1)
			{
				obj1.visibility="hide"; // if there is a current step, hide it
				
				//now swap pictures if necessary
				for (myloop=1;myloop<=imagenum;myloop++)
				{
				pic1=eval("replaceexists" + myloop + "[" + x + "]");
				if (pic1)
				{
					z=x*1-1;
					for (zcounter=z;zcounter>=0;zcounter--)
					{
						foundreplace=0;
						pic2=eval("replaceexists" + myloop + "[" + zcounter + "]");
						if (pic2)
						{
							replaceval = eval("document.replace" + zcounter);
							replaceval.visibility="visible";
							foundreplace=1;
							break;//stop after you find the first one previous
						}
					}
					
					if (foundreplace==0)//then put in original 
					{
						replaceval = eval("document.graphic" + myloop);
						replaceval.visibility="visible";
					}
					newval = eval("document.replace" + x);
					newval.visibility="hidden";
				}
			}
			}

			if(obj2exists==1)
			{
				obj2.visibility="show"; //if there is a previous step, show it

				thing1=obj2.id; //the id value of the layer
				thing2=parseInt(thing1.charAt(4));//the number after "step"
				thing2=parseInt(thing2);//the number as a number
				
				if (thing1.length>4) //then it's a 2-digit number
				{
					thing2=parseInt(thing2+(thing1.charAt(5)));
				}

				if(graphexists[thing2]==1)
				{
					showpointer();
				}
			}
			
			
			x -= 1; 
			y -= 1;
		}
			
		else
		{
			goPreviousPage();
		}
	}
	
	obj1exists=0; //reset at end of each step
	
}

function goNextPage()
	{
//		topicvar = nexttopic;
//		loadHelpTopic();
		location.href = nexttopic;
		if (mzx==1) {
			setTimeout("setstuff();",500);
				     //have to do this here, since NS6
				     //is too stupid to run onload commands
				     //when you load a page in a frame (sheesh!)
		}
	}

function goPreviousPage()
	{
		history.go(-1);
	}

function growme(id)
//this function sets a layer's height or width to 0, 
//then expands the layer back to its original size
//it preserves the original value for the smaller and expands the larger
{	
	//alert(id);
	if (id) //stuff to do when function called from outside
	{	
		//first stuff id into document.all[] or getObjectById()
		idobj1=eval(idholder1 + "id" + idholder2);
		
		//now get the height and width of the object
		//parseInt removes the pesky "px" that IE adds to value
		h = parseInt(idobj1.style.height);
		w = parseInt(idobj1.style.width);
		
		if (w>=h) //if width exceeds height
		{
			dimvar="width";
			dimval=w;
		}
		else //if height exceed width
		{
			dimvar="height";
			dimval=h;
		}
		
		xcount=0; //initialize width counter 
		
		//set a new variable to the object parameter
		//so it won't be undefined
		idx=id; 
		idobjx=eval(idholder1 + "idx" + idholder2);	
	}

	//now all the stuff that gets done recursively within the function
	//which is done this way because of stupid setTimeout method
	
	//increment width or height until it equals original size
	if (xcount<=dimval)
	{xcount+=5;}
	
	idobjx.style[dimvar]=xcount;
		
	//stuff in the recursive pause so you can see the animation	
	if (xcount<=dimval)
	{	
		growtimer=setTimeout("growme()", 1);	
	}
	else
	{
		clearTimeout(growtimer);
		idobjx.style[dimvar]=dimval;//reset or it keeps getting bigger
	}	

	
}
	
	
//showpointer calculates the midpoint of the layer
//hilite used in IE and displays the pointer there
//in NS.
function showpointer()
{
	thisobj=eval("document.step" + thing2 + "graphic");
	//thing2 is the integer portion of the step ID
	//and we only call this function after we've established
	//that thisobj will resolve to an object that actually exists
	horizontal1=parseInt(thisobj.left);
	horizontal2=parseInt(thisobj.clip.width);
	horizontalpos=horizontal1*1 + ((horizontal2)/2);
	vertical1=parseInt(thisobj.top);
	vertical2=parseInt(thisobj.clip.height);
	verticalpos=vertical1*1 + ((vertical2)/2);
	document.pointer.left=horizontalpos;
	document.pointer.top=verticalpos;
	document.pointer.visibility="visible";
}

//function invoked from goIndex() and goTOC(), located in localp.js
//this hides the Next and Back buttons for the Index and Contents pages
//and instead show text that says "select a topic..."
	//CH updated 9-20-01: removed if/else detection of browser, since the 
	// remaining code works in both IE and NS6, and NS4 has been deprecated
function showhidestuff()
{
	document.getElementById("Layer2").style.visibility="visible";
	document.getElementById("nextlayer").style.visibility="hidden";
	document.getElementById("backlayer").style.visibility="hidden";
//	writeTitle();
}


//reload on resize necessary in NS4
function nsreload()
{
if (ns4==1)
{location.reload}
}

/*
   Here's Clay's new function for making topic file "smart". Basically,
   each topic file should call this function on load if the topic has been
   opened directly. The function will replace the topic with the appropriate frameset
   and reload the topic in a frame. (Of course, if the topic is opened in a frame
   it should just open normally.)
   
   Note: the filename to reload will need to be hard-coded into each topic file.
   This is because MSIE stupidly forgets document.URL and window.location.* if the OS
   launches an HTML file with the default browser. It doesn't seem to matter whether
   you load it with a regular filespec or with a "file:" URL.
*/

  function loadFrames(filename) { 
     html = "<frameset rows=\"*,55\" cols=\"*\"> \n" + 	 
     "    <frame src=\"toc_top.htm\" name=\"topic\" frameborder=0 scrolling=\"auto\" noresize>\n" +
     "    <frameset cols=\"*,200\" frameborder=\"NO\" border=\"0\" framespacing=\"0\">\n" +
     "        <frame src=\"blank.htm\" name=\"title\" frameborder=0 scrolling=\"no\" noresize>\n" +
     "        <frame src=\"tutctrl.htm\" name=\"navframe\" frameborder=0 scrolling=\"no\" >\n" +
     "    </frameset>\n" +
     "</frameset></HTML>\n";   
     // resize and add new stuff   
     self.resizeTo(800,600);   
     document.write(html);   
     document.close();   
     parent.topicvar = filename;
     parent.onload = setstuff;
     setTimeout("loadHelpTopic()",500);
  }   
     

/*
   This is a sister function to launchHelpTopic() that loads a
   help topic into an existing topic frame. The main difference is
   that this version is used in situation where there is no "opener"
   window, and topicvar is a variable of the current window. This will
   generally be true when this help is called from an application or
   launched independently, rather than called from a browser window.
 */
   
function loadHelpTopic() {
		if (typeof topicvar != 'undefined') { //check for topicvar in the current window
			location.href=topicvar;
		}
}

/* Function to handle TOC and Index events */

function navEvent(navsrc,topicsrc) {
	// This has to work a little differently here than it does in help,
	// since here the TOC and index share the same frame with the topic.
	//
	// The primary consequence of this is that clicking a title that is
	// both a topic and a container for other topics will load that topic.
	// To expand such a topic the user must click the "+" icon.
	
	if ((typeof topicsrc != 'undefined') && (topicsrc != '')) {
		location.href = topicsrc;
	}
	else if ((typeof navsrc != 'undefined') && (navsrc != '')) {
//		showhidestuff();
		location.href = navsrc;
	}
}


function writeTitle() {
	var titlehtml = "";
	if (typeof titletext == "undefined") titletext = "";
 	titlehtml = "<HTML>\n" + 	       
           "  <HEAD><TITLE>Title Bar</TITLE>\n" + 
   	       "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" + 
   	       "  <link rel=\"stylesheet\" href=\"tutorial.css\" type=\"text/css\">\n" + 
   	       "  </HEAD>\n" + 
   	       "  <BODY background=\"../images/buttonbar.gif\">\n" + 
   	       "    <DIV ID=\"titlelayer\" class=\"title\">" + 
   	       "    <P>" + titletext + "</P></DIV>\n" +
   	       "  </BODY>\n" + 
   	       "</HTML>\n"; 
   	document.open("text/html"); //init new document to make sure old one is gone
   	document.write(titlehtml); 
   	setTimeout("document.close();",500);
 } 

function popup(title,text,caller) {
  //pops up a mini window to display the text string passed

  if (typeof popupwin != "undefined") {
	popupwin.close();
  }
  popupwin = window.open("","popup","height=200,width=300,resizable");
  html = "<HTML>\n<HEAD>\n<TITLE>" + title + "</TITLE>\n" + 
	 "<link rel='stylesheet' href='tutorial.css' type='text/css' />\n</HEAD>\n" +
	 "<BODY onload='self.resizeBy(0,document.getElementsByTagName(\"DIV\")[0].offsetHeight-180);'>\n" +
	 "<DIV name='def' class='glossary-definition'>\n" + text + "\n</DIV>\n</BODY>\n</HTML>\n";
  popupwin.document.write(html);
  popupwin.document.close();
}

function setclip(offset) {
	//this function sets a viewport for the main table cell (the one that holds the content)
	//so that it scrolls when the table cell is too small to hold the contents. This function
	//is called by the onload() and onresize() event handlers for nav pages (TOC & index).
	//param offset indicates a height offset necessary for nav pages, since they have a
	//non-scrolling control at the top

	var c = document.getElementById("scrolldiv").style;
	var t = document.getElementById("topframe");
	if (typeof offset == "undefined") offset = 0;
	c.top=t.offsetTop;
	c.width=(t.offsetWidth - 22);
	c.height=(t.offsetHeight - (20 + offset));
	c.left=t.offsetLeft;
}


function smartLink(targetList) {
//handle links with multiple targets.
//targets are passed as items in a targetList array
//with the URL for each target followed by its title
//thus a single target the value of targetList would be "['myURL.htm', 'My Title']"
	
	//In the case of a single target, just go there
	if (targetList.length <= 3) { //3 'cuz the array may end with an extra comma if written by a stylesheet
		location.href=targetList[0];

	//If multiple targets are specified, list the topics so the user can choose
	} else {
	

		var newContent = "<HTML><HEAD>\n";
		newContent += "<SCRIPT type=\"text/javascript\" src=\"tutorial.js\"></SCRIPT>\n";
		newContent += "<TITLE>Topics Found</TITLE>\n</HEAD>\n";
		newContent += '<BODY BGCOLOR="#FFFFFF"><H1 style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:x-small; font-weight:bold;">Select a topic:</H1>\n';

		for(var i = 0; i < targetList.length; i+=2) {
			if (targetList[i] != null) {
				newContent += '<div style="display:block; font-family:Verdana; font-size:x-small; margin-top:4px">'
				newContent += '<A href="' + targetList[i] + '">' + targetList[i+1] + "</A><BR>"
				newContent += '</div>\n'
			}
		}

		newContent += "</BODY>\n</HTML>";
		document.write(newContent);
		document.close(); //close layout stream
	}
}


