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;
  }
}


function bsa()  
{
  var wt = document.bsaCalculator.Subwt.value;
  ht = document.bsaCalculator.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 bodySA = 0.007184*Math.pow(ht,0.725)*Math.pow(wt,0.425);
    var bodySAround= (Math.round(bodySA*100))/100;
/*
    var sResult = "<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 Surface Area is <b>" + bodySAround +"<\/b>&#160;m&sup2;<\/p><p class='BodyText'>The formula used in this calculation (the Dubois formula) is:<br><br>Body Surface area (m&sup2;) =<br> 0.007184 &times; (patient height in cm)<sup>0.725<\/sup> &times; (patient weight in kg)<sup>0.425<\/sup>" 
 */
    document.bsaCalculator.result.value = bodySAround;
  }
}

function creatCheck()  
{
  // ***check that units have been selected***
  
  var age = document.creat.patientAge.value;
  var weight = document.creat.patientWeight.value;

  if(age < 18) {
    layerWrite('results',"<table width='300'><tr><td>Cockcroft and Gault formula is not for use in patients under 18 years<\/td><\/tr><\/table>");
  }
  else {

  if(age > 70 || weight < 45 || weight > 90) {
    alert("This calculator has not been validated to calculate creatinine clearance for patients with an age greater than 70 years or with an ideal weight outside of the range 45 to 90kg");
  }

  creatCalculate();
  }
}

function creatCalculate()       
{
  var male = document.creat.elements[0].checked;
  var age = document.creat.patientAge.value;
  var weight = document.creat.patientWeight.value;
  var c = document.creat.creatconc.value;


  var clearance = null;
  var sFormula = null;

    clearance = (140-age) * weight / c;

    if (male == false)
    {
      clearance = clearance * 1.04;
      sFormula = new String("The formula used in this calculation was:<br>Estimated creatinine clearance in mL/minute = ((140 - " + age + ") &times; " + weight +" &times; 1.04) / (" + c + "))<\/p>");
    }
    else
    {
      clearance = clearance * 1.23;
      sFormula = new String("The formula used in this calculation was:<br>Estimated creatinine clearance in mL/minute = ((140 - " + age + ") &times; " + weight +" &times; 1.23) / (" + c + "))<\/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<\/b><BR><BR>");
  
    layerWrite('results',"<table width='300'><tr><td>" + sResult + sFormula + "<\/td><\/tr><\/table>");
  }
}

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 (t < 4 || t > 24)
  {
        alert("Please enter a time in hours between 4 and 24.");
  }
  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;
}
