
<!--

function toggle(div) {
	if (document.getElementById) {
		this.obj = document.getElementById(div);
		this.style = this.obj.style;
	} else if (document.all) {
		this.obj = document.all[div];
		this.style = this.obj.style;
	} else if (document.layers) {
		this.obj = document.layers[div];
		this.style = this.obj;
	} else {
		return false
	}

	if (this.style.display=="block") this.style.display="none";
	else this.style.display="block";
}

function showMetric() {
	turnOff('imperial');
	turnOn('metric');
}

function showImperial() {
	turnOff('metric');
	turnOn('imperial');
}

function reset_energy() {
	turnOff('result');
	return true;
}

function getRadioValue(ref) {
	var found_it;
	for (var i=0; i<ref.length; i++)  { 
		if (ref[i].checked)  {
			found_it = ref[i].value
		}
	}
	return found_it;
}

function calculate_enery() {

	// Get reference to the form
	var ref = document.energy;

	// Check for values
	var message = "";
	if (!isInteger(ref.age)) {
		message += "> Please enter a number for your age.\n";
	}
	if (!isInteger(ref.weight)) {
		message += "> Please enter a value for your weight in pounds.\n";
	}
	if (!isInteger(ref.height_feet)) {
		message += "> Please enter a value for your height in feet.\n";
	}
	if (!isInteger(ref.height_inches)) {
		message += "> Please enter a number for your height in inches. If this is zero inches please enter '0'\n";
	}
	if (message) {
		alert("Please check the following and try again:\n\n"+message);
		return false;
	}
	var age = parseInt(ref.age.value);
	var gender = getRadioValue(ref.gender);
	var weight = parseInt(ref.weight.value/2.22);
	var height = (parseInt(ref.height_feet.value*12)+parseInt(ref.height_inches.value))*0.0254;
	var pa = getRadioValue(ref.pa);
	
	turnOn('result');
	writeDiv(cal_energy(age, gender, weight, height, pa), 'resultvalue');
	return true;
}

function cal_energy(age, gender, weight, height, pa) {
	var result;
	switch (gender) {
		case "F":
		default:
			result = Math.round((354-(6.91*age))+(pa*(9.36*weight))+(726*height));
			break;
		case "M":
			result = Math.round((662-(9.53*age))+(pa*(15.91*weight))+(539.6*height));
			break;
	}
	return result;
}

function writeDiv(text, id) {
	if (document.getElementById) {
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	} else if (document.all) {
		x = document.all[id];
		x.innerHTML = text;
	} else if (document.layers) {
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

function calculate(format) {

	// Get reference to the form
	var ref = document.bmi;

	if (format == "metric") {

		// Check for values
		var message = "";
		if (!isInteger(ref.weight_k)) {
			message += "> Please enter a number for your weight.\n";
		}
		if (!isInteger(ref.height_c)) {
			message += "> Please enter a number for your height.\n";
		}
		if (message) {
			alert("Please check the following and try again:\n\n"+message);
			return false;
		}
		ref.metricresult.value = cal_bmi(ref.weight_k.value, ref.height_c.value, format);
		ref.metricresult.focus();

	} else {

		// Check for values
		var message = "";
		if (!isInteger(ref.weight_p)) {
			message += "> Please enter a number for your weight.\n";
		}
		if (!isInteger(ref.height_f)) {
			message += "> Please enter a number for your height in feet.\n";
		}
		if (!isInteger(ref.height_i)) {
			message += "> Please enter a number for your height in inches.\n";
		}
		if (message) {
			alert("Please check the following and try again:\n\n"+message);
			return false;
		}

		height_t = parseInt((ref.height_f.value)*12)+parseInt(ref.height_i.value);
		ref.imperialresult.value = cal_bmi(ref.weight_p.value, height_t, format);
		ref.imperialresult.focus();
	}
}

function cal_bmi(weight, height, format) {

	if (format == "metric") {
		m = height/100;
		h2 = m * m;
		s_bmi = weight/h2;
	} else {
		h2 = height * height;
		s_bmi = weight/h2 * 703
	}

	f_bmi = Math.floor(s_bmi);
	diff  = s_bmi - f_bmi;
	diff = diff * 10;
	diff = Math.round(diff);

	if (diff == 10) {
		f_bmi += 1;
		diff = 0;
	}
	s_bmi = f_bmi + "." + diff;
	return s_bmi;
}

function isDigit(c) {
	return ((c >= "0") && (c <= "9"));
}

function empty(v) {
	return (v=="" || v==null || !defined(v));
}

function defined(v) {
	var undefined;
	return (v!=undefined);
}

var defaultEmptyOK = false;
function isInteger(reference) {
	var s = reference.value;
	var i;

    if (empty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++) {
    	// Check that current character is number.
		var c = s.charAt(i);

		if (!isDigit(c)){
			return false;
		}
	}
	return true;
}

function turnOff(DivID) {
	if (document.getElementById) { //gecko(NN6) & IE 5+
		document.getElementById(DivID).style.visibility = "hidden";
		document.getElementById(DivID).style.display = "none";
	} else if (document.all) { // IE 4+
		document.all[DivID].style.visibility = "hidden";
		document.all[DivID].style.display = "none";
	} else if (document.layers) { // NS4+
		document.layers[DivID].visibility = "hide";
		document.layers[DivID].display = "none";
	} else {
		// nothing
	}
}

function turnOn(DivID) {
	if (document.getElementById) { //gecko(NN6) & IE 5+
		document.getElementById(DivID).style.visibility = "visible";
		document.getElementById(DivID).style.display = "block";
	} else if (document.all) { // IE 4+
		document.all[DivID].style.visibility = "visible";
		document.all[DivID].style.display = "block";
	} else if (document.layers) { // NS4+
		document.layers[DivID].visibility = "show";
		document.layers[DivID].display = "block";
	} else {
		// nothing
	}
}

function bookmark(url, who){

	var url = url;
	if (!who) var who = "Circle of Responsibility";

	var ver = navigator.appName
	var num = parseInt(navigator.appVersion)
	if ((navigator.platform.indexOf("Win32") != -1) && (ver == "Microsoft Internet Explorer") && (num >= 4)) 
	{
   		window.external.AddFavorite(url,who);
	}
	else if (navigator.platform=="MacPPC" || navigator.platform=="Mac68K")
	{
		alert("Press Command and D together to bookmark this page.");
	}
	else
	{
   		alert("Press Control and D together to bookmark this page.");
	} 
}

function switchMap(newmap) {
	if (document.getElementById) {
		var mapref = document.getElementById("map");
	} else if (document.all) {
		var mapref = document.all["map"];
	} else {
		// nothing
	}
	mapref.src = "/images/maps/"+newmap+".jpg";

}

//-->
