//
// JavaScript 1.1 routines for mole.htm
// by Jef Rozenski (1998)
// last modification : 10-23-2006
//
function str(istr,strlen,dec)  // format a string or value for output
{
var mystr = ""+istr;
if (str.arguments.length > 2)
  {
  var i;
  var decpt = mystr.indexOf(".");
  if ((decpt < 0) && (dec>0))
    {
    mystr += ".";
    decpt = mystr.indexOf(".");
    }
  if (dec>0)
    {
    for (i=decpt;i<(strlen-dec-1);++i){mystr = " "+mystr;}
    for (i=mystr.length;i<strlen;++i){mystr += "0";}
    mystr = mystr.substring(0,strlen);
    }
  else
    {
    for (i=mystr.length;i<strlen;++i){mystr = " "+mystr;}
    }
  }
else
  {
  mystr = (mystr+"                    ").substring(0,strlen);
  }
return mystr;
}

var itel  = new Array();
var isoto = new Array();
var elem = new Array(), m = new Array, a = new Array(); i1 = new Array(); i2 = new Array();
elem[1]="C";   m[1]=12.000000;   a[1]=12.011;     i1[1]=0.011;   i2[1]=0;
elem[2]="H";   m[2]=1.0078246;   a[2]=1.00794;    i1[2]=0.00015; i2[2]=0;
elem[3]="Br";  m[3]=78.91839;    a[3]=79.904;     i1[3]=0;       i2[3]=0.9728;
elem[4]="Cl";  m[4]=34.9688531;  a[4]=35.4527;    i1[4]=0;       i2[4]=0.32;
elem[5]="Cs";  m[5]=132.90543;   a[5]=132.9054;   i1[5]=0;       i2[5]=0;
elem[6]="F";   m[6]=18.9984022;  a[6]=18.9984;    i1[6]=0;       i2[6]=0;
elem[7]="I";   m[7]=126.90466;   a[7]=126.90447;  i1[7]=0;       i2[7]=0;
elem[8]="K";   m[8]=38.96371;    a[8]=39.0983;    i1[8]=0.00012; i2[8]=0.07217;
elem[9]="Li";  m[9]=7.016;       a[9]=6.941;      i1[9]=0.081;   i2[9]=0;
elem[10]="Mg"; m[10]=23.98504;   a[10]=24.3050;   i1[10]=0.12;   i2[10]=0.1394;
elem[11]="N";  m[11]=14.0030738; a[11]=14.00674;  i1[11]=0.0037; i2[11]=0;
elem[12]="Na"; m[12]=22.98980;   a[12]=22.989768; i1[12]=0;      i2[12]=0;
elem[13]="O";  m[13]=15.9949141; a[13]=15.9994;   i1[13]=0.0004; i2[13]=0.002;
elem[14]="P";  m[14]=30.973764;  a[14]=30.97376;  i1[14]=0;      i2[14]=0;
elem[15]="Re"; m[15]=184.9530;   a[15]=186.20487; i1[15]=-1;     i2[15]=-1;
elem[16]="S";  m[16]=31.9720727; a[16]=32.066;    i1[16]=0.0079; i2[16]=0.044;
elem[17]="Se"; m[17]=79.9165;    a[17]=78.96;     i1[17]=-1;     i2[17]=-1;
elem[18]="Si"; m[18]=27.976929;  a[18]=28.0855;   i1[18]=0.051;  i2[18]=0.034;
elem[19]="Sn"; m[19]=119.90220;  a[19]=118.71;    i1[19]=-1;     i2[19]=-1;
elem[20]="Pt"; m[20]=194.9648;   a[20]=195.08;    i1[20]=-1;     i2[20]=-1;
elem[21]="X";  m[21]=0;          a[21]=0;         i1[21]=-1;     i2[21]=-1;

var forstr,mass = new Array();
var elec = 0.000548;

function init(ma)
{
document.inp.selel.length = elem.length;
mass = eval(ma);
for (var i=1;i<elem.length;++i)
  {
  document.inp.selel.options[i].text = (elem[i]+"  ").substr(0,3)+mass[i];
  }
}

function getmass()
{
var i=document.inp.selel.selectedIndex;
if (i > 0)
  {
  mass = (document.inp.X[0].checked)?m:a;
  document.inp.amass.value = mass[i];
  }
}

function putmass()
{
var i=document.inp.selel.selectedIndex;
if (i > 0)
  {
  mass = (document.inp.X[0].checked)?m:a;
  mass[i] = document.inp.amass.value;
  document.inp.selel.options[i].text = (elem[i]+"  ").substr(0,3)+mass[i];
  }
}

function getmul(p)
{
var pend=p;
var nr = "0123456789";
while (nr.indexOf(forstr.charAt(pend))>=0)
  {
  ++pend;
  if (pend>forstr.length) {break;}
  }
var count = parseInt(forstr.substring(p,pend));
count = (count>0)?count:1;
return count;
}

