function powerTime(sex,intensity,powerrow,t) {

	VykonMuzi = Array(23.50,11.50,7.60,6.62,
				23.16,11.35,7.46,6.50,
				22.82,11.20,7.33,6.38,
				22.48,11.05,7.19,6.26,
				22.14,10.90,7.06,6.14,
				21.80,10.75,6.92,6.02,
				21.46,10.60,6.79,5.91,
				21.12,10.45,6.65,5.79,
				20.78,10.30,6.52,5.67,
				20.44,10.15,6.38,5.55,
				20.10,10.00,6.25,5.43,
				19.76,9.85,6.11,5.31,
				19.42,9.70,5.97,5.19,
				19.08,9.55,5.84,5.07,
				18.74,9.40,5.70,4.95,
				18.40,9.25,5.57,4.84,
				18.06,9.10,5.43,4.72,
				17.72,8.95,5.30,4.60,
				17.38,8.80,5.16,4.48,
				17.04,8.65,5.03,4.36,
				16.70,8.50,4.89,4.24,
				16.36,8.35,4.75,4.12,
				16.02,8.20,4.62,4.00,
				15.68,8.05,4.48,3.88,
				15.34,7.90,4.35,3.76,
				15.00,7.75,4.21,3.64,
				14.66,7.60,4.08,3.53,
				14.32,7.45,3.94,3.41,
				13.98,7.30,3.81,3.29,
				13.64,7.15,3.67,3.17,
				13.30,7.00,3.53,3.05,
				12.96,6.85,3.40,2.93,
				12.62,6.70,3.26,2.81,
				12.28,6.55,3.13,2.69,
				11.94,6.40,2.99,2.57,
				11.60,6.25,2.86,2.46,
				11.26,6.10,2.72,2.34,
				10.92,5.95,2.59,2.22,
				10.58,5.80,2.45,2.10,
				10.24,5.65,2.32,1.98,
				9.90,5.50,2.18,1.86);


	VykonZeny = Array(19.98,9.78,6.46,5.63,
				19.69,9.65,6.34,5.53,
				19.40,9.52,6.23,5.42,
				19.11,9.39,6.11,5.32,
				18.82,9.27,6.00,5.22,
				18.53,9.14,5.88,5.12,
				18.24,9.01,5.77,5.02,
				17.95,8.88,5.65,4.92,
				17.66,8.76,5.54,4.82,
				17.37,8.63,5.42,4.72,
				17.09,8.50,5.31,4.62,
				16.80,8.37,5.19,4.51,
				16.51,8.25,5.07,4.41,
				16.22,8.12,4.96,4.31,
				15.93,7.99,4.85,4.21,
				15.64,7.86,4.73,4.11,
				15.35,7.74,4.62,4.01,
				15.06,7.61,4.51,3.91,
				14.77,7.48,4.39,3.81,
				14.48,7.35,4.28,3.71,
				14.20,7.23,4.16,3.60,
				13.91,7.10,4.04,3.50,
				13.62,6.97,3.93,3.40,
				13.33,6.84,3.81,3.30,
				13.04,6.72,3.70,3.20,
				12.75,6.59,3.58,3.09,
				12.46,6.46,3.47,3.00,
				12.17,6.33,3.35,2.90,
				11.88,6.21,3.24,2.80,
				11.59,6.08,3.12,2.69,
				11.31,5.95,3.00,2.59,
				11.02,5.82,2.89,2.49,
				10.73,5.70,2.77,2.39,
				10.44,5.57,2.66,2.29,
				10.15,5.44,2.54,2.18,
				9.86,5.31,2.43,2.09,
				9.57,5.19,2.31,1.99,
				9.28,5.06,2.20,1.89,
				8.99,4.93,2.08,1.79,
				8.70,4.80,1.97,1.68,
				8.42,4.68,1.85,1.58);
			
			
			if (sex=='F') {
				Vykon = VykonZeny;
			} else {
				Vykon = VykonMuzi;
			}

			switch (intensity) {
			case 0: tcor = t; break;
			case 1: tcor = t*2; break;
			case 2: tcor = t*2; break;
			case 3: tcor = t*2; break;
			case 4: tcor = 4*3600; break;
			case 5: tcor = 6*3600; break;
			case 6: tcor = 6*3600; break;
			case 7: tcor = 6*3600; break;
			}												
			
			if (tcor<60) {
				P = tcor*(Vykon[powerrow*4+1]-Vykon[powerrow*4])/55-(Vykon[powerrow*4+1]-Vykon[powerrow*4])/11+Vykon[powerrow*4];
			} else if(tcor<300) {
				P = tcor*(Vykon[powerrow*4+2]-Vykon[powerrow*4+1])/240-(Vykon[powerrow*4+2]-Vykon[powerrow*4+1])/4+Vykon[powerrow*4+1];
			} else if(tcor<720) {
				P12 = 1.04*Vykon[powerrow*4+3];
				P = tcor*(P12-Vykon[powerrow*4+2])/420-(P12-Vykon[powerrow*4+2])/1.4+Vykon[powerrow*4+2];				
			} else {
				P = Vykon[powerrow*4+3]-Vykon[powerrow*4+3]*0.05*Math.log(tcor/1200)/Math.log(2);				
			}
			
			switch (intensity) {			
			case 2: P*=0.9; break;
			case 3: P*=0.7; break;
			case 4: P*=0.9; break;
			case 5: P*=0.7; break;
			case 6: P*=0.6; break;
			case 7: P*=0.5; break;
			}				
			
			return P;	
}

