﻿function checkNull(src){
	var result = document.getElementById("txtresult");
	if (!src) {
		/*result.value = "Please enter your height in inches and your weight in pounds first before clicking the calculate button."*/
		return false;
	}
    return true;
}
	
		
function calcBMI(height, weight) {	
	return (weight/(Math.pow(height, 2)));
}
		
function converttocm(height) {	
	return (height * 0.025397);
}
		
function converttokg(weight) {
	return (weight * 0.4545);
}
		
function btncalcBMI() {
        
            var height = 0;
            var weight;
            var bmi;
            var result = document.getElementById("txtresult").firstChild;
            var feetElement = document.getElementById("feet");
            var inchesElement = document.getElementById("inches");
            var weightElement = document.getElementById("weight");
            var bmiElement = document.getElementById("txtbmi");
            var surveyResult = document.getElementById("surveyResults");
            var readinessSurvey = document.getElementById("readinesssurvey");
            
            surveyResult.style.display="block";
            
            if (!(checkNull(feetElement.value)) && !(checkNull(inchesElement.value)) && !(checkNull(weightElement.value))) {
                result.nodeValue="Please enter your height in feet and inches, and your weight in pounds";
                return false;
            }
            else if (!(checkNull(inchesElement.value)) && !(checkNull(weightElement.value))) {
                result.nodeValue="Please also enter your height in inches, and your weight in pounds";
                return false;
            }
            else if (!(checkNull(inchesElement.value)) && !(checkNull(feetElement.value))) {
                result.nodeValue="Please also enter your height in feet and inches";
                return false;
            }
            else if (!(checkNull(weightElement.value)) && !(checkNull(feetElement.value))) {
                result.nodeValue="Please also enter your height in feet, and your weight in pounds";
                return false;
            }
            else if (!(checkNull(weightElement.value))) {
                result.nodeValue="Please also enter your weight in pounds";
                return false;
            }
            else if (!(checkNull(inchesElement.value))) {
                result.nodeValue="Please also enter your height in inches";
                return false;
            }
            else if (!(checkNull(feetElement.value))) {
                result.nodeValue="Please also enter your height in feet";
                return false;
            }
            
            if (isNaN(feetElement.value) || feetElement.value<3 || feetElement.value>7){
                return false;
            }
            else if (isNaN(inchesElement.value) || inchesElement.value<0 || inchesElement.value>11.5){
                return false;
            }
            else if (isNaN(weightElement.value) || weightElement.value<0 || weightElement.value>999){
                return false;
            }
            else {
                height = Number(12*feetElement.value);
                height += Number(inchesElement.value);
                height = converttocm(height);
            }

            if (!(checkNull(weightElement.value))) {
                return false;
            }
            else {
                weight = converttokg(weightElement.value);	
            }

            bmi = calcBMI(height, weight);
            
            if (bmi < 18.5) {
                result.nodeValue = "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)) {
                result.nodeValue = "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 < 100)) {
                
                result.nodeValue = "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. Many OPTIFAST Programs offer options to treat people who have a BMI such as yours.  Take the Readiness Survey below to see if OPTIFAST may be right for you at this time.";
            
            	readinessSurvey.style.display = "block";
            }
                
            //b=Math.round(b);
            bmiElement.value = parseInt(bmi);
            
            return false;
}
		
		
		function btncalcReadiness() {
		
		var rsurveyResults = document.getElementById("rsurveyResults");
		
		dcsMultiTrack('DCS.dcsuri','/submit_readiness_survey.do','WT.ti','Submit Survey','WT.si_n','BMI Calculator Funnel','WT.si_x','3');
		
		var readiness = 0;
		var a1 = document.getElementById("a1");
		var a2 = document.getElementById("a2");
		var a3 = document.getElementById("a3");
		var a4 = document.getElementById("a4");
		var a5 = document.getElementById("a5");
		var a6 = document.getElementById("a6");
		var a7 = document.getElementById("a7");
		var a8 = document.getElementById("a8");
		var a9 = document.getElementById("a9");
		var a10 = document.getElementById("a10");
		var a11 = document.getElementById("a11");
		var a12 = document.getElementById("a12");
		var a13 = document.getElementById("a13");
		var a14 = document.getElementById("a14");
		var a15 = document.getElementById("a15");
		var a16 = document.getElementById("a16");
		var a17 = document.getElementById("a17");
		var a18 = document.getElementById("a18");
		var a19 = document.getElementById("a19");
		var a20 = document.getElementById("a20");
		
		var readinessresult = document.getElementById("readinessresult").firstChild;
		
		
		
		if (a1.checked) readiness++;
		if (a2.checked) readiness++;
		if (!a3.checked) readiness++;
		if (!a4.checked) readiness++;
		if (a5.checked) readiness++;
		if (a6.checked) readiness++;
		if (a7.checked) readiness++;
		if (a8.checked) readiness++;
		if (a9.checked) readiness++;
		if (!a10.checked) readiness++;
		if (!a11.checked) readiness++;
		readiness++;
		if (!a13.checked) readiness++;
		if (!a14.checked) readiness++;
		if (a15.checked) readiness++;
		if (a16.checked) readiness++;
		if (a17.checked) readiness++;
		if (!a18.checked) readiness++;
		if (!a19.checked) readiness++;
		if (!a20.checked) readiness++;
		
		if (readiness>11) {
			readinessresult.nodeValue="Your BMI and Readiness Survey results indicate that you may be an appropriate candidate for the OPTIFAST program. To continue your journey to weight loss and improved health, contact an OPTIFAST clinic near you to schedule a free, no-obligation consultation.";
		} else {
		
			readinessresult.nodeValue="OPTIFAST is a serious solution for weight loss, and success requires a serious commitment. Based on your responses to the Readiness Survey, it is not clear that you are ready to make that commitment. An OPTIFAST professional can discuss the best approach to treatment based on your medical condition and motivation level.";
		}
		
		
		
		rsurveyResults.style.display="block";
		return false;

		}