/**
*
*@author		skey
*@date		2008-08-30
*@description 体重指数(BMI)
*
*/

//id
function $id(id){
	return document.getElementById(id);
}

//是否为数值
String.prototype.is_number = function(){

	return /^[1-9][0-9]*(.[0-9]+)?$/.test(this);

}

//去除空格
String.prototype.trim = function(){
	return this.replace(/(^s*)|(s*$)/g,'');
}

//健康值检查
function health_check(){

	var _weight = $id('weight').value.trim();

	var _height = $id('height').value.trim();

	if(!_weight || !_height){

		alert('请输入你的身高和体重！');

		return false;

	}

	if(_weight.is_number() && _height.is_number()){

		//身高和体重检测
		if(parseFloat(_height) <= 0 || parseFloat(_height) >=250|| parseFloat(_weight.value) <= 0|| parseFloat(_weight.value) >=250){

			alert("---");

			return false;

		}
		else{

			//体重指数
			_BMI = BMI(_weight,_height/100);

			if($id('sex').value == 1){
				//男
				ideal_weight = Math.round(50+(2.3*(_height-152))/2.54);
			}else{
				//女
				ideal_weight = Math.round(45.5+(2.3*(_height-152))/2.54);
			}

			//中国标准
			if (_BMI >= 40) {
				output('Ⅲ度肥胖','非常严重增加',_BMI,ideal_weight);
			}
			else if (_BMI >= 30 && _BMI < 40) {
				output('II度肥胖','严重增加',_BMI,ideal_weight);
			}
			else if (_BMI >27 && _BMI <= 29.9) {
				output('I度肥胖','中度增加',_BMI,ideal_weight);
			}
			else if (_BMI >= 24) {
				output('肥胖前期','增加',_BMI,ideal_weight);
			}
			else if (_BMI >= 18.5 && _BMI <= 23.9) {
				output('正常范围','平均水平',_BMI,ideal_weight);
			}
			else{
				output('体重过低','低，但其它疾病危险性增加',_BMI,ideal_weight);
			}

		}

	}else{

		alert('请输入有效的身高和体重！');

		return false;

	}

}

//体质指数 = 体重(kg)/身高的平方(m)
function BMI(weight, height) {

	return  weight/eval(height*height);

}

function output(c_level,c_stat,c_bmi,c_weight){

	$id('c_box').style.display = 'block';

	$id('c_stat').innerHTML = c_level+'(相关疾病发病的危险性处于'+c_stat+'状态)';

	$id('c_weight').value = c_weight;

	$id('c_bmi').value = c_bmi;

}

