<!--

// stores the previous valid weight
var weight = null ;

// stores the previous valid height
var height = null ;

//
// Validates the weight
//
function onWeightChanged () {
	// validate new value
	var accept = validateNumber (document.frmCalculator.valWeight.value) ;

	// restore value is new valud is no good
	if (accept == false) {
		if (weight == null) {
			document.frmCalculator.valWeight.value = "" ;
		} else {
			document.frmCalculator.valWeight.value = weight
		}
	} else {
		weight = document.frmCalculator.valWeight.value ;
	}

	calculateBMI () ;
}

//
// Validates the height
//
function onHeightChanged () {
	// validate new value
	var accept = validateNumber (document.frmCalculator.valHeight.value) ;

	// restore value is new valud is no good
	if (accept == false) {
		if (height == null) {
			document.frmCalculator.valHeight.value = "" ;
		} else {
			document.frmCalculator.valHeight.value = height
		}
	} else {
		height = document.frmCalculator.valHeight.value ;
	}

	calculateBMI () ;
}

//
// Calculates the BMI
//
function calculateBMI () {
	if (height == null || height == "") {
		document.frmCalculator.valBMI.value = "" ;
		document.frmCalculator.valResult.value = "Please enter a height!" ;
		return false ;
	}

	// check weight
	if (weight == null || weight == "") {
		document.frmCalculator.valBMI.value = "" ;
		document.frmCalculator.valResult.value = "Please enter your weight!" ;
		return false ;
	}

	// convert weight to kg
	var unitWeight = weight * document.frmCalculator.unitWeight.value ;

	// convert height to cm
	var unitHeight = height * document.frmCalculator.unitHeight.value ;

	// calculate the BMI
	var bmi = Math.floor (unitWeight / (Math.pow (unitHeight, 2))) ;

	// set results
	document.frmCalculator.valBMI.value = bmi ;

	if (bmi < 18.5) {
		document.frmCalculator.valResult.value = "Your weight is less than ideal for your height based on present height/weight tables and weight loss would not be appropriate for you at this time." ;
	} else if ((bmi > 18.4) && (bmi < 24.9)) {
		document.frmCalculator.valResult.value = "Your weight is within a normal range for your height. Concentrate on getting regular exercise and making healthy food choices to prevent weight gain.";
	} else if ((bmi > 24.8)&&(bmi < 29.9)) {
		document.frmCalculator.valResult.value = "Your BMI indicates you may be at an increased risk for developing health problems caused by being overweight. Weight loss could benefit your health. Other factors, such as your medical condition, need to be considered when selecting a treatment plan. Westshore Weight Management Clinic offers options to treat people who have a BMI such as yours. Please visit our PROGRAMS page to learn more.";
	} else if ((bmi > 29.8) && (bmi < 100)) {
		document.frmCalculator.valResult.value = "Your BMI indicates that you may be an appropriate candidate for OPTIFAST treatment. You may or may not have other medical conditions caused by your weight such as diabetes, hypertension, or heart disease. You are certainly at higher risk for developing these types of diseases. It may be very important that your weight loss is medically supervised. Westshore Weight Management Clinic offers options to treat people who have a BMI such as yours. Please visit our PROGRAMS page to learn more.";
        } else {
                document.frmCalculator.valResult.value = "Invalid inputs!" ;
                document.frmCalculator.valBMI.value = "N/A" ;
        }
}

//
// Validates a number and return true if it's a valid number else return false
//
function validateNumber (value) {
	// accepted alphanumberical values
	var alphas = "0123456789." ;

	// total number of points
  	var cPoints = 0 ;

	for (i = 0;  i < value.length;  i++) {
    	var ch = value.charAt (i) ;

		for (j = 0;  j < alphas.length;  j++) {
      		if (ch == alphas.charAt (j)) {
        		break ;
			}
		}

		if (j == alphas.length) {
    		return false ;
		}

		if (ch == ".") {
			if (cPoints >= 1) {
				return false ;
			} else {
				cPoints++ ;
			}
		}
  	}

	return true ;
}
//-->
