<!--Copyright © 1999, 2000 Center for Computer-Assisted Legal Instruction. All Rights Reserved. -->
//
//#######################################################
//################################## START OF COMMON ####
//#######################################################

// 2/19/98 SJG Common JavaScript functions included for all CALI Author page types, a library of sorts.
// 7/99 Added Scoring

var popuppage
var pop

if (top==self && popuppage==null)
{
//If we are being displayed in a standalone window and not a Popup, it's an error
	window.onerror=myerrhandler
	//document.writeln("<blink>Warning!</blink> This page must be viewed within the Frame setting to work properly.")
}else{
	if (parent.debug!=null)
		window.onerror=myerrhandler
	else
	//	window.onerror=null
;
}

function myerrhandler(msg,url,linenum){
	var errmsg=url + '\nLine# ' +linenum + ': '+ msg
	if (hasdebugbox())
		status(errmsg )
	else
		alert(errmsg)
//		errlist=errlist + errmsg + '\n'
   return true
}


var RIGHT  ="R"
var WRONG  ="W"
var MAYBE  ="M"
var INFO   ="I"
var POSSIBLE=MAYBE
var RIGHTCOLOR="#CCFFCC"
var WRONGCOLOR="#FFCCCC"
var MAYBECOLOR="#FFFFDD"
var INFOCOLOR= "#DDDDFF"

var gradecolor=new Array(RIGHTCOLOR,WRONGCOLOR,MAYBECOLOR,INFOCOLOR);
var gradetitle=new Array("Correct","Wrong","Possibly","Information")
var gradeicon= new Array(
	"good.gif",
	"bad.gif",
	"possible.gif",
	"info.gif")

var Browser = navigator.appName
var Version = navigator.appVersion;
if ((Browser == 'Microsoft') && (Version.indexOf('MSIE ') != -1)) 
	Version = Version.substring(Version.indexOf('MSIE ')+5);
Version = Version.substring(0,1);
var NETSCAPE = Browser == "Netscape"
var IE = Browser =="Microsoft"
var LAYERS = (Version>=4)
var helpanchor


//### Shared Book Page functions

function GenerateBookPage()
{
	nhotspots=hotspots.length	
	nslides=slides.length
	slide=0
	BASE=1
	if (picture!="") BASE++
	if (video!="") BASE+=2

   if (video=="" && nslides==0 && nhotspots==0 && sound=="")
   {
   	helpanchor="TEXT"
   	writeoptions("When you are finished reading click the Next button to go to the next page.",false,false,true)

   	if (picture=="")
   		document.write(text)
   	else
   	{
   		if (alignRight==orientation)
				document.write("<table border=0><tr valign=top><td align=center>"+
						"<img src="+picture+"></td><td>"+text+"</td></tr></table>") //"<img align=left src="+picture+">"+text)
   		else
   		if (alignLeft==orientation)
				document.write("<table border=0><tr valign=top><td>"+text+"</td>"+
					"<td align=center><img src="+picture+"></td></tr></table>")
   		else
   		if (alignBottom==orientation) document.write("<center><img src="+picture+"></center>"+text)
   		else
   		if (alignTop==orientation) document.write(text+"<center><img src="+picture+"></center>")
   		else
			if (text!="")
				document.write("<table border=0><tr valign=top><td>"+text+"</td>"+
					"<td align=center><img src="+picture+"></td></tr></table>")
			else
				document.write("<center><img src="+picture+"></center>")

   		document.write(BRCLR)
   	}
   	writehelp()
	}

   if (video!="")
   {
   	helpanchor="VIDEO"
   	writeoptions("Click the <img border=0 src=../share/video.gif> button to view the video.",false,false,next!="")
   	document.writeln(text)
   	writehelp()
   	document.writeln("<center><a href=javascript:popvideo('" + video + "')>")
   	document.writeln("<img src=../share/video.gif></a></center>")
   }


   if (nhotspots>0)
   {
   	helpanchor="IMAGEMAP"
   	writeoptions("Click on the picture",false,false,next!="")
   	writehelp()
   	document.writeln("<img src="+picture+" usemap=\"#cop\">")
   	document.writeln("<map name=\"cop\">");
   	for (h=0;h<nhotspots;h++)
   			document.writeln("<area shape=RECT coords=\""+
   			hotspots[h].left+','+hotspots[h].top+','+
   			hotspots[h].right+','+hotspots[h].bottom+
   			"\" href=\"javascript:mapclick("+h+")\">");
   	document.writeln("</map>")
   }


   if (nslides>0)
   {
   	helpanchor="SLIDE"
   	writeoptions("Look at each item in the slide show by pressing the <img border=0 src=../share/ss-next.gif> button.",false,false,next!="")

   	document.writeln(text)
   	writehelp()
   	icons=new Array("ss-back.gif","ss-back_.gif","ss-next.gif","ss-next_.gif")
   	document.write("<table width=100%>")
   	document.write("<td align=center><img src=" +slides[0]+"></td></tr><tr><td align=center>")
   	document.write("<a href=javascript:goslide(-1)><img border=0 src=../share/ss-back.gif></a>")
   	document.write("<a href=javascript:goslide(1)><img border=0 src=../share/ss-next.gif></td></tr>")
   	document.write("</table>")

   	goslide(0)
   }


   if (sound!="")
   {
   	helpanchor="SOUND"
   	writeoptions("Click the speaker icon to listen to the sound.",false,false,next!="")
   	document.writeln(text)
   	writehelp()
   	document.write("<BR><center>")
   	if (picture!="") document.write("<img src="+picture+">")
   	if (sound!="")
   	{
   		document.write("<a onclick=\"javascript:soundclick()\" href="+sound+"><img src=../share/sound.gif></a>")
   		document.writeln("<div id=transcript>"+transcript+"</div>")
   	}
   }

	writepagename()
}

