//CHECKJS  D:\js\safety\safety.js 5/29/2003 7:57:12 AM
VER="6/3/03 feeback: <a href=mailto:hansonr@stolaf.edu>Bob Hanson</a>"
hading=""

ifnumbers=true
ABC=(ifnumbers?"123456789":"abcdefghij")


MINUS=sym("-")
EACUTE=sym("#3")
sdigits=" 0123456789"
istest=false&&true
isqok=false
iscramble=true
toggle=1
sqlist=""
Ans=new Array()
Score=new Array()
nQuest=0
nq=1
nimage=0
mytime0=0
docstyle="<style>"
docstyle+="body,td,th,tr,p,.p {font-size:24pt}"
docstyle+="H2 {font-size:24pt}"
docstyle+="H3 {font-size:16pt}"
docstyle+="table,form,blockquote,td,tr,body{margin-top: 1px; margin-bottom: 1px;}"
docstyle+=".q{position:relative}"
docstyle+=".table0{background-color:white}"
docstyle+=".table1{background-color:#F0F8FF}"
docstyle+=".table2{background-color:#FFF8DC}"
docstyle+="sub,sup{font-size:80%}"
docstyle+="a{color:darkred}"
docstyle+=".btn{position:absolute;top:100;left:500}"
docstyle+=".pt{background-color:#FFCC00}"
docstyle+=".p{background-color:#FFCC00}"
docstyle+=".symbol{font-family:symbol}"
docstyle+=".prologbody{background-color:#000000}"
docstyle+=".prologinfobody{background-color:#000000;color:#FFCC00;font-size:14pt;margin-left:11mm;margin-right:11mm}"
docstyle+=".epiloginfobody{background-color:#000000;color:#FFCC00;font-size:14pt;margin-left:11mm;margin-right:11mm}"
docstyle+=".epilogbody{background-color:#000000}"
docstyle+="</style>"
document.write(docstyle)

function check(n,a,i){
	var isok=(Ans[n].indexOf(","+a+",")>=0)
	document.images[i].src=(isok?"ok.gif":"x.gif")
	if(!Score[n])Score[n]=(isok?1:-1)
}

function countQuestions(){
	nQuest=0
	for(var i=1;i<Questions.length;i++)nQuest+=(Questions[i][1]!=""?1:0)
}

function getQuestion(i){
	var sout=""
	var s=""
	var section=Questions[i][0]
	var q=Questions[i][1]
	var a=Questions[i][2]
	var x=Questions[i][3]

	if(a=="")isqok=(x.indexOf("x")>=0)
	if(x.indexOf("0")>=0)nq=1

	if(!isqok)return ""

	var ishorizontal=(x.indexOf("h")>=0)
	var allok=(a.indexOf("*")<0)
	var A=a.split(";")
	var nsort=A.length-(parseInt(x)?parseInt(x):0)
	var r=""
	var isok=0
	var ni=0
	
	if(section!=""){
		sout="<hr><h3>"+section+"</h2><p>"
		return sout
	}
	
	toggle=3-toggle

	if(sqlist.indexOf("?")>=0){
		//if(nq==4)alert(nq+" "+(sqlist.indexOf(","+nq+","))+" "+sqlist)
		if(sqlist.indexOf(","+nq+",")<0){
			Score[nq]=1
			nq++
			return ""
		}
	}
	sout+="<table width=800><tr><td valign=top align=left class=table"+toggle+">"
	sout+="\n<table "+(istest?"border":"")+" width=800>"
	+"<tr><td valign=top width=80></td><td align=left width=20></td><td width=500></td></tr>"
	+"<tr><td valign=top align=right>"

	+nq+".</td>"+"<td align=left colspan=10 valign=top>"+q+"<br>&nbsp;</td></tr>"
	if(q.indexOf("<img")>=0)nimage++
	if(iscramble){
		for(var j=0;j<nsort-1;j++){if(Math.random()>0.5){
				a=A[j]
				A[j]=A[j+1]
				A[j+1]=a
		}}
	}
	Ans[nq]=""
	if(ishorizontal)sout+="<tr><td></td><td valign=top colspan=8>"
	for(var j=0;j<A.length;j++){
		s=A[j]
		if(allok || s.indexOf("*")>=0){
			isok=true
			Ans[nq]+=","+j+","
			if(!istest)s=s.replace(/\*/g,"")
		}else{
			isok=false
		}

		if(s.indexOf("~")==0)s=setsubs(s.substring(1,s.length),0)
		if(!ishorizontal)sout+="\n<tr><td valign=top align=left>"
		sout+="<img src="+(!istest?"transp.gif":isok?"ok.gif":"x.gif")+" name=img"+nimage+">"
		if(!ishorizontal)sout+="</td><td valign=top align=right>"
		sout+="<a href=javascript:check("+nq+","+j+","+nimage+")>"+ABC.charAt(j)+"</a>)&nbsp;&nbsp;"
		if(!ishorizontal)sout+="</td><td align=left valign=top>"
		sout+=s

		if(!ishorizontal)sout+="</td></tr>"
		nimage++
	}
	if(ishorizontal)sout+="</td></tr>"
	//if(nq==5)alert(sout)
	sout+="</table>"
	sout+="</td></tr></table>"
	sout=sout.replace(/\[\[\-\]\]/g,MINUS).replace(/\[\[eacute\]\]/g,EACUTE)
	nq++

	sout+="<table><tr height=150><td></td></tr></table>"
	
	return sout
}

