function get_bmi(weight,height,version)
{
  var bmi = 0;
  if (version=="metric")
  {
    bmi = (weight / (height * height))*10000;
  }else{
    bmi = (weight / (height*height))*703;
  }
  return bmi;
}

function get_tdee(bmr,activity)
{
  var tdee = Math.round(bmr * activity);
  return tdee;
}

function get_bmr(weight,height,age,version,gender)
{
  var bmr = 0;
  if (version=="metric")
  {
    if(gender=="female")
    {
      bmr = 655 + ( 9.6 * weight ) + ( 1.8 * height ) - ( 4.7 * age );
    }else{
      bmr = 66 + ( 13.7 * weight ) + ( 5 * height ) - ( 6.8 * age );
    }
  }else{
    if(gender=="female")
    {
      bmr = 655 + ( 4.35 * weight ) + ( 4.7 * height ) - ( 4.7 * age );
    }else{
      bmr = 66 + ( 6.23 * weight ) + ( 12.7 * height ) - ( 6.8 * age );
    }
  }
  return bmr;
}

$(document).ready(function(){
  var version = $.cookie('version');
  var current_weight = $.cookie('current_weight');
  var current_height = $.cookie('current_weight');
  var bmr_val = $.cookie('bmr');
  if (version != "")
  {
    if (version == "metric")
    {
      $("span.weight").text("KG");
      $("span.height").text("CM");
    }else if (version=="imperial")
    {
      $("span.weight").text("POUNDS");
      $("span.height").text("INCHES");
    }
  }
  $("p.check").hide();
  $("div.result").hide();
  $("input[name='version']").change( function() {
    $("input[name='version']").each(function(){
      if ($(this).attr("checked")){
        $.cookie('version',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      }
    });
    $("p.check").show();
  });
  $("input[name='current_weight']").keyup( function() {
      if ($(this).val()!=""){
        $.cookie('current_weight',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
        $("p.check").show();
      }else{
        $("p.check").hide();
      }

  });
  $("input[name='current_height']").keyup( function() {
      if ($(this).val()!=""){
        $.cookie('current_height',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
        var bmi = get_bmi(current_weight,$(this).val(),version);
        $("p.check").show();
        $("div.bmi_result .value").html(bmi);
        $.cookie('bmi',bmi, { expires: 1,path: '/weight-loss-plan/' });
        $("div.result").show();
      }else{
        $("p.check").hide();
        $("div.result").hide();
      }
  });
  $("input[name='goal_weight']").keyup( function() {
      $.cookie('goal_weight',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if (($(this).val()!="")&&($("input[name='goal_days']").val()!="")){
        $("p.check").show();
      }else{
        $("p.check").hide();
      }
  });
  $("input[name='lose_weight']").keyup( function() {
      $.cookie('lose_weight',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if ($(this).val()!=""){
        var lw = ($(this).val()*3500)/7;
        $.cookie('lw',lw, { expires: 1,path: '/weight-loss-plan/' });
        $("div.lw_result .value").html(lw);
        $("div.lw_result .lw").html($(this).val());
        $("div.result").show();
        $("p.check").show();
      }else{
        $("p.check").hide();
        $("div.result").hide();
      }
  });
  $("input[name='gain_weight']").keyup( function() {
      $.cookie('gain_weight',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if ($(this).val()!=""){
        var gw = ($(this).val()*3500)/7;
        $.cookie('gw',gw, { expires: 1,path: '/weight-loss-plan/' });
        $("div.gw_result .value").html(gw);
        $("div.gw_result .gw").html($(this).val());
        $("div.result").show();
        $("p.check").show();
      }else{
        $("p.check").hide();
        $("div.result").hide();
      }
  });
  $("input[name='goal_days']").keyup( function() {
      $.cookie('goal_days',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if (($(this).val()!="")&&($("input[name='goal_weight']").val()!="")){
        $("p.check").show();
      }else{
        $("p.check").hide();
      }
  });
  $("select[name='gender']").click( function() {
      $.cookie('gender',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if (($(this).val()!="")&&($("input[name='age']").val()!=""))
      {
        var bmr = get_bmr(current_weight,current_height,$("input[name='age']").val(),version,$(this).val());
        $.cookie('bmr',bmr, { expires: 1,path: '/weight-loss-plan/' });
        $("div.bmr_result .value").html(bmr);
        $("div.result").show();
        $("p.check").show();
      }else{
        $("div.result").hide();
        $("p.check").hide();
      }
  });
  $("input[name='age']").keyup( function() {
      $.cookie('age',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if (($(this).val()!="")&&($("select[name='gender']").val()!=""))
      {
        var bmr = get_bmr(current_weight,current_height,$(this).val(),version,$("select[name='gender']").val());
        $.cookie('bmr',bmr, { expires: 1,path: '/weight-loss-plan/' });
        $("div.bmr_result .value").html(bmr);
        $("div.result").show();
        $("p.check").show();
      }else{
        $("div.result").hide();
        $("p.check").hide();
      }
  });
  $("select[name='activity']").click( function() {
      $.cookie('activity',$(this).val(), { expires: 1,path: '/weight-loss-plan/' });
      if ($(this).val()!="")
      {
        var tdee = get_tdee(bmr_val,$(this).val());
        $.cookie('tdee',tdee, { expires: 1,path: '/weight-loss-plan/' });
        $("div.tdee_result .value").html(tdee);
        $("div.result").show();
        $("p.check").show();
      }else{
        $("div.result").hide();
        $("p.check").hide();
      }
  });
});