<html>
<head>
<title>Body Mass Index Calculator</title>
<script type="text/javascript">
function $(id) {
return document.getElementById(id);
}
function isInteger(s) {
return /^[+-]?\d+$/.test(s);
}
function createDetails() {
var f = document.forms[0];
var feet = $('feet').value;
var inches = $('inches').value;
var weight = $('weight').value;
var errmsg = "";
// validate fields
if (!isInteger(feet)) errmsg += "Invalid number for feet\n";
else {
feet = parseInt(feet);
if (feet<=0) errmsg += "Feet must be greater than 0\n";
if (feet>=10) errmsg += "Feet must be less than 10\n";
}
if (!isInteger(inches)) errmsg += "Invalid number for inches\n";
else {
inches = parseInt(inches);
if (inches<0) errmsg += "Inches must be greater than or equal to 0\n";
if (inches>=12) errmsg += "Inches must be less than 12\n";
}
if (!isInteger(weight)) errmsg += "Invalid number for weight\n";
else {
weight = parseInt(weight);
if (weight<=0) errmsg += "Weight must be greater than 0\n";
}
// check for errors
if (errmsg != "") {
alert(errmsg);
return false;
}
// calculate body mass index
var totInches = feet * 12 + inches;
var bmi = (weight/(totInches*totInches)) * 703;
var strBMI = bmi.toFixed(1);
// open window and display results
var win = window.open("", "_blank", "width=250,height=300,status=no");
win.document.writeln('<html><head><title>BMI Details</title>');
win.document.writeln('<style type="text/css">');
win.document.writeln('td, th { text-align: right; }');
win.document.writeln('</style>');
win.document.writeln('</head>');
win.document.writeln('<body>');
win.document.writeln('<h3>BMI Details</h3>');
win.document.writeln('<p>Feet: ' + feet + '</p>');
win.document.writeln('<p>Inches: ' + inches + '</p>');
win.document.writeln('<p>Weight: ' + weight + '</p>');
win.document.writeln('<p>Body Mass Index: ' + strBMI + '</p>');
var status = "overweight";
if (bmi<25) status = "normal";
if (bmi>30) status = "obesity";
win.document.writeln('<p>A BMI of ' + strBMI + ' indicates ' + status + '</p>');
win.document.writeln('</body></html>');
win.document.close();
return false;
}
</script>
</head>
<body>
<h3>BMI Calculator</h3>
<form action="BMI.php"
method="get" id="bmiform"
onsubmit="return createDetails();">
<table border="0">
<tr>
<td><label for="feet">Feet</label></td>
<td><label for="inches">Inches</label></td>
<td><label for="weight">Weight</label></td>
</tr>
<tr>
<td><input type="text" size="20" id="feet" name="feet" /></td>
<td><input type="text" size="20" id="inches" name="inches" /></td>
<td><input type="text" size="20" id="weight" name="weight" /></td>
</tr>
<tr>
<td colspan="3">
<input type="reset" value="Clear" />
<input type="submit" value="Calc" />
</td>
</tr>
</table>
</form>
</body>
</html> |