/*
mwcalc.js - v1.0 (c) holsoft 2002
molecular weight calculator
belongs to mwcalc1.htm-page
*/

// 2-letter elements:
//atom numbers:
AtNr2 = "2, 3, 4, 10,11,12,13,14,17,18,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,48,49,50,51,52,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,93,94,95,96,97,98,99,100";
//elements:
Elem2 = "He,Li,Be,Ne,Na,Mg,Al,Si,Cl,Ar,Ca,Sc,Ti,Cr,Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr,Rb,Sr,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn,Sb,Te,Xe,Cs,Ba,La,Ce,Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu,Hf,Ta,Re,Os,Ir,Pt,Au,Hg,Tl,Pb,Bi,Po,At,Rn,Fr,Ra,Ac,Th,Pa,Np,Pu,Am,Cm,Bk,Cf,Es,Fm";
//atom mass:
AtMs2 = "4, 6.9, 9, 20.2, 23, 24.3, 27, 28.1, 35.5, 39.9, 40.1, 45, 47.9, 52, 54.9, 55.8, 58.9, 58.7, 63.5, 65.4, 69.7, 72.6, 74.9, 79, 79.9, 83.8, 85.5, 87.6, 91.2, 92.9, 95.9, 97, 101.1, 102.9, 106.4, 107.9, 112.4, 114.8, 118.7, 121.8, 127.6, 131.3, 132.9, 137.3, 138.9, 140.1, 140.9, 144.2, 145, 150.4, 152, 157.3, 158.9, 162.5, 164.9, 167.3, 168.9, 173, 175, 178.5, 180.9, 186.2, 190.2, 192.2, 195.1, 197, 200.6, 204.4, 207.2, 209, 209, 210, 222, 223, 226, 227, 232, 231, 237, 244, 243, 247, 247, 251, 252, 257";
AtNr2Arr = AtNr2.split(","); 
Elem2Arr = Elem2.split(","); 
AtMs2Arr = AtMs2.split(",");
Siz2 = AtNr2Arr.length; 

// 1-letter elements:
//atom numbers:
AtNr1 = "1,5,6,7,8,9,15,16,19,23,39,53,74,92";
//elements:
Elem1 = "H,B,C,N,O,F,P,S,K,V,Y,I,W,U";
//atom mass:
AtMs1 = "1, 10.8, 12, 14, 16, 19, 31, 32.1, 39.1, 50.9, 88.9, 126.9, 183.9, 238";
AtNr1Arr = AtNr1.split(","); 
Elem1Arr = Elem1.split(","); 
AtMs1Arr = AtMs1.split(","); 
Siz1 = AtNr1Arr.length; 

function getMolw() {
	lin = document.form1.molec.value;
	var dig;
	wght = 0;
	RE = new RegExp ("^[0-9]*"); //find digits at front end of a string
	found = -1;
	stri = "";

	if (lin.length>0) { Go = true; } 
	else { Go = false; }
	
	while (Go) {
	
	if (lin.length>1) {
	
	sublin = lin.substring(0,2); // first 2 characters
	for (k=0;k<Siz2;k++) {
		if (sublin==Elem2Arr[k]) {
			found = k;
			lin = lin.substring(2); // remove first 2 characters
			stri += sublin+" ";
			document.form1.weight.value = stri;
			//find digits if present:	
			dig = RE.exec(lin);
			if (dig[0]=="") { dig[0]=1; }
			wght += dig[0]*AtMs2Arr[k];
			lin = RegExp.rightContext;
			stri += "("+dig[0]+") ";
			document.form1.weight.value = stri;
		}
	}
	} // if lin.length>1
	
	//if no 2-letter element is found, try 1-letter elements:
	if (found<0) {
		sublin = lin.substring(0,1); // 1st character
		for (k=0;k<Siz1;k++) {
			if (sublin==Elem1Arr[k]) {
				found = k;
				lin = lin.substring(1); // remove 1st character
				stri += sublin+" ";
				document.form1.weight.value = stri;
				//find digits if present:	
				dig = RE.exec(lin);
				if (dig[0]=="") { dig[0]=1; }
				wght += dig[0]*AtMs1Arr[found];
				lin = RegExp.rightContext;
				stri += "("+dig[0]+") ";
				document.form1.weight.value = stri;
			}	
		}
	}

	if (found<0) {
		window.alert("Formula is not recognized...");
		stri = "";
		wght = 0;
		Go = false;
		}
	else {
		//reset found:
		found = -1;
		if (lin=="") { Go = false; }
	} 
		
	} // end WHILE

//	document.form1.weight.value = stri;
	document.form1.weight.value = " "+Math.floor(wght*100+0.5)/100;
}
