// javascript for blood conversion program 11-06-03 DRH (blood_conv.js) 
// This script works. Debugged 11-7-03 DRH ---------------
var i, chol, chol2, trig, trig2, num;

function convert1( )
{
  chol = document.form1.txtChol_mg.value;
  document.form1.txtChol_mmoles.value = "";
  document.form1.txtTrig_mmoles.value = "";
  //alert("Chol = " + chol +"\n" + "Trig = " + trig);
  //alert("chol type = " + typeof chol);

  // Convert chol mg/dL --> mmoles --------------------
  chol2 = parseFloat(chol);
  if (isNaN(chol2)) 
  {
     alert("You must enter a valid number. Try again.");
     document.form1.txtChol_mg.select();
     document.form1.txtChol_mg.focus();
     return;
  }
  //alert("Chol2 =" + chol2);     
  if (chol<= 0) chol=0;
  chol2 = chol/38.7;
  chol2 = Math.round(chol2*100)/100;
  if (chol2 <= 0) document.form1.txtChol_mmoles.value = "";
  else
  document.form1.txtChol_mmoles.value = chol2;

  // Convert trig mg/dL --> mmoles --------------------
  trig = document.form1.txtTrig_mg.value;
  trig2=parseFloat(trig);
  if (isNaN(trig2))
  {
    alert("You must enter a valid number. Try again.");
    this.form1.txtTrig_mg.select();
    this.form1.txtTrig_mg.focus();
    return;
  }
  if (trig <=0) trig=0;
  trig2 = trig/88.6;
  trig2 = Math.round(trig2*100)/100;
  if (trig2 <=0) document.form1.txtTrig_mmoles.value = "";
    else
  document.form1.txtTrig_mmoles.value = trig2;
}

function convert2()
{
  // Convert from mmoles to mg/dL --------------------------
  with(document.form1)
  {
    txtChol_mg.value="";
    txtTrig_mg.value="";
    chol= txtChol_mmoles.value;
    chol2=parseFloat(chol);
    if (isNaN(chol2))
    {
      alert("You must enter a valid number. Try again.");
      txtChol_mmoles.select();
      txtChol_mmoles.focus();
      return;
    }
    chol2=chol2*38.7;
    chol2= Math.round(chol2);
    if (chol2 > 0) txtChol_mg.value = chol2;
    else
    txtChol_mg.value="";

    trig = txtTrig_mmoles.value;
    trig = parseFloat(trig);
    if (isNaN(trig))
    {
      alert("You must enter a valid number. Try again.");
      txtTrig_mmoles.select();
      txtTrig_mmoles.focus();
      return;
    }
    trig2 = trig * 88.6;
    trig2 = Math.round(trig2);
    if (trig2 > 0) txtTrig_mg.value = trig2;
    else
    txtTrig_mg.value = "";
  }
}
