// Gas Laws - General Binary Gas Laws Script
thepage="Gas Laws - General Binary Gas Laws"
//updated 11/18/02-netscape 7
//global variables
thefactor=0
theanswer=theanswr=""
changeref=0
dimkey=anskey=0
attempts=0
tried=0
anrs=0
corrt=0
totl=0
first=second=third=fourth=0
alldata=howlinked=0
varlabels=new Array("P","V","n","T","P","V","n","T")
varwords=new Array(" pressure "," volume "," number of moles "," temperature "," pressure "," volume "," number of moles "," temperature ")
datalabels=new Array("atm","L","moles","K","torr","mL","moles","<sup>o</sup>C")
relatekey=new Array(-1,-1,1,1)
changeterm=new Array(" increased "," decreased "," should increase "," should decrease ") 
function startit(){
	clear()
	getdata()
	setproblem()
}
function getdata(){
	alldata=new Array(0,0,0,0)
	while (alldata[0]<.1)alldata[0]=(Math.random()*2)
	alldata[4]=alldata[0]*760
	while(alldata[1]<.05)alldata[1]=(Math.random()*3)
	alldata[5]=1000*alldata[1]
	while (alldata[2]<.005)alldata[2]=(Math.random()*.05)
	alldata[6]=alldata[2]
	alldata[3]=(alldata[0]*alldata[1]/(0.082*alldata[2]))
	alldata[7]=alldata[3]-273
	for(g=0;g<alldata.length;g++)alldata[g]=number(alldata[g],3,3)
	first=Math.floor(4*Math.random())
	second=first
	while(first==second)second=Math.floor(4*Math.random())
	for(k=0;k<4;k++){
		if(k!=first && k!=second )third=k
		if(k!=first && k!=second && k!=third)fourth=k
	}
	for(k=0;k<4;k++){
		if(k!=first && k!=second && k!=third)fourth=k
	}
	alldata[8]=0
	thefactor=0
	while(thefactor<.1)thefactor=4*Math.random()
	alldata[8]=(thefactor*alldata[first])
	alldata[8]=number(alldata[8],3,3)
	howlinked=relatekey[first]*relatekey[second]
	howlinked>0?alldata[9]=alldata[first]*alldata[second]/alldata[8]:alldata[9]=alldata[second]*alldata[8]/alldata[first];
	alldata[9]=number(alldata[9],3,3)
	datalabels[8]=datalabels[first]
	datalabels[9]=datalabels[second]
	if(first==0){alldata[10]=number(alldata[8]*760,3,3);datalabels[10]=datalabels[4]}
	if(first==1){alldata[10]=number(alldata[8]*1000,3,3);datalabels[10]=datalabels[5]}
	if(first==2){alldata[10]=alldata[8];datalabels[10]=datalabels[6]}
	if(first==3){alldata[10]=number(alldata[8]-273,3,3);datalabels[10]=datalabels[7]}
	if(second==0){alldata[11]=number(alldata[9]*760,3,3);datalabels[11]=datalabels[4]}
	if(second==1){alldata[11]=number(alldata[9]*1000,3,3);datalabels[11]=datalabels[5]}
	if(second==2){alldata[11]=alldata[9];datalabels[11]=datalabels[6]}
	if(second==3){alldata[11]=number(alldata[9]-273,3,3);datalabels[11]=datalabels[7]}
	changeref=new Array()
	thefactor<1?changeref[0]=1:changeref[0]=0;
	howlinked<0?changeref[1]=(changeref[0]+2):changeref[1]=(3-changeref[0]);
	//alert(alldata)
}
function setproblem(){
	dimkey=new Array(0,0,0,0)
	anskey=new Array()
	if(Math.random()>.5){first=first+4;dimkey[0]=1}
	if(Math.random()>.5){second=second+4;dimkey[1]=1}
	anskey[0]=first
	anskey[1]=second
	fifth=8
	sixth=9
	swapthem=Math.floor(2*Math.random())
	if(Math.random()>.5){fifth=fifth+2;dimkey[2]=1}
	if(Math.random()>.5){sixth=sixth+2;dimkey[3]=1}
	swapthem==0?theanswr=alldata[sixth]:theanswr=alldata[fifth]
	anskey[2]=fifth
	anskey[3]=sixth
	thequest="A system had initial values of "+varlabels[first]+" and "+varlabels[second]+" of "+alldata[first]+datalabels[first]+" and "+alldata[second]+datalabels[second]+",respectively. If the "
	swapthem==0?thequest+=(varwords[first]+" is changed to "+alldata[fifth]+datalabels[fifth]):thequest+=(varwords[second]+" is changed to "+alldata[sixth]+datalabels[sixth]);
	thequest+=" under conditions of constant "+varlabels[third]+" and "+varlabels[fourth]+", what is the resultant "
	swapthem==0?thequest+=varwords[second]:thequest+=varwords[first];
	if(swapthem==0){
		if(datalabels[second]!=" number of moles ")thequest+=" in "+datalabels[second];
	}
	else {
		if(datalabels[first]!=" number of moles ")thequest+=" in "+datalabels[first];
	}
	thequest+="?"
	buildanswer(dimkey)	
	showquest(thequest)
}
function buildanswer(){
	theanswer="The relationship between "+varlabels[first]+" and "+varlabels[second]+" is "
	howlinked<0?theanswer+="direct.<br>Since the":theanswer+="inverse. <br>Since the"
	swapthem==0?theanswer+=varwords[first]:theanswer+=varwords[second];
	if(howlinked<0){
		theanswer+=(" "+changeterm[changeref[0]]);
		swapthem==0?theanswer+=(", the "+varwords[second]):theanswer+=(", the "+varwords[first]);
	}
	else{
		if(swapthem==1){
			changeref[0]=0?changeref[0]=1:changeref[0]=0;
			changeref[1]=(3-changeref[0])
		}
		theanswer+=(" "+changeterm[changeref[0]]);
		swapthem==0?theanswer+=(", the "+varwords[second]):theanswer+=(", the "+varwords[first]);
	}
	//alert(changeref)
	theanswer+=("  "+changeterm[changeref[1]])	
	convert=getconvert()
	eqnset=geteqns()
	if(clist.length>5)theanswer+="<br>The following conversion(s) are needed:<br>"
	theanswer+="<br>"+convert+eqnset
	//showanswer()
}
function getconvert(){
	clist=""
	if(dimkey[0]!=dimkey[2]){
		if(first!=2 && first!=3 && first!=6 && first!=7){
			dimkey[0]==0?clist+=(alldata[first]+datalabels[first]+"=>"+alldata[first+4]+datalabels[first+4]):clist+=(alldata[first]+datalabels[first]+"=>"+alldata[first-4]+datalabels[first-4]);
			dimkey[0]==0?anskey[0]=first+4:anskey[0]=first-4
			clist+="<br>"
		}
	}		
	if(dimkey[1]!=dimkey[3]){
		if(second!=2 && first!=3 && first!=6 && first!=7){
			dimkey[1]==0?clist+=(alldata[second]+datalabels[second]+"=>"+alldata[second+4]+datalabels[second+4]):(alldata[second]+datalabels[second]+"=>"+alldata[second-4]+datalabels[second-4]);
			dimkey[1]==0?anskey[1]=second:anskey[1]=second-4;
			clist+="<br>"
		}
	}
	if(first==7){clist+=alldata[7]+"<sup>o</sup>C=>"+alldata[3]+"K<br>";anskey[0]=3}
	if(second==7){clist+=alldata[7]+"<sup>o</sup>C=>"+alldata[3]+"K<br>";anskey[1]=3}
	if((first==3 || first==7) & dimkey[2]==1){clist+=alldata[10]+"<sup>o</sup>C=>"+alldata[8]+"K<br>";anskey[2]=anskey[2]-2}
	if((second==3 || second==7) & dimkey[3]==1){clist+=alldata[11]+"<sup>o</sup>C=>"+alldata[9]+"K<br>";anskey[3]=anskey[3]-2}
	return clist
}
function geteqns(){
	swapthem==0?vareqn="<br>("+varlabels[second]+"<sub>1</sub>":vareqn="<br>("+varlabels[first]+"<sub>1</sub>"
	howlinked<0?vareqn+="/":vareqn+="*";
	swapthem==0?vareqn+=(varlabels[first]+"<sub>1</sub>)"):vareqn+=(varlabels[second]+"<sub>1</sub>)");
	howlinked<0?vareqn+="*":vareqn+="/";
	swapthem==0?vareqn+=(varlabels[first]+"<sub>2</sub>="+varlabels[second]+"<sub>2</sub>"):vareqn+=(varlabels[second]+"<sub>2</sub>="+varlabels[first]+"<sub>2</sub>");
	swapthem==0?thesolved="<br>("+alldata[anskey[1]]:thesolved="<br>("+alldata[anskey[0]]
	howlinked<0?thesolved+="/":thesolved+="*";
	swapthem==0?thesolved+=alldata[anskey[0]]+")":thesolved+=alldata[anskey[1]]+")";
	howlinked<0?thesolved+="*":thesolved+="/";
	swapthem==0?thesolved+=(alldata[anskey[2]]+"="+alldata[anskey[3]]):thesolved+=(alldata[anskey[3]]+"="+alldata[anskey[2]]);
	eqnset=vareqn+thesolved
	return eqnset
}
function answer(ans){
	if(tried==1)alert("You've done this one. Get a new problem.");
	else{
	attempts++
	if(tried==2){totl++
		document.forms[0].total.value=totl
		}
	if(ans>.95*theanswr & ans<1.05*theanswr ){ 
    		document.forms[0].results.value="correct"
    		corrt++
		document.forms[0].correct.value=corrt
		tried=1
	}
	else {document.forms[0].results.value="incorrect"
	tried=2}
}}
