thepage="Using Heats of Formation(69)"
choice=0
thecmpds=0
reverse=0
theheat=0
function startit(){
	clear()
	totl++
	document.forms[0].total.value=totl
	choice=Math.floor(Math.random()*reactions.length)
	builddatafile()
}
function builddatafile(){
	thecmpds=new Array()
	for(j=1;j<reactions[choice].length;j=j+2){
				thecmpds[thecmpds.length]=new Array(reactions[choice][j+1],getformula(dataset[reactions[choice][j]][0]),dataset[reactions[choice][j]][2],thephases[dataset[reactions[choice][j]][1]])			
	}
	buildtable()
	buildeqn()
}
function buildtable(){
	thetable="<table border=2><tr><td>Compound</td><td>&Delta;H<sub>f</sub>(KJ)</td></tr>"
	for(j=0;j<thecmpds.length;j++){
		thetable+="<tr><td>"+thecmpds[j][1]+"</td><td>"+thecmpds[j][2]+"</td></tr>"
	}
	thetable+="</table>"
}
function buildeqn(){
	theequation=""
	reverse=Math.floor(2*Math.random())
	if(reverse==0){
	for(j=0;j<thecmpds.length;j++){
		thecmpds[j][0]>1?theequation+=(thecmpds[j][0]+thecmpds[j][1]+thecmpds[j][3]):theequation+=(thecmpds[j][1]+thecmpds[j][3]);
		if(j!=(reactions[choice][0]-1) && j!=(thecmpds.length-1)){
			theequation+=" + "
			}
		else{
			if(j==reactions[choice][0]-1)theequation+=" => "
			}		
		}
	}
	else{
	for(k=(thecmpds.length-1);k>-1;k--){
		thecmpds[k][0]>1?theequation+=(thecmpds[k][0]+thecmpds[k][1]+thecmpds[k][3]):theequation+=(thecmpds[k][1]+thecmpds[k][3]);
		if(k!=reactions[choice][0] && k!=0){
			theequation+=" + "
		}
		else{
			if(k==reactions[choice][0])theequation+=" => "
		}		
	}}
	thequest=theequation+thetable
	showquest(thequest)
	buildanswer()
}
function buildanswer(){
	line1="&Delta;H=("
	line2="&Delta;H=("
	theheat=0
	line0="Reaction Enthalpies can be determined using known heats of formation:<br>&Delta;H=&Sigma;&Delta;H<sub>f</sub>(products)-&Sigma;&Delta;H<sub>f</sub>(reagents)<br>For the reaction shown above:<br>"
	if(reverse==0){
	for(j=reactions[choice][0];j<thecmpds.length;j++){
		line1+=(thecmpds[j][0]+"*&Delta;H<sub>f</sub>("+thecmpds[j][1]+")")
		line2+=(thecmpds[j][0]+"*"+thecmpds[j][2])
		theheat=theheat+(thecmpds[j][0]*thecmpds[j][2])
		if(j<(thecmpds.length-1)){line1+="+";line2+="+"}
	}
	line1+=")-("
	line2+=")-("
	for(j=0;j<reactions[choice][0];j++){
		line1+=(thecmpds[j][0]+"*&Delta;H<sub>f</sub>("+thecmpds[j][1]+")")
		line2+=(thecmpds[j][0]+"*"+thecmpds[j][2])
		theheat=theheat-(thecmpds[j][0]*thecmpds[j][2])
		if(j<(reactions[choice][0]-1)){line1+="+";line2+="+"}
		}
	}
	else{
	for(j=0;j<reactions[choice][0];j++){
		line1+=(thecmpds[j][0]+"*&Delta;H<sub>f</sub>("+thecmpds[j][1]+")")
		line2+=(thecmpds[j][0]+"*"+thecmpds[j][2])
		theheat=theheat+(thecmpds[j][0]*thecmpds[j][2])
		if(j<(reactions[choice][0]-1)){line1+="+";line2+="+"}
		}
	line1+=")-("
	line2+=")-("
	for(j=reactions[choice][0];j<thecmpds.length;j++){
		line1+=(thecmpds[j][0]+"*&Delta;H<sub>f</sub>("+thecmpds[j][1]+")")
		line2+=(thecmpds[j][0]+"*"+thecmpds[j][2])
		theheat=theheat-(thecmpds[j][0]*thecmpds[j][2])
		if(j<(thecmpds.length-1)){line1+="+";line2+="+"}
		}
	}
	theheat=number(theheat,3,3)
	//document.forms[0].theans.value=theheat
	theanswer=line0+"<br>"+line1+")<br><br>"+line2+")="+theheat+"KJ"
	//showanswer()
}	
function answer(ans){
	if(tried==1)alert("You've already done this one! Get a new problem!")
	else{
	gotit=0
	if(tried==2){totl++
		document.forms[0].total.value=totl
	}
	if(theheat<0){
		if(ans>1.05*theheat && ans<.95*theheat)gotit=1;
	}
	else{
		if(ans<1.05*theheat && ans>.95*theheat)gotit=1;
	}
	if(gotit==1){
		document.forms[0].results.value="Correct"
		corrt++
		document.forms[0].correct.value=corrt
		tried=1
	}
	else {
		tried=2
		document.forms[0].results.value="incorrect"
	}
}}
