// Gas Laws - Ideal Law Script
thepage="Gas Laws - Ideal Law"
//11/20/02-updated netscape 7
//global variables
//refnumber=57
attempts=0
which=0
theanswer=0
corrt=0
totl=0
tried=0
missing=""
function parts(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10){
	this.p1=p1
	this.p2=p2
	this.p3=p3
	this.p4=p4
	this.p5=p5
	this.p6=p6
	this.p7=p7
	this.p8=p8
	this.p9=p9
	this.p10=p10
}
theR=new Array(.0821,62.4)
ideal="<br>PV=nRT"
ideallaws=new Array("=>P=nRT/V","=>V=nRT/P","=>n=PV/RT","=>T=PV/nR")
seq=new Array(2,3,1,2,3,0,0,1,3,0,1,2)
labels=new Array("P","V","n","T")
dimensions=new Array("pressure","volume","moles","temperature")
units=new Array(" atm"," L"," moles"," K"," torr"," mL"," moles","&deg;C")
data=new Array()
question=new Array()
question[0]=new parts("A gas system has ",","," and "," of ",", respectively. What is the "," in ","?")
ansphrase=new parts("First, organize the data converting the temperature and volume, if needed:<br>","Reaarange  the ideal gas equation to solve for the missing quantity<br>","<br>Substitute in the knowns and solve,using the appropriate ideal gas constant and converting the initial result to the desired dimension, as necessary:<br>")
function startit(){
	clear()
	totl++
	document.forms[0].total.value=totl
	//first calculate three values data[0]=P,data[1]=V,data[2]=n,data[3]=T
	P=number((2*Math.random()),3,3)
	V=number((10*Math.random()),3,3)
	n=number((Math.random()),3,3)
	T=number(((P*V)/(0.0821*n)),3,3)
	if(T<200 || T>1000){totl--;startit()}
	else{
	//set up arrays with v in L and mL, etc;n is only in moles
	data=new Array()
	data[0]=new Array(P,number((P*760),3,3))
	data[1]=new Array(V,number(1000*V,3,3))
	data[2]=new Array(n,n)
	data[3]=new Array(T,number((1*T-273),4,3))
	buildquest()
}}
function buildquest(){
	missing=new Array(Math.floor(4*Math.random()),Math.floor(2*Math.random()))
	toshow=new Array()
	for(var j=0;j<=3;j++){
		if(j!=missing[0]){
		choose=Math.floor(2*Math.random())
		toshow[toshow.length]=dimensions[j]
		toshow[toshow.length]=data[j][choose]
		toshow[toshow.length]=units[j+4*choose]
		data[j][data[j].length]=choose
		}
	}
	thequest=question[0].p1+toshow[0]+","+toshow[3]+" and "+toshow[6]+" of "+toshow[1]+toshow[2]+","+toshow[4]+toshow[5]+" and "+toshow[7]+toshow[8]+", respectively. "
	missing[0]==2?thequest+="How many moles of gas are present?":thequest+="What is the "+dimensions[missing[0]]+" in "+units[missing[0]+4*missing[1]]+"?";
	showquest(thequest);
	buildanswer(data,choose)
}
function buildanswer(data,choose){
	datatable=""
	lines=new Array()
	for(j=0;j<=3;j++){
		if(j==missing[0])lines[j]=labels[j]+"=?(in "+units[missing[0]+4*missing[1]]+")";
		else{			
			lines[j]=labels[j]+"="+data[j][data[j][2]]+units[j+4*data[j][2]]
			if(j==1 & data[1][2]==1)lines[1]+=("=>"+data[1][0]+"L");
			if(j==3 & data[3][2]==1)lines[3]+=("=>"+data[3][0]+"K");
		}
	}
	for(j=0;j<=3;j++)datatable+=lines[j]+"<br>";
	theanswer=""
	finish=buildeqn()
	theanswer=ansphrase.p1+datatable+ansphrase.p2+ideal+ideallaws[missing[0]]+ansphrase.p3+finish
}
function buildeqn(){
	theeqn=""
	st=3*missing[0]
	if(missing[0]==0){theeqn="("+data[2][data[2][2]]+"*"+theR[missing[1]]+"*"+data[3][0]+")/"+data[1][0]+"="+data[0][missing[1]]}
	if(missing[0]==1){theeqn="("+data[2][data[2][2]]+"*"+theR[data[0][2]]+"*"+data[3][0]+")/"+data[0][data[0][2]]+"="+data[1][0]
	if(missing[1]==1)theeqn+="L=>"+data[1][1]
	}	
	if(missing[0]==2){theeqn="("+data[0][data[0][2]]+"*"+data[1][0]+")/("+theR[data[0][2]]+"*"+data[3][0]+")="+data[2][0]}
	if(missing[0]==3){theeqn="("+data[0][data[0][2]]+"*"+data[1][0]+")/("+theR[data[0][2]]+"*"+data[2][0]+")="+data[3][0]
		if(missing[1]==1)theeqn+="K=>"+data[3][1]}
	theeqn+=units[missing[0]+4*missing[1]]
	return theeqn
}
function answer(theans){
	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(data[missing[0]][missing[1]]<0 && theans<0){
		if(theans<.95*data[missing[0]][missing[1]] & theans>1.05*data[missing[0]][missing[1]]){
			document.forms[0].results.value="Correct"
		corrt++
		document.forms[0].correct.value=corrt
		tried=1
		}
		else{
			document.forms[0].results.value="Incorrect"
			tried=2
		}
	}
	else{
	if(theans>.95*data[missing[0]][missing[1]] & theans<1.05*data[missing[0]][missing[1]]){
		document.forms[0].results.value="Correct"
		corrt++
		document.forms[0].correct.value=corrt
		tried=1
	}
	else{
		document.forms[0].results.value="Incorrect"
		tried=2
	}
	}
	}
}