function hotspot(l,t,r,b,link,popup,scored)
{
	this.left=l
	this.top=t
	this.right=r
	this.bottom=b
	this.link=link
	this.popup=popup
	this.scored=scored
}


function soundclick()
{
	showLayer("transcript")
	return true;
}

function goslide(delta)
{
	slide+=delta
	if (slide<0) slide=0;
	if (slide>nslides-1) slide=nslides-1;
	document.images[BASE+0].src=slides[slide]
	document.images[BASE+1].src="../share/"+icons[0+(slide==0)]
	document.images[BASE+2].src="../share/"+icons[2+(slide==nslides-1)]
}

function mapclick(h)
{
	if (hotspots[h].popup)
		pophtml(hotspots[h].link)//,hotspots[h].scored)
	else
		document.location=hotspots[h].link
}

function popvideo(popup)
{
	var popwin2=window.open(popup,"Video",'resizable=0,scrollbars=0,width=200,height=200');
}

//### End shared book page functions



//### Shared Multiple choice Page functions

function GenerateMCPage()
{
	nbuttons=buttons.length
	ndetails=details.length
	attempts=0
	iconadjust=1
	writeoptions("Click on the button(s) to indicate your choice.",false,true,next!="")

	document.write(ques)

	writehelp()
	document.write("<table>")
	if (justbuttons)
	{
		document.write("<tr align=center>")
		for (var c=0;c<nbuttons;c++)
		{
			document.write("<td><img src=../share/blank.gif></td>")
			document.write("<td><input type=button onclick='mc_choose(" + c + ");' value=' "+buttons[c]+" '></td>")
		}
		document.write("</tr>")
	}
	else if (multiplebuttons)
	{
		for (var d=0;d<ndetails;d++)
		{
			document.writeln("<tr><td><b>"+(d+1)+".</b></td>")
			for (var b=0;b<nbuttons;b++)
			{
				document.write("<td><img src=../share/blank.gif></td>")
				document.writeln("<td valign=center><input type=button onclick='mc_choosemulti(" + d + "," + b + ");' value=\""+buttons[b]+"\"></td>" )
			}
			document.writeln("<td>"+details[d]+"</td></tr>")
		}
	}
	else
	{
		iconadjust=2
		for (var c=0;c<ndetails;c++)
		{
			document.write("<tr>")
			document.write("<td><img src=../share/blank.gif></td>")
			document.write("<td valign=center><a href='' onclick='mc_choose(" + c + ");return false'><img border=0 src=../share/"+letter.substring(c,c+1)+".gif></a></td>" )
			document.write("<td>"+details[c]+"</td></tr>")
		}
	}
	document.write("</table>")
	writepagename()
}

