//1:single groups 2:double groups
//3:triple groups 4:root groups
//5:multiroot groups
Levelprob=new Array(""
,"1222"
,"3"
,"445"
)
ithis=0
theanswer=""
quizlevel=0
Compound[2]=new Array(0,0,0,0,0,0)

function showrandomcompound(ilevel){
 var s=theanswer
 while(theanswer.length==0 || theanswer==s || theanswer=="benzene"){theanswer=getrandomcompound(ilevel)}
 showanswer(0)
}

function getrandomcompound(ilevel){
 var ipt=0
 var i=0
 var ipos=0
 var C=new Array()
 var itype=0
 var sgroup1=""
 var sgroup2=""
 var sgroup3=""
 var sfield=""
 var s=""
 var n=0
 ithis=-1
 while(true){
	ipt=rand(Levelprob[ilevel].length)
	itype=eval(Levelprob[ilevel].substring(ipt,ipt+1))
	if(itype==5){
		sgroup1=randomgroup(multirootnames)
		sgroup2=sgroup1
		sgroup3=(rand(2)?sgroup2:"")
		break
	}
	if(itype==4){
		sgroup1=randomgroup(rootnames)
		sgroup2=randomgroup(ambinames+groupnames)
		sgroup3=(rand(2)?sgroup2:randomgroup(ambinames+groupnames))
		break
	}
	sgroup1=randomgroup(ambinames+groupnames)
	if(itype==1)break
	sgroup2=(rand(2)?sgroup1:randomgroup(ambinames+groupnames))
	if(itype==2)break
	if(rand(2)){
		sgroup2=randomgroup(rootnames)
		if(rand(2))sgroup1=""
	}else{
		sgroup3=(rand(3)==0?sgroup2:randomgroup(ambinames+groupnames))
	}
	break
 }
 Compound[2]=new Array(0,0,0,0,0,0)
 ipos=rand(3)
 Compound[2][ipos]=findsub(Group,sgroup1)
 if(sgroup2.length){
	while(Compound[2][ipos])ipos=rand(6)
	Compound[2][ipos]=findsub(Group,sgroup2)
 }
 if(sgroup3.length){
	while(Compound[2][ipos])ipos=rand(6)
	Compound[2][ipos]=findsub(Group,sgroup3)
 }
 drawcmpd(2)
 s= getname(2)
 return (ilevel>1||s.indexOf("acet")<0?s:"")
}

function drawcmpd(n){for(var i=0;i<6;i++)drawsub(n,i,Compound[n][i],0)}

function randomgroup(slist){
 var i=-1
 var i1=-1
 var s=""
 var n=0
 while(i<0){
	s=slist.substring(rand(slist.length),slist.length)
	i=s.indexOf(";")
	if(i>=0){
		i1=s.indexOf("::",i)
		if(i1>=0){
			s=s.substring(i+1,i1)
			s=strsub(s,";","")
			return s
		}
	}
	i=-1
 }
 return ""
}

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



function setdivpositions(){
  initdivs()
  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)
  drawcmpd(2)  
}

function showmod(itype){return}

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

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


function nameof(i){
 var s="$"+(i<0?theanswer+"|":Cmpd2[i])
 s=strsub(s,"$o-","$<i>o</i>-")
 s=strsub(s,"$m-","$<i>m</i>-")
 s=strsub(s,"$p-","$<i>p</i>-")
 s=strsub(s,"N-","<i>N</i>-")
 s=strsub(s,"N,","<i>N</i>,")
 return "&nbsp;"+s.substring(1,s.indexOf("|"))
}

function showanswer(i){
 drawanswer((i==0?"&nbsp;<p><a href=javascript:donameit()>Answer</a>":nameof(i)+"<p><a href=javascript:doanother(2)>Another</a>")) 
}

////user interface////

function donameit(){
 showanswer(ithis)  
}
 
function doanother(itype){
	var d=getdivlistbox("info"+itype,"form"+itype+".thecmpd")
	d.selectedIndex=0
	if(quizlevel==0){showcompound(itype,-1,false)}else{showrandomcompound(quizlevel)}
}

function dothecmpd(itype){
	var d=getdivlistbox("info"+itype,"form"+itype+".thecmpd")
	var i=d[d.selectedIndex].value
	quizlevel=0
	var d=getdivlistbox("info"+itype,"form"+itype+".q")
	d[0].checked=true
	showcompound(itype,i,true)
}
function showcompound(itype,iwhich,ishowname){
	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=""
        if(iwhich<0)d.selectedIndex=0
	while(iwhich<=0)iwhich=nextquest(Cmpd2.length-1)
	s=eval("Cmpd"+itype+"["+iwhich+"]")
	ithis=iwhich
	s=s.substring(s.indexOf("|")+1,s.length)
	if(cmpd2keepset.indexOf(s.charAt(0))>=0)s=s.substring(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)
	drawcmpd(itype)
	showanswer(ishowname?ithis:0)
}


function setquizlevel(ilevel){
 quizlevel=ilevel
 doanother(2)
}

//overloaded:
function doclick(icmpd,which){
	var d=getdivlistbox("message","formg.thegroup")
	ipt=d.selectedIndex-1
	var i=-1
	while(i<0){
		ipt++
		i=d[ipt].value
	}
	d.selectedIndex=ipt
        if(Compound[icmpd][which]==i)i=0
        Compound[icmpd][which]=i
 	drawsub(icmpd,which,i,0)
	theanswer=getname(2)
	if(theanswer=="")theanswer="<small>Not capable of determining this structure's name.</small>"
	var d=getdivlistbox("info2","form2.thecmpd")
	d.selectedIndex=0
	showanswer(-1)
}

////initialization////
//          xxxxxxxxxxxxxxxxx

function loadinfo(){
setdivpositions()
s="<html><form name=form2><table><tr><td rowspan=4 valign=top>Quiz type:"
s+='<td rowspan=4 valign=top><input type=radio checked name=q onclick="setquizlevel(0)">'+(Cmpd2.length-1)+'-Compound List'
s+=setselect(2,"thecmpd",Cmpd2,0,cmpd2keepset,"")
s+='<td>Full Collection (by difficulty):'
s+='<tr><td><input type=radio name=q onclick="setquizlevel(1)">least difficult (400 compounds)'
s+='<tr><td><input type=radio name=q onclick="setquizlevel(2)">more difficult (17,000 compounds)'
s+='<tr><td><input type=radio name=q onclick="setquizlevel(3)">most difficult (24,000 compounds)'
s+="</table></form></html>"
writediv("info2",s)


s="<html><form name=formg>"
s+=setselect(-1,"thegroup",Group,3)
s+=" With this selection box, you can create your own compounds."
s+=" To change a substituent, select it from the list and click <B>on the carbon</B>"
s+=" where you want the group to appear. If you click on a carbon already"
s+=" having the selected group, that position will revert to H."
s+=" If the name can be determined, it will appear to the right of the structure."
s+=" (Some compounds you can create are beyond the capabilities of this program to name.)"
s+="</form></html>"
writediv("message",s)


sloadinfo="Select a compound from the list to see its structure, "
sloadinfo+="or click on \"Answer\" to see the name of the currently displayed structure. "
sloadinfo+="Only the most common name will be shown."
sloadinfo+="\n\nThis page is best viewed full-screen."
setTimeout("doanother(2);alert(sloadinfo)",100)
}