function calcTimeFromPower(sloup,dist,from,to,frict,res,m,P)
{
	ov = 0;	
	t=0;
	s=0;
	last = from;
	for (i=0; i<sloup.length; i++) {
		if (dist[i]<from) continue;
		if (i>0 && dist[i-1]>to) break;
						
		if (dist[i]>to) rdist=to;				
		else rdist = dist[i];
		
		FG = 9.81*m*(sloup[i]+frict);
		tmp = P*P/(4*res*res)+FG*FG*FG/(27*res*res*res);
		if (tmp<0) tmp=0;
		else tmp = Math.pow(tmp,0.5);
		tmp2 = P/(2*res)-tmp;
		if (tmp2<0) tmp2=-Math.pow(-tmp2,1/3);
		else tmp2=Math.pow(tmp2,1/3);
		
		v = Math.pow((P/(2*res)+tmp),1/3)+tmp2;
		
		if (isNaN(v)) v=ov;
		else ov=v;
		
		if (v<2.5) v=2.5;
		
		t+=(rdist-last)/v;
		s+=rdist-last;
		last=rdist;
	}
	return t;
}


function calcLevel(P,t,sex,intensity,m)
{	
	for (i=0; i<41; i++) {
			
		NP=powerTime(sex,intensity,i,t); 	
		
		if (P>NP*m) {			
			return (i-1)+(P-NP*m)/((ONP-NP)*m);
		}			
		ONP = NP;
	}
	return 40+P/(NP*m);
}


function powercalc()
{
	fr = document.getElementById("powercalc");
	m = parseFloat(fr.mass.value)+parseFloat(fr.bikemass.value);
	slope = parseFloat(fr.slope.value)/100;	
	position = parseFloat(fr.position.value);
	position2 = parseFloat(fr.position2.value);
	
	switch (parseFloat(fr.biketype.value)) {	
		case 1:
			frict = 0.004;
			res = 0.46/2*position*position2;
			break;
		case 2:
			frict = 0.006;
			res = 0.55/2*position*position2;
			break;
		case 3:
			frict = 0.008;
			res = 0.64/2*position*position2;
			break;	
	}
		
	
	FG = 9.81*m*(slope+frict);
	
	if (!isNaN(parseFloat(fr.power.value))) {
		P = parseFloat(fr.power.value);
									
		tmp = P*P/(4*res*res)+FG*FG*FG/(27*res*res*res);
		if (tmp<0) tmp=0;
		else tmp = Math.pow(tmp,0.5);
		tmp2 = P/(2*res)-tmp;
		if (tmp2<0) tmp2=-Math.pow(-tmp2,1/3);
		else tmp2=Math.pow(tmp2,1/3);
		
		 fr.velocity.value = Math.round((Math.pow((P/(2*res)+tmp),1/3)+tmp2)*36)/10;
	} else if (!isNaN(parseFloat(fr.velocity.value))) {
		v = parseFloat(fr.velocity.value)/3.6;		
		fr.power.value = Math.round(v*(FG+v*v*res));
	} else {
		alert('Musíte vyplnit buď výkon, nebo rychlost');
	}
	
}