function mc_choose(c)
{
	attempts=attempts+1
	if (feedback[c].feedback!="")
	{
		setgradeicon(c*iconadjust,feedback[c].grade)
		showfeedback(feedback[c].feedback,feedback[c].grade)
		//SaveScore(title,feedback[c].grade,attempts)
	}
	if (feedback[c].dest!="")
		gotopage(feedback[c].dest)
}


function mc_choosemulti(q,c){
	attempts=attempts+1
	var idx=mc_checkindex(q,c)
	setgradeicon(idx,feedback[idx].grade)
	showfeedback(feedback[idx].feedback,feedback[idx].grade)
}

function mc_checkindex(q,c)
{
	return c+q*nbuttons
}

function mc_doreveal()
{
	if (attempts==0) {tryitonce();return;}
	if (multiplebuttons)
		for (q=0;q<ndetails;q++)
			for (c=0;c<nbuttons;c++)
			{
				var idx=mc_checkindex(q,c)
				setgradeicon(idx,feedback[idx].grade)
			}
	else
	if (justbuttons)
		for (c=0;c<nbuttons;c++) setgradeicon(c,feedback[c].grade);
	else
		for (c=0;c<ndetails;c++) setgradeicon(c*iconadjust,feedback[c].grade);
}


//### End of shared Multiple choice Page functions

function hasdebugbox()
{
	return (document.forms["debug"]!=null)
}
function debug(s)
{
	if (hasdebugbox()){
		var e=document.forms["debug"].elements["trace"]
		e.value=s + " [] " + e.value
	}else if (parent.WriteInstructions==null) window.status=s;else parent.WriteInstructions("Status",s);//window.status=s
}


function htmlgradeicon(grade)
{
	return "<img src=../share/"+gradeicon[gradeindex(grade)]+">"
}

function FEEDBACK(feedback,grade,dest)
{
	this.feedback=feedback
	this.grade=grade
	this.dest=dest
}
function gradeindex(grade){
	var idx="RWMI".indexOf(grade);
	if (idx < 0)
		alert("Grade index underflow!");
	return idx;
}

//################# Write various components to the page

var helpmsg

function embedhelp(helpmsg)
{
	document.write(
		"<table  width=100% border=0><tr><td width=50%></td><td width=50%>"+
		"<i><a href='javascript:parent.nav.popupdirections()'><font size=-1 color=#444488>" + helpmsg +
		"</font></a></i>"+
		"</td></tr></table>"+
		"\n")

//	document.write("<p style='width:50%;align:right'><i><a href='javascript:parent.nav.popupdirections()'><font size=-1 color=#444488>" + helpmsg + "</font></a></i></P><BR>")

}

function writehelp()
{
	embedhelp(helpmsg)
//11/1/99
	if (toolbar!=null && parent!=null && parent.nav!=null)
	{
		if (toolbar[0]=="CLEAR")
			parent.nav.clearlinks()
		for (var i=1;i<toolbar.length;i+=2)
		{
			parent.nav.addlink(toolbar[i],toolbar[i+1])
		}
	}
}

function writeoptions(instructions,grade,reveal,nextpage)
{
// Output the page title and instructions and activate appropriate navigator buttons

	if (pop!=null) { pop.close();pop=null;}//1/99 Close the feedback window if it's open.

	
	if (grade==null) grade=true
	if (reveal==null) reveal=true
	if (nextpage==null) nextpage=true

	helpmsg=instructions

	if (parent.WriteInstructions)
	{
		parent.WriteInstructions(title,instructions)
		parent.nav.SetNavBar(nextpage,grade,reveal)
	}
	else if(0)
	{ //This stuff is printed on the page if the page is in it's own document and not in a frame set with a Nav Bar
		document.write("<table width=100% border=0 cellpadding=6 cellspacing=0><tr bgcolor=#eeeeee><td>&nbsp;")
		if (grade) document.write("<a href=javascript:dograde()>GRADE</a> ")
		if (reveal) document.write("<a href=javascript:doreveal()>REVEAL</a> ")
		if (nextpage) document.write("<a href=" + next + ">NEXT</a> ")
		document.write("<td>"+instructions+"</td>")
		document.write("</td></tr></table>")
	}
}


