//Equilibrium - Basic K Calculations Script
thepage="Equilibrium K Calculations"
//11/18/02-updated netscape 7
totl=0
corrt=0
tried=0
attempts=0
which=0
threaction=0
conc=0
econc=0
theK=0
theQ=0
anser=0
theanswer=""
vformula=""
gformula=""
cmpd=new Array()
coef=new Array()
cmpd[1]=new Array(2,"H<SUB>2</SUB>","O<SUB>2</SUB>","H<SUB>2</SUB>O")
coef[1]=new Array(-2,-1,2)
cmpd[2]=new Array(2,"N<SUB>2</SUB>","H<SUB>2</SUB>","NH<SUB>3</SUB>") 
coef[2]=new Array(-1,-3,2)
cmpd[3]=new Array(1,"CH<SUB>3</SUB>OH","CO","H<SUB>2</SUB>")
coef[3]=new Array(-1,1,2)
cmpd[4]=new Array(2,"CO","H<SUB>2</SUB>","CH<SUB>3</SUB>OH")
coef[4]=new Array(-1,-2,1)
cmpd[5]=new Array(2,"NO","O<SUB>2</SUB>","NO<SUB>3</SUB>")
coef[5]=new Array(-1,-1,1)
cmpd[6]=new Array(1,"NO<SUB>3</SUB>","NO"," O<SUB>2</SUB>")
coef[6]=new Array(-1,1,1)
cmpd[7]=new Array(2,"NO<SUB>3</SUB>"," NO","NO<SUB>2</SUB>")
coef[7]=new Array(-1,-1,2)
cmpd[8]=new Array(1,"NO<SUB>2</SUB>","NO<SUB>3</SUB>"," NO")
coef[8]=new Array(-2,1,1)
cmpd[9]=new Array(2,"NO","O<SUB>2</SUB>","NO<SUB>2</SUB>")
coef[9]=new Array(-2,-1,2)
cmpd[10]=new Array(2,"H<SUB>2</SUB>","ICl","I<SUB>2</SUB>","HCl")
coef[10]=new Array(-1,-2,1,2)
cmpd[11]=new Array(2,"I<SUB>2</SUB>","HCl","H<SUB>2</SUB>","ICl")
coef[11]=new Array(-1,-2,1,2)
cmpd[12]=new Array(2,"CO","NO","CO<SUB>2</SUB>","N<SUB>2</SUB>")
coef[12]=new Array(-2,-2,2,1)
cmpd[13]=new Array(2,"CO<SUB>2</SUB>","N<SUB>2</SUB>","CO","NO")
coef[13]=new Array(-2,-1,2,2)
cmpd[14]=new Array("SO<SUB>2</SUB>","O<SUB>2</SUB>","SO<SUB>3</SUB>")
coef[14]=new Array(2,1,2)
cmpd[15]=new Array(1,"SO<SUB>3</SUB>","SO<SUB>2</SUB>","O<SUB>2</SUB>")
coef[15]=new Array(-2,2,1)
cmpd[16]=new Array(1,"N<SUB>2</SUB>O<SUB>4</SUB>","NO<SUB>2</SUB>")
coef[16]=new Array(-1,2)
cmpd[17]=new Array(1,"NO<SUB>2</SUB>","N<SUB>2</SUB>O<SUB>4</SUB>")
coef[17]=new Array(-2,1)
cmpd[18]=new Array(1,"NOBr","NO"," Br<SUB>2</SUB>")
coef[18]=new Array(-2,1,2)
cmpd[19]=new Array(2,"CH<SUB>4</SUB>"," O<SUB>2</SUB>","H<SUB>2</SUB>CO","H<SUB>2</SUB>O")
coef[19]=new Array(-1,-1,1,1)
cmpd[20]=new Array(2,"NO","Br<SUB>2</SUB>","NOBr")
coef[20]=new Array(-2,-1,2)
cmpd[21]=new Array(2,"CS<SUB>2</SUB>","H<SUB>2</SUB>","CH<SUB>4</SUB>","H<SUB>2</SUB>S")
coef[21]=new Array(-1,-4,1,2)
cmpd[22]=new Array(2,"CO","H<SUB>2</SUB>O","CO<SUB>2</SUB>","H<SUB>2</SUB>")
coef[22]=new Array(-1,-1,1,1)
cmpd[23]=new Array(2,"CO<SUB>2</SUB>","H<SUB>2</SUB>","CO","H<SUB>2</SUB>O")
coef[23]=new Array(-1,-1,1,1)
cmpd[24]=new Array(2,"CH<SUB>4</SUB>","H<SUB>2</SUB>S","CS<SUB>2</SUB>","4H<SUB>2</SUB>")
coef[24]=new Array(-1,-2,1,4)
cmpd[25]=new Array(1,"SO<SUB>2</SUB>Cl<SUB>2</SUB>","SO<SUB>2</SUB>","Cl<SUB>2</SUB>")
coef[25]=new Array(-1,1,1)
cmpd[26]=new Array(2,"SO<SUB>2</SUB>","Cl<SUB>2</SUB>","SO<SUB>2</SUB>Cl<SUB>2</SUB>")
coef[26]=new Array(-1,-1,1)
cmpd[27]=new Array(2,"Cl<SUB>2</SUB>","F<SUB>2</SUB>","ClF<SUB>3</SUB>")
coef[27]=new Array(-1,-3,2)
cmpd[28]=new Array(1,"ClF<SUB>3</SUB>","Cl<SUB>2</SUB>","F<SUB>2</SUB>")
coef[28]=new Array(-2,1,3)
cmpd[29]=new Array(1,"COF<SUB>2</SUB>","CO<SUB>2</SUB>","CF<SUB>4</SUB>")
coef[29]=new Array(-2,1,1)
cmpd[30]=new Array(2,"CO<SUB>2</SUB>","CF<SUB>4</SUB>","COF<SUB>2</SUB>")
coef[30]=new Array(-1,-1,2)
cmpd[31]=new Array(2,"CO","Cl<SUB>2</SUB>","COCl<SUB>2</SUB>")
coef[31]=new Array(-1,-1,1)
cmpd[32]=new Array(1,"COCl<SUB>2</SUB>","CO","Cl<SUB>2</SUB>")
coef[32]=new Array(-1,1,1)
cmpd[33]=new Array(1,"PCl<SUB>5</SUB>","PCl<SUB>3</SUB>","Cl<SUB>2</SUB>")
coef[33]=new Array(-1,1,1)
cmpd[34]=new Array(2,"PCl<SUB>3</SUB>","Cl<SUB>2</SUB>","PCl<SUB>5</SUB>")
coef[34]=new Array(-1,-1,1)
cmpd[35]=new Array(2,"N<SUB>2</SUB>O","O<SUB>2</SUB>","N<SUB>2</SUB>O<SUB>4</SUB>")
coef[35]=new Array(-2,-3,2)
cmpd[36]=new Array(1,"N<SUB>2</SUB>O<SUB>4</SUB>","N<SUB>2</SUB>O","O<SUB>2</SUB>")
coef[36]=new Array(-2,2,3)
basequest=new Array("Given the balanced equation and the equilibrium concentrations shown:<br>","What is the value of K?")
ansphrase=new Array("First, write the equation for Q","Then carefully substitute in the values and calculate K, making sure that you include the exponents in your calculation.")
//begin programs

