var fat;
var dist1,dist2,dist3,dist4,dist5,dist6;
var min1,min2,min3,min4,min5,min6;
var pace1,pace2,pace3,pace4,pace5,pace6;
var x,y,z,d,m,x0,cal;
var check=false;
var twitter_com ="";

function twitterpost() {
    if(twitter_com ==""){
	window.alert("You must push \'Calculate\' before twitter post.");
	}else{
	TwitterAPI.statuses.update(twitter_com);
	}
}

function weight_check(){
//	alert("Weight is memorized.");
	if (document.getElementById("weightsave").checked){
		init_weight = document.getElementById("m").value;
		if(document.f.m000[0].checked){lbs=true;}else{lbs=false;}
		}else{
		init_weight=0;
		}
	setCookie("init_weight", init_weight);
	setCookie("init_lbs", lbs);

}
function getCookie(key,  tmp1, tmp2, xx1, xx2, xx3) 
{
	tmp1 = " " + document.cookie + ";";
	xx1 = xx2 = 0;
	len = tmp1.length;
	while (xx1 < len) {
        	xx2 = tmp1.indexOf(";", xx1);
        	tmp2 = tmp1.substring(xx1 + 1, xx2);
        	xx3 = tmp2.indexOf("=");
        	if (tmp2.substring(0, xx3) == key) {
            		return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
        	}
        	xx1 = xx2 + 1;
	}
	return("");
}

function setCookie(key, val, tmp) 
{
	tmp = key + "=" + escape(val) + "; ";
	tmp += "expires=Tue, 31-Dec-2030 23:59:59; ";
	document.cookie = tmp;
}

function clearCookie(key) {
	document.cookie = key + "=" + "xx; expires=Tue, 1-Jan-1980 00:00:00;";
}

function init_load()
{
	init_weight = getCookie("init_weight");
	if ((init_weight == 0)||(init_weight == "")){document.getElementById("m").value="";}
		else{document.getElementById("m").value=init_weight;
		document.getElementById("weightsave").checked=1;
		}
	init_lbs = getCookie("init_lbs");
//	alert(init_lbs);

	if (init_lbs == "true"){document.f.m000[0].checked=1;}
		else{document.f.m000[1].checked=1;
		}

}
function fat_show()
{
	fit_max=100;
  	var chart = new EJSC.Chart(
    	"fatChart" ,
    	{
      		show_titlebar: false ,
      		axis_bottom: { visible: false },
      		axis_left: { visible: false },
      		show_legend: false,
      		show_messages: false
    	}
  	);
  
  	chart.addSeries(
    		new EJSC.AnalogGaugeSeries(
      		new EJSC.ArrayDataHandler(
        	[ [fat,''] ]
      	) ,
      	{
        axis: {
          color          : 'rgb(180,190,255)' ,
          innerBorderColor    : 'rgb(0,0,0)' ,
          innerBorderOpacity    : 0 ,
          innerBorderWidth    : 1 ,
          innerBorderVisible    : false ,
          opacity          : 100 ,
          outerBorderColor    : 'rgb(0,0,150)' ,
          outerBorderOpacity    : 100 ,
          outerBorderWidth    : 2 ,
          outerBorderVisible    : true ,
          thickness        : 10
        },
	min:0,
	max:fit_max,
        tickCount: 5 ,
        label: {
          className        : 'blueLabel' ,
          textAlign        : 'center' ,
          position        : 'centerLeft' ,
          lines          : 1
        	}
      	}
    	)
  );

}

