<!--Copyright © 1999 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("This page must be viewed within the Frame setting!")
}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





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=../HTMLSHARE/"+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 align=right width=50%><tr><td><i><a href='javascript:parent.nav.popupdirections()'><font size=-1 color=#444488>" + helpmsg + "</font></a></i></td></tr></table><P><BR>")

//	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)
}

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="../HTMLSHARE/"+gradeicon[gradeindex(grade)];
}
function donextpage(){gotopage(next)}
function dooutline(){window.location="lesson_outline.html"}

function gotopage(page){window.location=page;}//7/98
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');
}
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=parent.scorecount + "/" + parent.scoretotal + " (" + Math.floor(parent.scorecount*100/parent.scoretotal) + "%)"
	}
	document.write("<p align=right><a href=javascript:showscore()>Score: " + score + "</a>\n")
	document.write("<font color=#aaaaaa><small>"+title+"</small></font></p>")
}
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 (grade==null) grade=INFO


	//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
   	pop=window.open("","Popup","resizable=1,scrollbars=1,width=400,height=" + h);
	if (pop==null) return;
	if (NETSCAPE) pop.focus();

	pop.document.writeln("<title>" + gradetitle[grade] + "</title>")
	pop.document.writeln( "<body bgcolor=" + gradecolor[grade] + ">") 
		//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='javascript'>")
	pop.document.writeln("function pophtml(popup){popwin=window.open(popup,\"Popup\",'resizable=1,scrollbars=1,width=400,height=400');}")
	pop.document.writeln("function jumphtml(gourl){if (opener) opener.location=gourl;else if (parent) parent.location=gourl;self.close();}")
	pop.document.writeln("</script>")




	pop.document.writeln("<img src=../HTMLSHARE/" + 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 ######
//#######################################################
