// Body Mass Index Calculator

function noteText() {
  alert('This result field is used for displaying the calculated BMI value.\n\nTo perform a calculation:\n\n1. Enter your height & weight\n2. Press - Calculate BMI');
  }

function imperialVal(){
  var f = document.imperialbmi;
  w = f.weight_lbs.value;
  v = f.height_ft.value;
  u = f.height_in.value;
  if (!chkw(u)){
    var ii = 0;
    f.height_in.value = 0;
  } else {
    var ii = parseInt(f.height_in.value);
  }
  var fi = parseInt(f.height_ft.value * 12);
  var i = fi + ii;
  if (!chkw(v)){
    alert("Please enter a number for your height.");
    f.height_ft.focus();
    return;
  }
  if (!chkw(w)){
    alert("Please enter a number for your weight.");
    f.weight_lbs.focus();
    return;
  }
  f.imperialResult.value = cal_imperialBMI(w, i);

var result_text = new Array();
result_text[0] = "";
result_text[1] = "<b><font size=5>" + f.imperialResult.value + "</font> BMI<br>You are normal.</b>";
result_text[2] = "<b><font size=5>" + f.imperialResult.value + "</font> BMI<br>Your are underweight.</b>";
result_text[3] = "<b><font size=5>" + f.imperialResult.value + "</font> BMI<br><font color=red>You are overweight.</font></b>";
result_text[4] = "<b><font size=5>" + f.imperialResult.value + "</font> BMI<br><font color=red>You are obese.</font></b>";

var result_info = new Array();
result_info[0] = "Normal";
result_info[1] = "<b>Congratulations!</b><br> Your BMI is in the healthy range.<br><br>Watching what you eat will help keep your body fat down and your activity level high. Following a healthy balanced diet of low-fat, high-fiber meals will keep you trim and help avoid the complications of diabetes. Taking supplements to reinforce your diet is a good way of ensuring your body stays healthy and avoid diabetes complications.";
result_info[2] = "<b>Suggestion/s:</b><br>While not as serious as being overweight, you may want to add a little more protein and carbohydrates in your diet to attain the normal healthy BMI. A properly balanced diet supplemented with enough vitamins and minerals will go a long way to helping you avoid the problems associated with undernourishment.";
result_info[3] = "<b>Suggestion/s:</b><br>You may need to cut back on fats and calories.<p>People who are overweight have a moderate chance of developing high blood pressure, high cholesterol, type-2 diabetes and its complications, heart disease, stroke and certain cancers. <font color=red>Fortunately, even a 10% weight loss can help lower your risk of developing these diseases.</font></p><p>Adopting a daily exercise regimen and following a healthy low-fat, high-fiber diet will help trim excess body fat. There are many different diet and exercise programs out there to help you achieve your ideal BMI. Taking supplements to reinforce your diet is a good way of ensuring your body stays healthy and avoiding diabetes complications.</p>";
result_info[4] = "<b>Suggestion/s:</b><br>Your BMI is in the danger zone.<p>People who are obese have a much greater chance of developing high blood pressure, high cholesterol, type-2 diabetes and its complications, heart disease, stroke and certain cancers. <font color=red>Fortunately, even a 10% weight loss can help lower your risk of developing these diseases.</font></p><p>Adopting a daily exercise regimen and following a healthy low-fat, high-fiber diet is essential. There are many different diet and exercise programs out there to help you achieve your ideal BMI. Consult your doctor on these to find the right one for you. Reinforcing your diet with supplements is a good way of ensuring your body stays healthy and avoiding diabetes complications.</p><p>For more information, consult your physician on your cholesterol, blood pressure and how to bring down your body weight to a safer, healthier level.</p>";

if (f.imperialResult.value < 18.5) 
{
results.innerHTML = result_text[2];
suggestions.innerHTML = result_info[2];
}

if (f.imperialResult.value >= 18.5 && f.imperialResult.value < 24.9) 
{
results.innerHTML = result_text[1];
suggestions.innerHTML = result_info[1];
}

if (f.imperialResult.value >= 25.0 && f.imperialResult.value < 29.9) 
{
results.innerHTML = result_text[3];
suggestions.innerHTML = result_info[3];
}

if (f.imperialResult.value > 30) 
{
results.innerHTML = result_text[4];
suggestions.innerHTML = result_info[4];
}

}

function cal_imperialBMI(lbs, ins){
  h2 = ins * ins;
  bmi = lbs/h2 * 703
  f_bmi = Math.floor(bmi);
  diff  = bmi - f_bmi;
  diff = diff * 10;
  diff = Math.round(diff);
  if (diff == 10){
    f_bmi += 1;
    diff = 0;
  }
  bmi = f_bmi + "." + diff;
  return bmi;
}

