// BMI Calculator Script

function CalcIt(form) {
	var Age = Number(form.Years.value);

	var weight =  Number(form.wt.value);

	if (!checkWeight(weight)) return false;
	if (form.wu.selectedIndex == 0) {	// 0 = weight in lbs
		kg = weight * 0.45359237;
	} else {								// 1 = weight in kg.
		kg = weight;
	}
	if (kg < 10) {
		alert("Weights should be heavier than 10 kilograms (22 pounds).");
		return false;
	}
	if (kg > 200) {
		alert("Weights should be lighter than 200 kilograms (441 pounds).");
		return false;
	}


	var height =  Number(form.ht.value);

	if ((isNaN(height)) || (height == null)  || (height == "") || (height < 0)) {
		feetAndInches(form);
		height =  Number(form.ht.value);
	}

//	if (!checkHeight(height)) return false;
	if (form.hu.selectedIndex == 0) {		//  if height units are "inches"
		heightInches = height;
		heightMeters = height * 2.54 / 100;
	} else {									// else if height units are "cm".
		heightInches = height / 2.54;
		heightMeters = height / 100;
	}
	if (heightMeters < 0.33) {
		alert("Heights should be taller than 33 centimeters (31.5 inches).");
		return false;
	}
	if (heightMeters > 2.41) {
		alert("Heights should be shorter than 241 centimeters ( 7 feet, 11 inches).");
		return false;
	}
	setFeetAndInches(form,heightInches);

	if ((isNaN(Age)) || (Age == null)  || (Age == "")) {
		Age = GetAge(form);
		;
		if (form.AgeCat.selectedIndex == 26) {
			alert("Please enter a specific age for a child.");
			return false;
		}
	} else {
		if ( Age < 1 ) {
			alert("Ages younger than 1 year old are too young for this calculation. Sorry.");
			return false;
		} else {
			if (Age > 120) {
				alert("All ages from 70 to 120 are treated as age 75.");
				return false;
			}
		}
		if (Age > 18) {
			form.AgeCat.selectedIndex = 25;	// Set menu to "Adult"
		} else {
			form.AgeCat.selectedIndex = 26;	// Set menu to "Child".
		}
	}



	// Calculate BMI
	bmi = kg / Math.pow(heightMeters,2);
 	form.bmi.value = rounding(bmi,1);
 
	// Calculate Lean Body Weight and Ideal Body Weight
	if (form.Gender.selectedIndex == 1)  {  // sex is female.
		leanConvert = 148;
		idealConvert = 45.5;
	} else {										// sex is male.
		leanConvert = 128;
		idealConvert = 50; 
	}
	var leanKg = 1.1 * kg - leanConvert * (Math.pow(kg,2) / Math.pow((100 * heightMeters),2));
	var idealKg = idealConvert + 2.3 * (heightInches - 60);
	if (form.wu.selectedIndex == 0) {	// 0 = weight in lbs.
		form.lean.value = Math.round(leanKg * 2.2046226) + " lbs";
//		form.ideal.value = Math.round(idealKg * 2.2046226) + " lbs";
	} else {
		form.lean.value = Math.round(leanKg) + " kg";
//		form.ideal.value = Math.round(idealKg) + " kg";
	}

	if (form.wu.selectedIndex == 0) {  // 0 = weight in lbs.
		if (form.Gender.selectedIndex == 1) {  // sex is female.
			if (Age > 18) {
				form.ideal.value = Math.round((3.1099 * heightInches) - 75.626) + " - " + Math.round((3.2352 * heightInches) - 70.189) + " lbs";
			} else {  // age < 18
				form.ideal.value = Math.round(0.0684*heightInches*heightInches - 3.8156*heightInches + 78.145) + " lbs";
			}
		} else {										// sex is male.
			if (Age > 18) {
				form.ideal.value = Math.round((2.8929 * heightInches) - 50.607) + " - " + Math.round((3.2571 * heightInches) - 63.476) + " lbs";
			} else {  // age < 18
				form.ideal.value = Math.round(0.074*heightInches*heightInches - 4.3748*heightInches + 90.237) + " lbs";
			}
		}
	} else {									// 1 = weight in kg.
		if (form.Gender.selectedIndex == 1) {  // sex is female.
			if (Age > 18) {
				form.ideal.value = Math.round((1.4106 * heightInches) - 34.304) + " - " + Math.round((1.4674 * heightInches) - 31.837) + " kgs";
			} else {  // age < 18
				form.ideal.value = Math.round(0.031*heightInches*heightInches - 1.7307*heightInches + 35.446) + " kgs";
			}
		} else {										// sex is male.
			if (Age > 18) {
				form.ideal.value = Math.round((1.3122 * heightInches) - 22.955) + " - " + Math.round((1.4774 * heightInches) - 28.792) + " kgs";
			} else { // age < 18
				form.ideal.value = Math.round(0.0336*heightInches*heightInches - 1.9844*heightInches + 40.93) + " kgs";
			}
		}
	}



	// Set Body Description.

	var interp = "Healthy Weight For Most Adults:\n\r According to medical experts, your weight falls into the healthy range, it represents the lowest level of risk for heart disease, diabetes and hypertension. To further reduce your risk, focus on healthy eating with a menu that is low in saturated fat, and rich in fruits, vegetables and whole grains, as well as staying active for life. Congratulations to you if you have recently reduced your weight to this level staying committed to your healthy lifestyle will help you maintain both your weight and your well-being.";
	var oldAdj = 0;
	
	if (Age > 64.9) {
		oldAdj = 3;
	} else { 
		if (Age > 54.9) {
			oldAdj = 2;
		} else {
			if (Age > 44.9) {
				oldAdj = 1;
			}
		}
	}

	if (Age < 20) {	// Use W/H^2.88 instead of bmi for children.  
		var whkids = kg / Math.pow(heightMeters,2.88);
		if (form.Gender.selectedIndex == 1) {	// sex is female.
			if (Age < 12) {
				if (whkids > 15.82) {		// over 95th percentile. 97%ile = 16.33
					interp = "Very Overweight:\n\r Assuming you do not have a high level of muscle mass or are extremely tall and carry a large skeletal frame* your weight falls into the very overweight range. From a health standpoint, this increases your risk for heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 240. The good news is that by losing as little as 10% of your current weight, you can reduce your risk for all these conditions. If you already have one of these conditions, you can bring it into better control.\n\rA combination of healthy eating, an active lifestyle, and a positive mindset can help you reach a personally realistic goal.\n\rTo stay motivated to reach and maintain your goals, look for One-on-One support.";
				} else {
					if (whkids > 14.17) {		// over 85th percentile. 90%ile = 14.55
						interp = "Overweight: \n\r Assuming you do not have extra muscle mass or a large skeletal frame* your weight falls into the overweight range. This means you have a slightly increased risk for health problems like heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 200. The good news is that through healthy eating and regular physical activity, you can reduce both your weight and disease risks. And to maintain your success, focus on consistent, small changes that you can build into your lifestyle. \n\r \n\r *Generally, BMI is an accurate reflection of body fat. However, it is less accurate if you are pregnant or breastfeeding, a competitive or body-building athlete, have a high level of muscle, or skeletal mass or have a chronic health condition. Your individual history and consultation with your personal physician can be helpful.";				// 75%ile = 13.4
					} else {
						if (whkids < 10.23) {		// under 5th percentile.  97%ile = 10.08
							interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";
						} else {
							if (whkids < 10.85) {		// under 15th percentile. 90%ile = 10.61
								interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";    // under 75%ile = 11.34
							}
						}
					}
				}
			} else {	// post-pubertal girls.
				if (whkids > 15.80) {		// over 95th percentile. 97%ile = 15.89
					interp = "Very Overweight:\n\r Assuming you do not have a high level of muscle mass or are extremely tall and carry a large skeletal frame* your weight falls into the very overweight range. From a health standpoint, this increases your risk for heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 240. The good news is that by losing as little as 10% of your current weight, you can reduce your risk for all these conditions. If you already have one of these conditions, you can bring it into better control.\n\rA combination of healthy eating, an active lifestyle, and a positive mindset can help you reach a personally realistic goal.\n\rTo stay motivated to reach and maintain your goals, look for One-on-One support.";
				} else {
					if (whkids > 15.21) {		// over 85th percentile.  90%ile = 15.58
						interp = "Overweight: \n\r Assuming you do not have extra muscle mass or a large skeletal frame* your weight falls into the overweight range. This means you have a slightly increased risk for health problems like heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 200. The good news is that through healthy eating and regular physical activity, you can reduce both your weight and disease risks. And to maintain your success, focus on consistent, small changes that you can build into your lifestyle. \n\r \n\r *Generally, BMI is an accurate reflection of body fat. However, it is less accurate if you are pregnant or breastfeeding, a competitive or body-building athlete, have a high level of muscle, or skeletal mass or have a chronic health condition. Your individual history and consultation with your personal physician can be helpful.";		// 75%ile = 14.46
					} else {
						if (whkids < 11.23) {		// under 5th percentile.  97%ile = 11.06
							interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";
						} else {
							if (whkids < 12.01) {		// under 15th percentile.  90%ile = 11.64
								interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";		// 25%ile = 12.74
							}
						}
					}
				}
			} 
		} else {									// sex is male.
			if (whkids > 15.77) {		// over 95th percentile.  97%ile = 16.16
				interp = "Very Overweight:\n\r Assuming you do not have a high level of muscle mass or are extremely tall and carry a large skeletal frame* your weight falls into the very overweight range. From a health standpoint, this increases your risk for heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 240. The good news is that by losing as little as 10% of your current weight, you can reduce your risk for all these conditions. If you already have one of these conditions, you can bring it into better control.\n\rA combination of healthy eating, an active lifestyle, and a positive mindset can help you reach a personally realistic goal.\n\rTo stay motivated to reach and maintain your goals, look for One-on-One support.";
			} else {
				if (whkids > 14.45) {		// over 85th percentile.  90%ile = 14.81
					interp = "Overweight: \n\r Assuming you do not have extra muscle mass or a large skeletal frame* your weight falls into the overweight range. This means you have a slightly increased risk for health problems like heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 200. The good news is that through healthy eating and regular physical activity, you can reduce both your weight and disease risks. And to maintain your success, focus on consistent, small changes that you can build into your lifestyle. \n\r \n\r *Generally, BMI is an accurate reflection of body fat. However, it is less accurate if you are pregnant or breastfeeding, a competitive or body-building athlete, have a high level of muscle, or skeletal mass or have a chronic health condition. Your individual history and consultation with your personal physician can be helpful.";		// 75%ile = 13.73
				} else {
					if (whkids < 10.65) {		// under 5th percentile.  97%ile = 10.46
						interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";
					} else {
						if (whkids < 11.36) {		// under 15th percentile.  90%ile = 11.14
							interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";		// 25%ile = 11.81
						}
					}
				}
			}
		}
	} else {		// Use bmi for adults over 19 year old.
	if (form.Gender.selectedIndex == 1)  {  // sex is female.
		if (bmi < (19.1+oldAdj)) {
			interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";
		} else {
			if (bmi < (25.8+oldAdj)) {
				interp = "in Normal Range";
			} else {
				if (bmi < (27.3+oldAdj)) {
					interp = "txt.marginal";
				} else {
					if (bmi < (32.2+oldAdj)) {
						interp = "Overweight: \n\r Assuming you do not have extra muscle mass or a large skeletal frame* your weight falls into the overweight range. This means you have a slightly increased risk for health problems like heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 200. The good news is that through healthy eating and regular physical activity, you can reduce both your weight and disease risks. And to maintain your success, focus on consistent, small changes that you can build into your lifestyle. \n\r \n\r *Generally, BMI is an accurate reflection of body fat. However, it is less accurate if you are pregnant or breastfeeding, a competitive or body-building athlete, have a high level of muscle, or skeletal mass or have a chronic health condition. Your individual history and consultation with your personal physician can be helpful.";
					} else {
						interp = "Very Overweight:\n\r Assuming you do not have a high level of muscle mass or are extremely tall and carry a large skeletal frame* your weight falls into the very overweight range. From a health standpoint, this increases your risk for heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 240. The good news is that by losing as little as 10% of your current weight, you can reduce your risk for all these conditions. If you already have one of these conditions, you can bring it into better control.\n\rA combination of healthy eating, an active lifestyle, and a positive mindset can help you reach a personally realistic goal.\n\rTo stay motivated to reach and maintain your goals, look for One-on-One support.";
					}
				}
			}
		}
	} else {										// sex is male.
		if (bmi < (20.7+oldAdj)) {
			interp = "Underweight:\n\rIf you have a genetic predisposition to leanness, or if you are a competitive runner or other endurance athlete, and you are a healthy individual, a BMI in this range may be appropriate. Even if you are genetically thin, it is still important to focus on healthy eating habits and a variety of physical activities. However, intentional weight loss to reach this range if it's not your norm is not advised as it places you at risk for malnutrition and offers no additional health benefits.";
		} else {
			if (bmi < (26.4+oldAdj)) {
				interp = "in Normal Range";
			} else {
				if (bmi < (27.8+oldAdj)) {
					interp = "Marginally Overweight";
				} else {
					if (bmi < (31.1+oldAdj)) {
						interp = "Overweight: \n\r Assuming you do not have extra muscle mass or a large skeletal frame* your weight falls into the overweight range. This means you have a slightly increased risk for health problems like heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 200. The good news is that through healthy eating and regular physical activity, you can reduce both your weight and disease risks. And to maintain your success, focus on consistent, small changes that you can build into your lifestyle. \n\r \n\r *Generally, BMI is an accurate reflection of body fat. However, it is less accurate if you are pregnant or breastfeeding, a competitive or body-building athlete, have a high level of muscle, or skeletal mass or have a chronic health condition. Your individual history and consultation with your personal physician can be helpful.";
					} else {
						interp = "Very Overweight:\n\r Assuming you do not have a high level of muscle mass or are extremely tall and carry a large skeletal frame* your weight falls into the very overweight range. From a health standpoint, this increases your risk for heart disease, diabetes and hypertension. For example, your level of risk is similar to that of an individual with a serum cholesterol level of 240. The good news is that by losing as little as 10% of your current weight, you can reduce your risk for all these conditions. If you already have one of these conditions, you can bring it into better control.\n\rA combination of healthy eating, an active lifestyle, and a positive mindset can help you reach a personally realistic goal.\n\rTo stay motivated to reach and maintain your goals, look for One-on-One support.";
					}
				}
			}
		}
 	}
	if (bmi < 10 || bmi > 50) interp = "Check your numbers";
	}
	form.interp.value = interp;




	// Prepare vars for percentile calculations.
	var p05 = 0;
	var p10 = 0;
	var p25 = 0;
	var p50 = 0;
	var p75 = 0;
	var p90 = 0;
	var p95 = 0;
	var a3 = Age*Age*Age;
	var a2 = Age*Age;
	var m = 0;
	var b = 0;
	var theP = 0;
	var cm = heightMeters * 100;

	// Calculate height percentiles based on Age.
	if (form.Gender.selectedIndex == 0) { // 0 = boys.
		if (Age < 14) {	// for boys
			p05 = 0.0194*a3 - 0.5474*a2 + 9.9211*Age + 63.662;	//	y = 0.0194x3 - 0.5474x2 + 9.9211x + 63.662
			p10 = 0.0205*a3 - 0.5777*a2 + 10.340*Age + 63.884;	//	y = 0.0205x3 - 0.5777x2 + 10.340x + 63.884
			p25 = 0.0243*a3 - 0.6607*a2 + 11.060*Age + 64.520;	//	y = 0.0243x3 - 0.6607x2 + 11.060x + 64.520
			p50 = 0.0265*a3 - 0.7214*a2 + 11.865*Age + 64.940;	//	y = 0.0265x3 - 0.7214x2 + 11.865x + 64.94
			p75 = 0.0254*a3 - 0.6807*a2 + 11.695*Age + 67.605;	//	y = 0.0254x3 - 0.6807x2 + 11.695x + 67.605
			p90 = 0.0203*a3 - 0.5317*a2 + 10.667*Age + 72.121;	//	y = 0.0203x3 - 0.5317x2 + 10.667x + 72.121
			p95 = 0.0163*a3 - 0.4320*a2 + 10.022*Age + 75.195;	//	y = 0.0163x3 - 0.4320x2 + 10.022x + 75.195
		} else {	// Ages 14 to 18.
			if (Age < 18) {
				p05 = -0.2089*a3 + 9.0951*a2 - 125.48*Age + 695.81;	//	y = -0.2089x3 + 9.0951x2 - 125.48x + 695.81
				p10 = -0.1736*a3 + 7.4352*a2 - 99.659*Age + 565.85;	//	y = -0.1736x3 + 7.4352x2 - 99.659x + 565.85
				p25 = -0.1431*a3 + 5.9303*a2 - 75.321*Age + 441.63;	//	y = -0.1431x3 + 5.9303x2 - 75.321x + 441.63
				p50 = -0.0721*a3 + 2.5753*a2 - 23.241*Age + 181.33;	//	y = -0.0721x3 + 2.5753x2 - 23.241x + 181.33
				p75 = -0.0127*a3 - 0.1893*a2 + 19.096*Age - 26.852;	//	y = -0.0127x3 - 0.1893x2 + 19.096x - 26.852
				p90 =  0.0476*a3 - 3.0191*a2 + 62.800*Age - 244.25;	//	y =  0.0476x3 - 3.0191x2 + 62.800x - 244.25
				p95 =  0.0342*a3 - 2.4648*a2 + 55.316*Age - 208.71;	//	y =  0.0342x3 - 2.4648x2 + 55.316x - 208.71
			} else {	// Ages over 18.
				p05 = -0.00056*a2    + 0.029184*Age   + 165.79;		// y = -0.00056x2 + 0.029184x + 165.79
				p10 = -0.0001999*a2  - 0.017192*Age   + 169.26;		// y = -0.0001999x2 - 0.017192x + 169.26
				p25 = -0.00020089*a2 - 0.02219*Age    + 173.15;		// y = -0.0002x2 - 0.0222x + 173.15
				p50 = -0.00021406*a2 - 0.019145*Age   + 177.38;		// y = -0.00021406x2 - 0.019145x + 177.38
				p75 = -0.00027718*a2 - 0.0063148*Age  + 181.55;		// y = -0.00027718*a2 - 0.0063148*Age + 181.55
				p90 = -0.00032888*a2 + 0.00064673*Age + 185.44;		// y = -0.0003x2 + 0.0006x + 185.44
				p95 = -0.00047454*a2 + 0.01074*Age    + 187.78;		// y = -0.00047454x2 + 0.01074x + 187.78
			}
		}
	} else {	// 1 = girls.
		if (Age < 12) {	// for girls
			p05 =  0.0243*a3 - 0.5860*a2 + 9.8384*Age + 63.451;		//	y = 0.0243x3 - 0.5860x2 + 9.8384x + 63.451
			p10 =  0.0263*a3 - 0.6341*a2 + 10.352*Age + 63.232;		//	y = 0.0263x3 - 0.6341x2 + 10.352x + 63.232
			p25 =  0.0214*a3 - 0.5393*a2 + 10.163*Age + 65.105;		//	y = 0.0214x3 - 0.5393x2 + 10.163x + 65.105
			p50 =  0.0252*a3 - 0.6313*a2 + 11.023*Age + 65.845;		//	y = 0.0252x3 - 0.6313x2 + 11.023x + 65.845
			p75 =  0.0150*a3 - 0.4146*a2 + 9.8715*Age + 70.412;		//	y = 0.0150x3 - 0.4146x2 + 9.8715x + 70.412
			p90 =  0.0062*a3 - 0.2181*a2 + 8.8153*Age + 74.425;		//	y = 0.0062x3 - 0.2181x2 + 8.8153x + 74.425
			p95 =  0.0048*a3 - 0.1831*a2 + 8.6498*Age + 76.294;		//	y = 0.0048x3 - 0.1831x2 + 8.6498x + 76.294
		} else {	// Ages 12 to 18.
			if (Age < 18) {
				p05 =  0.0703*a3 - 3.5675*a2 + 61.196*Age - 202.19;		//	y = 0.0703x3 - 3.5675x2 + 61.196x - 202.19
				p10 =  0.0798*a3 - 4.0209*a2 + 68.248*Age - 235.42;		//	y = 0.0798x3 - 4.0209x2 + 68.248x - 235.42
				p25 =  0.1036*a3 - 5.0946*a2 + 84.021*Age - 306.46;		//	y = 0.1036x3 - 5.0946x2 + 84.021x - 306.46
				p50 =  0.0857*a3 - 4.3137*a2 + 72.758*Age - 248.39;		//	y = 0.0857x3 - 4.3137x2 + 72.758x - 248.39
				p75 =  0.0702*a3 - 3.6496*a2 + 63.391*Age - 200.49;		//	y = 0.0702x3 - 3.6496x2 + 63.391x - 200.49
				p90 =  0.0570*a3 - 3.0762*a2 + 55.138*Age - 156.94;		//	y = 0.057x3 - 3.0762x2 + 55.138x - 156.94
				p95 =  0.0690*a3 - 3.6052*a2 + 62.739*Age - 190.30;		//	y = 0.069x3 - 3.6052x2 + 62.739x - 190.3
			} else { // Ages over 18.
				p05 = -0.00081401*a2 + 0.043692*Age + 152.54;		// y = -0.0008x2 + 0.0437x + 152.54
				p10 = -0.00057890*a2 + 0.015437*Age + 155.36;		// y = -0.0006x2 + 0.0154x + 155.36
				p25 = -0.001078*a2   + 0.06457*Age  + 158.08;		// y = -0.0011x2 + 0.0646x + 158.08
				p50 = -0.0010755*a2  + 0.068967*Age + 162.21;		// y = -0.0010755x2 + 0.068967x + 162.21
				p75 = -0.00084927*a2 + 0.063996*Age + 166.4;			// y = -0.00084927x2 + 0.063996x + 166.4
				p90 = -0.00077974*a2 + 0.058524*Age + 170.35;		// y = -0.00077974x2 + 0.058524x + 170.35
				p95 = -0.0013401*a2  + 0.10838*Age  + 172;			// y = -0.0013401x2 + 0.10838x + 172
			}
		}
	}
	if (cm < p10) {
		m = (10-5) / (p10-p05);
		b = 5 - (m * p05);
	} else {
		if (cm < p25) {
			m = (25-10) / (p25-p10);
			b = 10 - (m * p10);
		} else {
			if (cm < p50) {
				m = (50-25) / (p50-p25);
				b = 25 - (m * p25);
			} else {
				if (cm < p75) {
					m = (75-50) / (p75-p50);
					b = 50 - (m * p50);
				} else {
					if (cm < p90) {
						m = (90-75) / (p90-p75);
						b = 75 - (m * p75);
					} else {	// 90th percentile or higher.
						m = (95-90) / (p95-p90);
						b = 90 - (m * p90);
					}
				}
			}
		}	
	}
	theP = m * cm + b;	// Don't forget to truncate >98% or < 2% values.

	form.cmP.value = SetPercentile( rounding( theP,0) );


	// Now calculate weight percentiles based on Age.
	if (form.Gender.selectedIndex == 0) { // 0 = boys.
		if (Age < 13) {	// for boys
			p05 = 0.0151*a3 - 0.2543*a2 + 2.9269*Age + 5.2554;	//	y = 0.0151x3 - 0.2543x2 + 2.9269x + 5.2554
			p10 = 0.0164*a3 - 0.2730*a2 + 3.0951*Age + 5.4301;	//	y = 0.0164x3 - 0.273x2 + 3.0951x + 5.4301
			p25 = 0.0168*a3 - 0.2520*a2 + 3.0074*Age + 6.4072;	//	y = 0.0168x3 - 0.252x2 + 3.0074x + 6.4072
			p50 = 0.0162*a3 - 0.2086*a2 + 2.8620*Age + 7.5723;	//	y = 0.0162x3 - 0.2086x2 + 2.862x + 7.5723
			p75 = 0.0130*a3 - 0.0842*a2 + 2.1322*Age + 9.7301;	//	y = 0.013x3 - 0.0842x2 + 2.1322x + 9.7301
			p90 = 0.0039*a3 + 0.1614*a2 + 0.7913*Age + 12.873;	//	y = 0.0039x3 + 0.1614x2 + 0.7913x + 12.873
			p95 = 0.00001036*a3 + 0.26585*a2 + 0.48998*Age + 13.7581;  // y = -1E-05x3 + 0.2659x2 + 0.49x + 13.758
		} else {	// Ages 13 to 18.
			if (Age < 18) {
				p05 = -0.0949*a3 + 4.1266*a2 - 54.814*Age + 257.13;	//	y = -0.0949x3 + 4.1266x2 - 54.814x + 257.13
				p10 = -0.1163*a3 + 5.0742*a2 - 68.248*Age + 320.82;	//	y = -0.1163x3 + 5.0742x2 - 68.248x + 320.82
				p25 = -0.1009*a3 + 4.3194*a2 - 55.903*Age + 258.30;	//	y = -0.1009x3 + 4.3194x2 - 55.903x + 258.3
				p50 = -0.1002*a3 + 4.2414*a2 - 53.824*Age + 248.29;	//	y = -0.1002x3 + 4.2414x2 - 53.824x + 248.29
				p75 = -0.1198*a3 + 4.9664*a2 - 62.023*Age + 282.18;	//	y = -0.1198x3 + 4.9664x2 - 62.023x + 282.18
				p90 = -0.0396*a3 + 1.6386*a2 - 16.128*Age + 78.991;	//	y = -0.0396x3 + 1.6386x2 - 16.128x + 78.991
				p95 = -0.0480*a3 + 1.9133*a2 - 18.210*Age + 84.006;	//	y = -0.0480x3 + 1.9133x2 - 18.210x + 84.006
			} else {	// Ages 18 & over.  // UPDATED
				p05 = 0.00010400*a3 - 0.020217*a2 + 1.1485*Age + 40.074;	  		// y = 0.0001x3 - 0.0202x2 + 1.1485x + 40.074
				p10 = 0.00011589*a3 - 0.022773*a2 + 1.3102*Age + 41.8122;	  		// y = 0.0001x3 - 0.0228x2 + 1.3102x + 41.812
				p25 = 0.00014208*a3 - 0.027526*a2 + 1.6236*Age + 42.5526;  		// y = 0.0001x3 - 0.0275x2 + 1.6236x + 42.553
				p50 = 0.00020664*a3 - 0.038051*a2 + 2.1964*Age + 41.8864;  		// y = 0.0002x3 - 0.0381x2 + 2.1964x + 41.886
				p75 = 0.00020737*a3 - 0.039689*a2 + 2.3832*Age + 45.7613;  		// y = 0.0002x3 - 0.0397x2 + 2.3832x + 45.761
				p90 = 0.00013622*a3 - 0.027699*a2 + 1.7089*Age + 66.7699;			// y = 0.0001x3 - 0.0277x2 + 1.7089x + 66.77
				p95 = 0.00018733*a3 - 0.033970*a2 + 1.8877*Age + 72.8718;			// y = 0.0002x3 - 0.034x2 + 1.8877x + 72.872
			}
		}
	} else {	// 1 = girls.
		if (Age < 12) {	// for girls
			p05 = 0.0095*a3 - 0.1010*a2 + 1.7861*Age + 6.9646;	//	y = 0.0095x3 - 0.101x2 + 1.7861x + 6.9646
			p10 = 0.0094*a3 - 0.0783*a2 + 1.6247*Age + 7.8356;	//	y = 0.0094x3 - 0.0783x2 + 1.6247x + 7.8356
			p25 = 0.0079*a3 - 0.0108*a2 + 1.2513*Age + 9.1771;	//	y = 0.0079x3 - 0.0108x2 + 1.2513x + 9.1771
			p50 = 0.0052*a3 + 0.0744*a2 + 0.9429*Age + 10.35;	//	y = 0.0052x3 + 0.0744x2 + 0.9429x + 10.35
			p75 = 0.0022*a3 + 0.1911*a2 + 0.3279*Age + 12.472;	//	y = 0.0022x3 + 0.1911x2 + 0.3279x + 12.472
			p90 = -0.0072*a3 + 0.4382*a2 - 0.8397*Age + 14.988;	//	y = -0.0072x3 + 0.4382x2 - 0.8397x + 14.988
			p95 = -0.0042*a3 + 0.3937*a2 - 0.3007*Age + 14.558;	//	y = -0.0042x3 + 0.3937x2 - 0.3007x + 14.558
		} else {	// Ages 12 to 18.
			if (Age < 18) {
				p05 = -0.0499*a3 + 1.9088*a2 - 20.733*Age + 90.527;	//	y = -0.0499x3 + 1.9088x2 - 20.733x + 90.527
				p10 = -0.0518*a3 + 1.9683*a2 - 21.193*Age + 92.833;	//	y = -0.0518x3 + 1.9683x2 - 21.193x + 92.833
				p25 = -0.0461*a3 + 1.6498*a2 - 15.569*Age + 65.388;	//	y = -0.0461x3 + 1.6498x2 - 15.569x + 65.388
				p50 = -0.0428*a3 + 1.4459*a2 - 11.597*Age + 46.479;	//	y = -0.0428x3 + 1.4459x2 - 11.597x + 46.479
				p75 = -0.0215*a3 + 0.4432*a2 + 3.8150*Age - 24.390;	//	y = -0.0215x3 + 0.4432x2 + 3.815x - 24.39
				p90 = -0.0186*a3 + 0.2527*a2 + 7.8181*Age - 41.963;	//	y = -0.0186x3 + 0.2527x2 + 7.8181x - 41.963
				p95 = -0.0520*a3 + 1.6665*a2 - 10.888*Age + 41.294;	//	y = -0.052x3 + 1.6665x2 - 10.888x + 41.294
			} else {
				p05 = -0.00053082*a2 + 0.03064*Age + 44.7151;		// y = -0.00053082x2 + 0.03064x + 44.7151
				p10 = -0.0011469*a2  + 0.12694*Age + 45.33326;		// y = -0.0011469x2 + 0.12694x + 45.33326	
				p25 = -0.0015841*a2  + 0.21677*Age + 47.8295;		// y = -0.0015841x2 + 0.21677x + 47.8295
				p50 = -0.0031915*a2  + 0.42977*Age + 50.1321; 		// y = -0.0031915x2 + 0.42977x + 50.1321
				p75 = -0.0033*a2     + 0.48549*Age + 54.8661;		// y = -0.0033x2 + 0.48549x + 54.8661
				p90 = -0.0044646*a2  + 0.53019*Age + 63.7756;		// y = -0.0044646x2 + 0.53019x + 63.7756
				p95 = -0.0059725*a2  + 0.61948*Age + 72.597;			// y = -0.0059725x2 + 0.61948x + 72.597
			}
		}
	}

	if (kg < p10) {
		m = (10-5) / (p10-p05);
		b = 5 - (m * p05);
	} else {
		if (kg < p25) {
			m = (25-10) / (p25-p10);
			b = 10 - (m * p10);
		} else {
			if (kg < p50) {
				m = (50-25) / (p50-p25);
				b = 25 - (m * p25);
			} else {
				if (kg < p75) {
					m = (75-50) / (p75-p50);
					b = 50 - (m * p50);
				} else {
					if (kg < p90) {
						m = (90-75) / (p90-p75);
						b = 75 - (m * p75);
					} else {	// 90th percentile or higher.
						m = (95-90) / (p95-p90);
						b = 90 - (m * p90);
					}
				}
			}
		}	
	}
	theP = m * kg + b;	// Don't forget to truncate >98% or < 2% values.

	form.kgP.value = SetPercentile( rounding( theP,0) );



	// Now calculate Weight for Height percentiles based on age.
	a2 = cm*cm;
	a3 = cm*cm*cm;
	var bAge = 0;

	if (form.Gender.selectedIndex == 0) { // 0 = boys.
		if (Age < 20) {					// The default curves for ages < 20
			bAge = 0;						// has the best data for 5,10,25,50,75,90,95th percentiles,
		} else {							// so we use those figures. But for older persons,
			if (Age >60) {				// weight increases with  age (at any given height).
				bAge = 10.42;				// So we add an age-correction factor.
			} else {
				bAge = 0.00005875*Age*Age*Age - 0.013788*Age*Age + 1.0521*Age - 15.5505;
			}
		}
		if (cm < 108) {	// for boys
			p05 = 0.0006751*a2 + 0.11615*cm - 3.7583 + bAge;
			p10 = 0.0007797*a2 + 0.10631*cm - 3.4062 + bAge;
			p25 = 0.0010213*a2 + 0.069875*cm - 1.7025 + bAge;
			p50 = 0.0011909*a2 + 0.045418*cm - 0.40064 + bAge;
			p75 = 0.0014481*a2 + 0.0041937*cm + 1.9037 + bAge;
			p90 = 0.0019934*a2 - 0.08739*cm + 6.1923 + bAge;
			p95 = 0.0020223*a2 - 0.090233*cm + 6.6428 + bAge;
		} else {
			p05 = 0.0041*a2 - 0.6013*cm + 32.772 + bAge;
			p10 = 0.0047*a2 - 0.7466*cm + 42.307 + bAge;
			p25 = 0.0051*a2 - 0.8004*cm + 44.662 + bAge;
			p50 = 0.0064*a2 - 1.1345*cm + 66.411 + bAge;
			p75 = 0.0068*a2 - 1.1863*cm + 68.24 + bAge;
			p90 = 0.0072*a2 - 1.198*cm + 65.901 + bAge;
			p95 = 0.0061*a2 - 0.8618*cm + 41.639 + bAge;
		}

	} else {	// 1 = girls.
		if (Age < 20) {				// Actually, I could improve this by converting
			bAge = 0;					// it into a polynomial expression rather than
		} else {						// this nested if statement.
			bAge = -0.0017*Age*Age + 0.3736*Age - 4.5167;
		}
		if (cm < 108) {	// for girls
			p05 = 0.00054315*a2 + 0.13267*cm - 4.7356 + bAge;	// y = 0.0005x2 + 0.1327x - 4.7356
			p10 = 0.00069365*a2 + 0.11083*cm - 3.6288 + bAge;	//	y = 0.0007x2 + 0.1108x - 3.6288
			p25 = 0.00087519*a2 + 0.08603*cm - 2.4346 + bAge;
			p50 = 0.00094087*a2 + 0.086395*cm - 2.285 + bAge;
			p75 = 0.0010928*a2 + 0.071861*cm - 1.5772 + bAge;
			p90 = 0.0015789*a2 + 0.0026713*cm + 1.2486 + bAge;
			p95 = 0.0016361*a2 - 0.0080545*cm + 2.2782 + bAge;
		} else {
			p05 = 0.0051794*a2 - 0.90195*cm + 53.0782 + bAge;
			p10 = 0.0055258*a2 - 0.9577*cm + 55.8166 + bAge;
			p25 = 0.0051527*a2 - 0.81698*cm + 45.7372 + bAge;
			p50 = -0.00010449*a3 + 0.048528*a2 - 6.706*cm + 308.78 + bAge;
			p75 = -0.00018369*a3 + 0.0811*a2 - 11.0314*cm + 496.88 + bAge;
			p90 = -0.00019825*a3 + 0.085569*a2 - 11.3447*cm + 497.48 + bAge;
			p95 = -0.00019873*a3 + 0.085064*a2 - 11.0916*cm + 477.31 + bAge;
		}
	}

	if (kg < p10) {
		m = (10-5) / (p10-p05);
		b = 5 - (m * p05);
	} else {
		if (kg < p25) {
			m = (25-10) / (p25-p10);
			b = 10 - (m * p10);
		} else {
			if (kg < p50) {
				m = (50-25) / (p50-p25);
				b = 25 - (m * p25);
			} else {
				if (kg < p75) {
					m = (75-50) / (p75-p50);
					b = 50 - (m * p50);
				} else {
					if (kg < p90) {
						m = (90-75) / (p90-p75);
						b = 75 - (m * p75);
					} else {	// 90th percentile or higher.
						m = (95-90) / (p95-p90);
						b = 90 - (m * p90);
					}
				}
			}
		}	
	}
	theP = m * kg + b;	// Don't forget to truncate >98% or < 2% values.

	form.kgcmP.value = SetPercentile( rounding( theP,0) );



return null;
}

