//----------------------------------------------------------------------------------------------------------------
// WellTech Calculators
// JavaScript Support File
//
// Author: Steve Thorn steve@welltechsolutions
// Revision history:
//   1.0 11/23/2004 SPT Original release
//   1.10 12/17/2007 SPT totally new interface
//    
// Description: JavaScript support functions for AJAX
//
//----------------------------------------------------------------------------------------------------------------
//
// --- CONFIGURATION SECTION ---
//
var proxy = "WTC_proxy.php"; // Set this to the relative file name of the proxy
//
// --- CUSTOMIZE THIS FUNCTION FOR YOUR PURPOSES ---
//
// Display the results on the web page
// "obj" is an object that contains the calculator results and passed parameters
// e.g. obj.results.bmi, or obj.parameters.username, or obj.error[0][key]
//
function WTC_displayResults(obj)
{
  var displayString = "<span style='color:";
  if (obj.errors) {
    displayString += "red'>";
    // Errors are returned in an array
    for (i=0; i<obj.errors.length; i++)
      for (key in obj.errors[i])
      {
        //displayString += key + ": ";
        displayString += obj.errors[i][key] + "<br>";
      }
    displayString += "</span>";
  } else {
    switch(obj.parameters.calculator)
    {
      case "bmi":
        if (obj.parameters.name)
	        displayString += "blue'>" + obj.parameters.name + ", your BMI value was calculated to be " + obj.results.bmi + ", putting you in the <b>" + obj.results.category + "</b> category.</span>";
        else
	        displayString += "blue'>Your BMI value was calculated to be " + obj.results.bmi + ", putting you in the <b>" + obj.results.category + "</b> category.</span>";
        break;
      case "cal":
        displayString += "blue'>Here is what we calculated based on the information you entered.<br>";
        displayString += "[Note: <i>If a zero value is shown, then insufficient information was entered for that formula</i>]</span>";
        displayString += "<table style='border-collapse: collapse; border:1px solid navy'>";
          displayString += "<tr style='background-color:#88F; color:black; font-weight:bold;'>";
            displayString += "<td>Formula</td>";
            displayString += "<td>Calories</td>";
          displayString += "</tr><tr>";
            displayString += "<td>1. Body Weight calculation:</td>";
            displayString += "<td>" + obj.results.BWint + "</td>";
          displayString += "</tr><tr>";
            displayString += "<td>2. Harris-Benedict calculation:</td>";
            displayString += "<td>" + obj.results.HBint + "</td>";
          displayString += "</tr><tr>";
            displayString += "<td>3. Mifflin-St. Jeor calculation:</td>";
            displayString += "<td>" + obj.results.MSint + "</td>";
          displayString += "</tr><tr style='background-color:#FEE; color:red; font-weight:bold; font-size:125%;'>";
            displayString += "<td>AVERAGE:</td>";
            displayString += "<td>" + obj.results.AVEint + "</td>";
          displayString += "</tr>";
        displayString += "</table>";
        break;
      case "ibw":
        if (obj.parameters.method == "OLD")
	  displayString += "blue'>Your ideal body weight was calculated to be " + obj.results.ibw + " lbs.</span>";
	else
	  displayString += "blue'>Your ideal body weight was calculated to be between " + obj.results.ibwlow + " and " + obj.results.ibwhigh + " lbs.</span>";
        break;
      case "thr":
        displayString += "blue'>We calculate that your target heart rate should be from " + obj.results.ratelo + " to " + obj.results.ratehi + " ";
        displayString += "beats per minute. Note: you should never exceed " + obj.results.ratemax + " bpm (your maximum heart rate).</span>";
        break;
      case "met":
        displayString += "blue'>You burned off " + obj.results.cal + " calories.</span>";
        break;
      case "mtm":
        displayString += "blue'>" + obj.results.cal + " calories, " + obj.results.mil + " miles.</span>";
        break;
      default:
        break;
    }
  }
  if (obj.parameters.result)
    document.getElementById(obj.parameters.result).innerHTML = displayString;
  else
    document.getElementById(obj.parameters.calculator+'Result').innerHTML = displayString;
}


//
// --- DO NOT MODIFY JAVASCRIPT CODE BELOW THIS LINE ---
//
// Create XMLHTTP (AJAX connection) object
var WTC_http = (navigator.appName=="Microsoft Internet Explorer") ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

// Call proxy with form fields
function WTC_ajax(obj, calculator) {
  // Read all form fields into a parameter string
  var parameters = "";
  for (var i=0; i<obj.form.elements.length; i++) {
    var el = obj.form.elements[i];
    if (el.type=="radio" && el.checked==false)
      continue;
    if (el.type=="select-one") {
      if (el.name == 'wtlbs') {
        parameters += "&wtlbs=" + el.options[el.selectedIndex].value;
        continue;
      }
      if (el.name == 'ht') {
        var val = el.options[el.selectedIndex].value;
        var ft = Math.floor(val/12);
        parameters += "&htft=" + ft;
        parameters += "&htin=" + (val - ft*12);
        continue;
      }
    }
    parameters += "&" + el.name + "=" + el.value;
  }
  // Establish the AJAX connection
  WTC_http.open('get', proxy + '?'+parameters);
  WTC_http.onreadystatechange = WTC_ajax_response;
  // Send the AJAX request
  WTC_http.send(null);
  return false;
}
// Handle response from proxy
function WTC_ajax_response() {
  // This function is called several times, but we don't do anything unless the
  // status indicates that the proxy has returned results successfully.
  if (WTC_http.readyState==4 && WTC_http.status==200) {
    // Convert the JSON response string into a JavaScript object
    var jsonObj = eval('(' + WTC_http.responseText + ')');
    WTC_displayResults(jsonObj);
  }
}