function addiso(elem,n)
{
if ((i1[elem] < 0) || (isoto[0] == -1)) {isoto[0]=-1;return;}
var i;
for (i=9;i>=0;--i)
  {
  isoto[i+1] += isoto[i] * n * i1[elem];
  isoto[i+2] += isoto[i] * n * (n-1) * Math.pow(i1[elem],2) / 2;
  isoto[i+3] += isoto[i] * n * (n-1) * (n-2) * Math.pow(i1[elem],3) / 6;
  isoto[i+4] += isoto[i] * n * (n-1) * (n-2) * (n-3) * Math.pow(i1[elem],4) / 24;
  isoto[i+5] += isoto[i] * n * (n-1) * (n-2) * (n-3) * (n-4) * Math.pow(i1[elem],5) / 120;
  isoto[i+6] += isoto[i] * n * (n-1) * (n-2) * (n-3) * (n-4) * (n-5) * Math.pow(i1[elem],6) / 720;
  isoto[i+7] += isoto[i] * n * (n-1) * (n-2) * (n-3) * (n-4) * (n-5) * (n-6) * Math.pow(i1[elem],7) / 5040;
  isoto[i+8] += isoto[i] * n * (n-1) * (n-2) * (n-3) * (n-4) * (n-5) * (n-6) * (n-7) * Math.pow(i1[elem],8) / 40320;
  isoto[i+9] += isoto[i] * n * (n-1) * (n-2) * (n-3) * (n-4) * (n-5) * (n-6) * (n-7) * (n-8) * Math.pow(i1[elem],9) / 362880;
  isoto[i+2] += isoto[i] * n * i2[elem];
  isoto[i+4] += isoto[i] * n * (n-1) * Math.pow(i2[elem],2) / 2;
  isoto[i+6] += isoto[i] * n * (n-1) * (n-2) * Math.pow(i2[elem],3) / 6;
  isoto[i+8] += isoto[i] * n * (n-1) * (n-2) * (n-3) * Math.pow(i2[elem],4) / 24;
  }
}

function drawiso()
{
if (isoto[0] < 0) {return "\nCannot draw isotope distribution (isotopes for all elements are not defined)";}
var thetable = "\n<TABLE BORDER=0 CELLPADDING=3><TR VALIGN='bottom' ALIGN='center'>",i;
var maxiso = isoto[0];
for (i=1;i<10;++i)
  {
  if (isoto[i] > maxiso) {maxiso = isoto[i];}
  }
for (i=0;i<10;++i)
  {
  isoto[i] = isoto[i]/maxiso;
  thetable += "<TD WIDTH=25>";
  thetable += (Math.round(isoto[i]*150)>0)?("<IMG SRC='images/red.gif' WIDTH=10 HEIGHT="+Math.round(isoto[i]*150)+" ALT=\"\" TITLE=\""+str(isoto[i]*100,4,2)+"%\">"):"&nbsp;";
  thetable += "</TD>";
  }
thetable += "</TR>\n<TR ALIGN='center'><TH><TT>M</TT></TH>";
for (i=1;i<10;++i)
  {
  thetable += "<TH><TT>M+"+i+"</TT></TH>";
  }
thetable += "</TR></TABLE>";
return thetable;
}

function DoCalc()
{
var totmass=0,outstr="",i,j;

mass = (document.inp.X[0].checked)?m:a;
for (i=1;i<mass.length;++i) {itel[i] = 0;}
for (i=0;i<19;++i) {isoto[i] = 0;}
forstr=document.inp.forla.value;
formul=0;
isoto[0] = 1;

for (i=0;i<forstr.length;++i)
  {
  for (j=elem.length;j>0;--j)
    {
    if (forstr.substring(i,i+2) == elem[j]) {itel[j] += getmul(i+2);break;}
    if (forstr.substring(i,i+1) == elem[j]) {itel[j] += getmul(i+1);break;}
    }
  }

outstr = "<HTML><BODY BGCOLOR='#F0D000'><PRE>"+document.title.bold()+"\n\nFormula: ";

for (j=1;j<=elem.length;++j)
{
if (itel[j]>0)
  {
  totmass += itel[j]*mass[j];
  outstr += elem[j]+(itel[j]+"").sub();
  addiso(j,itel[j]);
  }
}

outstr += "<P>\n";
outstr += (document.inp.X[0].checked)?"Monoisotopic":"Average";
outstr += " mass : "+str(totmass,12,5)+"<P>\n";
outstr += (document.inp.X[0].checked)?"           [M+H]+ : "+str(totmass+m[2]-elec,12,5)+"<BR>           [M-H]- : "+str(totmass-m[2]+elec,12,5)+"<P>\n":"";
for (j=0;j<=elem.length;++j)
  {
  if (itel[j]>0)
    {
    outstr += (elem[j]+"  ").substr(0,2)+"("+str(mass[j],7,3)+") : "+str(100*itel[j]*mass[j]/totmass,10,3)+" %\n";
    } 
  }
outstr += drawiso();

outstr += "<P></PRE><FORM><INPUT TYPE='button' VALUE=' Close ' onClick='window.close()'></FORM></BODY></HTML>";

result = window.open("","result","resizable=1,menubar=1,scrollbars=1,width=550,height=350");
result.document.clear();
result.document.writeln(outstr);
result.document.close();
result.focus();
}
