function writeText(text,id)
{
  if (document.getElementById)
  {
    x = document.getElementById(id);
    x.innerHTML = '';
    x.innerHTML = text;
  }
  else if (document.all)
  {
    x = document.all[id];
    x.innerHTML = text;
  }
  else if (document.layers)
  {
    x = document.layers[id];
    //text2 = text;
    x.document.open();
    x.document.write(text);
    x.document.close();
  }
}

function convert_Inch_To_Centimetres()
{
  var one_inch = 2.54;
  var inchIndex = document.BMI.getInches.selectedIndex;
  var feetIndex = document.BMI.getFeet.selectedIndex; 
  var total = (document.BMI.getInches[inchIndex].value*1)+(document.BMI.getFeet[feetIndex].value*12);
  document.BMI.getCentimetres.value = Math.round(total*one_inch);
}

function convert_Pounds_To_Kilograms()
{
  var one_lbs = 0.45359237;
  var poundsIndex = document.BMI.getPounds.selectedIndex;
  var stonesIndex = document.BMI.getStones.selectedIndex; 
  var total = (document.BMI.getPounds[poundsIndex].value*1)+(document.BMI.getStones[stonesIndex].value*14);  
  document.BMI.getKilograms.value = Math.round(total*one_lbs);
}

function calculateBMI()
{
  var myArray = new Array(7);
  myArray[0] = '<span class="Bold">Underweight</span><br><span class="Normal">Being underweight can be dangerous in some circumstances. If the body is not supplied with the right amount of nutrients and fluids you could be at risk of becoming seriously ill. </span><br>';
  myArray[1] = '<span class="Bold">Normal</span><br><span class="Normal">This range is associated with good health. With a continuation of a well balanced diet and regular exercise it will minimise your risk of most health related problems such as heart disease, certain cancers, osteoporosis and many more. </span><br>';
  myArray[2] = '<span class="Bold">Overweight</span><br><span class="Normal">Your health would benefit from losing some weight. Being overweight can be associated with many problems such as high blood pressure, heart disease, strokes, certain cancers and other types of illnesses, including types of diabetes.</span><br><br><span class="Normal">Placing yourself on a steady well-balanced diet with regular exercise will help you to lose weight. It is not a fast process, so getting advised on the correct diet and exercise would be wise.</span><br>';
  myArray[3] = '<span class="Bold">Obese</span><br><span class="Normal">Being obese you would be wise to lose weight. You would benefit from professional advice on dieting and exercise. Obesity  is linked with heart disease, high blood pressure, back pain, strokes, certain cancers, type 2 diabetes and many other illnesses. Combining a healthy diet with exercise could add years onto your life.</span>';
  myArray[4] = '<span class="Bold">Largely Obese</span><br><span class="Normal">It is very important that you do not gain any more weight. Being largely obese you have potentially opened yourself to type 2 diabetes, heart disease, fertility problems, back pain, certain cancers, strokes and many more illnesses.<br><br>Gaining professional advice on dieting and exercise would be very beneficial.</span>';
  myArray[5] = '<span class="Bold">Extremely Obese</span><br><span class="Normal">You are extremely obese. You would benefit from specialist medical advice. Getting placed on a specific diet and exercise program is essential. It is very important that you do not gain any more weight.</span><br><br><span class="Normal">You have put yourself at extreme risk of high blood pressure, heart disease, fertility problems, breathlessness, cancer, osteoarthritis, type two diabetes along with many other health problems. Not to mention a shortened life span.</span>';
  
//Calculation: kilograms/(metres*metres)   e.g.  70kg/(1.77*1.77);
  var cm=document.BMI.getCentimetres.value/100;
  var kg=document.BMI.getKilograms.value; 
  var bmi=Math.round(kg*10/cm/cm)/10;
  var str="\0";

  if ((cm == "")||(kg == ""))
  { 
    if ((cm == "")&&(kg == "")) { alert("Please enter Height & Weight"); return; }
    if (cm == "") alert("Please enter Height");
	else alert("Please enter Weight");
  }
  else
  {
    if (document.BMI.getCentimetres.value*1 > 241) { alert("Maximum Height: 241cm (7 ft 11 inches)"); document.BMI.getCentimetres.value = "241"; return; }
    if (document.BMI.getCentimetres.value*1 < 112) { alert("Minimum Height: 112cm (4 ft)"); document.BMI.getCentimetres.value = "112"; return; }	
    if (document.BMI.getKilograms.value*1 > 200) { alert("Maximum Weight: 200 Kilograms (441 lbs )"); document.BMI.getKilograms.value = "200"; return; }
    if (document.BMI.getKilograms.value*1 < 32) { alert("Minimum Weight: 32 Kilograms (70 lbs )"); document.BMI.getKilograms.value = "32"; return; }	
    else
	{
      if (bmi*1 <= 18.5) { str = " (Underweight)"; writeText(myArray[0],"infoDiv"); }
	  else if ((bmi*1 > 18.5)&&(bmi <= 24.9)) { str = " (Normal)"; writeText(myArray[1],"infoDiv"); }
	  else if ((bmi*1 > 24.9)&&(bmi*1 <= 29.9)) { str = " (Overweight)"; writeText(myArray[2],"infoDiv"); }
	  else if ((bmi*1 > 29.9)&&(bmi*1 <= 34.9)) { str = " (Obese)"; writeText(myArray[3],"infoDiv"); }
	  else if ((bmi*1 > 34.9)&&(bmi*1 <= 39.9)) { str = " (Largely Obese)"; writeText(myArray[4],"infoDiv"); }
	  else if (bmi*1 > 39.9) { str = " (Extremely Obese)"; writeText(myArray[5],"infoDiv"); }   
	  document.BMI.theBMI.value = bmi + str;
	}  
  }	
}