/*
  Main Stuff
*/

// Returns 'm' or 'f' depending on user's choice in the radio button.  If you
// are 'f' and are
// A) Reading this message
// B) Following my advice on ideal weight
// C) Smart enough to know why I put "//" in front of these lines
// D) Really insecure and easily pushed around
//
// ...call me.
function get_gender() {
  return $('male').checked ? 'm' : ($('female').checked ? 'f' : '');
}

function compute_bmi_results() {
  var weight, height, bmi, ideal_bmi, ideal_weight;

  // Make sure all values are numbers
  weight  = Number($('weight_pounds').value);
  height  = Number($('height_inches').value) + Number($('height_feet').value) * 12;

  bmi = calc_bmi(height, weight);
  ideal_bmi = 16.0;
  ideal_weight = Math.round(reverse_calc_bmi(ideal_bmi, height));

  display_results(weight, ideal_weight, 'Your BMI is currently <b>' + bmi + '</b>.');
}

// Computes body fat and determines how overweight our fatty is.
function compute_body_fat_results() {
  var gender, weight, waist, wrist, hip, forearm, percent;

  // Make sure all values are numbers
  weight  = Number($('weight_pounds').value);
  waist   = Number($('waist_inches').value);
  wrist   = Number($('wrist_inches').value);
  hip     = Number($('hip_inches').value);
  forearm = Number($('forearm_inches').value);

  gender = get_gender();
  // Desired measurements and body fat per gender:
  var ideal_waist, ideal_wrist, ideal_hip, ideal_forearm, ideal_percent, ideal_weight;
  if (gender == 'm') {
    ideal_waist = 26;
    ideal_percent = 1.0;
  }
  else {
    ideal_waist = 22;
    ideal_percent = 1.0
    ideal_hip = 30;
    ideal_forearm = 8;
    ideal_wrist = 4;
  }

  percent = calc_body_fat(gender, weight, wrist, waist, hip, forearm);
  overfat = Math.round(weight * (percent - ideal_percent) / 100.0);

  ideal_weight = weight - overfat;

  display_results(weight, ideal_weight, 'Your body fat percentage is currently <b>' + percent + '%</b>.');
}

// Given a weight and ideal weight, determine how disgusting our fat slob of
// a user actually is.
function display_results(weight, ideal, message) {
  var category = Math.round((weight - ideal) / 10);
  if (category < -1) {category = -1};
  if (category > 10) {category = 10};

  var overweight_message;

  switch (category) {
    case -1:
      overweight_message = 'You are a beacon of hope to us all!  Your weight is <b>below</b> my recommended ideal weight by a good amount.  Congratulations!';
      break;
    case 0:
      overweight_message = 'Not too shabby!  Your weight is definitely within the realm of sexy.  Congratulations!';
      break;
    case 1:
      overweight_message = "You aren't too far from the mark, but you do need to work some.  You're far better off than most health-conscious Americans, so be proud of that.";
      break;
    case 2:
      overweight_message = "You're disgusting.  Oh, let me guess, somebody out there thinks you're perfectly &quot;healthy&quot;.  Well, <b>not me</b>, fatty.";
      break;
    case 3:
    case 4:
      overweight_message = "Good lord, how do you even get off coming to this website?  You are a disgrace to anorexics everywhere.  Begone!";
      break;
    case 5:
    case 6:
      overweight_message = "Don't take this the wrong way, but your type really make me sick.";
      break;
    case 7:
    case 8:
    case 9:
      overweight_message = "OH GOD MY EYES ARE BURNING!  OW OW OW!  <b>Please</b> leave my site before you ooze into your computer and infect me with obesity!";
      break;
    case 10:
      overweight_message = "I really don't know what to say, you take the cake.  Literally.  I think there's still a little fried chicken in your fat rolls.  Oh, oops, my mistake - that's just the fat rolls' fat rolls.";
      break;
  }
  results = 'Your current weight: <b>' + weight +
    '</b><br />Your ideal anorexic/bulimic weight: <b>' + ideal +
    '</b><br />' + message +
    '<p />' + overweight_message;
  $('results_text').innerHTML = results;
}

/*
  Utility functions
*/

// Computes the BMI for the given height and weight
function calc_bmi(height_inches, weight_pounds) {
   // Return the BMI value rounded to the nearest 0.1
   return Math.round(weight_pounds * 703 * 10 / height_inches / height_inches) / 10
}

// Computes the weight for the given BMI and height
function reverse_calc_bmi(bmi, height_inches) {
  return Math.round(bmi * height_inches * height_inches / 703)
}

// Based on http://www.bmi-calculator.net/body-fat-calculator/body-fat-formula.php,
// this little function computes body fat.  This is only an estimate, of course,
// and truly devoted anorexics and bulimics need to get a more accurate measurement
// from a doctor!
function calc_body_fat(gender, weight_pounds, wrist_inches, waist_inches, hip_inches, forearm_inches) {
  var body_fat = 0.0;
  var factor_1, factor_2, factor_3, factor_4, factor_5, lean_mass;
  if (gender == 'm') {
    factor_1 = (weight_pounds * 1.082) + 94.42;
    factor_2 = (waist_inches * 4.15);
    lean_mass = factor_1 - factor_2;
  }
  else if (gender == 'f') {
    factor_1 = (weight_pounds * 0.732) + 8.987;
    factor_2 = wrist_inches / 3.140;
    factor_3 = waist_inches * 0.157;
    factor_4 = hip_inches * 0.249;
    factor_5 = forearm_inches * 0.434;
    lean_mass = factor_1 + factor_2 - factor_3 - factor_4 + factor_5;
  }
  else {
    lean_mass = 0;
  }

  // Compute fat, then round to nearest tenth of a percent.
  body_fat = 100 - (lean_mass * 100 / weight_pounds);
  return Math.round(body_fat * 10) / 10;
}
























