// JavaScript Document
//updated 11/14/00-added complete solution
thepage="Density-WP(9)"
//11/18/02-updated complete solution
refnumber=7
attempts=0
tried=0
thequest=""
theanswer=""
first=""
second=""
third=""
wq=0
densgml=0
massg=0
volcc=0
totl=0
theansr=0
convertv=new Array()
needed=new Array()
needed[3]="mass"
needed[2]="volume"
needed[1]="density"
function parts(p1,p2,p3,p4,p5,p6,p7){
      this.p1=p1
      this.p2=p2
      this.p3=p3
      this.p4=p4
	this.p5=p5
	this.p6=p6
	this.p7=p7
}
quest=new Array();
quest[1]=new parts("What is the density of a substance in ",", if a sample with a volume of "," has a mass of ","?")
quest[2]=new parts("What volume in "," would be occupied by "," of a material with a density of ","?")
quest[3]=new parts("What is the mass in "," of a "," sample, if it has a density of "," ?")
formula=new Array(5)
formula[1]="d=m/V"
formula[2]="V=m/d"
formula[3]="m=d*V"
ansphrase=new parts("(I)d=m/V<br>Organize your data, identifying the givens and the unknown, converting the values, if necessary:<br><b>mass</b>=","<br><b>volume</b>=","<br><b>density</b>=","<br>Since you need to calculate<b> ","</b>, arrange equation(I):<br> ","<br>Substitute in the values and perform the calculation:")
function startit(){
	convertv=new Array(0,0,0,0)
	answered=0
	clear()
	totl++
	document.forms[0].total.value=totl
//set up initial values in g,ml,g/ml
	wq=Math.ceil(3*Math.random())
	massg=densgml=99
	while(densgml<0.5 || densgml>15)densgml=number(Math.random()*20,3,3);
	while(massg<0.5 || massg>50)massg=number(Math.random()*20,3,3);
	volcc=number(massg/densgml,3,3)
//choose mass unit
	masspick=Math.random()
	if(masspick<.33){
	mass=massg
	massunit="grams"}
else{
	if(masspick<.67){
	convertv[1]=1
	mass=number(massg/1000,3,3)
	massunit="kg"}
	else{convertv[1]=2
		mass=number(massg*1000,3,3)
		massunit="mg"}}
massfunit="unit"+massunit
//choose volume unit
	volpick=Math.random()
	if(volpick<.33){
	vol=volcc 
	volunit="cm<sup>3</sup>"}
	else{
		if(volpick<.67){
		vol=volcc
		volunit="mL"}
		else{ convertv[2]=1
			vol=Math.round(100*volcc)/100000
			volunit="liters"}}
//calculate new density
	denspick=Math.random()
	g=Math.random()
	if(denspick<.25){
		density=densgml
		if(g<.5){
			densityunit="g/mL"}
		else{
			convertv[3]=1
			densityunit="kg/L"
	}}
	else{
		if(denspick<.5){
		density=number(1000*densgml,3,3)
		if(g<.5){
			convertv[3]=2
			densityunit="g/L"}
		else{
			densityunit="mg/mL"
			convertv[3]=3}
		}
			else{
				if(denspick<.75){
					density=number(densgml/1000,3,3)
					convertv[3]=4
				densityunit="kg/mL"
				}
				else{convertv[3]=5
					density=number(1e6*densgml,3,3)
					densityunit="mg/L"}
	}}
	if(Math.random()>.33 & volunit=="liters"){
		vol=number(1000*vol,3,3)
		mass=number(1000*mass,3,3)
		massg=number(1000*massg,3,3)
		volcc=Math.round(1000*volcc)
	}
	volf=vol+volunit
	massf=mass+massunit
	densityf=density+densityunit
	if (wq==1){first=densityunit
		second=volf
		third=massf
        theansr=density}
	if (wq==2){first=volunit
		second=massf,
		third=densityf
        theansr=vol}
	if (wq==3){first=massunit
		second=volf
		third=densityf
        theansr=mass}
buildquest()
buildanswer()
}
function buildquest(){
thequest=quest[wq].p1+first+quest[wq].p2+second+quest[wq].p3+third+quest[wq].p4
showquest(thequest)
}
function buildanswer(){
	choices=new Array(5)
	if(convertv[1]==0)choices[1]=massg+"grams";
	else choices[1]=massf+"=>"+massg+"grams"
	if(convertv[2]==0)choices[2]=volcc +"cm<sup>3</sup>";
	else choices[2]=volf+"=>"+volcc +"cm<sup>3</sup>";
	if(convertv[3]==0)choices[3]=densgml+"g/cm<sup>3</sup>";
	else choices[3]=densityf+"=>"+densgml+"g/cm<sup>3</sup>";
	if(wq==1){
		thefinal=massg+"g/"+volcc+"cm<sup>3</sup>="+densgml+"g/cm<sup>3</sup>";
		if(denspick>=.25)thefinal+=("=>"+density+densityunit);//correction
		else if(g>=.5)thefinal+=("=>"+density+densityunit);
		choices[3]="?"
		}
	if(wq==2){
		thefinal=massg+"g/"+densgml+"g/cm<sup>3</sup>="+volcc+"cm<sup>3</sup>";
		if(volpick>=.33)thefinal+=("=>"+vol+volunit);//correction
		choices[2]="?"
		}
	if(wq==3){
		thefinal=densgml+"g/cm<sup>3</sup>*"+volcc+"cm<sup>3</sup>="+massg+"g"
		if(masspick>=.33)thefinal+=("=>"+mass+massunit);//correction
		choices[1]="?"
		}
	theanswer=ansphrase.p1+choices[1]+ansphrase.p2+choices[2]+ansphrase.p3+choices[3]+ansphrase.p4+needed[wq]+ansphrase.p5+formula[wq]+ansphrase.p6+thefinal
}
	
function answer(){
	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 (1.05*theansr>document.forms[0].theans.value &&
        .95*theansr<document.forms[0].theans.value){
        	document.forms[0].results.value="correct"
		corrt++
        	document.forms[0].correct.value=corrt
		tried=1	
        }
	else{document.forms[0].results.value="incorrect"
		tried=2}
	}
}