function writeunsupported()
{
	document.writeln("<b>The full functionality of this page requires version 4 or higher of "+Browser+"</B>") 
}

//########### Process interactions


function tryitonce()
{
	note("Gee whiz, you should try it at least once.");
}
function setgradeicon(iconindex,grade)
{
	if (iconindex>=0) document.images[iconindex].src="../share/"+gradeicon[gradeindex(grade)];
}
function donextpage()
{
	if (next)
		if (next!="")
			gotopage(next)
}
function dooutline()
{
	window.location="lesson_outline.htm"
}

function gotopage(page)
{//7/98
	window.location=page;
}

function note(notemsg)
{
//We might want to use a different window for notes in the future so we use note() instead of alert() 
	alert(notemsg)
}


function pophtml(popup)//Called when user clicks on a popup link within text
{
	pop=window.open(popup,"popup",'resizable=1,scrollbars=1,width=400,height=400,');
//11/24/99 if you leave out "Popup", popup replaces current frame!
}
function jumphtml(popup)//Called when user clicks on a jump link within text of either regular or popped up page
{	
	window.location=popup 
}



function writepagename()//6/7/99 used to attach the name of the CALIAuthor page to the end of the HTML page
{
	var score=""
	if (parent.scoretotal>0)
	{
		score="<a href=javascript:showscore()>Score: " +
			parent.scorecount + "/" + parent.scoretotal + " (" + Math.floor(parent.scorecount*100/parent.scoretotal) + "%)" +
			"</a>"
	}
	document.write("<p align=right>" + score + "\n")
	document.write("<font color=#aaaaaa><small>"+title+"</small></font></p>\n")
}
function showscore()
{
	parent.showscore()
}


function showfeedback(feedback,grade)
{
//Display text of feedback in popup window. Grade is of type 'R,W,M,I' which determines which icon and background to use.
	if (feedback==null) return //if not feedback, don't show a popup.
	if (grade==null) grade=INFO //if no grade given, assume Informational.

	//we could save the scores in acookie for recording to Server :-)
	if (parent.scoreinfo.indexOf("<Q>"+title+"</Q>")<0)
		if (grade==RIGHT || grade==WRONG) 
	{	//If question not already answered, and is either RIGHT or WRONG add to list and increment score tally.
		parent.scoreinfo=parent.scoreinfo + "<Q>" + title + "</Q><G>" + grade + "</G>\n";
		if (grade==RIGHT)
		{
			parent.scorecount+=1;//increment score and total
			parent.scoretotal+=1;
		}
		if (grade==WRONG)
		{
			parent.scorecount+=0;//no score but increment total
			parent.scoretotal+=1;
		}
	}


	grade=gradeindex(grade)
	if (feedback.length<200)
		h=150;
	else
		h=400;



//Create the popup window. 10/20/99 These windows must be closed before proceeding.
	pop=window.open("","Popup","resizable=1,scrollbars=1,width=400,height=" + h); 
	if (pop==null) return;
	if (pop.focus!=null) pop.focus();

	pop.document.writeln("<title>" + gradetitle[grade] + "</title>")
	pop.document.writeln( "<body bgcolor=" + gradecolor[grade]  +">")

 // + " onBlur=self.focus()"

		//8/26/98 Don't do auto focus since IE4 scrolls to the bottom. Find afix?
		//" onload=\"document.forms[0].elements[0].focus();self.scroll(0,0);\">"); 


	//pop.document.writeln("<script language=\"javascript\" src=\"common.js\"></script>")'Using this Include script causes IE4 to CRASH AND BURN!

//1/29/99 Added JumpHTML to handle jump links in feedback pops.
	pop.document.writeln("<script language='javascript1.2'>")
	pop.document.writeln("function pophtml(popup){popwin=window.open(popup,\"Popup\",'resizable=1,scrollbars=1,width=400,height=400');}")//Popup
	pop.document.writeln("function jumphtml(gourl){if (opener) opener.location=gourl;else if (parent) parent.location=gourl;self.close();}")
	pop.document.writeln("</script>")



	if (feedback.indexOf("<P>")==0)
		feedback=feedback.substring(3,feedback.length)
	pop.document.writeln("<P><img src=../share/" + gradeicon[grade] + ">");
	pop.document.writeln(feedback);
	pop.document.writeln("<center><form onsubmit=\"self.close();return false;\">");
	pop.document.writeln("<input type=submit value=\"Continue\"></form></center>");




	pop.document.writeln( "</body>");
	pop.document.close();
}


