//copyright 2001 Robert M. Hanson, St. Olaf College, Northfield, MN
//all rights reserved

ntypes=Types.length
TF=0
Choice=1
YesNo=2
image1=0
image2=0

function dowrite(doc,s,isrep)
{
 if(arguments.length==2)isrep=true
 isrep?doc.open("text/html","replace"):doc.open()
 doc.write("<html><body>"+s+"</body></html>")
 doc.close()
} 

function loadquestion(){
 return doquestion(getrnd(3))
}

function doquestion(itype){
 if(itype==Choice)return doChoice()
 if(itype==TF)return doTF(0)
 if(itype==YesNo)return doTF(1)
}

function doChoice(){
 var itype=gettwoimages()
 var isoptional=getrnd(2)
 var s1=""
 var s2=""
 var s="<center><h3>How are these two compounds related?</h3><p>"
 for(var i=0;i<ntypes;i++){
	var s1=stypeof(i,isoptional)
	if(s1!=s2)s+="<a href=javascript:ans("+i+")>"+(s1.indexOf("They are")<0?"they are "+s1:s1)+"</a><br>"
	if(s1==s2 && itype==i)itype=i-1
	s2=s1
 }
 s+="<a href=javascript:ans(-1)>they are identical</a><br>"
 s+="<p>"
 s+="<img src="+root+image1+".gif>"
 s+="<img src="+root+image2+".gif>"
 answer=itype
 sorry="They are "+stypeof(itype)+"."
 return s
}

function doTF(isyesno){
 var itype=gettwoimages()
 var istrue=getrnd(2)
 var isthis=getrnd(2)
 var isoptional=getrnd(2)
 var swhatthis=stypeof(itype,isoptional)
 var i=itype
 while(i==itype)i=getrnd(ntypes)
 var swhatother=stypeof(i,isoptional)
 var swhat=(istrue?swhatthis:swhatother)
 answer=(istrue?0:1)
 if(isyesno){
        var s="<center><h3>Are these two compounds "+swhat+"?<p><a href=javascript:ans(0)>Yes</a> or <a href=javascript:ans(1)>No</a></h3><p>"
 }else{
	var s="<center><h3><a href=javascript:ans(0)>True</a> or <a href=javascript:ans(1)>False</a>:<p>"
        s+="These two compounds are "+swhat + ".</h3><p>" 
 }
 s+="<img src="+root+image1+".gif>"
 s+="<img src="+root+image2+".gif><p>"
 sorry="They are "+swhatthis+"."
 return s
}

sorry=""
function ans(i){
 if(answer==i){alert("Correct.");getanother();return}
 alert("No. "+sorry)
}

function getanother(){parent.fraIndexE.setTimeout("doit(iagain)",100)}

function stypeof(itype,isoptional){
 var swhat=""
 if(itype<0){
	 swhat="identical"
 }else{
	 swhat=Types[itype]
 }
 var i=swhat.indexOf("|")
 if(i>=0){
	if(isoptional){
		swhat=swhat.substring(i+1,swhat.length)
	}else{
		swhat=swhat.substring(0,i)
	}
 }
 return swhat
}

function getrnd(n){return Math.floor(Math.random()*n)}

function gettwoimages(){
 image1=getrnd(Data.length-1)+1
 image2=image1
 while(image2==image1 || DontCompare.indexOf("("+image1+","+image2+")")>=0 || DontCompare.indexOf("("+image2+","+image1+")")>=0)image2=getrnd(Data.length-1)+1
 var itype=-1
 for(i=0;i<ntypes && itype==-1;i++){
	if(Data[image1][i]*Data[image2][i]>0 && Data[image1][i]!=Data[image2][i])itype=i
 }
 return itype 
} 

