function layerWrite(id, text) 
{
  if (document.all)
  {
    document.all[id].innerHTML = text;
  }
  else (document.getElementById)
  {
     var div = document.getElementById(id);
     div.innerHTML = text;
  }
}


function bmi()  
{
  var wt = document.bmiCalculator.Subwt.value
  var ht = document.bmiCalculator.Subht.value

  if ((wt <= 0) || (ht <= 0)) 
  {
    alert("Some of the data you have entered is either a negative or zero value.  Please correct it and try again");
  }
  else 
  {
    var bodymassindex = wt/(ht*ht);
    var bodymassindexround= (Math.round(bodymassindex*100))/100;
/*
    sResult = new String("<p class='PageTitle'>Results<\/p><p class='BodyText'>Subject weight is <b>" + wt +"<\/b>&#160;kg<BR>Subject height is <b>" + ht +"<\/b>&#160;m<BR>Body Mass Index is <b>" + bodymassindexround +"<\/b>&#160;kg/m&sup2;<\/p>");
*/
    document.bmiCalculator.result.value = bodymassindexround;
  }
}

/*
 *   This is not used
 *      - always knew my job was worthwhile!
 */
function bsa()  
{
  var wt = document.bsaCalculator.Subwt.value * 1000;

  if (wt <= 0)
  {
    alert("Some of the data you have entered is either a negative or zero value.  Please correct it and try again");
  }
  else 
  {
    var bodySA = (4.688*Math.pow(wt,0.8168-(0.0154*(Math.log(wt)/Math.LN10))))/10000;
    var bodySAround = (wt > 28000) ? (Math.round(bodySA*10))/10 : (Math.round(bodySA*100))/100;
    var bodySAdp = (Math.round(bodySAround) == bodySAround) ? bodySAround + ".0" : bodySAround ;
    document.bsaCalculator.result.value = bodySAdp;
  }
}

function creatCheck()  
{
  // ***check that units have been selected***
  
  var i = document.creat.unitSelect.selectedIndex;
  var indexvalue = document.creat.unitSelect.options[i].value;

  if ((indexvalue == "milligrams") || (indexvalue == "micromols")) 
  {
    creatCalculate();
  }
  else  
  {
    alert("Please select the units for serum creatinine measurement and try again");
  }
}

function creatCalculate()       
{
  var male = document.creat.elements[0].checked;
  var age = document.creat.patientAge.value;
  var c = document.creat.creatconc.value;
  var i = document.creat.unitSelect.selectedIndex;
  var indexvalue = document.creat.unitSelect.options[i].value;

  var sBlurb = new String("<p class='BodyText'>Glomerular filtration rate (GFR) is usually<br>measured by creatinine clearance.<\/p>");
  var sTableHead = new String("<TABLE cellpadding='3' cellspacing='0' border='1' ><tr><td><\/td><td class='BodyText' align='center'>GFR<BR>(mL/min)<\/td><td class='BodyText' align='center'>Serum creatinine<BR> (approx, &micro;mol/L)<\/td><\/tr>");
  var sTableRow1 = new String("<tr><td class='BodyText'>mild<BR>impairment<\/td><td class='BodyText' align='center'>20 - 50<\/td><td class='BodyText' align='center'>150 - 300<\/td><\/tr>");
  var sTableRow2 = new String("<tr><td class='BodyText'>moderate<BR>impairment<\/td><td class='BodyText' align='center'>10 - 20<\/td><td class='BodyText' align='center'>300 - 700<\/td><\/tr>");
  var sTableRow3 = new String("<tr><td class='BodyText'>severe<BR>impairment<\/td><td class='BodyText' align='center'>&lt;10<\/td><td class='BodyText' align='center'>&gt;700<\/td><\/tr><\/table>");


  var clearance = null;
  var sFormula = null;

  if (indexvalue == "milligrams")
  {
    clearance = ((145-age)/c) - 3;

    if (male == false)
    {
      clearance = clearance * 0.85;
      sFormula = new String("The formula used in this calculation was:<br>(0.85&times;(145-patient age)/(serum creatinine)) - 2.55<\/p>");
    }
    else
    {
      sFormula = new String("The formula used in this calculation was:<br>((145-patient age)/(serum creatinine)) - 3<\/p>");
    }
  }
  else if (indexvalue == "micromols")
  {
    clearance = (88 * (145-age)/c) - 3;

    if (male == false)
    {
      clearance = clearance * 0.85;
      sFormula = new String("The formula used in this calculation was:<br>(74.8&times;(145-patient age)/(serum creatinine)) - 2.55<\/p>");
    }
    else
    {
      sFormula = new String("The formula used in this calculation was:<br>(88&times;(145-patient age)/(serum creatinine)) - 3<\/p>");
    }
  }

  if (sFormula == null)
    alert("There is an error in the script");
  else
  {
    clearance = (Math.round(clearance*100))/100;
    var sResult = new String("<p class='PageTitle'>Results<\/p><p class='BodyText'>The estimated creatinine clearance is:<br><b>" + clearance + " ml/min per 70kg<\/b><BR><BR>");
  
    layerWrite('results',"<table width='300'><tr><td>" + sResult + sFormula + sBlurb + "<\/td><\/tr><\/table><br>" + sTableHead + sTableRow1 + sTableRow2 + sTableRow3);
  }
}