function check_data() {    

	check=true;

	dist1=document.f.item1_dist.value;
	dist2=document.f.item2_dist.value;
	dist3=document.f.item3_dist.value;
	dist4=document.f.item4_dist.value;
	dist5=document.f.item5_dist.value;
	dist6=document.f.item6_dist.value;
	min1=document.f.item1_min.value;
	min2=document.f.item2_min.value;
	min3=document.f.item3_min.value;
	min4=document.f.item4_min.value;
	min5=document.f.item5_min.value;
	min6=document.f.item6_min.value;
	pace1=document.f.item1_pace.value;
	pace2=document.f.item2_pace.value;
	pace3=document.f.item3_pace.value;
	pace4=document.f.item4_pace.value;
	pace5=document.f.item5_pace.value;
	pace6=document.f.item6_pace.value;

	m=document.f.m.value;  //m...weight(kg)

	if (dist1.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.item1_dist.focus();
        }
	if (min1.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the time.");
            check=false;
            document.f.item1_min.focus();
        }
	if (pace1.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the pace.");
            check=false;
            document.f.item1_pace.focus();
        }
	if (dist2.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.item2_dist.focus();
        }
	if (min2.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the time.");
            check=false;
            document.f.item2_min.focus();
        }
	if (pace2.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the pace.");
            check=false;
            document.f.item2_pace.focus();
        }
	if (dist3.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.item3_dist.focus();
        }
	if (min3.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the time.");
            check=false;
            document.f.item3_min.focus();
        }
	if (pace3.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the pace.");
            check=false;
            document.f.item3_pace.focus();
        }
	if (dist4.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.item4_dist.focus();
        }
	if (min4.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the time.");
            check=false;
            document.f.item4_min.focus();
        }
	if (pace4.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the pace.");
            check=false;
            document.f.item4_pace.focus();
        }
	if (dist5.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.item5_dist.focus();
        }
	if (min5.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the time.");
            check=false;
            document.f.item5_min.focus();
        }
	if (pace5.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the pace.");
            check=false;
            document.f.item5_pace.focus();
        }
	if (dist6.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.item6_dist.focus();
        }
	if (min6.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the time.");
            check=false;
            document.f.item6_min.focus();
        }
	if (pace6.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the pace.");
            check=false;
            document.f.item6_pace.focus();
        }
	if (m.match(/[^0-9\.]/g)){
            alert ("Things except the numerical value are included in the distance.");
            check=false;
            document.f.m.focus();
        }

	dist1=dist1-0;dist2=dist2-0;dist3=dist3-0;
	dist4=dist4-0;dist5=dist5-0;dist6=dist6-0;
	min1=min1-0;min2=min2-0;min3=min3-0;
	min4=min4-0;min5=min5-0;min6=min6-0;
	pace1=pace1-0;pace2=pace2-0;pace3=pace3-0;
	pace4=pace4-0;pace5=pace5-0;pace6=pace6-0;

	if (x=="") { x=0 } ;
	if (y=="") { y=0 } ;
	if (z=="") { z=0 } ;
	if (d=="") { d=0 } ;

	if(m<0){
		alert("Please input the weigh(lbs/Kg) above 0.");
        	check=false;
        	document.f.m.focus();
	}
	if((dist1 != 0)&&(min1 != 0)&&(pace1 != 0)){
		alert("Distance, Time, and the Pace cannot be specified at the same time.");
        	check=false;
        	document.f.item1_dist.focus();
	}
	if((dist2 != 0)&&(min2 != 0)&&(pace2 != 0)){
		alert("Distance, Time, and the Pace cannot be specified at the same time.");
        	check=false;
        	document.f.item2_dist.focus();
	}
	if((dist3 != 0)&&(min3 != 0)&&(pace3 != 0)){
		alert("Distance, Time, and the Pace cannot be specified at the same time.");
        	check=false;
        	document.f.item3_dist.focus();
	}
	if((dist4 != 0)&&(min4 != 0)&&(pace4 != 0)){
		alert("Distance, Time, and the Pace cannot be specified at the same time.");
        	check=false;
        	document.f.item4_dist.focus();
	}
	if((dist5 != 0)&&(min5 != 0)&&(pace5 != 0)){
		alert("Distance, Time, and the Pace cannot be specified at the same time.");
        	check=false;
        	document.f.item5_dist.focus();
	}
	if((dist6 != 0)&&(min6 != 0)&&(pace6 != 0)){
		alert("Distance, Time, and the Pace cannot be specified at the same time.");
        	check=false;
        	document.f.item6_dist.focus();
	}

	if (check==true) { calc(); }
}

