// JavaScript Document
// Author: liu xiao
// E-mail: wdreams@126.com

//***********BMI Calculator Start *****************

//lbs weiht
//ins height
//return the bmi result

function cal_bmi(lbs, ins)
{
   var h2 = ins * ins;
   bmi = lbs/h2 * 703
   f_bmi = Math.floor(bmi);
   diff  = bmi - f_bmi;
   diff = diff * 10;
   diff = Math.round(diff);

   if (diff == 10){
      f_bmi += 1;
      diff   = 0;
   }
   bmi = f_bmi + "." + diff;
   return bmi;
}

function computeBMI(){
  // var result = document.getElementById("bmi_result");
   var weight = document.getElementById("bmi_weight");
   var feet = document.getElementById("bmi_feet");
   var inches = document.getElementById("bmi_inches");

   if(!checkNum(feet.value)){	//check the height
	   alert("Please enter your height.");
	   feet.focus();
	   return;
   }
   
   if(!checkNum(inches.value)){	//check the height
	   alert("Please enter your height.");
	   inches.focus();
	   return;
   }

   if(!checkNum(feet.value) && !checkNum(inches.value)){	//check the height
	   alert("Please enter your height.");
	   feet.focus();
	   return;
   }

   if(!checkNum(weight.value)){	//check the weight
	   alert("Please enter your weight.");
	   weight.focus();
	   return;
   }

   var heights =  parseInt(feet.value * 12) + parseInt(inches.value*1.0);

   // Perform the calculation
   var res = cal_bmi(parseInt(weight.value), heights);
   res = checkIndex(res);
 //  res = "Your BMI is " + res;
   window.location.href = "Result.html?result=" + res;
}

function checkIndex(res) {
	if(res < 18.5) {
		return "Your Body Mass Index is " + res + ". You are probably underweight.";
	} else if(res>=18.5 && res<=24.9) {
		return "Your Body Mass Index is " + res + ". You have normal weight.";
	} else if(res>=25 && res<=29.9) {
		return "Your Body Mass Index is " + res + ". You are probably overweight.";
	} else if(res>30) {
		return "Your Body Mass Index is " + res + ". You may be obese.";
	}
}


//***********BMI Calculator End *****************


//***********Heart Rate Calculator Start ***************
function cal_hr(age){
	var rate = 220 -age;
	return rate;
}

function computeHR() {
	//var result = document.getElementById("hr_result");
	var age = document.getElementById("hr_age");
	
	if(!checkNum(age.value)) {
		alert("Please enter a legal number for your age.");
		//document.getElementById("hr_age_1").innerText = "*";
		age.focus();
		return;
	}
	//document.getElementById("hr_age_1").innerText = "";
	var rate = cal_hr(parseInt(age.value));
	var t_res  = "Your target heart rate is " + rate;
    window.location.href = "Result.html?result=" + t_res;
}

//***********Heart Rate Calculator End *****************

//***********Baby Due Day Calculator Start ***************

// 	checkData validates the input data

    var dueDay = 0;
	var dueMonth = 0;
	var dueYear = 0;
	var newDay;
	var ok;
	var days = 280;

	var MonthCount = new Array();
		MonthCount[1] = 31;
		MonthCount[2] = 28;
		MonthCount[3] = 31;
		MonthCount[4] = 30;
		MonthCount[5] = 31;
		MonthCount[6] = 30;
		MonthCount[7] = 31;
		MonthCount[8] = 31;
		MonthCount[9] = 30;
		MonthCount[10] = 31;
		MonthCount[11] = 30;
		MonthCount[12] = 31;

	var MonthName = new Array();
		MonthName[1] = "January";
		MonthName[2] = "February";
		MonthName[3] = "March";
		MonthName[4] = "April";
		MonthName[5] = "May";
		MonthName[6] = "June";
		MonthName[7] = "July";
		MonthName[8] = "August";
		MonthName[9] = "September";
		MonthName[10] = "October";
		MonthName[11] = "November";
		MonthName[12] = "December";
		
	function checkData(month, day, year){
		var answer;

		if(month == "" || day == "" || year == "" || isNaN(month) || isNaN(day) || isNaN(year)){
			alert("The values you entered are invalid.");
			ok = 0;
		}else{

			if(month > 12 || month < 0){ 
				alert("Month needs to be a value between 1 and 12");
				ok = 0;
			}	

			else if(day > 31 || day < 0){
				alert("Day needs to be a value between 1 and 31");
				ok = 0;
			}else{
				ok = 1;
			}
		}
		
		if(((eval(year) + 1) %4 == 0 && eval(month) >= 5) || (eval(year) %4 == 0 && eval(month) < 3)){
			MonthCount[2] = 29;
		}else{
			MonthCount[2] = 28;
		}

		return ok;
	} 
// end of checkData function

//method280 Function calculates 280 days from LMP
	function method280(month, day, year){
		ok = checkData(month, day, year);

		if(ok == 1){
			
			dueYear = year;
			var firstDays = MonthCount[month] - eval(day);
			MonthCount[month];
			dueDay = 280 - firstDays;
			dueMonth = eval(month) + 1;

			while(dueDay > 31){

				if(dueMonth > 12){
					dueYear = eval(year) + 1;
					dueMonth = dueMonth - 12;
				}

				dueDay = dueDay - MonthCount[dueMonth];
				dueMonth = dueMonth + 1;
			}

			if(dueMonth > 12){
					dueYear = eval(year) + 1;
					dueMonth = dueMonth - 12;
			}		

			if(dueDay > MonthCount[dueMonth]){
				dueDay = dueDay - MonthCount[dueMonth];
				dueMonth = dueMonth + 1;
			}

			return (MonthName[dueMonth] + " " + dueDay + ", " + dueYear);
		}else{
			return "1";
		}
	}