function instomms() 
{
  var result=document.lenconversions.ins.value;
  var lenconvresult=(result*25.4);
  lenconvresult=Math.round(lenconvresult*100)/100;
  document.lenconversions.tomms.value=lenconvresult;
}

function mmstoins() 
{
  var result=document.lenconversions.mms.value;
  var lenconvresult=(result/25.4);
  lenconvresult=Math.round(lenconvresult*100)/100;
  document.lenconversions.toins.value=lenconvresult;
}

function feettometres() 
{
  var result=document.lenconversions.feet.value;
  var lenconvresult=(result*0.3048);
  lenconvresult=Math.round(lenconvresult*100)/100;
  document.lenconversions.tometres.value=lenconvresult;
}

function metrestofeet() 
{
  var result=document.lenconversions.metres.value;
  var lenconvresult=(result/0.3048);
  lenconvresult=Math.round(lenconvresult*100)/100;
  document.lenconversions.tofeet.value=lenconvresult;
}

function yardstometres() 
{
  var result=document.lenconversions.yards.value;
  var lenconvresult=(result*0.9144);
  lenconvresult=Math.round(lenconvresult*100)/100;
  document.lenconversions.tometres2.value=lenconvresult;
}

function metrestoyards() 
{
  var result=document.lenconversions.metres2.value;
  var lenconvresult=(result/0.9144);
  lenconvresult=Math.round(lenconvresult*100)/100;
  document.lenconversions.toyards.value=lenconvresult;
}


function floztomls() 
{
  var result=document.volconversions.floz.value;
  var volconvresult=(result*28.409);
  volconvresult=Math.round(volconvresult*100)/100;
  document.volconversions.tomls.value=volconvresult;
}

function mlstofloz() 
{
  var result=document.volconversions.mls.value;
  var volconvresult=(result/28.409);
  volconvresult=Math.round(volconvresult*100)/100;
  document.volconversions.tofloz.value=volconvresult;
}

function pintstolitres() 
{
  var result=document.volconversions.pints.value;
  var volconvresult=(result*0.56825);
  volconvresult=Math.round(volconvresult*100)/100;
  document.volconversions.tolitres.value=volconvresult;
}

function litrestopints() 
{
  var result=document.volconversions.litres.value;
  var volconvresult=(result/0.56825);
  volconvresult=Math.round(volconvresult*100)/100;
  document.volconversions.topints.value=volconvresult;
}

function gallonstolitres() 
{
  var result=document.volconversions.gallons.value;
  var volconvresult=(result*4.5454);
  volconvresult=Math.round(volconvresult*100)/100;
  document.volconversions.tolitres2.value=volconvresult;
}

function litrestogallons() 
{
  var result=document.volconversions.litres2.value;
  var volconvresult=(result/4.5454);
  volconvresult=Math.round(volconvresult*100)/100;
  document.volconversions.togallons.value=volconvresult;
}

function lbstokgs() 
{
  var result=document.wtconversions.lbs.value;
  var wtconvresult=(result*0.45359);
  wtconvresult=Math.round(wtconvresult*100)/100;
  document.wtconversions.tokilos.value=wtconvresult;
}

