function getBMIimperial(f)
{
	f.feet.value.replace(/[^0-9]/, '');
	f.inches.value.replace(/[^0-9\.]/, '');
	f.pounds.value.replace(/[^0-9\.]/, '');
	if(f.feet.value.length==0 || parseInt(f.feet.value) == 0)
	{
		alert("You must enter a nonzero numeric value in the \"feet\" field.")
		f.feet.focus();
		return false;
	}
	if(f.inches.value.length == 0)
		f.inches.value = '0';
	if(f.pounds.value.length == 0 || parseInt(f.pounds.value) == 0)
	{
		alert("You must enter a nonzero numeric value in the \"pounds\" field.")
		f.pounds.focus();
		return false;
	}
	var lbs = parseFloat(f.pounds.value);
	var inches = parseFloat(f.feet.value) * 12.0 + parseFloat(f.inches.value);
	var m = (inches * 2.54) / 100;
	var kg = lbs / 2.2046;
	var bmi = kg / (m * m);
	bmi = parseInt(bmi * 10.0);
	bmi = parseFloat(bmi) / 10.0;
	return bmi;
}

function getBMImetric(f)
{
	f.cm.value.replace(/[^0-9]/, '');
	f.kg.value.replace(/[^0-9\.]/, '');
	if(f.cm.value.length==0 || parseInt(f.cm.value) == 0)
	{
		alert("You must enter a nonzero numeric value in the \"cm\" field.")
		f.cm.focus();
		return false;
	}
	if(f.kg.value.length == 0 || parseInt(f.kg.value) == 0)
	{
		alert("You must enter a nonzero numeric value in the \"kg\" field.")
		f.kg.focus();
		return false;
	}
	var m = parseFloat(f.cm.value) / 100;
	var kg = parseFloat(f.kg.value);
	var bmi = kg / (m * m);
	bmi = parseInt(bmi * 10.0);
	bmi = parseFloat(bmi) / 10.0;
	return bmi;
}

