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 printPage (elementPrint) {
	var printDiv = Get(elementPrint);

	if (document.getElementsByTagName) {
		linkEl = document.getElementsByTagName('link');
		styleEl= document.getElementsByTagName('style');
		}
	printWindow = window.open("","printWindow","left=50,top=50,width=400,height=400,scrollbars=yes,"+"status=yes,resizable=yes");
    printWindow.opener = self;
    d = printWindow.document;
	//Write the printed contents
    d.open();
	d.writeln("<html><head><title>" + document.title + "</title>");
	if (linkEl) {
	  	d.writeln ("<link rel='stylesheet' type='text/css' href=\'" +linkEl[0].getAttribute('href') + "\'/>");
		d.writeln ("<style type='text/css'>" + styleEl[0].innerHTML + "</style>");
	}
	d.writeln("</head><body>");
	d.writeln(printDiv.innerHTML);
	d.writeln("<br /><b style='font-family:tahoma;'>Calculated at www.freedieting.com/tools/calorie_calculator.htm</b>");
	d.writeln("</html>");
	d.close();
	printWindow.print();
	printWindow.close();
}

	
function calcIt() {
oForm = document.calc;

weight = parseInt(Get("weight").value*1);
age = parseInt(Get("age").value*1);
feet = parseInt(Get("feet").value*1);
inches = parseInt(Get("inches").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(feet,"Feet:","feet"))
	return false;
else if ( (feet < 4) || (feet > 7) ) {
	alert ("Height must be between 4 and 7 feet");
	Get("feet").focus();
	Get("feet").select();
	return false;
}

if (!isNumeric(inches,"Inches:","inches"))
	return false;

optObject = Get("weighttype");
if (optObject[0].checked) 
	weight = weight/2.2;
	
if (!isNumeric(weight,"Weight:","weight"))
	return false;
else if ( (weight <= 40) || (weight > 500) ) {
	alert ("Please enter a valid weight");
	Get("weight").focus();
	Get("weight").select();
	return false;
}

height = ((feet*12) + inches) * 2.54;

optObject = Get("sex");
if (optObject[1].checked) {
	//Male
	//result = 66.5 + (13.75 * weight) + (5.003 * height) - (6.775 * age); //HB
	//result =  879 + (10.2 * weight); // Owen's
	result = 5 + (10 * weight) + (6.25 * height) - (5 * age); //Miffin
} else {
	//Femme
	//result = 655.1 + (9.563 * weight) + (1.850 * height) - (4.676 * age);
	//result = 795 + (7.18 * weight);
	result = -161 + (10*weight) + (6.25 * height) - (5 * age);
}

var maintain = result * Get("activity").options[Get("activity").selectedIndex].value;
rockBottom = (weight*2.2)*8;
var suffix = " Calories/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;
}
