//copyright 2001 Robert M. Hanson, St. Olaf College, Northfield, MN 55057
//////drawing part//////
//styles are all set here
infotop=0
starttop=infotop+100
starttop2=starttop
startleft=new Array(70,500,70)
startleftr=280
starttopr=starttop+20
msgleft=0
msgtop=225
Noleft=new Array("CO2H")
Places=new Array(49,-15, 107,17, 107,80, 49,104, -8,80, -10,17)//left,top

function setdivpositions(){
  divanchor("anchor")
  divmove("info2",startleft[2],infotop)
  divmove("compound",startleft[2],starttop)
  for(var i=0;i<6;i++)divmove("H2"+i,startleft[2]+Places[2*i],starttop+Places[2*i+1])
  divmove("answer",startleftr,starttopr)
  divmove("message",msgleft,msgtop)
}

function showmod(itype){return}

function drawanswer(sinfo){
 writediv("answer","<font size=4><b>"+sinfo+"</b></font>")
}


////// calc part //////

ithis=0
Answer=new Array(0,0,0,0,0,0)

function calcanswer(itype){
 var ipt=0
 var isym=0
 var A=new Array(0,0)
 var B=new Array()
 Reagenttype[0]=new Array("GENERIC",0,0,99,"x","s","o","m","p",0)
 getproduct(Compound[itype],0,Compound[1],false) 
 checksymmetry(Compound[itype],A)
 B=Symmetry[A[0]+1].split(",")
 for(var i=0;i<6;i++)if(Compound[1][i]==99)ipt=i
 isym=B[(ipt+A[1]) % 6]
 for(var i=0;i<6;i++)Answer[i]=(B[(i+A[1])%6]==isym)  
}


function nameof(i){
 var s=Cmpd2[i]
 var slist="o- m- p-"
 if (slist.indexOf(s.substring(0,2))>=0)s="<i>"+s.substring(0,1)+"</i>"+s.substring(1,s.length)
 return "&nbsp;"+s.substring(0,s.indexOf("|"))
}

function showanswer(i){
 var n=0
 if(i==-1 || i>=0 && Answer[i])n+=showselectivity(2)
 if(i==-1){
  drawanswer("The most reactive position"+(n==1?" is":"s are")+" highlighted.<br>&nbsp;<br><a href=javascript:doanother(2)>Another</a>")
 }else{
  drawanswer((i<0?"Click on <b>the carbon atom</b> where<br>you expect the most reaction.":(Answer[i]?"Correct.<br>&nbsp;":"No. Try again.<br>&nbsp;"))+"<br><a href=javascript:doanother(2)>Another</a>")
 }
}

function showselectivity(itype){
 var n=0
 for(var i=0;i<6;i++){if(Answer[i])n+=drawsub(itype,i,Compound[itype][i],true)}
 return n
}

function setselectbyvalue(d,v){
 for(var i=0;i<d.length;i++){
  if(d[i].value==v){d.selectedIndex=i;return}
 }
}
function showcompound(itype,iwhich,ishowanswer){
	var d=getdivlistbox("info"+itype,"form"+itype+".thecmpd")
	var israndom=(iwhich<=0)
	var ilast=(israndom?ithis:0)
	var nsubs=0
	var ipt=0
	var sh="H,H,H,H,H,H"
	var s=""
	while(iwhich<=0){
		iwhich=nextquest(Cmpd2.length-1)
		ithis=iwhich
		s=eval("Cmpd"+itype+"["+iwhich+"]")
		s=s.substring(s.indexOf("|")+1,s.length)
		if(cmpd2keepset.indexOf(s.charAt(0))>=0)iwhich=-1
	}
	setselectbyvalue(d,iwhich)
	s=eval("Cmpd"+itype+"["+iwhich+"]")
	s=s.substring(s.indexOf("|")+1,s.length)
	ipt=0
	while(ipt>=0){nsubs++;ipt=s.indexOf(",",ipt+1)}
	for(var i=0;i<Noleft.length;i++)if(s.indexOf(Noleft[i])>=0)nsubs=6
	s=sh.substring(0,rand(6-nsubs)*2)+s+","+sh
        Compound[itype]=new Array()
        getCompoundfromlist(Compound[itype],s)
	for(var i=0;i<6;i++)drawsub(itype,i,Compound[itype][i],0)
	calcanswer(itype)
	showanswer(ishowanswer?-1:-2)
	if(ishowanswer){
		
	}
}

////user interface////

function doclick(i){
 showanswer(i)
}

function doanother(itype){
	var d=getdivlistbox("info"+itype,"form"+itype+".thecmpd")
	d.selectedIndex=0
	showcompound(itype,-1,false)
}
 
function dothecmpd(itype){
	var d=getdivlistbox("info"+itype,"form"+itype+".thecmpd")
	var i=d[d.selectedIndex].value
	showcompound(itype,i,true)
}

function doinit(){
	var d=getdivlistbox("info2","form2.thecmpd")
	drawanswer(d.length + " compounds<br>&nbsp;<br><a href=javascript:doanother(2)>Start Quiz</a>")	
}
////initialization////

function loadinfo(){
initdivs()
setdivpositions()
s="<html><form name=form2>"
s+=setselect(2,"thecmpd",Cmpd2,0,cmpd2keepset,"#")
//s+='&nbsp;&nbsp;&nbsp;<a href="javascript:doanother(2)">NEW COMPOUND</a>'
s+="</form></html>"
writediv("info2",s)
sloadinfo="Select a compound to highlight the most probable hydrogen to be replaced in an electrophilic aromatic substitution reaction, "
sloadinfo+="or click on \"Start Quiz\" to have one randomly selected so that you can practice. "
sloadinfo+="\n\nThis page is best viewed full-screen."

setTimeout("doinit();alert(sloadinfo)",100)
}