function setsubs(sform,icharge){
	var s=""
	var sf=""
	if(arguments.length==1)icharge=0
	var iabs=Math.abs(icharge)
	for (var i=0;i<sform.length;i++)
	{
		s=sform.charAt(i)
		sf=sf + (sdigits.indexOf(s)>0?"<sub>"+s+"</sub>":s)
	}
	if(icharge)sf+="<sup>"+(iabs==1?"":iabs)+(icharge>0?"+":minussign)+"</sup>"
	return sf
}

function showAllQuestions(){
	
	var S=(unescape(document.location+"@0")).split("@")
	sqlist=S[0]
	mytime0=parseInt(S[1])
	nimage=document.images.length
	nq=1
	var sout="<form name=info>"
	for(var i=1;i<Questions.length;i++)sout+=getQuestion(i)
	sout+="</form>"
	document.write(sout)
}

function showEnd(){
	var sout="<center><form><input type=button value=\"Score the Quiz\" onclick=showScore()></form></center>"
	+"<center><form><input type=button value=\"New Blank Exam\" onclick=showNew()></form></center><pre>\n\n\n\n\n</pre>"
	document.write(sout)
	now=new Date()
}

function showNew(){
	var S=(document.location+"").split("?")
	document.location=S[0]
}

function showScore(){
	var nok=0
	var nx=0
	var nblank=0
	var slist=""
	var s=""
	for(var i=1;i<nq;i++){
		if(Score[i] && Score[i]<0){
			nx++
			slist+=","+i
		}else if(Score[i] && Score[i]>0){
			nok++
		}else{
			nblank++
			slist+=","+i
		}
	}
	var mytime=mytime0+((new Date())-now)/1000
	var mm=Math.floor(mytime/60)
	var ss=Math.floor(mytime-mm*60)
	var mmss=mm+" minute"+(mm==1?"":"s")+" and "+ss+" second"+(ss==1?"":"s")
	alert("It took you "+mmss+" to finish the test.\nYou answered "+(nx+nok)+" of "+(nq-1)+" questions. "+nok+(nok==1?" was":" were")+" correct. Your score is "+Math.floor(nok/(nq-1)*100)+"%")
	if(slist!=""){
		alert("The questions you got wrong or did not answer will now be asked again.")
		s=document.location+""
		if(s.indexOf("?")>=0)s=s.substring(0,s.indexOf("?"))
		s+="?"+slist+",@"+mytime
		document.location=s
	}else{
		alert("Congratulations! You have answered all the questions correctly. In all, it took you "+mmss+" to complete the test. "+(mytime<=600?"That is a very good time!":"You should repeat the test and try to get 100% within 10 minutes."))
	}
}

function showTop(){
	countQuestions()
	var sout=""
	+"<span class=heading>"+heading+"</span>"
	
	document.write(sout)
}