function GetAge(form) {
	if (form.AgeCat.selectedIndex == 0) { // 70+ years,
		return 75;
	} else {
		if (form.AgeCat.selectedIndex < 6) { // 1 thru 5 are decades 20s thru 60s.
			return 65 - (form.AgeCat.selectedIndex -1)*10;
		} else {
			if (form.AgeCat.selectedIndex == 6 ) {
				return 19;
			} else {
				if (form.AgeCat.selectedIndex < 23) { // 7 thru 22 are ages 17 thru 2.
					return 17 - (form.AgeCat.selectedIndex-7);
				} else {
					if (form.AgeCat.selectedIndex == 23) { // age 1.5 yrs.
						return 1.5;
					} else {
						if (form.AgeCat.selectedIndex == 24) {	// age 1 yrs.
							return 1;
						} else {
							if (form.AgeCat.selectedIndex == 25) {
								return 30;
							} else {
								return 25;
							}
						}
					}
				}
			}
		}
	}
}

function SetAge(form) {
	if (form.Years.value > 0) {	// Only change the Age field, if a value is already there.
	
		if (form.AgeCat.selectedIndex == 0) { // 70+ years,
			form.Years.value = 75;
		} else {
			if (form.AgeCat.selectedIndex < 6) { // 1 thru 5 are decades 20s thru 60s.
				form.Years.value = 65 - (form.AgeCat.selectedIndex -1)*10;
			} else {
				if (form.AgeCat.selectedIndex == 6 ) {
					form.Years.value = 19;
				} else {
					if (form.AgeCat.selectedIndex < 23) { // 7 thru 22 are ages 17 thru 2.
						form.Years.value = 17 - (form.AgeCat.selectedIndex-7);
					} else {
						if (form.AgeCat.selectedIndex == 23) { // age 1.5 yrs.
							form.Years.value = 1.5;
						} else {
							if (form.AgeCat.selectedIndex == 24) {	// age 1 yrs.
								form.Years.value = 1;
							} else {
								if (form.AgeCat.selectedIndex == 25) {
									form.Years.value = 30;
								} else {
									if (form.Years.value > 19) form.Years.value = "";
								}
							}
						}
					}
				}
			}
		}
	}
	return true;
}
function setFeetAndInches(form,inchies) {
	var feet = Math.min( Math.max( Math.floor( inchies / 12 ), 1), 7);
	form.htf.selectedIndex = feet - 1;

	inchies = rounding( inchies - feet*12,0);
	form.hti.selectedIndex = Math.min( Math.max( inchies,0), 11 );
	return true;
}

