// BMR
// Copyright © 2004 - www.linear-software.com All Rights Reserved
//

function formHandler()
{
  var URL =
    document.form.site.options[document.form.site.selectedIndex].value;
  window.location.href = URL;
}

//-------------------------- AJAX stuff ---------------------------------------

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
    @if (@_jscript_version >= 5)
    try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object 
var script_url = "cgi-bin/bmrcalc.php?"; // The server-side script
var working = false;

function handleHttpResponse() {
  if (http.readyState == 4) {
    working = false;
    if (http.responseText.indexOf('invalid') == -1) {
      results = http.responseText.split(",");
      document.bmr_form.bmr.value    = setComma(parseFloat(results[0]));
      document.bmr_form.bmr_af.value = setComma(parseFloat(results[1]));
      document.bmr_form.bmi.value = results[2];
      working = false;
    }
  }
}


function calculate(url) {
  if (!working) {
    http.open("GET", url, true);
    working = true;
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
  }
}

//----------------------------- Variables -------------------------------------
var POUNDS      = 1;
var KILOGRAMS   = 2;
var INCHES      = 4;
var CENTIMETERS = 5;
var MALE        = 6;
var FEMALE      = 7;

var AF_SEDENTARY = 1.2;
var AF_LIGHT     = 1.375;
var AF_MODERATE  = 1.55;
var AF_VERY      = 1.725;
var AF_EXTRA     = 1.9;

var length_bmr  = INCHES;
var weight_bmr  = POUNDS;
var sex_bmr     = MALE;
var af          = AF_SEDENTARY;

var my_bmr      = 0;

//-----------------------------------------------------------------------------
// Unitsbmr 
//-----------------------------------------------------------------------------
function UnitsBMR()
{
  if (document.bmr_form.units_bmr[0].checked)       
  {
    weight_bmr = POUNDS;
    document.bmr_form.weight.value   =
      roundFloat(document.bmr_form.weight.value   * 2.2, 2);
  }
  else if (document.bmr_form.units_bmr[1].checked)  
  {
    weight_bmr = KILOGRAMS;
    document.bmr_form.weight.value   =
      roundFloat(document.bmr_form.weight.value   / 2.2, 2);
  }
}

//-----------------------------------------------------------------------------
// LengthBMR 
//-----------------------------------------------------------------------------
function LengthBMR()
{
  if (document.bmr_form.length_bmr[0].checked)       
  {
    length_bmr = INCHES;
    document.bmr_form.height.value = roundFloat(document.bmr_form.height.value
        / 2.54, 2);
  }                    
  else if (document.bmr_form.length_bmr[1].checked)  
  {
    length_bmr = CENTIMETERS;
    document.bmr_form.height.value = roundFloat(document.bmr_form.height.value
        * 2.54, 2);
  }
}

//-----------------------------------------------------------------------------
// Units_Sex
//-----------------------------------------------------------------------------
function Units_Sex()
{
  if (document.bmr_form.units_sex[0].checked)       
  {
    sex_bmr = MALE;
  }                    
  else if (document.bmr_form.units_sex[1].checked)  
  {
    sex_bmr = FEMALE;
  }
}

//-----------------------------------------------------------------------------
// RadioAF
//-----------------------------------------------------------------------------
function RadioAF()
{
  if (document.bmr_form.radio_af[0].checked)       
  {
    af = AF_SEDENTARY;
  }                    
  else if (document.bmr_form.radio_af[1].checked)  
  {
    af = AF_LIGHT;
  }
  else if (document.bmr_form.radio_af[2].checked)  
  {
    af = AF_MODERATE;
  }
  else if (document.bmr_form.radio_af[3].checked)  
  {
    af = AF_VERY;
  }
  else if (document.bmr_form.radio_af[4].checked)  
  {
    af = AF_EXTRA;
  }
}


//-----------------------------------------------------------------------------
// resetBMR - Clear out all fields for BMR
//-----------------------------------------------------------------------------
function resetBMR()
{
  document.bmr_form.height.value = 0;
  document.bmr_form.weight.value = 0;
  document.bmr_form.age.value    = 0;
}     

//-----------------------------------------------------------------------------
// checkFieldsBMR - Make sure all fields are populated before calculating BMR
//-----------------------------------------------------------------------------
function checkFieldsBMR()
{
  var errorField = false;

  if (!(IsNumber(document.bmr_form.height.value))) 
  {
    alert("You must enter a valid number for Height.\n");
    errorField = true;
  }

  if (!(IsNumber(document.bmr_form.weight.value))) 
  {
    alert("You must enter a valid number for Weight.\n");
    errorField = true;
  }

  if (!(IsNumber(document.bmr_form.age.value))) 
  {
    alert("You must enter a valid number for Age.\n");
    errorField = true;
  }

  if (errorField) 
    return false;

  return true;
}

//-----------------------------------------------------------------------------
// calculateBMR_BMI- Calculate BMR/BMI
//-----------------------------------------------------------------------------
function calculateBMR_BMI() 
{
  var weight = parseFloat(document.bmr_form.weight.value);
  var height = parseFloat(document.bmr_form.height.value);

  var bmr_height = height;
  var bmi_height = height;
  var bmr_weight = weight;
  var bmi_weight = weight;

  if (length_bmr == INCHES)
  {
    bmr_height *= 2.54;
  } else {
    bmi_height /= 2.54;
  }

  if (weight_bmr == POUNDS)
  {
    bmr_weight = weight / 2.20462;
  } else {
    bmi_weight = weight * 2.2;
  }

  url = script_url;
  if (sex_bmr == MALE) {
    url += "sex=m";
  } else {
    url += "sex=f";
  }
  url += "&age=" + escape(document.bmr_form.age.value);
  url += "&af=" + af;
  url += "&bmr_weight=" + bmr_weight;
  url += "&bmr_height=" + bmr_height;
  url += "&bmi_weight=" + bmi_weight;
  url += "&bmi_height=" + bmi_height;

  calculate(url);
} 