function calc(){

	if (document.f.m000[0].checked){m_00=m;m=m*0.454;}else{m=m;m_00=m/0.454;}

	if ((pace1 == 0) && (dist1 != 0)){pace1=min1*60*50/dist1;}
	if ((min1 == 0) ){min1=dist1*pace1/50/60;}
	if ((dist1 == 0) && (pace1 != 0)){dist1=min1*60*50/pace1;}
	if ((pace2 == 0) && (dist2 != 0)){pace2=min2*60*50/dist2;}
	if ((min2 == 0) ){min2=dist2*pace2/50/60;}
	if ((dist2 == 0) && (pace2 != 0)){dist2=min2*60*50/pace2;}
	if ((pace3 == 0) && (dist3 != 0)){pace3=min3*60*50/dist3;} 
	if ((min3 == 0) ){min3=dist3*pace3/50/60;}
	if ((dist3 == 0) && (pace3 != 0)){dist3=min3*60*50/pace3;}
	if ((pace4 == 0) && (dist4 != 0)){pace4=min4*60*50/dist4;}
	if ((min4 == 0) ){min4=dist4*pace4/50/60;}
	if ((dist4 == 0) && (pace4 != 0)){dist4=min4*60*50/pace4;}
	if ((pace5 == 0) && (dist5 != 0)){pace5=min5*60*50/dist5;}
	if ((min5 == 0) ){min5=dist5*pace5/50/60;}
	if ((dist5 == 0) && (pace5 != 0)){dist5=min5*60*50/pace5;} 
	if ((pace6 == 0) && (dist6 != 0)){pace6=min6*60*50/dist6;}
	if ((min6 == 0) ){min6=dist6*pace6/50/60;}
	if ((dist6 == 0) && (pace6 != 0)){dist6==min6*60*50/pace6;}

	cal1=calc_cal(document.f.item1.value,dist1,min1,m);
	cal2=calc_cal(document.f.item2.value,dist2,min2,m);
	cal3=calc_cal(document.f.item3.value,dist3,min3,m);
	cal4=calc_cal(document.f.item4.value,dist4,min4,m);
	cal5=calc_cal(document.f.item5.value,dist5,min5,m);
	cal6=calc_cal(document.f.item6.value,dist6,min6,m);

	var dist_sum=dist1+dist2+dist3+dist4+dist5+dist6;
	var min_sum=min1+min2+min3+min4+min5+min6;
	var cal=cal1+cal2+cal3+cal4+cal5+cal6;

//cal=(y0*1000/60+17.5)*z*m/1000 ;y...v(km/h) z...min(分）m...weight(Kg)
//フルマラソン平均タイムはhttp://www.honolulumarathon.jp/runners_square/special/time/index.html
//から5.3時間とすると、平均速度は8km/h
//

	running_min=1000*cal/150.8/m;
	running_dist=running_min*8/60;
	fat=cal/7/2;
fatoz=fat/28.3495;
	mets=cal/m/min_sum*60;//
	kj=4.184*cal;
	kw=kj/3600;
	stb=60*kw;
	dnk=kw*10;
	rice=cal/252;
	beer=cal/203;
	shop=cal/2.5;
	clean=cal/3.8;
cburger=cal/310;
beer2=cal/153.1;
cleanning=cal/1.59/m_00*60;

//RUN
//100ｍ ウサイン＝ボルト ジャマイカ 9秒69 2008/08/16 
//200ｍ ウサイン＝ボルト ジャマイカ 19秒30 2008/08/20 
//400ｍ マイケル＝ジョンソン アメリカ 43秒18 1999/08/26 
//800ｍ ウィルソン＝キプケテル デンマーク 1分41秒11 1997/08/24 
//1500ｍ ヒシャム＝エルゲルージ モロッコ 3分26秒00 1998/07/14 
//5000ｍ ケネニサ＝ベケレ エチオピア 12分37秒35 2004/05/31 
//10000ｍ ケネニサ＝ベケレ エチオピア 26分17秒53 2005/08/26 

	if (dist_sum <= 100){kiroku_d=100;kiroku_sec=9.69/100;}
	if ((dist_sum > 100)&&(dist_sum<=200)){kiroku_d=200;kiroku_sec=19.30/200;}
	if ((dist_sum > 200)&&(dist_sum<=400)){kiroku_d=400;kiroku_sec=43.18/400;}
	if ((dist_sum > 400)&&(dist_sum<=800)){kiroku_d=800;kiroku_sec=101.11/800;}
	if ((dist_sum > 800)&&(dist_sum<=1500)){kiroku_d=1500;kiroku_sec=206.00/1500;}
	if ((dist_sum > 1500)&&(dist_sum<=5000)){kiroku_d=5000;kiroku_sec=757.35/5000;}
	if (dist_sum > 5000){kiroku_d=10000;kiroku_sec=1577.53/10000;}

//SWIM
//50m自由形 21秒28 イーモン・サリバン  オーストラリア 2008年3月28日 
//100m自由形 47秒05 イーモン・サリバン  オーストラリア 2008年8月13日
//200m自由形 1分42秒96 マイケル・フェルプス  アメリカ合衆国 2008年8月12日
//400m自由形 3分40秒08 イアン・ソープ  オーストラリア 2002年7月30日
//800m自由形 7分38秒65 グラント・ハケット  オーストラリア 2005年7月28日
//1500m自由形 14分34秒56 グラント・ハケット オーストラリア 2001年7月29日

	if (dist_sum <= 50){skiroku_d=50;skiroku_sec=21.28/50;}
	if ((dist_sum > 50)&&(dist_sum<=100)){skiroku_d=100;skiroku_sec=47.05/100;}
	if ((dist_sum > 100)&&(dist_sum<=200)){skiroku_d=200;skiroku_sec=102.96/200;}
	if ((dist_sum > 200)&&(dist_sum<=400)){skiroku_d=400;skiroku_sec=220.08/400;}
	if ((dist_sum > 400)&&(dist_sum<=800)){skiroku_d=800;skiroku_sec=458.65/800;}
	if (dist_sum > 800){skiroku_d=1500;skiroku_sec=864.56/1500;}

	document.f.cal.value=Math.round(cal*10)/10;
	document.f.fatoz.value=Math.round(fatoz*10)/10;
	document.f.fat.value=Math.round(fat*10)/10;
	document.f.mets.value=Math.round(mets*10)/10;
	document.f.kw.value=Math.round(kw*10)/10;
	document.f.stb.value=Math.round(stb*10)/10;
	document.f.dnk.value=Math.round(dnk*10)/10;
	document.f.cburger.value=Math.round(cburger*10)/10;
	document.f.beer2.value=Math.round(beer2*10)/10;
	document.f.shop.value=Math.round(shop*10)/10;
	document.f.cleanning.value=Math.round(cleanning*10)/10;
	document.f.running.value=Math.round(running_min*10)/10;
	document.f.running_d.value=Math.round(running_dist*10)/10;
	document.f.srecord.value=skiroku_d;
	document.f.srecord_m.value=Math.round(dist_sum*skiroku_sec/60*10)/10;
	document.f.record.value=kiroku_d;
	document.f.record_m.value=Math.round(dist_sum*kiroku_sec/60*10)/10;

	var twitter_hour,twitter_cal,twitter_credit;

	if (document.f.cal.value==0){
		twitter_cal = "Good job! ";
		}else{
		twitter_cal = " "+document.f.cal.value+" cal.It\'s "+document.f.cburger.value+" P of Cheeseburger. Or "+document.f.beer2.value+" of 12 floz Beer.";
		}

	twitter_credit=" #swimsim http://42.195km.net/e";

	twitter_com="Swimming! "+dist_sum+"m "+min_sum+" min, "+twitter_cal+twitter_credit;

//window.alert(twitter_com);

	kekka_head="<table border=\"0\"><tr><td align=\"center\" class=\"kekka\"> Trainning </td><td class=\"kekka\"> Distance(m) </td><td class=\"kekka\"> Time(min) </td><td class=\"kekka\">Pace<font size=\"1\">(sec/50m)</font></td></tr>";
	kekka_bar="<tr><td colspan=4><hr></td></tr>";
	kekka_bottom="</table>";

	if(document.f.item1.value != "x"){
		kekka_1="<tr><td align=\"center\" class=\"kekka\">"+document.f.item1.value+"</td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist1))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min1*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(pace1*10)/10))+"</td></tr>";
	}else{
		kekka_1="";
	}

	if(document.f.item2.value != "x"){
		kekka_2="<tr><td align=\"center\" class=\"kekka\">"+document.f.item2.value+"</td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist2))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min2*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(pace2*10)/10))+"</td></tr>";
	}else{
		kekka_2="";
	}

	if(document.f.item3.value != "x"){
		kekka_3="<tr><td align=\"center\" class=\"kekka\">"+document.f.item3.value+"</td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist3))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min3*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(pace3*10)/10))+"</td></tr>";
	}else{
		kekka_3="";
	}

	if(document.f.item4.value != "x"){
		kekka_4="<tr><td align=\"center\" class=\"kekka\">"+document.f.item4.value+"</td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist4))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min4*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(pace4*10)/10))+"</td></tr>";
	}else{
		kekka_4="";
	}

	if(document.f.item5.value != "x"){
		kekka_5="<tr><td align=\"center\" class=\"kekka\">"+document.f.item5.value+"</td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist5))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min5*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(pace5*10)/10))+"</td></tr>";
	}else{
		kekka_5="";
	}

	if(document.f.item6.value != "x"){
		kekka_6="<tr><td align=\"center\" class=\"kekka\">"+document.f.item6.value+"</td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist6))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min6*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(pace6*10)/10))+"</td></tr>";
	}else{
		kekka_6="";
	}

	if((kekka_1 =="")&&(kekka_2 =="")&&(kekka_3 =="")&&(kekka_4 =="")&&(kekka_5 =="")&&(kekka_6 =="")){
		kekka_1="<tr><td align=\"center\" class=\"kekka\">Not Select</td><td align=\"right\" class=\"kekka\">*****</td><td align=\"right\" class=\"kekka\">*****</td><td align=\"right\" class=\"kekka\">****.*</td></tr>";
		kekka_sum="<tr><td align=\"center\" class=\"kekka\"> Total </td><td align=\"right\" class=\"kekka\">*****</td><td align=\"right\" class=\"kekka\">*****</td><td align=\"right\" class=\"kekka\">****.*</td></tr>";
	}else{
		kekka_sum="<tr><td align=\"center\" class=\"kekka\"> Total </td><td align=\"right\" class=\"kekka\">"+String(Math.round(dist_sum))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(+min_sum*10)/10))+"</td><td align=\"right\" class=\"kekka\">"+addZero1(String(Math.round(min_sum*60*50/dist_sum*10)/10))+"</td></tr>";
	}

	document.getElementById("kakka_table").innerHTML =kekka_head+kekka_1+kekka_2+kekka_3+kekka_4+kekka_5+kekka_6+kekka_bar+kekka_sum+kekka_bottom;

	fat_show();