function feetAndInches(form) {
	var inchies = 0;
	inchies = ((form.htf.selectedIndex+1) * 12) + form.hti.selectedIndex;

	if (form.hu.selectedIndex == 0) form.ht.value = inchies;
	if (form.hu.selectedIndex == 1) form.ht.value = rounding( inchies * 2.54,0);
	return true;
}


function SetPercentile( pc ) {
	if (pc > 98) pc = "> 98th percentile";
	else  {  if (pc < 2) pc = "< 2nd percentile";
		else { 
			if ( rightDigit(pc) == 1) { pc = pc + "st percentile"; }
			else { if ( rightDigit(pc) == 2) { pc = pc + "nd percentile"; }
				else { if ( rightDigit(pc) == 3) { pc = pc + "rd percentile"; }
						else pc = pc + "th percentile";
				}
			}
		}
	}
	return pc;
}

function poundsAndKilos(form) {
	var weight = Number(form.wt.value);
	if ( weight > 0) {
		if (form.wu.selectedIndex == 0) {	// 0 = pounds.
			form.wt.value = rounding( weight / 0.45359237,0);
		} else {								// 1 = kilograms.
			if (weight > 219) {
				form.wt.value = rounding( weight * 0.45359237,0);
			} else {
				form.wt.value = rounding( weight * 0.45359237,1);
			}
		}
		form.wt.select()
		form.wt.focus()
	}
	return true;
}

function inchesCm(form) {
	var height = Number(form.ht.value);
	if (height > 0) {
		if (form.hu.selectedIndex == 0) { // is now inches, was cm.
			setFeetAndInches(form, height / 2.54); 
			form.ht.value = rounding( height / 2.54,1) ; 
		} else {								// is now cm, was inches.
			setFeetAndInches(form, height);  // Always pass inches in height to this function.
			form.ht.value = rounding( height * 2.54,0);  }  
		form.ht.select()
		form.ht.focus()
	}
	return true;
}

function rightDigit(num) {
	num = num - (Math.floor(num/10)*10);
	return num;
}

function rounding(number,decimal) {
	multi = Math.pow(10,decimal);
	number = Math.round(number * multi) / multi;
	return number;
}


function checkWeight(val) {
	if ((isNaN(val)) || (val == null)  || (val == "") || (val < 0)) {
		alert( "Please enter a value for Weight.");
		return false;
	}
	return true;
}