//End of method280 Function

function computeBDD() {

	var month = document.getElementById("bdd_month");
	var day = document.getElementById("bdd_day");
	var year = document.getElementById("bdd_year");
	var d = method280(parseInt(month.value),parseInt(day.value),parseInt(year.value));
	if (d == 1) {
		
	} else {
		var res = "Your baby is due on " + d;
		window.location.href = "Result.html?result=" +res;
	}
}

//***********Baby Due Day Calculator End ***************

//***********CalorieCalculator Start ***************

function computeCC() {
	var height_m = document.getElementById("cc_m");
	var height_cm = document.getElementById("cc_cm");
	var weight = document.getElementById("cc_weight");
	var age = document.getElementById("cc_age");
	var flag = document.getElementById("cc").value;
	
   if(!checkNum(height_m.value)){	//check the height
	   alert("Please enter your height.");
	   height_m.focus();
	   return;
   }
   
   if(!checkNum(height_cm.value)){	//check the height
	   alert("Please enter your height.");
	   height_cm.focus();
	   return;
   }

   if(!checkNum(height_m.value) && !checkNum(height_cm.value)){	//check the height
	   alert("Please enter your height.");
	   height_m.focus();
	   return;
   }	
   
   if(!checkAge(age.value)){	//check the height
	   alert("Please enter your age.");
	   height_m.focus();
	   return;
   }
   
   var h = joinHeight(parseInt(height_m.value),parseInt(height_cm.value));
   var w = parseInt(weight.value);
   var a = parseInt(age.value);
   var r = cal_cc(h,w,a,flag);
  // var tex = "Your daily calorie input should be\r\n"
  //          + "      " + Math.ceil(r) + ", if you are not active\r\n"
		//	+ "      " + Math.ceil(r*1.2) + ", if you are moderately active\r\n"
			//+ "      " + Math.ceil(r*1.5) + ", if you are active\r\n"
			//+ "      " + Math.ceil(r*1.9) + ", if you are very active";
   window.location.href = "Result2.html?result=" + r;
}


//calculate the height
function joinHeight(m,cm) {
	var res;
	res = (m*12 + cm)*2.54;
	return res;
}

// calculate the Calorie
function cal_cc(height,weight,age,flag) {
	weight = weight / 2.2;
	var res;
	if (flag == "m") {	//10 x weight (kg) + 6.25 x height (cm) - 5 x age (y) + 5
		res = 10*weight + 6.25*height - 5*age + 5;
	} else if ( flag == "f") {
		res = 10*weight + 6.25*height - 5*age - 161; 
	}
	return res;
}

//***********LDL cholesterol calculator Start *************

function computeLDL() {
	var total = document.getElementById("total_ldl");
	var hdl = document.getElementById("hdl_ldl");
	var tc = document.getElementById("tc_ldl");
	//var result = document.getElementById("result_ldl");
	
	if(!checkNum(total.value)) {
	   alert("Please enter your total cholesterol level.");
	   total.focus();
	   return;	
	}
	
	if(!checkNum(hdl.value)) {
	   alert("Please enter your HDL cholesterol level.");
	   hdl.focus();
	   return;	
	}
	
	if(!checkNum(tc.value)) {
	   alert("Please enter your triglyceride level.");
	   tc.focus();
	   return;	
	}
	
	var r = cal_ldl(parseInt(total.value),parseInt(hdl.value),parseInt(tc.value));
	var tex;
	if(r<131) {
		tex = "Your LDL cholesterol level is normal";
	} else if(r >= 131 && r <= 159) {
		tex = "Your LDL cholesterol level is high";
	} else if(r >= 160) {
		tex = "Your LDL cholesterol level is risky";
	}
	//result.value = tex;
	window.location.href = "Result.html?result=" + tex;
}

function cal_ldl(total,hdl,tc) {
	var res;
	res = total - hdl - Math.floor(tc/5);
	return res;
}

//***********LDL cholesterol calculator End ***************


//*********** Common Start ********************
function checkNum(w){
   if (isNaN(parseInt(w))){
      return false;
   } else if (w < 0){
  	  return false;
  } else {
 	  return true;
  }
}

function checkAge(str) {
	var pattern=/^[0-9]{1,3}$/g;
	if(pattern.test(str)) {
		return true;
	}
	return false;
}

function changSel() {
	var sel = document.getElementById("sel");
	var re = sel.options[sel.options.selectedIndex].value;
	var frame = document.getElementById("iframes");
	
	if (re==1) {
		frame.src="calculators/BMICalculator.html";
	} else if(re==2) {
		frame.src="calculators/CalorieCalculator.html";
	} else if(re==3) {
		frame.src="calculators/LDLCalculator.html";
	} else if(re==4) {
		frame.src="calculators/HeartRateCalculator.html";
	} else if(re==5) {
		frame.src="calculators/BDDBMICalculator.html";
	}
}

//*********** Common End ********************

















