function bmi(){
			
	if($("imperial").checked == true){
		feet = floatval($F("height_imperial_feet"));
		inches = floatval($F("height_imperial_inches"));
		total_inches = ((feet*12)+inches);
		
		//	alert(total_inches);
		
		stones = floatval($F("weight_imperial_stones"));
		pounds = floatval($F("weight_imperial_pounds"));
		total_pounds = ((stones*14)+pounds);
		
		//	alert(total_pounds);
		
		if(total_inches==0 || total_pounds==0) return _no_values();
		if(total_inches<0 || total_pounds<0 || total_inches>120 || total_pounds>490) return _review_values("10 feet", "35 stone");
		bmi_value = _bmi_imperial(total_inches, total_pounds);
	
	}
	else{
		height = floatval($F("height_metric"));
		weight = floatval($F("weight_metric"));
		//alert(height + ' | ' + weight);
		if(height==0 || weight==0) return _no_values();
		if(height<0 || weight<0 || height>300 || weight>225) return _review_values("300cm", "225kg");
		bmi_value = _bmi_metric(height/100, weight);
	}
	
	_show_bmi(bmi_value);
	return false;
	
}


function floatval( mixed_var, base ) {

    var tmp;
	
	//alert(typeof( mixed_var ));
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseFloat(mixed_var*1);
        if(isNaN(tmp) || !isFinite(tmp)){
            return 0;
        } else{
            return parseFloat(tmp.toString(base || 10));
        }
    } else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){
        return isFinite(mixed_var);
    } else{
        return 0;
    }
}

function show_div(id){
	
	return $(id).show();
	
}

function hide_div(id){

	return $(id).hide();

}

function toggle_measurements(id_to_show, id_to_hide){
	
	show_div(id_to_show + "_div");
	hide_div(id_to_hide + "_div");
	hide_div("error_block");
	hide_div("bmi_div");
	return true;

}

function _bmi_imperial(inches, pounds){
	//alert(inches + ' ' + pounds);
	
	return ((pounds*703) / (inches*inches));
	
}

function _bmi_metric(m, kg){
		
	return (kg / (m*m));
	
}

function _no_values(){
	hide_div("bmi_div");
	hide_div("error_block");
	$("error_block").innerHTML = "<p>Please enter both a height and a weight</p>";
	Effect.Appear("error_block", {duration:0.3});
	return false;
}


function _review_values(height_limit, weight_limit){
	hide_div("bmi_div");
	hide_div("error_block");
	$("error_block").innerHTML = "<p>Please provide a height between 0 and " + height_limit + " and a weight between 0 and " + weight_limit + "</p>";
	Effect.Appear("error_block", {duration:0.3});
	return false;
}

function _show_bmi(bmi_val){

	hide_div("error_block");
	hide_div("bmi_div");
	
	bmi_val.parseFloat;
	
	start = "<p class=\"txt-medium\">Your BMI is <strong>" + (Math.round(bmi_val*10)/10) + "</strong></p>";
	end = 	"<p class=\"txt-small\">Always consult your doctor before you begin any diet or exercise programme.</p> \
			<p class=\"txt-small\">Your BMI on its own is not enough to assess whether you are at risk from certain conditions. Your waist-to-hip ratio can show if you are more likely to develop conditions such as heart disease and diabetes. <a href=\"http://www.eidoactive.co.uk/healthy-weight/waist-to-hip-calculator\" title=\"Waist-to-hip ratio calculator\">Click here to go to the waist-to-hip ratio calculator</a>.</p>";
	
	if(bmi_val < 19.5){
		$("bmi_div").className = "underweight";
		result = "<p>You are in the <strong class=\"txt-medium\">underweight</strong> range.<br />You should not lose any weight but try to make sure you are eating a healthy diet and maintaining a healthy lifestyle.</p>";
		
	}
	else if(bmi_val >= 18.4  &&  bmi_val <= 24.9){
		$("bmi_div").className = "normal";
		result = "<p>You are a <strong class=\"txt-medium\">healthy</strong> weight for your height.<br />Try to maintain a healthy diet and lifestyle.</p>";
	}
	else if(bmi_val >= 25  &&  bmi_val <= 29.9){
		$("bmi_div").className = "overweight";
		result = "<p>Your are <strong class=\"txt-medium\">overweight</strong> for you height.<br />Your health will benefit from a diet and exercise programme aimed at you losing some weight.</p>";
	}
	else if(bmi_val >= 30 &&  bmi_val <= 39.9){
		$("bmi_div").className = "obese";
		result = "<p>You are in the range classified as <strong class=\"txt-medium\">obese</strong>.<br />Your health would greatly benefit from a diet and exercise programme aimed at long-term weight loss.</p>";
	}
	else if(bmi_val >= 40){
		$("bmi_div").className = "clinically-obese";
		result = "<p>You are in the range classified as <strong class=\"txt-medium\">very obese</strong>.<br />This puts your health at risk. You should get help and advice on starting a programme of healthy eating and exercise aimed at long-term weight loss.</p>";
	}
	
	$("bmi_div").innerHTML = start + result + end;
	Effect.Appear("bmi_div", {duration:0.3});
	return true;
	
}

function stripAlphaChars(pstrSource) { 
	var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}