//################################## END OF COMMON




//################################## TOP OF LAYERS
// Netscape or IE layer functions
//GENERAL FUNCTIONS


	if (NETSCAPE){
		layerRef="document.layers";//layerRef is the general reference to all layers
		styleSwitch="";//not used yet (but necessary to access layers in Microsoft
		visibleVar="show";
		windowWidth=window.innerWidth;//this is the way Netscape gets the width and height of the viewing window
		windowHeight=window.innerHeight;
		pxSwitch="";
		widthClipSwitch=".clip.right";
		heightClipSwitch=".clip.bottom";
		eventX="e.pageX";
		eventY="e.pageY";
	}else{
		layerRef="document.all";
		styleSwitch=".style";
		visibleVar="visible";
		windowWidth=window.screen.availWidth;
		windowHeight=window.screen.availHeight;
		pxSwitch="px";
		widthClipSwitch=".posWidth";
		heightClipSwitch=".posHeight";
		eventX="event.clientX";
		eventY="event.clientY";
	}

// ####################
// Must declare functions MOUSEDOWN(), MOUSEUP(),MOUSEDRAG(nx,ny,dx,dy),GETELEMENT(x,y)
	var objSelected=null;
	var objSelectedIndex=null;
// ####################


function startCapture(){
	if (LAYERS)
		if (NETSCAPE) {
			document.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN|Event.MOUSEDRAG);
			document.onmousedown=doMouseDownN;
			document.onmouseup=doMouseUpN;
		}else{
//			document.ondragstart = doMouseDownIE//doDragStartIE;
			document.onmousedown = doMouseDownIE;
			document.onmousemove = doMouseMoveIE;
			document.onmouseup = doMouseUpIE;
		}
}

//NETSCAPE CLICK N DRAG FUNCTIONS
function doMouseDownN(event) { 
//mouse is down, find out which draggable object may have been clicked.
	objSelected=GETELEMENT(event.pageX,event.pageY);
	if (objSelected != null){
		document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove=doMouseDragN; 
  		oldX=event.pageX;
		oldY=event.pageY;
		MOUSEDOWN()
		return false;
	}
}
function doMouseUpN(event) {
	if (objSelected != null){
		document.onmousemove=0; 
		document.releaseEvents(Event.MOUSEMOVE);
		MOUSEUP()
		objSelected=null;
		return false;
	}
}
function doMouseDragN(event) {
	if (objSelected != null){
		MOUSEDRAG(event.pageX,event.pageY,event.pageX - oldX, event.pageY - oldY);
		oldX = event.pageX;
		oldY = event.pageY;
	}
}
//---------IE CLICK N DRAG CODE--------------------
function getxy(){
	x=event.clientX;
	if (event.srcElement.tagName=="IMG")
		y=event.y
	else
		y=event.offsetY  +event.srcElement.style.posTop
}
function doMouseDownIE() {
  if (event.button==1){
		getxy()
		objSelected = GETELEMENT(x,y);
//debug(event.srcElement.tagName+":"+x+","+y+"  ; "+event.clientY +","+event.offsetY+","+event.y+","+ event.srcElement.style.pixelTop+","+event.srcElement.style.posTop+"  -> "+objSelected)
  		oldX=x;oldY=y;
		if (objSelected!=null) MOUSEDOWN()
	}
/*  if ((event.button==1) && (event.srcElement.tagName=="IMG")){
		objSelected = event.srcElement
		MOUSEDOWN()
	}*/
}
function doMouseMoveIE() {
	if ((event.button==1) && (objSelected!=null)) {
		getxy()
		event.returnValue = false
		event.cancelBubble =true
		MOUSEDRAG(x,y,x-oldX,y-oldY)
		oldX=x;oldY=y
	}
}

		//var x=event.clientX-document.all.OuterDiv.offsetLeft-(objSelected.offsetWidth/2)
		//if (newleft<0) newleft=0
		//objSelected.style.pixelLeft= newleft
		//var y=event.clientY -document.all.OuterDiv.offsetTop-(objSelected.offsetHeight/2)
		//if (newtop<0) newtop=0
		//objSelected.style.pixelTop= newtop

