// Acids & Bases - pH of Solutions of Weak Acids Script
thepage="pH of Solutions of Weak Acids"
//updated 11/18/02-netscape 7
//global variables
//refnumber=114
theanswer=""
attempts=0
phans=0
corrt=0
totl=0
which=0
function parts(p1,p2,p3,p4,p5){
	this.p1=p1
	this.p2=p2
	this.p3=p3
	this.p4=p4
	this.p5=p5
}
function data(formula,name,K){
	this.formula=formula
	this.name=name
	this.K=K
}
checkstring="z0123456789"
hsym="[H<sup>+</sup>]"
avalue="(C<sub>i</sub> &#215; K<sub>a</sub>)<sup>1/2</sup> = "+hsym
acids=new Array()
acids[0]=new data("CH3COOH","Acetic",1.8e-5)
acids[1]=new data("C6H5COOH","Benzoic",6.5e-5)
acids[2]=new data("HClO","Hypochlorous",3.5e-8)
acids[3]=new data("CH2ClCOOH","Chloroacetic",1.4e-3)
acids[4]=new data("HCOOH","Formic",1.8e-4)
acids[5]=new data("HF","Hydrofluoric",3.5e-4)
acids[6]=new data("HNO2","Nitrous",4.5e-4)
acids[7]=new data("HClO2","Chlorous",1e-2)
acids[8]=new data("CH3CH(OH)COOH","Lactic",1.4e-4)
acids[9]=new data("HCN","Hydrocyanic",4.9e-10)
acids[10]=new data("HO2C2OOH","Oxalic",5.4e-2)
acids[11]=new data("H2SO3","Sulfurous",1.3e-2)
acids[12]=new data("H3PO4","Phosphoric",7.1e-3)
acids[13]=new data("H2CO3","Carbonic",4.9e-7)
acids[14]=new data("H2S","Hydrosulfuric",1.1e-7)
acids[15]=new data("H3BO3","Boric",5.8e-10)
function constants(){
var cont=window.open("","cont","scrollbars=yes,width=300,height=300")
cont.document.writeln ("<html>");
cont.document.writeln ("<head><title>Ionization Constants</title>");
cont.document.writeln ("</head>");
cont.document.writeln ("<body bgcolor = FFFFFF><b>Acids</b><table>");
for(k=0;k<=15;k++){
theK=acids[k].K
theK=number(theK,2,2)
cont.document.writeln("<tr><td>",acids[k]['name'],"</td><td>");
cont.document.writeln("<td>",showformula(k),"</td><td>");
cont.document.writeln("<td>",theK,"</td></tr>")};
cont.document.writeln("</table><form><input type=button value='Close Window' onClick='window.close()'></form>");
cont.document.writeln("<html>");
cont.document.close();
cont.focus();
}

ansphrase=new Array()
compare=new Array()
compare[0]=new Array(" <B>less</B> "," <B>valid</B>")
compare[1]=new Array(" <B>greater</B> "," <B>invalid</B> ")
ansphrase[1]=new parts(" acid has a K<sub>a</sub> of ",". First, calculate the value for the weak acid approximation.<br>"," Since this is "," than 5% of the inital concentration ",",  the approximate value is ",".")
insphrase=new Array(" and the answer is pH = "," and you must calculate the 'exact' answer using either successive approximations or the quadratic. This yields [H<sup>+</sup>] = ")
pathway=new Array()
pathway[0]=new Array(" <B>less than</B> "," <B>valid</B> ")
pathway[1]=new Array(" <B>greater than</B> "," <B>invalid</B> ") 
function startit(){
	clear()
	totl++
	document.forms[0].results.value=document.forms[0].theans.value=""
	document.forms[0].total.value=totl
	which=Math.floor(acids.length*Math.random())
	getconc()
}
function getconc(){
	first=0
	while(first<11)first=Math.ceil(100*Math.random());
	conc="0."+zero()+first
	getanswer(which,conc)
	buildquest(which,conc)
}
function zero(){
	added=""
	for(j=1;j<=Math.floor(3*Math.random());j++)added+="0";
	return added
}
function getanswer(which,conc){
	approx=Math.sqrt(conc*acids[which].K)
	check=.05*conc
	approx=number(approx,3,3)
	check=number(check,3,3)
	if(approx>check && Math.random()<.75){getconc()}
	else{
	if(approx<check){ans=approx;vkey=0;exans=approx}
	else{exans=(-acids[which].K+Math.sqrt(acids[which].K*acids[which].K+4*acids[which].K*conc))/2;vkey=1}
	phans=number((-Math.log(exans)/2.302),3,3)	
	buildanswer(which,conc,vkey,phans,exans,approx,check)
}}

function buildanswer(which,conc,vkey,phans,exans,approx,check){
	theanswer=""
	theanswer=acids[which].name+ansphrase[1].p1+number(acids[which].K,3,2)+ansphrase[1].p2+avalue+"<br>("+conc+" &#215; "+number(acids[which].K,3,2)+")<sup>1/2</sup> = "+number(approx,3,2)
	theanswer+=("<br>"+ansphrase[1].p3+compare[vkey][0]+ansphrase[1].p4+"("+number(check,3,2)+")"+ansphrase[1].p5+compare[vkey][1]+insphrase[vkey])
	if(vkey==0)theanswer+=phans;
	else theanswer=theanswer+number(exans,3,3)+" and pH = "+phans;
}
function buildquest(){
	thequest="What is the pH of a "+conc+"M solution of "
	Math.random()>.5?thequest+=(acids[which].name+" acid?"):thequest+=(getformula()+"?");
	showquest(thequest)
}
function answer(ans){
	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(ans>.95*phans & ans<1.05*phans){
		document.forms[0].results.value="Correct"
		corrt++
		document.forms[0].correct.value=corrt
		tried=1
	}
	else{
		document.forms[0].results.value="Incorrect"
		tried=2
	}}
}
function showformula(k){
	theformula=""
	teststring=acids[k].formula
	for(j=0;j<teststring.length;j++){
		checkstring.indexOf(teststring.charAt(j))>0?theformula+="<sub>"+teststring.charAt(j)+"</sub>":theformula+=teststring.charAt(j);
	}
	return theformula
}

function getformula(){
	theformula=""
	teststring=acids[which].formula
	for(j=0;j<teststring.length;j++){
		checkstring.indexOf(teststring.charAt(j))>0?theformula+="<sub>"+teststring.charAt(j)+"</sub>":theformula+=teststring.charAt(j);
	}
	return theformula
}
