/*
 * $Id:$
 *
 * This is a javascript file used for calculating the ideal weight given
 * various parameters and a standard weight loss table.
 *
 * We assume that Prototype has already been imported so we can 
 * depend on its functionality.
 */



/*
 * We define our table of data. This structure is setup as follows:
 * 1) an object with either the key male or female,
 * 2) an object with height in inches mapped to a list of ranges
 * 3) the ranges represent small/medium/large frames - in that order.
 */
var IDEAL_WEIGHT_DATA = {
  
  female: { 72: [ r(138, 151), r(148, 162), r(158, 179) ],
            71: [ r(135, 148), r(145, 159), r(155, 176) ],
            70: [ r(132, 145), r(142, 156), r(152, 173) ],
            69: [ r(129, 142), r(139, 153), r(149, 170) ],
            68: [ r(126, 139), r(136, 150), r(146, 167) ],
            67: [ r(123, 136), r(133, 147), r(143, 163) ],
            66: [ r(120, 133), r(130, 144), r(140, 159) ],
            65: [ r(117, 130), r(127, 141),	r(137, 155) ],
            64: [ r(114, 127), r(124, 138), r(134, 151) ],
            63: [ r(111, 124), r(121, 135), r(131, 147) ],
            62: [ r(108, 121), r(118, 132), r(128, 143) ],
            61: [ r(106, 118), r(115, 129), r(125, 140) ],
            60: [ r(104, 115), r(113, 126), r(122, 137) ],
            59: [ r(103, 113), r(111, 123), r(120, 134) ],
            58: [ r(102, 111), r(109, 121), r(118, 131) ] 
  },
  male: { 76: [ r(162, 176), r(171, 187), r(181, 207) ],
          75: [ r(158, 172), r(167, 182), r(176, 202) ],
          74: [ r(155, 168), r(164, 178), r(172, 197) ],
          73: [ r(152, 164), r(160, 174), r(168, 192) ],
          72: [ r(149, 160), r(157, 170), r(164, 188) ],
          71: [ r(146, 157), r(154, 166), r(161, 184) ],
          70: [ r(144, 154), r(151, 163), r(158, 180) ],
          69: [ r(142, 151), r(148, 160), r(155, 176) ],
          68: [ r(140, 148), r(145, 157), r(152, 172) ],
          67: [ r(138, 145), r(142, 154), r(149, 168) ],
          66: [ r(136, 142), r(139, 151), r(146, 164) ],
          65: [ r(134, 140), r(137, 148), r(144, 160) ],
          64: [ r(132, 138), r(135, 145), r(142, 156) ],
          63: [ r(130, 136), r(133, 143), r(140, 153) ],
          62: [ r(128, 134), r(131, 141), r(138, 150) ] 
  }
};


/*
 * Provided a bunch of data, do our table lookup.
 * If we can't find the value in the table, then 
 * return back false.
 */
function lookupIdealWeight(gender, heightFeet, heightInches, frameSize) {
  var height = (parseInt(heightFeet) * 12) + parseInt(heightInches);
  var frame  = (frameSize == 'small' ? 0 :
                (frameSize == 'medium' ? 1 :
                 (frameSize == 'large' ? 2 : false)));
  if(IDEAL_WEIGHT_DATA[gender] && 
     IDEAL_WEIGHT_DATA[gender][height] &&
     IDEAL_WEIGHT_DATA[gender][height][frame]) {
    return IDEAL_WEIGHT_DATA[gender][height][frame]
      } else {
    return false;
  }
}

function calculateIdealWeight(f) {
  var idealWeight = lookupIdealWeight($R(f.gender), $F(f.height_feet), $F(f.height_inches), $R(f.frame));

  if(idealWeight) {
    $(f.ideal_weight).value = idealWeight.toString();
    var weightLoss = idealWeight.sub($F(f.current_weight));

    if(weightLoss.crossesZero()) {
      $(f.recommendation).value = 'You are at your ideal weight';
      $(f.weight_loss).value = 'You have no weight to lose';
    } else if(weightLoss.isNeg()) {
      var weightGain = weightLoss.abs();
      $(f.recommendation).value = 'You need to gain some weight. Use Herbalife ShapeWorks QuickStart Program in addition to your regular diet';
      $(f.weight_loss).value = 'You need to gain: ' + weightGain.toString();
    } else {
      $(f.weight_loss).value = 'You need to lose: ' + weightLoss.toString();
      if(weightLoss.between(1, 15)) {
        $(f.recommendation).value = 'For 1 to 15 pound weight loss use Herbalife ShapeWorks (1) QuickStart Program.';
      } else if(weightLoss.between(16, 30)) {
        $(f.recommendation).value = 'For 16 to 30 pound weight loss use Herbalife ShapeWorks (4) QuickStart Protein Plus Program.';  
      } else if(weightLoss.between(31, 45)) {
        $(f.recommendation).value = 'For 31 to 45 pound weight loss use Herbalife ShapeWorks (2)Advanced Program.';  
      } else if(weightLoss.between(46, 60)) {
        $(f.recommendation).value = 'For 46 to 60 pound weight loss use Herbalife ShapeWorks (5)Advanced Protein Plus Program.';  
      } else if(weightLoss.between(61, 75)) {
        $(f.recommendation).value = 'For 61 to 75 pound weight loss use Herbalife ShapeWorks (3) Ultimate Program.';  
      }  else if(weightLoss.between(76, 1000)) {
        $(f.recommendation).value = 'For over 75 pound weight loss use Herbalife ShapeWorks Ultimate (6) Protein Plus Program.';  
      } else {
        $(f.recommendation).value = '';
      }
    }
  } else {
    alert("Unable to calculate your ideal weight. Double check the form and try again");
  }

}


<!-- Original:  Marat Rikelman (rikelman@bellsouth.net) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

function mod(div,base) {
  return Math.round(div - (Math.floor(div/base)*base));
}
function calcBmi() {
  var w = document.bmi.current_weight.value * 1;
  var HeightFeetInt = document.bmi.height_feet.value * 1;
  var HeightInchesInt = document.bmi.height_inches.value * 1;
  HeightFeetConvert = HeightFeetInt * 12;
  h = HeightFeetConvert + HeightInchesInt;
  displaybmi = (Math.round((w * 703) / (h * h)));
  var rvalue = true;
  if ( (w <= 35) || (w >= 500)  || (h <= 48) || (h >= 120) ) {
    alert ("Invalid data.  Please enter your gender");
    rvalue = false;
  }
  if (rvalue) {
    if (HeightInchesInt > 11) {
      reminderinches = mod(HeightInchesInt,12);
      document.bmi.height_inches.value = reminderinches;
      document.bmi.height_feet.value = HeightFeetInt + 
        ((HeightInchesInt - reminderinches)/12);
      document.bmi.bmi_answer.value = displaybmi;
    }
    if (displaybmi <19) 
      document.bmi.comment.value = "Underweight - Protein Range A";
    if (displaybmi >=19 && displaybmi <=25) 
      document.bmi.comment.value = "Desirable, Ideal weight - Protein Range B";
    if (displaybmi >=26 && displaybmi <=29) 
      document.bmi.comment.value = "Overweight - prone to health risks - Protein Rage C";
    if (displaybmi >=30 && displaybmi <=40) 
      document.bmi.comment.value = "Obese - Protein Range D";
    if (displaybmi >40) 
      document.bmi.comment.value = "Extremely obese - serious health risk - Protein Range D";
    document.bmi.bmi_answer.value = displaybmi; 
  }
  return rvalue;
}