function metricVal(formVal){
  var f = formVal;
  w = f.weight_kg.value;
  i = parseInt(f.height_cm.value);
  if (!chkw(i)){
    alert("Please enter a number for your height.");
    f.height_cm.focus();
    return;
  }
  if (!chkw(w)){
    alert("Please enter a number for your weight.");
    f.weight_kg.focus();
    return;
  }
  f.metricResult.value = cal_metricBMI(w, i);

var result_text = new Array();
result_text[0] = "";
result_text[1] = "<b><font size=5>" + f.metricResult.value + "</font> BMI<br>You are normal.</b>";
result_text[2] = "<b><font size=5>" + f.metricResult.value + "</font> BMI<br>Your are underweight.</b>";
result_text[3] = "<b><font size=5>" + f.metricResult.value + "</font> BMI<br><font color=red>You are overweight.</font></b>";
result_text[4] = "<b><font size=5>" + f.metricResult.value + "</font> BMI<br><font color=red>You are obese.</font></b>";

var result_info = new Array();
result_info[0] = "Normal";
result_info[1] = "<b>Congratulations!</b><br> Your BMI is in the healthy range.<br><br>Watching what you eat will help keep your body fat down and your activity level high. Following a healthy balanced diet of low-fat, high-fiber meals will keep you trim and help avoid the complications of diabetes. Taking supplements to reinforce your diet is a good way of ensuring your body stays healthy and avoid diabetes complications.";
result_info[2] = "<b>Suggestion/s:</b><br>While not as serious as being overweight, you may want to add a little more protein and carbohydrates in your diet to attain the normal healthy BMI. A properly balanced diet supplemented with enough vitamins and minerals will go a long way to helping you avoid the problems associated with undernourishment.";
result_info[3] = "<b>Suggestion/s:</b><br>You may need to cut back on fats and calories.<p>People who are overweight have a moderate chance of developing high blood pressure, high cholesterol, type-2 diabetes and its complications, heart disease, stroke and certain cancers. <font color=red>Fortunately, even a 10% weight loss can help lower your risk of developing these diseases.</font></p><p>Adopting a daily exercise regimen and following a healthy low-fat, high-fiber diet will help trim excess body fat. There are many different diet and exercise programs out there to help you achieve your ideal BMI. Taking supplements to reinforce your diet is a good way of ensuring your body stays healthy and avoiding diabetes complications.</p>";
result_info[4] = "<b>Suggestion/s:</b><br>Your BMI is in the danger zone.<p>People who are obese have a much greater chance of developing high blood pressure, high cholesterol, type-2 diabetes and its complications, heart disease, stroke and certain cancers. <font color=red>Fortunately, even a 10% weight loss can help lower your risk of developing these diseases.</font></p><p>Adopting a daily exercise regimen and following a healthy low-fat, high-fiber diet is essential. There are many different diet and exercise programs out there to help you achieve your ideal BMI. Consult your doctor on these to find the right one for you. Reinforcing your diet with supplements is a good way of ensuring your body stays healthy and avoiding diabetes complications.</p><p>For more information, consult your physician on your cholesterol, blood pressure and how to bring down your body weight to a safer, healthier level.</p>";


if (f.metricResult.value < 18.5) 
{
results.innerHTML = result_text[2];
suggestions.innerHTML = result_info[2];
}

if (f.metricResult.value >= 18.5 && f.metricResult.value < 24.9) 
{
results.innerHTML = result_text[1];
suggestions.innerHTML = result_info[1];
}

if (f.metricResult.value >= 25.0 && f.metricResult.value < 29.9) 
{
results.innerHTML = result_text[3];
suggestions.innerHTML = result_info[3];
}

if (f.metricResult.value > 30) 
{
results.innerHTML = result_text[4];
suggestions.innerHTML = result_info[4];
}

}

function cal_metricBMI(kg, htc){
  m = htc/100;
  h2 = m * m;
  bmi = kg/h2;
  f_bmi = Math.floor(bmi);
  diff  = bmi - f_bmi;
  diff = diff * 10;
  diff = Math.round(diff);
  if (diff == 10){
    f_bmi += 1;
    diff = 0;
  }
  bmi = f_bmi + "." + diff;
  return bmi;
}

function chkw(w){
  if (isNaN(parseInt(w))){
    return false;
  } else if (w < 0){
    return false;
  }
  else{
    return true;
  }
}