//
// JavaScript 1.1 routines for oligoii.htm
// by Jef Rozenski (1998)
// jef.rozenski@rna.pharm.utah.edu
// last modification : 12/02/1998
//
window.defaultStatus = '';
var maA="average mass",maM="monoisotopic mass";
var sugarA = 99.109,sugarM = 99.0446;
var bases="CUTAG",recno;
var ph=0;
var ph0="no terminal phosphates", ph1="1 phosphate, 1 -OH", ph2="1 cyclic phosphate, 1 -OH";
var ph3="2 terminal phosphates", ph4="1 phosphate, 1 cyclic";
var mass=0,massX,massB,sugar;
var ma="",dr,totnuc;

function posB(ch) {return bases.indexOf(ch);}  // shortcut to keep data amount small

var mAX = new Array(7);  // additional average masses
      mAX[1] = 1.00794;  // hydrogen
      mAX[2] = 18.015;   // H2O
      mAX[3] = 79.980;   // HPO3
      mAX[4] = 80.09;    // furan
      mAX[5] = 15.999;   // oxygen
      mAX[6] = 22.9898;  // sodium
      mAX[7] = 39.0983;  // potassium

var mMX = new Array(7);  // additional monoisotopic masses
      mMX[1] = 1.00782;
      mMX[2] = 18.010;
      mMX[3] = 79.966;
      mMX[4] = 80.0262;
      mMX[5] = 15.995;
      mMX[6] = 22.9898;
      mMX[7] = 38.9637;

var mAB = new Array(bases.length);  // average masses for bases
      mAB[posB("C")]=110.096;mAB[posB("U")] = 111.081;mAB[posB("T")]=125.108;
      mAB[posB("A")]=134.121;mAB[posB("G")] = 150.121;

var mMB = new Array(bases.length);  // monoisotopic masses for bases
      mMB[posB("C")]=110.0354;mMB[posB("U")]=111.0194;mMB[posB("T")]=125.0351;
      mMB[posB("A")]=134.0467;mMB[posB("G")]=150.0416;

var massX = new Array(),massS = new Array(),massB = new Array();

function SetStatus(nr,nuc)
{
if (SetStatus.arguments.length<2) {nuc="    "};
var msg = new Array(7);
  msg[1] = "select calculation";
  msg[2] = "the average mass is the weighed average of all atom isotopes in the molecule";
  msg[3] = "the monoisotopic mass is the summ of the masses of the most abundant isotopes in the molecule";
  msg[4] = "here you can specify an offset mass (e.g. in case of a modified terminal)";
  msg[5] = "choose the oligonucleotide type";
  msg[6] = "select the terminal ends";
  msg[7] = "enter here the amount of "+nuc+"'s to be calculated";
window.status = msg[nr];
}

function HelpMe()  // display help
{
result = window.open("mongohlp.htm","","resizable=1,menubar=1,scrollbars=1,width=600,height=425");
result.document.close();
result.focus();
}

function myalert(nr)  // show alertbox if necessary
{
if (!document.forms[0].alertbt.checked) {return;}
var msgs = new Array();
      msgs[1] = "Please enter a sequence";
      msgs[2] = "Cannot calculate, sequence too long";
      msgs[100] = "This enzyme works only on RNA.\nContinue anyway?";
      msgs[101] = badseq;
if (nr<100) {alert(msgs[nr]);}
else {if (confirm(msgs[nr])) {--err}}
++err;
}

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)
    {
    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;
}

function CalcMW()
{
var mw = massX[2];
if (ph==0) {mw -= massX[3];}
if (ph==2) {mw = 0;}
if (ph==3) {mw += massX[3];}
if (ph==4) {mw = massX[3];}
for (i=0;i<bases.length;++i)
  {
  var numnuc = parseInt(eval("document.forms[0]."+bases.charAt(i)+".value"));
  if (numnuc > 0)
    {
    mw += (massB[posB(bases.charAt(i))]+sugar+massX[3])*numnuc;
    }
  }
mw += parseFloat(document.forms[0].offs.value);
return mw;
}

