// JavaScript Document
//updated 11/14/00-added complete solution
thepage="Mass, Moles, and Gram Formula Weight"
//11/20/02-updated netscape 7
//updated 9/7/98 added show answer
//globals
attempts=corrt=totl=tried=0
theanswer=missing=0
given=0
alldata=0
symbols=new Array("gfw","mass","moles")
equations=new Array()
equations[0]=symbols[0]+" = "+symbols[1]+" &#247; "+symbols[2]
equations[1]=symbols[1]+" = "+symbols[2]+" &#215; "+symbols[0]
equations[2]=symbols[2]+" = "+symbols[1]+" &#247; "+symbols[0]
ansphrase=new Array("To calculate the gfw, you divide the mass by the number of moles<br>","To calculate the mass, you take the product of the number of moles and thegfw<br>","To calculate the gfw, you divide the mass by the number of gfw<br>")
//This function clears all values in both tables on loading the page
//This function clears the first table
function clear(){
	document.forms[0].first.value=""
	document.forms[0].second.value=""
	document.forms[0].third.value=""
	attempts=0
	tried=0
	clearansspot()
}

//This is the function started when "New Problem" is pushed	
//This function first clears all old values using clear()
//The three variables(mass,moles and gfw) are then determined, using Math.random() to 
//generate gfw and mass and a ratio to generate moles. 
function startit(){
	clear()
	totl++
	document.forms[0].total.value=totl
	alldata=new Array()
	for(var j=0;j<3;j++)alldata[j]=0;	
	while(alldata[0]<24)alldata[0]=400*Math.random();
	while(alldata[1]<.01*alldata[0])alldata[1]=400*Math.random();
	alldata[2]=alldata[1]/alldata[0]
//The three variables are then converted to three significant figures.
	for(var j=0;j<3;j++)alldata[j]=number(alldata[j],3,3)
	setproblem()
}
//This function partially fills the table, setting the problem
function setproblem(){
	missing=Math.floor(3*Math.random())
	for(var j=0;j<3;j++){
		if(j!=missing)document.forms[0].elements[j].value=alldata[j];
		}
	document.forms[0].elements[missing].select()
	document.forms[0].elements[missing].focus()
	buildanswer()
}
function buildanswer(){
	getsolved()
	theanswer=ansphrase[missing]+equations[missing]+"<br>"+solved[missing]
}
function getsolved(){
	solved=new Array()
	solved[0]=alldata[1]+" g &#247; "+alldata[2]+" moles = "+alldata[0]+"<b> g/mol</b>"
	solved[1]=alldata[2]+" moles &#215; "+alldata[0]+" g/mol = "+alldata[1]+"<b> g</b>"
	solved[2]=alldata[1]+" g &#247; "+alldata[0]+" g/mol = "+alldata[2]+"<b> moles</b>"
}
// This is the "Check Answer Function
//This function checks for correctness by simply seeing if a/b is within 1%
//of c. The results are then posted to the second table. If you change the
//relationship between a and b, above, make sure you change this also.
function answer(form){
	if(tried==1)alert("You've done this one. Get a new problem!")
	else{
	if(tried==2){totl++;document.forms[0].total.value=totl}
	attempts++
	if (form.elements[missing].value<1.05*alldata[missing] & form.elements[missing].value>.95*alldata[missing]){	
		document.forms[0].results.value="correct"
		corrt++
		document.forms[0].correct.value=corrt
		tried=1
	}
	else {
		document.forms[0].results.value="incorrect"
		tried=2
		document.forms[0].elements[missing].select()
		document.forms[0].elements[missing].focus()
	}
	}
}