function startit(){
	clear()
	tried=0
	totl++
	document.forms[0].total.value=totl
	which=Math.ceil(36*Math.random())
	document.forms[0].results.value=which
	getreaction()
	getconcs()
	getform()
	theK=getconstant(conc)
	buildquest()
	buildanswer()
}
function getreaction(){
	cef=new Array()
	thereaction=""
	for(var b=0;b<coef[which].length;b++){
		(Math.abs(coef[which][b])==1)?cef[b]="":cef[b]=Math.abs(coef[which][b]);
	}
	for(var l=1;l<=cmpd[which][0];l++){
		l<cmpd[which][0]?thereaction+=(cef[l-1]+cmpd[which][l]+" + "):thereaction+=(cef[l-1]+cmpd[which][l]+"=>");
	}
	for(var l=(cmpd[which][0]+1);l<cmpd[which].length;l++){
		l<(cmpd[which].length-1)?thereaction+=(cef[l-1]+cmpd[which][l]+" + "):thereaction+=(cef[l-1]+cmpd[which][l]);
	}
	return thereaction
}
function getconcs(){
	conc=new Array()
	j=1
	for(var j=0;j<coef[which].length;j++){
		conc[j]=0
		while(conc[j]<.01 || conc[j]>.5)conc[j]=number(Math.random(),3,3);
	}
}
function getconstant(dataset){
	constant=1
	for(var j=0;j<coef[which].length;j++)constant=constant*Math.pow(dataset[j],coef[which][j]);
	return constant
}
function getform(){
	gformula="Q="
	vformula=""
	for(var j=cmpd[which][0]+1;j<cmpd[which].length;j++){
		gformula+=("["+cmpd[which][j]+"]")
		vformula+=("["+conc[j-1]+"]")
		if(Math.abs(coef[which][j-1])!=1){
			gformula+=("<sup>"+Math.abs(coef[which][j-1])+"</sup>");
			vformula+=("<sup>"+Math.abs(coef[which][j-1])+"</sup>");
		}
	}
	gformula+="<B>/</B>"
	vformula+="<B>/</B>"
	for(var j=1;j<=cmpd[which][0];j++){
		gformula+=("["+cmpd[which][j]+"]")
		vformula+=("["+conc[j-1]+"]")
		if(Math.abs(coef[which][j-1])!=1){
			gformula+=("<sup>"+Math.abs(coef[which][j-1])+"</sup>");
			vformula+=("<sup>"+Math.abs(coef[which][j-1])+"</sup>");
		}
	}
}
function buildquest(){
	thequest=basequest[0]+thereaction+"<br>Equilbrium Concentrations:<br>"
	for(j=1;j<cmpd[which].length;j++)thequest+=(cmpd[which][j]+":"+conc[j-1]+"<b>M</b><br>");
	thequest+=basequest[1]
	showquest(thequest)
}
function buildanswer(){
	theanswer=ansphrase[0]+"<br>"+gformula+"<br>"+ansphrase[1]+"<br>"+vformula+"<br>"+"<br>K="+number(theK,3,2)+"<br>"
}
function answer(ans){
	attempts++
	if(tried==1)alert("You've done this one! Get a new problem.");
	else{
	if(tried==2){
		totl++
		document.forms[0].total.value=totl
	}
	if(ans>.95*theK & ans<1.05*theK){
		document.forms[0].results.value="correct"
		corrt++
		document.forms[0].correct.value=corrt
		tried=1
	}
	else{
		document.forms[0].results.value="incorrect"
		tried=2
	}
}}
