function switchImage(id,image){
  document.getElementById(id).src = image;
}

function newImage(image){
  if (document.images) {
    rslt = new Image();
    rslt.src = image;
    return rslt;
  }
}

function checkValues(v){
  if (isNaN(parseInt(v))){
    return false;
  } else if (v < 0){
    return false;
  } else {
    return true;
  }
}

function addToFavorites(urlAddress,pageName) {
  if (window.sidebar){
    window.sidebar.addPanel(pageName,urlAddress,'');
  } else if (window.external) {
    window.external.AddFavorite(urlAddress,pageName);
  } else {
    alert("Sorry! Your browser doesn't support this function.");
  }
}

startList = function() {
  if (document.all&&document.getElementById) {
    navRoot = document.getElementById("nav");
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" over";
        }
        node.onmouseout=function() {
          this.className=this.className.replace(" over", "");
        }
      }
      for (j=0;j<node.childNodes.length;j++){
        subNode = node.childNodes[j];
        if (subNode.nodeName=="UL"){
          for(k=0;k<subNode.childNodes.length;k++){
            subLI = subNode.childNodes[k];
            if(subLI.nodeName=="LI"){
              subLI.onmouseover=function() {
                this.className+=" over";
              }
              subLI.onmouseout=function() {
                this.className=this.className.replace(" over", "");
              }
            }
          }
        }
      }
    }
  }
}
window.onload=startList;

function cal_bmi(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){
      // Need to bump up the whole thing instead
      f_bmi += 1;
      diff = 0;
   }
   bmi = f_bmi + "." + diff;
   return bmi;
}

function calculateBMI(){
 var f = self.document.forms[0];
 pounds = f.w_pounds.value;
 feet   = f.h_feet.value;
 inches = f.h_inches.value;
 if (!checkValues(pounds)){
   alert("Please enter a number for your weight.");
   return;
 }
 if (!checkValues(inches)){
   inches = 0;
 } else if (inches > 0){
   inches = parseInt(inches);
 }
 
 if (!checkValues(feet)){
   alert("Please enter a number for you height.");
 } else {
   feet_inches = parseInt(feet * 12);
 }
 height = parseInt(feet_inches) + parseInt(inches);
 bmi = cal_bmi(pounds, height);
 document.getElementById("bmiChart").src = "/themes/weightloss/images/bmichart.php?bmi="+bmi;
 document.getElementById("bmiSpan").innerHTML = bmi;
}

function cal_bmr(lbs, ins, age, gender){
  if (gender == 'f'){
    bmr = parseInt(655 + ( 4.35 * lbs ) + ( 4.7 * ins ) - ( 4.7 * age ));
    return bmr;
  } else if (gender == 'm') {
    bmr = parseInt(66 + ( 6.23 * lbs ) + ( 12.7 * ins ) - ( 6.8 * age ));
    return bmr;
  } else {
   alert("No Gender"); 
  }
}

function calculateBMR(){
  var f = self.document.forms[0];
  pounds = f.w_pounds.value;
  feet   = f.h_feet.value;
  inches = f.h_inches.value;
  gender = f.gender.value;
  age    = f.age.value;
  if (!checkValues(age)){
    alert("Please enter a number for your age.");
  }
  if (!checkValues(pounds)){
   alert("Please enter a number for your weight.");
   return;
  }
  if (!checkValues(inches)){
   inches = 0;
  } else if (inches > 0){
   inches = parseInt(inches);
  }
  if (!checkValues(feet)){
   alert("Please enter a number for you height.");
  } else {
   feet_inches = parseInt(feet * 12);
  }
  height = parseInt(feet_inches) + parseInt(inches);
  bmr = cal_bmr(pounds,height,age,gender);
  // alert("Your BMR is " + bmr);
  var f2 = self.document.forms[1];
  document.getElementById("bmrSpan").innerHTML = "Your BMR is " + bmr;
  f2.ex1.value = parseInt(bmr * 1.2);
  f2.ex2.value = parseInt(bmr * 1.375);
  f2.ex3.value = parseInt(bmr * 1.55);
  f2.ex4.value = parseInt(bmr * 1.725);
  f2.ex5.value = parseInt(bmr * 1.9);
}