//----------------------------------------------------------------------------------------------------------------
/**
* Calculator is an abstract base class.
* The class implements all kinds of calculations based on time and distance. 
* @class
* @version 1.1
* @author Lasse Storgaard Jacobsen
* @constructor
*/
function Calculator(){
}//Calculator end

//Adds the functions to the class
Calculator.prototype.get_time = get_time;
Calculator.prototype.get_speed = get_speed;
Calculator.prototype.get_distance = get_distance;
Calculator.prototype.get_pace = get_pace;
Calculator.prototype.get_purdy_point = get_purdy_point;
Calculator.prototype.get_procent = get_procent;
Calculator.prototype.get_effect = get_effect;
Calculator.prototype.get_jackson_pollock_bodyfat = get_jackson_pollock_bodyfat;
Calculator.prototype.get_body_mass_index = get_body_mass_index;
Calculator.prototype.get_helth_status = get_helth_status;


//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a time in seconds and convert it to a readable time string. 
* The return datatype is a string and have allways the format: [hh:mm:ss] or [mm:ss]
* @param {Int} second_int The time in secounds
* @returns {String} The secounds converted to at time format
*/
function get_time(second_int){
	var time_str = "";
	
	if (second_int>0){
		var u_hour = parseInt((second_int/3600));
		var u_min  = parseInt(((second_int-(u_hour*3600))/60));
		var u_sek  = parseInt(((second_int - (u_hour *3600))-(u_min*60)));
		
		if (u_hour >0){
			time_str += u_hour + ":";
		}//end if
	
		if (u_min > 0){
			if(u_min < 10){
				time_str += "0";
			}//end if	
			time_str += u_min + ":";		
		}else{
			time_str += "00:";		
		}//end if
	
		if (u_sek > 0){
			if(u_sek < 10){
				time_str += "0";
			}//end if
			time_str += u_sek;
		}else{
			time_str += "00"; 
		}//end if
	}else{
		time_str = "00:00";
	}//end if
	return time_str;

}//get_time end




//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a time in seconds and a distance in meters and convert it to a readable speed (km/t) string.
* The return datatype is a string and have allways the format: [12.34]
* @param {Int} second_int The time in secounds
* @param {Int} meters_int The distance in meters
* @returns {String} The speed in km/t
*/
function get_speed(second_int,meters_int){
	var speed_str = "";
	
	if(second_int > 0 && meters_int > 0){	
		var u_temp = ((3600/second_int)*(meters_int/1000)).toString();		
		var u_pos = u_temp.indexOf(".");
		
		if (u_pos == -1){
			u_pos += u_temp.length+1 
			speed_str = (u_temp + ".00").substring(0,u_pos+3);
		}else{
			speed_str = (u_temp+"00").substring(0,u_pos+3);
		}//end if		
	}else{
		speed_str = "0.00";
	}//end if
	return	speed_str;
}//get_speed end




//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a distance in meters and simply convert it to km.
* The return datatype is a string and have allways the format: [12.345]
* @param {Int} meters_int The distance in meters
* @returns {String} The distance i km
*/
function get_distance(meters_int){
	var distance_str;
	if (meters_int>0){
		distance_str  = (meters_int/1000).toString();
	}else{
		distance_str = "0";
	}//end if
	return distance_str;
}//get_distance end





//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a time in seconds and a distance in meters and convert it to a readable pace (min/km) string.
* The return datatype is a string and have allways the format: [mm.ss] 
* @param {Int} second_int The time in secounds
* @param {Int} meters_int The distance in meters
* @returns {String} The pace i time per km.
*/
function get_pace(second_int,meters_int){
	var pace_str;
	if(second_int>0 && meters_int>0){
		var u_sek = parseInt((second_int/(meters_int /1000)) % 60);
		var u_min = parseInt(((second_int)/60)/(meters_int/1000));
	
		pace_str = "" + u_min + ":";
	
		if(u_sek <10){
			pace_str += "0";
		}//end if
		pace_str += u_sek;
		
	}else{
		pace_str = "0:00";
	}//end if
	return pace_str;
}//get_pace end