function kgstolbs() 
{
  var result=document.wtconversions.kilos.value;
  var wtconvresult=(result/0.45359);
  wtconvresult=Math.round(wtconvresult*100)/100;
  document.wtconversions.tolbs.value=wtconvresult;
}

function stlbstokgs() 
{ 
  var result=document.wtconversions.stones.value;
  var result2=document.wtconversions.lbs2.value;
  if ((result2 >= 0) && (result2 <= 14)) 
  {
    var wtconvresult=((result*6.35029)+(result2*0.45359));
    wtconvresult=Math.round(wtconvresult*100)/100;
    document.wtconversions.tokilos2.value=wtconvresult;
  }
  else 
  {
    alert("The value for the number of pounds must be between 0 and 14");
    document.wtconversions.lbs2.value="";
    result2=0;
    var wtconvresult=((result*6.35029)+(result2*0.45359));
    wtconvresult=Math.round(wtconvresult*100)/100;
    document.wtconversions.tokilos2.value=wtconvresult;     
  }
}

function paraCalculate()  
{
  var hr = document.paracet.highrisk.checked;
  var lr = document.paracet.lowrisk.checked;
  var t = document.paracet.dosetime.value;
  var c = document.paracet.paraconc.value;
 
  if (((hr ==false) && (lr == false)) || ((hr ==true) && (lr == true))) 
  {
        alert("Please check one of either the high-risk or low-risk checkboxes.");
  }
  else 
  {
    if (hr == false)  
    {
      var safe = 200/Math.pow(2,((t/4)-1));
      var saferound = Math.round(safe*100)/100;
    }
    else  
    {
      var safe = 100/Math.pow(2, ((t/4)-1));
      var saferound = Math.round(safe*100)/100;
    }
    if (c > safe) 
    {
      layerWrite('result',"The treatment line reading is: " + saferound + " mg/l");
      layerWrite('treatment', "The patient's plasma paracetamol concentration is above the treatment line.");
/*
          layerWrite('results','rightPage',"<p class='PageTitle'>Results<\/p><p class='BodyText'>The treatment line reading is: <span class='Result'>&#160;" + saferound + " mg/l <\/span><BR><BR>The patient's plasma paracetamol concentration is <span class='Result'> &#160;above <\/span>&#160;the treatment line.<\/p>");
*/
          
          alert("The treatment line reading is: " + saferound + " mg/l  The patient's plasma paracetamol concentration is above the treatment line.");
    }
    else 
    {
      layerWrite('result',"The treatment line reading is: " + saferound + " mg/l");
      layerWrite('treatment', "The patient's plasma paracetamol concentration is below the treatment line.");
/*
          layerWrite('results','rightPage',"<p class='PageTitle'>Results<\/p><p class='BodyText'>The treatment line reading is: <span class='Result'>&#160;" + saferound + " mg/l <\/span><BR><BR>The patient's plasma paracetamol concentration is <span class='Result'>&#160;below <\/span>&#160;the treatment line.<\/p>");
*/
          alert("The treatment line reading is:  " + saferound + " mg/l  The patient's plasma paracetamol concentration is below the treatment line.");
    }
  }
}

function pregnancy()    
{
  var md = document.preg.mensDay.value;
  var mmind = document.preg.mensMonth.selectedIndex;
  var mm = document.preg.mensMonth.options[mmind].value;
  var myind =  document.preg.mensYear.selectedIndex;
  var my = document.preg.mensYear.options[myind].value;

  var mensDate = new Date();
  mensDate.setYear(my);
  mensDate.setMonth(mm);
  mensDate.setDate(md);

  var mensValue = mensDate.getTime();
  var dueValue = mensValue + 24192000000;
  var dueDate = new Date();
  dueDate.setTime(dueValue);

  var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  var datedue = days[dueDate.getDay()] + " " + dueDate.getUTCDate() + " " + months[dueDate.getMonth()] + " " + dueDate.getFullYear();

/*
  layerWrite('results','rightPage',"<p class='PageTitle'>Results<\/p><p class='BodyText'>Due date: " + datedue + "<\/p>");
*/
  var md = document.preg.result.value = datedue;
}
