// JavaScript Document
function calcPace(elapsedTime,distance) {

        //calculates the pace in min/mi given the distance and decimal time in minutes 
        //then converts the pace to a min and sec format
        var retStr = "0";

        if (distance <= 0) {
            //the runner has not traveled a distance
            retStr = "Undefined";
		}
        else {
            //calculate the pace
            var pace = elapsedTime / distance;
            //var paceMin = pace - (pace Mod 1);
			var paceMin = Math.floor(pace);
            var paceSec = (pace - paceMin) * 60;

            //format the pace into a string
			//retStr = "[" + paceMin + " min " + decimalPlaces(paceSec,2) + " sec] / mile";
			retStr = paceMin + " min " + decimalPlaces(paceSec,2) + " sec";
        }
        return (retStr);
}

function milesToKm(miles) {
        //converts miles to kilometers
        return (miles / 0.6214);
}
  
function kmToMiles(km) {
        //converts kilometers to miles
        return (km * 0.6214);
}

function clockToDecimal(mins, secs) {
       //converts minutes and seconds to minutes in decimal format
       return (parseFloat(mins) + parseFloat(secs / 60));
}

function myAlert(alertMsg) {
        window.alert(alertMsg);
		return (0);
}

function decimalPlaces(val, places) {
	var retval = 0;
	if (val.toFixed) { //if browser supports toFixed() method
		retval = val.toFixed(places);
	}
	else {
		factor = Math.pow(10,places);
		val *= factor;
		val = Math.round(val);
		val /= factor;
		retval = val;
	}
	return retval;
}

function LTrim (s) {
	//trim lefthand spaces
	while(''+s.charAt(0)==' ') {
		s = s.substring(1,s.length);
	}
	return s;
}

function RTrim (s) {
	//trim righthand spaces	
	if (s.length > 1) {
		while(''+s.charAt(s.length-1)==' ') {
			s = s.substring(0,s.length-1);
		}
	}
	return s;
}

function trim (str) {
	str=LTrim(str);
	str=RTrim(str);
	return str;
}