// http://vivi.dyndns.org/jog/theory.html
// http://www.edu.yamanashi.ac.jp/~koyama/class/Ex.%20prescription.pdf
// http://f-kawaraban.cside.com/bbs/c-board/c-board.cgi?cmd=one;no=115;id=
//・エネルギー消費量（kcal）　＝　METS　×　体重（ｋｇ）　×　運動時間（ｈ）　
// http://www1.biwakosportsnow.or.jp/shiga-sports/clm/clm_eiyo01.html
// 1cal（カロリー）は1gの水の温度を1℃上昇させるのに必要な熱量
//http://www.yonden.co.jp/kids/energy/a03.htm


}
function addZero1( x123 )
{
	nn = x123;
	if (nn.indexOf(".") == -1){nn = nn + ".0" ;}
	nn = nn.split(".")[0]+"."+(nn.split(".")[1]+"0").substring(0,1);
	return nn;
}

function calc_cal(item_x,dist_x,min_x,m_x)
{
	mets_x = 0;
	if (item_x =="x"){return 0;}
		else{

		xx= dist_x/min_x; // m/分
		if (item_x =="backstroke"){mets_x=7;}
		if (item_x =="butterflystroke"){mets_x=11;}
		if (item_x =="breaststroke"){mets_x=10;}
		if (item_x =="frontcrawl"){mets_x=3*xx/25+2.6;}
		if (item_x =="walking"){mets_x=5.5;}

		cal_x = 1.05 * mets_x * m_x * min_x / 60;
		return cal_x;
		}
}