//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a time in seconds and a distance in meters and convert it to a readable purdy point string. 
* The return datatype is a string and have allways the format: [700.79] 
* @param {Int} second_int The time in secounds
* @param {Int} meters_int The distance in meters
* @returns {String} The purdy point value.
*/
function get_purdy_point(second_int,meters_int){
	var purdy_point_str;
	if (second_int>0 && meters_int>0){
		purdy_point_str = ((2074/(second_int/60))*(Math.pow((meters_int/1000),1.0689))).toString();
											 
		var u_pos = purdy_point_str.indexOf(".");
		
		if (u_pos == -1){
			u_pos = purdy_point_str.length 
			purdy_point_str = (purdy_point_str + ".00").substring(0,u_pos+3);
		}else{
			purdy_point_str = (purdy_point_str+"00").substring(0,u_pos+3);
		}//end if		
		
		
		
	}else{
		purdy_point_str = "000.00";
	}//end if
	return purdy_point_str;
}//get_purdy_point end




//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a totaltime in seconds and a subtime in seconds calculate the procent. 
* The return datatype is a string and have allways the format: [12.34] 
* @param {Int} totalsecond_int The total time in secounds
* @param {Int} subsecond_int The cuttent time in secounds
* @returns {String} The procent.
*/
function get_procent(totalsecond_int,subsecond_int){
	
	
	
	var procent_str;
	if(totalsecond_int > 0 && subsecond_int>0){
		procent_str = (subsecond_int/totalsecond_int).toString();
		
		var u_pos = procent_str.indexOf(".");
		
		if (u_pos == -1){
			u_pos = procent_str.length 
			procent_str = (procent_str + ".00").substring(0,u_pos+3);
		}else{
			procent_str = (procent_str+"00").substring(0,u_pos+3);
		}//end if	
		
	}else{
		procent_str = "0.00";
	}//end if
	return procent_str;
}//get_procent end




//----------------------------------------------------------------------------------------------------------------
/**
* This function takes all trainings niveaus in km and calculates the total effect points. 
* The return datatype is a string and have allways the format: [12.3] 
* @param {Int} alterative_km_int The km in alternative training niveau
* @param {Int} niveau1_km_int The km in training niveau 1
* @param {Int} niveau2_km_int The km in training niveau 2
* @param {Int} niveau3_km_int The km in training niveau 3
* @param {Int} niveau4_km_int The km in training niveau 4
* @returns {String} The efect points.
*/
function get_effect(alterative_km_int,niveau1_km_int,niveau2_km_int,niveau3_km_int,niveau4_km_int){
	var effect_str=0;
	if(alterative_km_int>0){
		effect_str += alterative_km_int;
	}//end if
	
	if(niveau1_km_int>0){
		effect_str += niveau1_km_int;
	}//end if
	
	if(niveau2_km_int>0){
		effect_str += (1.5*niveau2_km_int);
	}//end if
	
	if(niveau3_km_int>0){
		effect_str += (2.5*niveau3_km_int) ;
	}//end if
	
	if(niveau4_km_int>0){
		effect_str += (4*niveau4_km_int);
	}//end if

	return effect_str;
}//end get_effect




//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a 3 measurement values in millimeters, a bodyweight i kilos, a age in years and a sex in a value of w for woman and m for man. Based on this values will the bodyfat percent be calculated. 
* The return datatype is a string and have allways the format: [12.3] 
* @param {Float} value1_float The 1th measurement point
* @param {Float} value2_float The 2nd measurement point
* @param {Float} value3_float The 3th measurement point
* @param {Float} weight_float The bodyweight
* @param {Int} age_int The age of the person
* @param {String} sex_str The sex of the person
* @returns {String} The bodyfat percent.
*/