function gearingcalc()
{
	fr = document.getElementById("gearingcalc");
	gf = parseFloat(fr.gfront.value);
	gb = parseFloat(fr.gback.value);
	switch (parseFloat(fr.biketype.value)) {	
		case 1:
			O = 2.080;
			break;
		case 2:
			O = 2.170;
			break;
		case 3:
			O = 1.990;
			break;		
	}
	
	if (!isNaN(parseFloat(fr.cadence.value))) {
		c = parseFloat(fr.cadence.value);		
		fr.velocity2.value= Math.round(36*O*c*gf/(gb*60))/10;
	} else if (!isNaN(parseFloat(fr.velocity2.value))) {
		v = parseFloat(fr.velocity2.value);		
		fr.cadence.value= Math.round(gb*60*v/(3.6*O*gf));
	} else {
		alert('Musíte vyplnit buď rychlost, nebo kadenci');
	}
	
}	


function hrcalc()
{
	fr = document.getElementById("hrcalc");
	age = parseFloat(fr.age.value);
	if (isNaN(parseFloat(fr.hrmax.value))) {
		if (isNaN(age)) {
			alert('Musíte vyplnit věk.');
		} else if (isNaN(parseFloat(fr.mass2.value))) {			
			switch (fr.sex.value) {	
			case 'F':
				hrmax = 209 - (age * 0.7)
				break;
			case 'M':
				hrmax = 214 - (age * 0.8)
				break;		
			}
		} else {
			m = fr.mass2.value;
			switch (fr.sex.value) {	
			case 'F':
				hrmax = 210 - (age * 0.5) - m*2.205*0.05;
				break;
			case 'M':
				hrmax = 210 - (age * 0.5) - m*2.205*0.05 + 4;
				break;	
			}
		}
		
		fr.hrmax.value = Math.round(hrmax);
		
	} else {
		hrmax = parseFloat(fr.hrmax.value);
	}		
	
	
	fr.hrlevel0.value = "<"+Math.round(hrmax*0.6);
	fr.hrlevel1.value = Math.round(hrmax*0.6)+"-"+Math.round(hrmax*0.65);
	fr.hrlevel2.value = Math.round(hrmax*0.65)+"-"+Math.round(hrmax*0.75);
	fr.hrlevel3.value = Math.round(hrmax*0.75)+"-"+Math.round(hrmax*0.82);
	fr.hrlevel4.value = Math.round(hrmax*0.82)+"-"+Math.round(hrmax*0.89);
	fr.hrlevel5.value = Math.round(hrmax*0.89)+"-"+Math.round(hrmax*0.94);
	fr.hrlevel6.value = ">"+Math.round(hrmax*0.94);
	
}	

function cpcalc()
{
	fr = document.getElementById("cpcalc");
	P = parseFloat(fr.cpower.value);
	tstr = fr.time.value;
	ttt = tstr.split(":");
	if (ttt.length>1) {		
		t = parseFloat(ttt[0])*60+parseFloat(ttt[1]);
	} else {
		t = parseFloat(tstr);
	}
	m = parseFloat(fr.mass3.value);
	sex = fr.sex.value;
	level = calcLevel(P,t,sex,0,m);
	
	P1 = m*powerTime(sex,0,Math.floor(level),12);
	P2 = m*powerTime(sex,0,Math.ceil(level),12);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp02.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),60);
	P2 = m*powerTime(sex,0,Math.ceil(level),60);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp1.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),360);
	P2 = m*powerTime(sex,0,Math.ceil(level),360);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp6.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),60*12);
	P2 = m*powerTime(sex,0,Math.ceil(level),60*12);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp12.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),60*30);
	P2 = m*powerTime(sex,0,Math.ceil(level),60*30);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp30.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),3600);
	P2 = m*powerTime(sex,0,Math.ceil(level),3600);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp60.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),60*90);
	P2 = m*powerTime(sex,0,Math.ceil(level),60*90);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp90.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
	
	P1 = m*powerTime(sex,0,Math.floor(level),60*180);
	P2 = m*powerTime(sex,0,Math.ceil(level),60*180);		
	if (isNaN(P2)) P2=0;
	P = (P1*(level-Math.floor(level))+P2*(Math.ceil(level)-level));	
	fr.cp180.value = Math.round(0.95*P)+"-"+Math.round(1.05*P);
}
