function init() {
	//showDebug();
	Get("age").focus();
}

window.onload = init;

//Cross-browser cross-frame element retrieval
function Get( oName, oFrame, oDoc ) {
	if( !oDoc ) { if( oFrame ) { oDoc = oFrame.document; } else { oDoc = window.document; } }
	if( oDoc[oName] ) { return oDoc[oName]; } if( oDoc.all && oDoc.all[oName] ) { return oDoc.all[oName]; }
	if( oDoc.getElementById && oDoc.getElementById(oName) ) { return oDoc.getElementById(oName); }
	for( var x = 0; x < oDoc.forms.length; x++ ) { if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }
	for( var x = 0; x < oDoc.anchors.length; x++ ) { if( oDoc.anchors[x].name == oName ) { return oDoc.anchors[x]; } }
	for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) {
		var theOb = Get( oName, null, oDoc.layers[x].document ); if( theOb ) { return theOb; } }
	if( !oFrame && window[oName] ) { return window[oName]; } if( oFrame && oFrame[oName] ) { return oFrame[oName]; }
	for( var x = 0; oFrame && oFrame.frames && x < oFrame.frames.length; x++ ) {
		var theOb = Get( oName, oFrame.frames[x], oFrame.frames[x].document ); if( theOb ) { return theOb; } }
	return null;
}

// Numeric test
function isNumeric(num, fieldPrompt, fieldName) {
	var regTest=/(^\d+$)|(^\d+\.\d+$)/
	if (regTest.test(num))
		return true;
	else {
		if (fieldPrompt) { 
			alert(fieldPrompt + " Must be a valid number");
			Get(fieldName).focus();
			Get(fieldName).select();
		}
		return false;
	}
}

function calcIt() {
oForm = document.calc;

weight3 = parseInt(Get("weight3").value*1);
age = parseInt(Get("age").value*1);
feet3 = parseInt(Get("feet3").value*1);
inches3 = parseInt(Get("inches3").value*1);

if (!isNumeric(age,"Age:","age"))
	return false;
else if ( (age <= 12) || (age > 80)) {
	alert ("Age must be between 13 and 80");
	Get("age").focus();
	Get("age").select();
	return false;
}

if (!isNumeric(feet3,"Feet3:","feet3"))
	return false;
else if ( (feet3 < 4) || (feet3 > 7) ) {
	alert ("Height must be between 4 and 7 feet3");
	Get("feet3").focus();
	Get("feet3").select();
	return false;
}

if (!isNumeric(inches3,"Inches3:","inches3"))
	return false;

optObject = Get("weighttype");
if (optObject[0].checked) 
	weight3 = weight3/2.2;
	
if (!isNumeric(weight3,"Weight3:","weight3"))
	return false;
else if ( (weight3 <= 40) || (weight3 > 500) ) {
	alert ("Please enter a valid weight");
	Get("weight3").focus();
	Get("weight3").select();
	return false;
}

height = ((feet3*12) + inches3) * 2.54;

optObject = Get("sex");
if (optObject[1].checked) {
	//Male
	//result = 66.5 + (13.75 * weight3) + (5.003 * height) - (6.775 * age); //HB
	//result =  879 + (10.2 * weight3); // Owen's
	result = 5 + (10 * weight3) + (6.25 * height) - (5 * age); //Miffin
} else {
	//Femme
	//result = 655.1 + (9.563 * weight3) + (1.850 * height) - (4.676 * age);
	//result = 795 + (7.18 * weight3);
	result = -161 + (10*weight3) + (6.25 * height) - (5 * age);
}

var maintain = result * Get("activity").options[Get("activity").selectedIndex].value;
rockBottom = (weight3*2.2)*8;
var suffix = " calories per day";


s = '<b>' + Math.round(maintain) + '</b>'  + suffix;
Get("answer").innerHTML = s;

loseFat = maintain - (maintain*0.20)
if (loseFat < rockBottom)
	loseFat = rockBottom;

var extLoseFat = maintain - (maintain*0.40)
if (extLoseFat < rockBottom)
	extLoseFat = rockBottom;
s = '<b>' + Math.round(extLoseFat) + " - " + Math.round(loseFat) + '</b>' + suffix;
Get("lose").innerHTML = s;



	
return true;
}