function get_jackson_pollock_bodyfat(value1_float,value2_float,value3_float,weight_float,age_int,sex_str){
	var fatsum_float = value1_float*1 + value2_float*1 + value3_float*1;
	
	var density_float;		
	if(sex_str == "m"){
		density_float = 1.10938-0.0008267*fatsum_float+0.0000016 *Math.pow(fatsum_float,2)-0.0002574*age_int;	
		
	}else{
		density_float = 1.0994921-0.0009929*fatsum_float+0.0000023 *Math.pow(fatsum_float,2)-0.0001392*age_int;
	}//end if/else

	var result = (Math.round((495/density_float-450) * Math.pow(10,1))/Math.pow(10,1)).toFixed(1);
	
	if(result<0.3){
		return (0).toFixed(1);	
	}

	return result;
}//end get_jackson_pollock_bodyfat


//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a weight in kg, and a height i cm. Based on this values is the body mass index calculated. 
* The return datatype is a float 
* @param {Float} weight_float The weight of the person
* @param {Int} height_int The height of the person
* @returns {Float} BMI.
*/
function get_body_mass_index(weight_float,height_int){	
		return (weight_float/((height_int/100)*(height_int/100))).toFixed(1);
}//end get_body_mass_index 



//----------------------------------------------------------------------------------------------------------------
/**
* This function takes a bodyfat in percent,a age in years and a sex (m og w). 
* The function then calculate an indication for the helthstate.
* The scheme i based on Jackson and Pollocks bodyfat data
* The return value will be a number between 1 and 4 indicating the grade on helth.
* 1: Lean
* 2: Ideal
* 3: Average wesern 
* 4: Fatt
* The return datatype is a int 
* @param {Float} fatprocent_float The bodyfat percent of the person
* @param {Int} age_int The age of the person
* @param {Str} sex_str The sex of the person
* @returns {Int} Status.
*/
function get_helth_status(fatprocent_float, age_int, sex_str){
	
	if(fatprocent_float<=0){return 0};
	
	var helthlist;
	if (sex_str=="m"){
		
		if(age_int<20){helthlist = new Array(6.3,14.4,20.3)}
		else if(age_int>=20 && age_int<=25){helthlist = new Array(7.4,15.5,21.3)}
		else if(age_int>25 && age_int<=30){helthlist = new Array(10.7,16.5,23.5)}
		else if(age_int>30 && age_int<=35){helthlist = new Array(11.8,17.6,24.6)}
		else if(age_int>35 && age_int<=40){helthlist = new Array(12.8,20.3,25.7)}
		else if(age_int>40 && age_int<=45){helthlist = new Array(13.9,21.4,26.7)}
		else if(age_int>45 && age_int<=50){helthlist = new Array(17.0,22.5,28.8)}	
		else if(age_int>50 && age_int<=55){helthlist = new Array(18.1,25.1,29.8)}	
		else if(age_int>55){helthlist = new Array(19.2,26.1,30.9)}	
		
	}else{
		
		if(age_int<20){helthlist = new Array(17.8,23.3,29.1)}
		else if(age_int>=20 && age_int<=25){helthlist = new Array(18.5,23.9,30.9)}
		else if(age_int>25 && age_int<=30){helthlist = new Array(21.0,26.2,31.6)}
		else if(age_int>30 && age_int<=35){helthlist = new Array(21.6,26.8,33.3)}
		else if(age_int>35 && age_int<=40){helthlist = new Array(22.3,27.4,33.9)}
		else if(age_int>40 && age_int<=45){helthlist = new Array(22.9,28.0,34.5)}
		else if(age_int>45 && age_int<=50){helthlist = new Array(23.5,30.2,35.1)}	
		else if(age_int>50 && age_int<=55){helthlist = new Array(26.0,30.8,36.7)}	
		else if(age_int>55){helthlist = new Array(26.6,31.4,37.3)}
			
	}//end if
	
	if (fatprocent_float<helthlist[0]){return 1}
	else if (fatprocent_float<helthlist[1] && fatprocent_float>=helthlist[0]){return 2}
	else if (fatprocent_float<helthlist[2] && fatprocent_float>=helthlist[1]){return 3}
	else if (fatprocent_float>=helthlist[2]){return 4}
	else{return 0;}			
	
	
}