function BMI_Calc_DisplayBMI(js_id, bmi) 
{
	var d = document.getElementById("BMI_Calc_Results_" + js_id);
	if(document.all)
	{ //IE can't handle DOM-compliant way, so directly inject the HTML
		var out = "You have a BMI of <strong>" + bmi + "</strong>.<br>";
		if(bmi <= 18.5)
			out += 'This shows that you may be underweight.';
		else if(bmi <= 24.9)
			out += 'This shows that you are of a normal weight.';
		else if(bmi <= 29.9)
			out += 'This shows that you are moderately overweight.<br>Your BMI is not high enough to qualify you for bariatric surgery.';
		else if(bmi < 39.9)
			out += 'This indicates that you are Obese.<br>Your BMI is generally high enough to qualify you for bariatric surgery if you have comorbid conditions such as diabetes (type II), hypertension, sleep apnea or other potentially life-threatening diseases.';
		else if(bmi < 49.9)
			out += 'This indicates that you are Extremely Obese.<br>Your BMI is high enough to qualify you for bariatric surgery.';
		else
			out += 'This puts you in the Super Morbid Obesity category.<br>Your BMI is extremely high and is enough to qualify you for bariatric surgery.';
		out += "<p>The table below shows value ranges and what they mean.</p>";
		out += "<table class=\"border\">";
		out += "<tr class=\"titlerow\"><th>Range</th><th>Meaning</th></tr>";
		out += "<tr class=\"oddrow\"><td style=\"padding:1px 6px\">less than 18.5</td><td style=\"padding:1px 6px\">Underweight</td></tr>";
		out += "<tr class=\"evenrow\"><td style=\"padding:1px 6px\">18.5 - 24.9</td><td style=\"padding:1px 6px\">Normal</td></tr>";
		out += "<tr class=\"oddrow\"><td style=\"padding:1px 6px\">25.0 - 29.9</td><td style=\"padding:1px 6px\">Overweight</td></tr>";
		out += "<tr class=\"evenrow\"><td style=\"padding:1px 6px\">30.0 - 39.9</td><td style=\"padding:1px 6px\">Obese</td></tr>";
		out += "<tr class=\"oddrow\"><td style=\"padding:1px 6px\">40.0 - 50</td><td style=\"padding:1px 6px\">Extremely Obese</td></tr>";
		out += "<tr class=\"evenrow\"><td style=\"padding:1px 6px\">over 50</td><td style=\"padding:1px 6px\">Super Obese</td></tr>";
		out += "</table>";
		
		out += "<br><table class=\"outerborder\">"
			+ "<tr><td><strong>The BMI Formula:</strong></td></tr>"
			+ "<tr><td align=\"center\" style=\"padding-bottom:1px;border-bottom:1px solid black\">(Weight in kilograms)</td></tr>"
			+ "<tr><td align=\"center\">(Height in meters)<sup style=\"font-size:7pt\">2</sup></td></tr>"
			+ "</table>"
		d.innerHTML = out;
	}
	else
	{ //Do it the DOM-compliant way for all non-IE browsers
		while(d.hasChildNodes())
			d.removeChild(d.firstChild);
		d.appendChild(document.createTextNode('You have a BMI of '));
		var b = document.createElement('strong');
		b.appendChild(document.createTextNode(bmi));
		d.appendChild(b);
		d.appendChild(document.createTextNode('.'));
		d.appendChild(document.createElement('br'));
		if(bmi <= 18.5)
			d.appendChild(document.createTextNode('This shows that you may be underweight.'));
		else if(bmi <= 24.9)
			d.appendChild(document.createTextNode('This shows that you are of a normal weight.'));
		else if(bmi <= 29.9)
		{
			d.appendChild(document.createTextNode('This shows that you are moderately overweight.'));
			d.appendChild(document.createElement('br'));
			d.appendChild(document.createTextNode('Your BMI is not high enough to qualify you for bariatric surgery.'));
		}
		else if(bmi < 39.9)
		{
			d.appendChild(document.createTextNode('This indicates that you are Obese.'));
			d.appendChild(document.createElement('br'));
			d.appendChild(document.createTextNode('Your BMI is generally high enough to qualify you for bariatric surgery if you have comorbid conditions such as diabetes (type II), hypertension, sleep apnea or other potentially life-threatening diseases.'));
		}
		else if(bmi < 49.9)
		{
			d.appendChild(document.createTextNode('This indicates that you are Extremely Obese.'));
			d.appendChild(document.createElement('br'));
			d.appendChild(document.createTextNode('Your BMI is high enough to qualify you for bariatric surgery.'));
		}
		else
		{
			d.appendChild(document.createTextNode('This puts you in the Super Morbid Obesity category.'));
			d.appendChild(document.createElement('br'));
			d.appendChild(document.createTextNode('Your BMI is extremely high and is enough to qualify you for bariatric surgery.'));
		}
		b = document.createElement('p');
		b.appendChild(document.createTextNode('The table below shows value ranges and what they mean.'));
		d.appendChild(b);
		var t = document.createElement('table');
		t.setAttribute('class','border');
	
		var tr = document.createElement('tr');
		tr.setAttribute('class', 'titlerow');
		var th = document.createElement('th');
		th.appendChild(document.createTextNode('Range'));
		tr.appendChild(th);
		th = document.createElement('th');
		th.appendChild(document.createTextNode('Meaning'));
		tr.appendChild(th);
		t.appendChild(tr);
		
		shaded = false;
		t.appendChild(BMI_Calc_DOM_Create_Row('less than 18.5', 'Underweight'));
		t.appendChild(BMI_Calc_DOM_Create_Row('18.5 - 24.9', 'Normal'));
		t.appendChild(BMI_Calc_DOM_Create_Row('25.0 - 29.9', 'Overweight'));
		t.appendChild(BMI_Calc_DOM_Create_Row('30.0 - 39.9', 'Obese'));
		t.appendChild(BMI_Calc_DOM_Create_Row('40.0 - 50', 'Extremely Obese'));
		t.appendChild(BMI_Calc_DOM_Create_Row('over 50', 'Super Obese'));
		
		d.appendChild(t);
		
		b = document.createElement('br');
		d.appendChild(b);
		
		t = document.createElement('table');
		t.setAttribute("class", "outerborder");
		tr = document.createElement('tr');
		th = document.createElement('th');
		th.appendChild(document.createTextNode('The BMI Formula:'));
		tr.appendChild(th);
		t.appendChild(tr);
		tr = document.createElement('tr');
		td = document.createElement('td');
		td.setAttribute("align", "center");
		b = document.createElement('span');
		b.setAttribute("style", "border-bottom:1px solid black;padding-bottom:2px");
		b.appendChild(document.createTextNode("(Weight in kilograms)"));
		td.appendChild(b);
		tr.appendChild(td);
		t.appendChild(tr);
		tr = document.createElement('tr');
		td = document.createElement('td');
		td.setAttribute("align", "center");
		td.appendChild(document.createTextNode("(Height in meters)"));
		b = document.createElement('sup');
		b.setAttribute('style','font-size:7pt');
		b.appendChild(document.createTextNode("2"));
		td.appendChild(b);
		tr.appendChild(td);
		t.appendChild(tr);
		
		d.appendChild(t);
	}
}

