﻿var validchars = "0123456789.";

function cherCheck(c) { // charチェック--数字と小数点のみ
   if (validchars.indexOf(c) != -1) return true;
   return false;
}

function inputCheck(ObjForm) { // 入力文字列のチェック
	var emsg = "半角数値を入力してください。";
	var emsg2 = "小数点が不正に入力されています。";
	var tstr = ObjForm.value;
	var counter = 0;

	for (i=0;i<tstr.length;i++) {
		if(tstr.charAt(i) == '.'){
			counter++;
			if(counter > 1){
				alert(emsg2);
				ObjForm.value = "";
				ObjForm.focus();
				return false; 
			}
		}
		if ( cherCheck(tstr.charAt(i)) ) continue;
		alert(emsg);
		ObjForm.value="";// 不正な入力をクリアし、フォーカスを与える
		ObjForm.focus();
		return false;
	}
}

// BMI計算処理
function bmi_calc(){
	var emsg_h1="エラー：「①身長」が入力されていません";
	var emsg_h2="エラー：「①身長」の入力値が不正です";
	var emsg_w1="エラー：「②体重」が入力されていません";
	var emsg_w2="エラー：「②体重」の入力値が不正です";
	var weight=Number(document.cal_bmi.weight.value);
	var height=Number(document.cal_bmi.height.value);
	var bmi;
	var best_weight;
	var buf= "";
	var comment = "";

	// 必須入力チェック
	if(height == ""){
		document.cal_bmi.comment.value = emsg_h1;
		document.cal_bmi.bmi.value = "";
		document.cal_bmi.best_weight.value = "";
		return false;
		}
	if(height < 50 || height > 250){
		document.cal_bmi.comment.value = emsg_h2;
		document.cal_bmi.bmi.value = "";
		document.cal_bmi.best_weight.value = "";
		return false;
		}
	if(weight == ""){
		document.cal_bmi.comment.value = emsg_w1;
		document.cal_bmi.bmi.value = "";
		document.cal_bmi.best_weight.value = "";
		return false;
		}
	if(weight < 10 || weight > 300){
		document.cal_bmi.comment.value = emsg_w2;
		document.cal_bmi.bmi.value = "";
		document.cal_bmi.best_weight.value = "";
		return false;
		}

	// 計算
	bmi = weight / height / height * 10000;
	bmi = Math.round(bmi * 100) / 100;
	best_weight = height * height * 22 / 10000;
	best_weight = Math.round(best_weight * 100) / 100;
	document.cal_bmi.bmi.value = bmi;
	document.cal_bmi.best_weight.value = best_weight;

	// コメント表示
	comment = "あなたは、";
	if(bmi < 18.5){
		comment = comment + "「やせ（低体重）」(BMI＜18.5)";
	} else if(bmi >= 18.5 && bmi < 25){
		comment = comment + "「普通体重」(18.5≦BMI＜25)";
	} else if(bmi >= 25 && bmi < 30){
		comment = comment + "「肥満1度」(25≦BMI＜30)";
	} else if(bmi >= 30 && bmi < 35){
		comment = comment + "「肥満2度」(30≦BMI＜35)";
	} else if(bmi >= 35 && bmi < 40){
		comment = comment + "「肥満3度」(35≦BMI＜40)";
	} else if(bmi >= 40){
		comment = comment + "「肥満4度」(40≦BMI)";
	}
	comment = comment + "です";
	document.cal_bmi.comment.value = comment;

	return false;
}

