﻿//singleton patern
var formFunkcja1 = {
						_frm : null,
						_VO2Max : null,
						_metric : null,
						_frmName : 'formFunkcja1',
						
						runConversion : function () {
													  var frm = this._frm;
													  // race time in min, raceLength in m and speed in m/min.
													  var time = parseInt(frm.hours.value, 10) * 60 + parseInt(frm.minutes.value, 10) + parseInt(frm.seconds.value, 10) / 60;
													  var rlength = frm.raceLength.value;
													  var speed = null;//speed will be calculated later
													  
													  if(time <= 0 || isNaN(time)) {
														alert('Please input a valid time');
														return;
													  }
													  
													  if(rlength <= 0 || isNaN(rlength)) {
														alert('Please input a valid race length.');
														return;
													  }
													  
													  if(frm.units.options[0].selected){
														rlength *= 1000;
													  }  else {
														rlength *= 1609;
													  }
													  
													  speed = rlength / time;
													  
													  this._VO2Max = this._velToVO2(speed) / this._timeToPercentVO2Max(time) ;
													  this.makeCalculations();
													  
										},
						makeCalculations : function () {
														// Toggle output type of paces.
														if(this._frm.paceType2.options[1].selected) {
															this._metric = false;
														} 
														else {
															this._metric = true;
														}
														  if(this._VO2Max <= 0){
															return;
														  }
															
														  var velEasy     = this._VO2ToVel(this._VO2Max * 0.7);
														  var velTempo   = this._VO2ToVel(this._VO2Max * 0.88);
														  var velMaximum = this._VO2ToVel(this._VO2Max);
														  var velSpeed   = this._VO2ToVel(this._VO2Max * 1.1);
														  var velxlong     = this._VO2ToVel(this._VO2Max * 0.6);
														  var velYasso = velMaximum * 1.95;  
														  
														  var toAppend;
														  if (this._metric) {
															toAppend=' km/h';
														  } else {
															toAppend=' mile/h';
														  }

														  var frm = this._frm;

														  frm.easy.value     =  this._speedConverter(velEasy)    + toAppend;
														  frm.tempo.value    =  this._speedConverter(velTempo)   + toAppend;
														  frm.maximum.value  =  this._speedConverter(velMaximum) + toAppend;
														  frm.speed.value    =  this._speedConverter(velSpeed)   + toAppend;
														  frm.xlong.value    =  this._speedConverter(velxlong)
																   + ' - ' + this._speedConverter(velEasy)    + toAppend;
														  var oldMetric = this._metric;
														  this._metric = false;
														  frm.yasso.value    =  this._speedConverter(velYasso) + toAppend;
														  this._metric = oldMetric;
														  
											},
						_speedConverter : function(speedInMeters){
																var kmPerHour = ( speedInMeters * 60 )/1000;
																if ( !this._metric ) {
																				kmPerHour *= 0.621;
																}
																return Math.round( kmPerHour );								
										},
						toggleMetric : function() {
												  if ( this._frm.paceType2.options[0].selected ) {
													this._metric = true;
												  } else {
													this._metric = false;
												  }
												  this.makeCalculations();
										},
						// Takes a velocity and converts it to a VO2 level. 
						_velToVO2 : function (vel) {
													return (-4.60 + 0.182258 * vel + 0.000104 * vel * vel);
									},
						// Takes a VO2 measurement and converts it to a velocity.
						_VO2ToVel : function (VO2) {
												return (29.54 + 5.000663 * VO2 - 0.007546 * VO2 * VO2);
						},
						// Takes a time in minutes and uses EQ 2 to convert it to a percent of VO2 maximum.   
						_timeToPercentVO2Max : function (minutes) {
																return (0.8 + 0.1894393 * Math.exp(-0.012778 * minutes) + 0.2989558 * Math.exp(-0.1932695 * minutes));
						},
						initiate : function () {
												
												var selektor = document.getElementById('paceType2');
												selektor.onchange = function () { formFunkcja1.toggleMetric();};
												var calculate = document.getElementById('Calculate');
												calculate.onclick = function () { formFunkcja1.runConversion();};
												formFunkcja1._frm = document.getElementById( formFunkcja1._frmName );
									},
						runTest : function () {
												this._frm.raceLength.value = 10;
												this._frm.hours.value = 1;
												var button = this._frm.Calculate;
												button.click();
									}
};



if (typeof window.onload === "function") {
			var old2 = window.onload;
			window.onload = function () { 
										old2(); 
										formFunkcja1.initiate();
										//formFunkcja1.runTest();
							};
}
else {
		window.onload = function () { 
										
										formFunkcja1.initiate();
										//formFunkcja1.runTest();
							};
}