function BMI_Calc_DOM_Create_Row(range, desc)
{
	var tr = document.createElement('tr');
	tr.setAttribute('class', (shaded ? 'evenrow' : 'oddrow'));
	shaded = !shaded;
	var td = document.createElement('td');
	td.setAttribute('style', 'padding:1px 6px');
	td.appendChild(document.createTextNode(range));
	tr.appendChild(td);
	td = document.createElement('td');
	td.setAttribute('style', 'padding:1px 6px');
	td.appendChild(document.createTextNode(desc));
	tr.appendChild(td);
	return tr;
}
function BMI_Calc_Update(js_id)
{
	if(typeof js_id == "number")
		js_id = js_id.toString();
	js_id = js_id.toString();
	var d = document.getElementById("BMI_Calc_Caption_" + js_id);
	if(document.all)
	{
		var out = '<div align="center"><form onsubmit="return false" style="margin:0px"><table class="noborder">';
		if(hpComponent_settings[js_id]['units'] == 'metric')
		{
			out += '<tr><td style=\"vertical-align:middle;padding-left:15px\">Height:</td><td style=\"vertical-align:middle;padding-right:15px\"><input type="text" name="cm" size="3" maxlength="3"> cm</td></tr>';
			out += '<tr><td style=\"vertical-align:middle;padding-left:15px\">Weight:</td><td style=\"vertical-align:middle;padding-right:15px\"><input type="text" name="kg" size="3" maxlength="3"> kg</td></tr>';
			out += '</table><input type="button" value="Calculate" onclick="if(bmi=getBMImetric(this.form)){BMI_Calc_DisplayBMI('+js_id+', bmi);}"></form>';
		}
		else
		{
			out += '<tr><td style=\"vertical-align:middle;padding-left:15px\">Height:</td><td style=\"vertical-align:middle;padding-right:15px\"><input type="text" name="feet" size="2" maxlength="1">&nbsp;feet&nbsp;&nbsp;<input type="text" name="inches" size="2" maxlength="2">&nbsp;inches</td></tr>';
			out += '<tr><td style=\"vertical-align:middle;padding-left:15px\">Weight:</td><td style=\"vertical-align:middle;padding-right:15px\"><input type="text" name="pounds" size="3" maxlength="3"> pounds</td></tr>';
			out += '</table><input type="button" value="Calculate" onclick="if(bmi=getBMIimperial(this.form)){BMI_Calc_DisplayBMI('+js_id+', bmi);}\"></form>';
		}
		out += '<div id="BMI_Calc_Results_' + js_id + '"></div></div>';
		d.innerHTML = out;
	}
	else
	{
		while(d.hasChildNodes())
			d.removeChild(d.firstChild);
		var div = document.createElement('div');
		div.setAttribute('align', 'center');
		var fm = document.createElement('form');
		fm.setAttribute('onsubmit', 'return false');
		var table = document.createElement('table');
		table.setAttribute('class', 'noborder');
		if(hpComponent_settings[js_id]['units'] == 'metric')
		{
			var tr = document.createElement('tr');
			var td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-left:15px');
			td.appendChild(document.createTextNode('Height:'));
			tr.appendChild(td);
			td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-right:15px');
			var i = document.createElement('input');
			i.setAttribute('type', 'text');
			i.setAttribute('name', 'cm');
			i.setAttribute('size', '3');
			i.setAttribute('maxlength', '3');
			td.appendChild(i);
			td.appendChild(document.createTextNode(' cm'));
			tr.appendChild(td);
			table.appendChild(tr);
					
			tr = document.createElement('tr');
			td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-left:15px');
			td.appendChild(document.createTextNode('Weight:'));
			tr.appendChild(td);
			td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-right:15px');
			var i = document.createElement('input');
			i.setAttribute('type', 'text');
			i.setAttribute('name', 'kg');
			i.setAttribute('size', '3');
			i.setAttribute('maxlength', '3');
			td.appendChild(i);
			td.appendChild(document.createTextNode(' kg'));
			tr.appendChild(td);
			table.appendChild(tr);
			fm.appendChild(table);
			i = document.createElement('input');
			i.setAttribute('type', 'button');
			i.setAttribute('value', 'Calculate');
			i.setAttribute('onclick', 'if(bmi=getBMImetric(this.form)){BMI_Calc_DisplayBMI(' + js_id + ', bmi);}');
			fm.appendChild(i);
			div.appendChild(fm);
			var d2 = document.createElement('div');
			d2.setAttribute('id', 'BMI_Calc_Results_' + js_id);
			div.appendChild(d2);
			d.appendChild(div);
		}
		else
		{
			var tr = document.createElement('tr');
			var td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-left:15px');
			td.appendChild(document.createTextNode('Height:'));
			tr.appendChild(td);
			td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-right:15px');
			var i = document.createElement('input');
			i.setAttribute('type', 'text');
			i.setAttribute('name', 'feet');
			i.setAttribute('size', '2');
			i.setAttribute('maxlength', '1');
			td.appendChild(i);
			td.appendChild(document.createTextNode(' feet '));
			i = document.createElement('input');
			i.setAttribute('type', 'text');
			i.setAttribute('name', 'inches');
			i.setAttribute('size', '2');
			i.setAttribute('maxlength', '2');
			td.appendChild(i);
			td.appendChild(document.createTextNode(' inches'));
			tr.appendChild(td);
			table.appendChild(tr);
					
			tr = document.createElement('tr');
			td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-left:15px');
			td.appendChild(document.createTextNode('Weight:'));
			tr.appendChild(td);
			td = document.createElement('td');
			td.setAttribute('style', 'vertical-align:middle; padding-right:15px');
			i = document.createElement('input');
			i.setAttribute('type', 'text');
			i.setAttribute('name', 'pounds');
			i.setAttribute('size', '3');
			i.setAttribute('maxlength', '3');
			td.appendChild(i);
			td.appendChild(document.createTextNode(' pounds'));
			tr.appendChild(td);
			table.appendChild(tr);
			fm.appendChild(table);
			i = document.createElement('input');
			i.setAttribute('type', 'button');
			i.setAttribute('value', 'Calculate');
			i.setAttribute('onclick', 'if(bmi=getBMIimperial(this.form)){BMI_Calc_DisplayBMI(' + js_id + ', bmi);}');
			fm.appendChild(i);
			div.appendChild(fm);
			var d2 = document.createElement('div');
			d2.setAttribute('id', 'BMI_Calc_Results_' + js_id);
			div.appendChild(d2);
			d.appendChild(div);
		}				
	}
}
function hpcomponent_bmi_calc_init(js_id) //, settings)
{
	if(typeof js_id == "number")
		js_id = js_id.toString();
	if(typeof(hpComponent_settings) == 'undefined')
		hpComponent_settings = new Array();
	if(typeof(hpComponent_settings[js_id]) == 'undefined')
		hpComponent_settings[js_id] = new Array();
	//hpComponent_settings[js_id] = settings;
	hpComponent_callbacks[js_id] = 'BMI_Calc_Update('+js_id+')';
	BMI_Calc_Update(js_id);
}