function doMouseUpIE(){
	objSelected=null;
	MOUSEUP()
}
function doDragStartIE() {
  // Don't do default drag operation.
  if ("DIV"!=event.srcElement.tagName)
    event.returnValue=false;
}
  


// LAYER CODES


function LayerInfo(x,y,w,h){
	this.x=x
	this.y=y
	this.w=w
	this.h=h
}

function layerInfo(layerName){
	if (NETSCAPE){
		with (document.layers[layerName])
			return new LayerInfo(pageX,pageY,clip.width,clip.height)
	}else{
		return new LayerInfo(0,0,0,0)
	}
}

// Functions taking a layer NAME
function layerObject(layerName)
	{var obj=eval(layerRef+'["'+layerName+'"]');if (obj==null) alert(layerName+" not defined!");return obj}

function showLayer(layerName)
	{eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');}

function hideLayer(layerName)
	{eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');}

function layerResize(layerName,sx,sy)
	{var obj=eval(layerRef+'["'+layerName+'"]');if (NETSCAPE) obj.resizeTo(sx,sy);else {obj.style.pixelWidth=sx;obj.style.pixelHeight=sy;}}

function layerZDepth(layerName,newz)
	{if (NETSCAPE) document.layers[layerName].zIndex=newz; else document.all[layerName].style.zIndex=newz;}

function layerColor(layerName,color)
	{if (NETSCAPE) document.layers[layerName].bgColor=color;else document.all[layerName].style.backgroundColor=color;}

// Functions taking a layer OBJECT
function layerLeft(obj)
	{if (NETSCAPE) return obj.pageX; else return obj.offsetLeft;}

function layerTop(obj)
	{if (NETSCAPE) return obj.pageY; else return obj.offsetTop;}

function layerWidth(obj)
	{if (NETSCAPE) return obj.clip.width; else return obj.style.pixelWidth;}

function layerMove(obj,x,y) //WAS BY NAME! 
	{if (NETSCAPE) obj.moveTo(x,y);else {obj.style.pixelLeft=x;obj.style.pixelTop=y;}}

function layerOffset(obj,dx,dy)
	{if (NETSCAPE) obj.offset(dx,dy); else {obj.style.pixelLeft+=dx;obj.style.pixelTop+=dy;}}


function layerContainsPoint(obj,clickX,clickY){
	var l,t,r,b;
	if (obj==null) return false;
	if (NETSCAPE){
		l=obj.left;
		r=obj.left+obj.clip.width;
		t=obj.top;
		b=obj.top+obj.clip.height;
	}else{
//alert(obj.style.posTop +","+obj.style.pixelTop)
		l=obj.style.posLeft;
		r=obj.style.posLeft+obj.style.posWidth;
		t=obj.style.posTop;
		b=obj.style.posTop+obj.style.pixelHeight;
	}
	return (clickX > l) && (clickX < r) && (clickY > t) && (clickY < b);
}

function pagewidth(){
	if (NETSCAPE)
		return window.innerWidth;
	else
		return document.body.clientWidth;
}

function layerInit(layername,newx,newy,w,h,showit,color){
	var obj=layerObject(layername)//eval(layerRef+'["'+layername+'"]')
	if (NETSCAPE){
		obj.pageX=newx;
		obj.pageY=newy
		obj.clip.width=w;
	 	obj.clip.height=h;
	}else{
		obj.style.pixelLeft=newx;
		obj.style.pixelTop=newy
		obj.style.pixelWidth=w
		obj.style.pixelHeight=h
		obj.style.clip="rect(0,auto,"+h+",0)"
//		obj.style.clip="rect(0,"+w+","+h+",0)"
	}
	if (color!=null) layerColor(layername,color)
	if (showit) showLayer(layername); else hideLayer(layername);
}

function layerText(layerName,text){
	if (NETSCAPE) {
		document.layers[layerName].document.write(text);
		document.layers[layerName].document.close();
	}else{
		document.all[layerName].innerHTML=text;
	}
}
//#######################################################
//################################## END OF LAYERS ######
//#######################################################
