
function calcTime()
{
	var status=0
	var endSecs
	var startSecs
	var diffSecs

	if (validTime(document.timecalc.endTime.value) > 0) {
		alert("Invalid end time!")
		status=1
		document.timecalc.endTime.focus()
		document.timecalc.endTime.select()
	}
	if (validTime(document.timecalc.startTime.value) > 0) {
		alert("Invalid start time!")
		status = 1
		document.timecalc.startTime.focus()
		document.timecalc.startTime.select()
	}
	if (status == 0) {
		endSecs = getTime(document.timecalc.endTime.value)
		startSecs = getTime(document.timecalc.startTime.value)
	
		diffSecs = formatTime(endSecs - startSecs)
		document.timecalc.elapsedTime.value = diffSecs	
		if (	document.pacecalc.time.value == "")
			document.pacecalc.time.value = diffSecs
	}
}

function validTime(strTime)
{
	var status=0
	var ch
	
	if (strTime.length != 8) {
		status = 1
	}
	
	for (var i = 0; i < strTime.length; i++) {
		ch = strTime.substring(i, i+1)
		if (ch < "0" || ch > "9") {
			if (ch != ":" && ch != ".") {
				status = 2;
       	 	}
		}
	}
	return status;
}

function formatTime(strTime)
{
	var mins,secs;

	mins = Math.floor(strTime/60);
	secs = Math.floor(strTime - (mins*60));
	if (secs > 10)
		return (mins + ":" + secs);
	else
		return (mins + ":0" + secs);
}

function getTime(strTime)
{
	var hrs, mns, scs
	
	hrs = strTime.substring(0,2)
	mns = strTime.substring(3,5)
	scs = strTime.substring(6,8)
	return (parseInt(hrs)*60*60) + (parseInt(mns)*60) + parseInt(scs)
}


// pace calc code starts here
function calcPace ()
{
	var status=0
	var colonCnt=0
	var tmpTime=""
	var tmpDist=""
	var tmpClimb=""
    var totalDist
	var totalTime
		
	tmpDist = document.pacecalc.dist.value
	tmpTime = document.pacecalc.time.value
	tmpClimb = document.pacecalc.climb.value
	
	if (tmpDist.length == 0 || tmpTime.length == 0) {
		status = 1
	}
	
	if (status == 0) {
		for (var i = 0; i < tmpDist.length; i++) {
			var ch = tmpDist.substring(i, i+1)
			if (ch < "0" || ch > "9") {
				if (ch != ".") {
					status =  2;
    	     	}
			}
		}
		for (var i = 0; i < tmpTime.length; i++) {
			var ch = tmpTime.substring(i, i+1)
			if ((tmpTime.charAt(i) == ":") || (tmpTime.charAt(i) == ".")) {
				if ((tmpTime.substring(i+1,tmpTime.length)) > 59) {
					status = 3;
				}
			}
			if (tmpTime.charAt(i) == ":" || tmpTime.charAt(i) == ".") {
				if (++colonCnt > 1)
					status=3
			}
							
			if (ch < "0" || ch > "9") {
				if (ch != ":" && ch != ".") {
					status = 3;
        	 	}
			}
		}

		for (var i = 0; i < tmpClimb.length; i++) {
			var ch = tmpClimb.substring(i, i+1)
			if (ch < "0" || ch > "9") {
				if (ch != ".") {
					status =  4;
   	     		}
			}
		}
	}
	
	if (status == 0) {
		totalDist = parseFloat(tmpDist) + parseFloat(tmpClimb/100)
		totalTime = calcSeconds(tmpTime)
		document.pacecalc.pace.value=formatPace(totalTime,totalDist)
	}
	if (status == 1) {
		alert("Must enter both time and distance!")
		document.pacecalc.dist.focus()
		document.pacecalc.dist.select()
		document.pacecalc.pace.value = ""
	} else if (status == 2) {
		alert("Invalid distance!")
		document.pacecalc.dist.focus()
		document.pacecalc.dist.select()
		document.pacecalc.pace.value = ""
	} else if (status == 3) {
		alert("Invalid time!")
		document.pacecalc.time.focus()
		document.pacecalc.time.select()
		document.pacecalc.pace.value = ""
	} else if (status == 4) {
		alert("Invalid climb!")
		document.pacecalc.climb.focus()
		document.pacecalc.climb.select()
		document.pacecalc.pace.value = ""
	}

	return 0;
}

function formatPace(totalTime, totalDist)
{
	var mins,secs;
	var secsPerK;

	secsPerK = totalTime / totalDist;	
	
	mins = Math.floor(secsPerK/60);
	secs = Math.floor(secsPerK - (mins*60));
	if (secs > 10)
		return (mins + ":" + secs);
	else
		return (mins + ":0" + secs);
}

function calcSeconds(sTime)
{
	var mins,secs;

	if(sTime.length==0)
		return "nn:nn";

	for (var i=0;i<sTime.length;i++) {
		if ((sTime.charAt(i) == ":") || (sTime.charAt(i) == ".")) {
			mins = sTime.substring(0,i);
			secs = sTime.substring(i+1,sTime.length);
			return ((Math.floor(mins)*60) + Math.floor(secs));
		}
	}
	return "nn:nn";
}
// pace calc code ends

function paceReset()
{
	document.pacecalc.dist.value=""
	document.pacecalc.time.value=""
	document.pacecalc.climb.value=""
	document.pacecalc.pace.value=""
	document.pacecalc.dist.focus()
}

function timeReset()
{
	document.timecalc.endTime.value="hh:mm:ss"
	document.timecalc.startTime.value="hh:mm:ss"
	document.timecalc.elapsedTime.value=""
	document.timecalc.endTime.focus()
	document.timecalc.endTime.select()
}

function init()
{
	document.timecalc.endTime.focus()
	document.timecalc.endTime.select()
}

