function calculate(row) {
	
	// clear all output fields
	for (var i = 1; i<=43; i++) {
		document.getElementById('pace' + i).value = '';
		document.getElementById('speed' + i).value = '';
		document.getElementById('cumDist' + i).value = '';
		document.getElementById('cumTime' + i).value = '';
		document.getElementById('cumPace' + i).value = '';
		document.getElementById('cumSpeed' + i).value = '';
	}

	// update cumulative distance

	var previousDistance;
	var thisDistance;
	var interimDistance;
	var cumulativeDistance;
	var thisHours;
	var thisMinutes;
	var thisSeconds;
	var thisFractions;
	var totalSeconds;
	var cumulativeTime;
	var previousSeconds
	var cumulativeSeconds;
			
	for (var i = 1; i<=row; i++) {

		if (i == 1) {
			previousDistance = 0;
			previousSeconds = 0;
		} else {
			previousDistance = document.getElementById('cumDist' + (i-1)).value;			
			previousSeconds = document.getElementById('cumSeconds' + (i-1)).value;
		}
		
		thisDistance = document.getElementById('dist' + i).value * 1;

		thisHours = document.getElementById('hh' + i).value * 1;
		thisMinutes = document.getElementById('mm' + i).value * 1;
		thisSeconds = document.getElementById('ss' + i).value * 1;
		thisFractions = document.getElementById('xx' + i).value * 1;	
		
		totalSeconds = thisHours * 3600 + thisMinutes * 60 + thisSeconds + thisFractions / 100
		
		document.getElementById('pace' + i).value = secondsToTime(totalSeconds/thisDistance);
		document.getElementById('speed' + i).value = formatNumber(thisDistance/totalSeconds * 3600, 2);

		cumulativeSeconds = previousSeconds * 1 + totalSeconds * 1;
		document.getElementById('cumSeconds' + i).value = cumulativeSeconds;
		
		cumulativeTime = secondsToTime(cumulativeSeconds);
		document.getElementById('cumTime' + i).value = cumulativeTime;
		
		cumulativeDistance = previousDistance * 1 + thisDistance * 1;		
		document.getElementById('cumDist' + i).value = cumulativeDistance;
		
		document.getElementById('cumPace' + i).value = secondsToTime(cumulativeSeconds/cumulativeDistance);
		document.getElementById('cumSpeed' + i).value = formatNumber(cumulativeDistance/cumulativeSeconds * 3600, 2);
	}
	
}

function secondsToTime(secs) {

	var ret = "";                 // Return value
	var s = Math.floor(secs); 	  // Whole seconds
	var m = Math.floor(s/60);     // Whole minutes
	var h = Math.floor(m/60);     // Whole hours

	if (h < 10)
	 ret += "0";
	ret += h;

	ret += ":";

	var m = m%60;

	if (m < 10)
	 ret += "0";
	ret += m;

	var s = s%60;

	ret += ":";

	if (s < 10)
	 ret += "0";
	ret += s;

	return ret;

}

function formatNumber(expr, decimals) {
	var str = "" + Math.round( eval(expr) * Math.pow(10,decimals))
	while (str.length <= decimals) { str = "0" + str } 
	var decpoint = str.length - decimals; 
	var result = str.substring(0,decpoint);
	if (decimals) result += "." + str.substring(decpoint,str.length);
	return result;
}

function example(who) {
	
	if (who == 'Phil_Riverkeeper') {
		var distArray = ['2', '.5', '.5', '1', '1', '2', '.5', '1.5', '1']
		var mmArray = ['9', '2', '2', '5', '5', '10', '2', '8', '4']
		var ssArray = ['53', '40', '28', '04', '20', '41', '54', '06', '55']
		var xxArray = ['68', '31', '04', '15', '88', '01', '12', '05', '11']
		var text = 'This was a 10 KM run that was 2 x 5K out-and-backs. Each out and back was 2.5 km. The mileage markers were set up oddly for this course, and I missed a few, so I took some odd splits. Using this tool, I am able to identify what my pace was along the route, and how my cumulative pace changed with distance. I wanted to keep 5:00 pace for the run, and was doing well up unitl 4K, where the wheels fell off a bit!';
	} else if(who == 'Lance_Boston2008') {
		var distArray = ['5', '5', '5', '5', '1.1', '3.9', '5', '5', '5', '2.2']
		var mmArray = ['20', '20', '20', '20', '4', '15', '19', '20', '19', '8']
		var ssArray = ['11', '58', '44', '49', '10', '22', '51', '01', '55', '57']
		var xxArray = ['', '', '', '', '', '', '', '', '', '']
		var text = 'This was Lance Armstrong\'s 2008 Boston Marathon. Check out the surge he made coming up to the half-marathon mark, dropping his pace from 4:09 to 3:47.';
	}
	
	document.timeForm.reset();
	
	for (i = 0; i < distArray.length; i++) {
		document.getElementById('dist' + (i+1)).value = distArray[i];
		document.getElementById('mm' + (i+1)).value = mmArray[i];
		document.getElementById('ss' + (i+1)).value = ssArray[i];
		document.getElementById('xx' + (i+1)).value = xxArray[i];		
	}
	
	calculate(i);
	
	window.alert(text)
}

