﻿<!-- // Copyright 2006 - Antonio Zamora
// These functions are used in MACRONUTRIENTS.HTML

// usda() is called when the USDA button is pressed
function usda(){
  var f = document.forms[0]; 
  var k, cp;
  f.kcal.value = 2000;   // 2000-kcal diet
  f.fiberg.value = 25;   // 25g fiber
  f.protpct.value = 15;  // 15% protein
  f.fatpct.value = 30;   // 30% fat
  cp = 100-f.protpct.value-f.fatpct.value;   // carb percent by difference
  document.getElementById('carbpct').innerHTML=cp;
  f.protg.value = parseInt( (f.protpct.value*f.kcal.value)/400) ;  // grams of protein
  f.fatg.value = parseInt( (f.fatpct.value*f.kcal.value)/900 );    // grams of fat
  k = parseInt(((cp*f.kcal.value)/400)) + parseInt(f.fiberg.value);
  f.carbg.value =  k;  // grams of carb
  return;
}
// zone() is called when the Zone button is pressed
function zone(){
  var f = document.forms[0]; 
  var k, cp;
  f.kcal.value = 2000;   // 2000-kcal diet
  f.fiberg.value = 25;   // 25g fiber
  f.protpct.value = 30;  // 30% protein
  f.fatpct.value = 30;   // 30% fat
  cp = 100-f.protpct.value-f.fatpct.value;   // carb percent by difference
  document.getElementById('carbpct').innerHTML=cp;
  f.protg.value = parseInt( (f.protpct.value*f.kcal.value)/400) ;  // grams of protein
  f.fatg.value = parseInt( (f.fatpct.value*f.kcal.value)/900 );    // grams of fat
  k = parseInt(((cp*f.kcal.value)/400)) + parseInt(f.fiberg.value);
  f.carbg.value =  k;  // grams of carb
  return;
}

// wch() is called when there is a change in weight for
//   protein, fat, carb, or fiber
function wch(){
  var f = document.forms[0]; 
  var p, fat, c, fib, tot, cp;
  // check P,F,C & fiber
  if (!chkw(f.protg.value) || !chkw(f.fatg.value) || !chkw(f.carbg.value) || !chkw(f.fiberg.value) ) {
     alert("Invalid data in weight fields.");
     return;
  }
  // update Energy and percentages
  p = parseInt(f.protg.value)*4;   // protein calories
  fat = parseInt(f.fatg.value)*9;  // fat calories
  c = parseInt(f.carbg.value);
  fib = parseInt(f.fiberg.value);
  c = (c - fib)*4;  // carb calories exclude fiber content
  tot = p+fat+c;  // Total energy (kcal)

  f.kcal.value = tot;  // display Energy
  f.protpct.value =  rounder( (100*p)/tot);  // percent protein
  f.fatpct.value = rounder( (100*fat)/tot);  // percent fat
  cp = rounder((100*c)/tot);                 // percent carbs
  document.getElementById('carbpct').innerHTML=cp;  
  if (fat < 135) {  // less than 15 grams of fat
    alert("Fat is a necessary nutrient.  Your value is too low.");
  }
  if (p < 150) {  // less than 37.5 grams of protein
    alert("Protein is a necessary nutrient.  Your value is too low.");
  }  
  return;
}

// pctch() is called when there is a change in the Energy, or the percentages of 
//  protein, fat, or carbs. 
//  A change in carbs basically does nothing (resets carb % to previous value)
function pctch(){
  var f = document.forms[0]; 
  var k, p, fat, cp;
  if (!chkw(f.protpct.value) || !chkw(f.fatpct.value) || !chkw(f.kcal.value) ) {
     alert("Invalid data in Energy or percent fields.");
     return;
  }
  if (f.protpct.value > 100 ||  f.fatpct.value > 100 ) {
     alert("Percentages should not exceed 100.");
     return;
  }  
  k = parseInt(f.protpct.value) + parseInt(f.fatpct.value);
  if (k > 100) {
     alert("The sum of Protein and Fat percentages should not exceed 100 ");
  }   
  // calculate carb percent
  cp = rounder( 100-f.protpct.value-f.fatpct.value);  
  if (cp < 0) {cp = 0;}
  document.getElementById('carbpct').innerHTML=cp;
  p = (f.protpct.value*f.kcal.value)/100;  // protein calories
  f.protg.value = parseInt( p/4) ;
  fat = (f.fatpct.value*f.kcal.value)/100;  // fat calories
  f.fatg.value = parseInt( fat/9 );   
  k = parseInt(((cp*f.kcal.value)/400)) + parseInt(f.fiberg.value);
  f.carbg.value =  k;  // total carb value includes weight of fiber
  if (fat < 135) {  // less than 15 grams of fat
    alert("Fat is a necessary nutrient.  Your value is too low.");
  }
  if (p < 150) {  // less than 37.5 grams of protein
    alert("Protein is a necessary nutrient.  Your value is too low.");
  } 
  return;
}

// rounder(x) - provides precision of one decimal place.
function rounder(x) {
  var x1;
   x = x + 0.05;  // round to tenths
   f_bmi = Math.floor(x);
   diff  = Math.floor((x - f_bmi)*10);
   x1 = f_bmi + "." + diff;
  return(x1);
}

// chkw(w) - check input argument w to see if it is a numeric
function chkw(w){
  if (isNaN(parseFloat(w))){
    return false;
  } 
  if (isNaN(w)){
    return false;
  }  
  if (w < 0){
    return false;
  }
  return true;
}