function CalcES(md)  // neg electrospray series
{
var mw = CalcMW();
var charge = 0;
var i = 0;
if (ph == 0) {charge = totnuc-1;}
if (ph == 1) {charge = totnuc;}
if (ph == 2) {charge = totnuc;}
if (ph == 3) {charge = totnuc+1;}
if (ph == 4) {charge = totnuc+1;}
if (charge < 1) {charge = 1;}
outrec[recno] = "   ch     m/z       M+Na      M+K      M+2Na\n";
++recno;
for (i=1;i<=charge;++i)
  {
  outrec[recno] = str(md*i,5)+str((mw+md*i*massX[1])/i,10,3);
  outrec[recno] += str((mw+massX[6]-massX[1]+md*i*massX[1])/i,10,3);
  outrec[recno] += str((mw+massX[7]-massX[1]+md*i*massX[1])/i,10,3);
  outrec[recno] += str((mw+2*massX[6]-2*massX[1]+md*i*massX[1])/i,10,3);
  if (i<charge) {recno++};
  }
}

function Calc0()  // molecular mass
{
outrec[recno] = str(CalcMW(),10,3);
++recno;
}

function Calc1()  // neg electrospray series
{
CalcES(-1);
}

function Calc2()  // pos electrospray series
{
CalcES(1);
}

function DoCalc()
{
recno= 0;
totnuc = 0;
var i,j,numnuc;

for (i=0;i<document.forms[0].dr.length;++i)
  {if (document.forms[0].dr[i].checked){dr = document.forms[0].dr[i].value;break}}
for (i=0;i<document.forms[0].ph.length;++i)
  {if (document.forms[0].ph[i].checked){ph = document.forms[0].ph[i].value;break}}
for (i=0;i<document.forms[0].ma.length;++i)
  {if (document.forms[0].ma[i].checked){ma = document.forms[0].ma[i].value;break}}

outhead = "<HTML><HEAD><TITLE>Oligo Mass Output</TITLE></HEAD>"
outhead += "<BODY BGCOLOR='#C0C0F0' TEXT='#000000' LINK='#0000FF' VLINK='#FF00FF'><PRE><B>";
outhead += document.title+"\n\n";
outhead += eval("ma"+ma+".toUpperCase()")+" CALCULATION FOR "+dr+"\n";
for (i=0;i<bases.length;++i)
  {
  numnuc = parseInt(eval("document.forms[0]."+bases.charAt(i)+".value"));
  if (numnuc > 0)
    {
    outhead += bases.charAt(i)+"<SUB>"+numnuc+"</SUB> ";
    totnuc += numnuc;
    }
  }
if (totnuc==0) return;

outhead += "</B> ("+eval("ph"+ph);
if (document.forms[0].offs.value != 0) {outhead += ", add "+document.forms[0].offs.value;}
outhead += ")\n<HR>\n";
massX = eval("m"+ma+"X");
massB = eval("m"+ma+"B");
sugar = eval("sugar"+ma);
if (dr=="RNA") {sugar += massX[5]};

result = window.open("","result","resizable=1,menubar=1,scrollbars=1,width=550,height=350");
result.opener = self;
result.document.clear();
result.document.writeln(outhead);

for (i=0;i<document.forms[0].sel.length;++i)
  {
  if (document.forms[0].sel[i].selected)
    {
    outhead = document.forms[0].sel[i].value.toUpperCase().bold()+"\n";
    outrec = new Array();
    recno = 0;
    eval("Calc"+i+"()");
    result.document.writeln(outhead);
    for (j=0;j<outrec.length;++j) {result.document.writeln(outrec[j]);}
    result.document.writeln();
    }
  }
result.document.write("</PRE>");
result.document.write("<FORM><INPUT TYPE='button' VALUE=' Close ' onClick='window.close()'></FORM>");
result.document.write("</BODY></HTML>");
result.document.close();
result.focus();
}
