/****************************************************************************************
Copyright (c) 2009 Ethicon Endo-Surgery, Inc.(EES).

This software is the confidential and proprietary information of EES. ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with EES.

DOC			REALIZE(tm) BMI Calculator
SCRIPT		JavaScript
VERSION 	1.0
AUTHOR		Doug Scamahorn
EMAIL		dscamahorn@fusionalliance.com

DATE       	NAME           	DESCRIPTON
08/06/2009 	Doug Scamahorn  Initial creation.

****************************************************************************************/

/* =STANDARD FUNCTIONS
---------------------------------------------------------------------------------------*/

	//Manage DOM load event
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
	//Utility function for performing a fade of a background element
	function fadeUp(element, red, green, blue) {
		if (element.fade) {
			clearTimeout(element.fade);
		}
		if (red == 255 && green == 255 && blue == 255) {
			element.style.backgroundColor = "transparent";
			return;
		}
		var newred = red + Math.ceil((255 - red) / 10);
		var newgreen = green + Math.ceil((255 - green) / 10);
		var newblue = blue + Math.ceil((255 - blue) / 10);
		element.style.backgroundColor = "rgb(" + newred + "," + newgreen + "," + newblue + ")";
		element.fade = setTimeout(function () {fadeUp(element, newred, newgreen, newblue);}, 80);
	}

/* =BMI CALCULATOR
---------------------------------------------------------------------------------------*/

	//Initialize the BMI Calculator Widget
	function initializeBMICalcWidget() {
		//Test for browser support of DOM
		if (!document.getElementById) return false;
		//Test for DOM elements
		if(!document.getElementById("bmiCalcWidget")) return false;
		var bmiCalcForm = document.getElementById("bmiCalcForm");
		var vSystem = bmiCalcForm.getAttribute("name");
		//Create Widget Height Error Message
		bmiCalcWidgetErrorHeight = document.createElement("span");	//Global variable accessible outside the function
		if (vSystem == "standard") {
			var bmiCalcWidgetErrorHeightText = document.createTextNode("Please select your height.");
		} else {
			var bmiCalcWidgetErrorHeightText = document.createTextNode("Please enter a number for your height.");
		}
		bmiCalcWidgetErrorHeight.setAttribute("class","cc_formError");
		bmiCalcWidgetErrorHeight.setAttribute("id","bmiCalcWidgetErrorHeight");
		bmiCalcWidgetErrorHeight.appendChild(bmiCalcWidgetErrorHeightText);
		//Create Widget Weight Error Message
		bmiCalcWidgetErrorWeight = document.createElement("span");	//Global variable accessible outside the function
		var bmiCalcWidgetErrorWeightText = document.createTextNode("Please enter a number for your weight.");
		bmiCalcWidgetErrorWeight.setAttribute("class","cc_formError");
		bmiCalcWidgetErrorWeight.setAttribute("id","bmiCalcWidgetErrorWeight");
		bmiCalcWidgetErrorWeight.appendChild(bmiCalcWidgetErrorWeightText);
		//Create the Widget Results - <p id="bmiCalcResults"><strong>Your Current BMI is:</strong><span></span> <strong id="bmiCalcResultValue">Unknown</strong></p>
		bmiCalcResults = document.createElement("p");	//Global variable accessible outside the function
		bmiCalcResults.setAttribute("id","bmiCalcResults");
		var bmiCalcResultsSpan = document.createElement("span");
		var bmiCalcResultsSpanStrong = document.createElement("strong");
		var bmiCalcResultsSpanStrongText = document.createTextNode("Your BMI is:");
		bmiCalcResultValue = document.createElement("strong");	//Global variable accessible outside the function
		var bmiCalcResultValueText = document.createTextNode("Unknown");
		bmiCalcResultValue.setAttribute("id","bmiCalcResultValue");
		bmiCalcResultValue.appendChild(bmiCalcResultValueText);
		bmiCalcResultsSpanStrong.appendChild(bmiCalcResultsSpanStrongText);
		bmiCalcResults.appendChild(bmiCalcResultsSpanStrong);
		bmiCalcResults.appendChild(bmiCalcResultsSpan);
		bmiCalcResults.appendChild(bmiCalcResultValue);
		//Attach the onclick event to the submit button
		var bmiCalcBtnSubmit = document.getElementById("bmiCalcBtnSubmit");
		bmiCalcBtnSubmit.onclick = function() {
			interactWithWidget();
			return false;
		}
	}
	//Function for managing form interaction
	function interactWithWidget() {
		//Gather page information
		var bmiCalcWidget = document.getElementById("bmiCalcWidget");
		var bmiCalcRowHeight = document.getElementById("bmiCalcRowHeight");
		var bmiCalcRowWeight = document.getElementById("bmiCalcRowWeight");
		var bmiCalcForm = document.getElementById("bmiCalcForm");
		var bmiCalcFormFieldsets = bmiCalcForm.getElementsByTagName("fieldset");
		var bmiCalcFormParagraphs = bmiCalcForm.getElementsByTagName("p");
		var vSystem = bmiCalcForm.getAttribute("name");
		//Reset error messaging amd results
		if(document.getElementById("bmiCalcWidgetErrorHeight")) {
			bmiCalcRowHeight.removeChild(bmiCalcWidgetErrorHeight);
		}
		if(document.getElementById("bmiCalcWidgetErrorWeight")) {
			bmiCalcRowWeight.removeChild(bmiCalcWidgetErrorWeight);
		}
		if(document.getElementById("bmiCalcResults")) {
			bmiCalcWidget.removeChild(bmiCalcResults);
		}
		//Gather the Form and it's values
		var vform = bmiCalcForm;
		var vWeight = vform.bmiCalcTxtFldWeight.value;
		var vHeight = vform.bmiCalcSelectHeight.value;
		//Validate that height is selected
		if (!checkValues(vHeight)) {
			bmiCalcRowHeight.appendChild(bmiCalcWidgetErrorHeight);
			vform.bmiCalcSelectHeight.focus();
			return;
		}
		//Validate that weight is input and it is a number
		if (!checkValues(vWeight)) {
			bmiCalcRowWeight.appendChild(bmiCalcWidgetErrorWeight);
			vform.bmiCalcTxtFldWeight.focus();
			return;
		}
		//Perform the calculation
		var vBMI = calculateBMI(vWeight, vHeight, vSystem);
		//Set the BMI Result Value
		if (bmiCalcResultValue.firstChild.nodeType==3) {
			bmiCalcResultValue.firstChild.nodeValue = vBMI;
		}
		//Show the Results
		bmiCalcWidget.appendChild(bmiCalcResults);
		fadeUpResults();
	}
	//Validate the values for Height and Weight are input and numeric
	function checkValues(fieldValue) {
		if (isNaN(parseInt(fieldValue))) {
			return false;
		} else if (fieldValue < 0) {
			return false;
		} else {
			return true;
		}
	}
	//Function for performing BMI calculation
	function calculateBMI(weight, height, system) {
		var vBMIresult;
		//Calculate BMI
		if (system == "metric"){
			//weight (kg) / [height (m)]2
			vBMIresult = Math.round(parseInt(weight) / ((parseInt(height)/100)*(parseInt(height)/100)));
		} else if (system == "standard") {
			//weight (lb) / [height (in)]2 x 703
			vBMIresult = Math.round(parseInt(weight) / (parseInt(height)* parseInt(height)) * 703);
		}
		//Attach identifier
		if (vBMIresult<18) vBMIresult += " - Underweight";
		if (vBMIresult>=18 && vBMIresult<=24) vBMIresult += " - Normal";
		if (vBMIresult>=25 && vBMIresult<=29) vBMIresult += " - Overweight";
		if (vBMIresult>29) vBMIresult += " - Obese";
		//Return results
		return vBMIresult;
	}
	//Function for performing the background fade on the results
	function fadeUpResults() {
		var bmiResultsContainer = document.getElementById("bmiCalcResults");
		fadeUp(bmiResultsContainer, 255, 247, 207);
	}

/* =CALL EVENTS
---------------------------------------------------------------------------------------*/
	
	addLoadEvent(initializeBMICalcWidget);
