function $(x){return document.getElementById(x);}

function check4enter(e){
  if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
	if (e==13){
		$('tCalc').focus();
		$('tCalc').click();
		return false;
	}
	else return true;
//  window.alert('The key pressed has keycode '+e+' and is key '+String.fromCharCode( e ) );
	return false;
}

function timerBtnClkd(){
	$('ctlTimerShowAnkr').blur();
	if ($('timerDiv').style.zIndex > 0)fade('timerDiv','out');
	else fade('timerDiv','in');
}

var runTimerTenths = 0;
var runTimerState = 0;

function timerStartStop() {
	if (runTimerState == 0) {
		timerReset();
		runTimerState = 1;
		if ($('tStop')) $('tStop').disabled=false;
		if ( !$('tStart').disabled) $('tStart').disabled='disabled';
		$('tStop').focus();
		runTimerThen = new Date();
		// testing stuff
		/*
		var d = new Date();var h=d.getHours();var m=d.getMinutes()-46;var s=d.getSeconds()-25;
		runTimerThen.setHours(h,m,s);
		*/
		// end testing stuff
		runTimerThen.setTime(runTimerThen.getTime() - runTimerTenths);
		timerDisplay();
	} else {
		runTimerState = 0;
		$('tStart').disabled=false;
		$('tStop').disabled='disabled';
		$('tStart').focus();
		runTimerNow = new Date();
		runTimerTenths = Math.floor((runTimerNow.getTime() - runTimerThen.getTime())/1000);
		document.runTimer.runTimerTime.value = showTime(runTimerTenths);
		calcPace(runTimerTenths);
	}
}

function calcPace(secs){
//	var dist=6.2;
	var dist=roundDec(totalDistMiles,2);
	if (!secs){
		secs=$('runTimerTime').value;
		var hmmss=secs.split(':');
		secs=0;
		secs+=hmmss[0]*3600;
		secs+=hmmss[1]*60;
		secs+=hmmss[2]*1;		// multiplying by 1, otherwise it appends the string
	}
	if (secs==0)return;
	if (dist==0)return;
	if ($('runTimerDist').value)dist=$('runTimerDist').value;//alert(secs+"\n"+dist);
	var milePace=secs/dist;
	$('distToday').innerHTML=dist;
	$('timeToday').innerHTML=showTime(secs);
	$('time1mile').innerHTML=showTime(roundDec(milePace,2));
	$('time5K').innerHTML=showTime(roundDec((milePace*3.1),2));
	$('time5mile').innerHTML=showTime(roundDec((milePace*5),2));
	$('time10K').innerHTML=showTime(roundDec((milePace*6.2),2));
	$('time10mile').innerHTML=showTime(roundDec((milePace*10),2));
	$('timeHalfMarathon').innerHTML=showTime(roundDec((milePace*13.1),2));
	$('time20mile').innerHTML=showTime(roundDec((milePace*20),2));
	$('timeMarathon').innerHTML=showTime(roundDec((milePace*26.2),2));
	$('paceTable').style.display="block";
}

function timerReset() {
	if (document.runTimer.tStop.disabled.value==null){}
	else		{document.runTimer.tStop.disabled.value="disabled";}
//	if (!$('tReset').disabled)$('tReset').disabled='disabled';
//	runTimerState = 0;
	runTimerTenths = 0;
	$('runTimerDist').value='';
	document.runTimer.runTimerTime.value = showTime(runTimerTenths);
	$('paceTable').style.display='none';
}

function timerDisplay() {
	setTimeout("timerDisplay();", 1000);
	if (runTimerState == 1)  {
		runTimerNow = new Date();
		runTimerTenths = Math.floor((runTimerNow.getTime() - runTimerThen.getTime())/1000);
//		document.runTimer.runTimerTime.value = runTimerTenths;
		$('runTimerTime').value = showTime(runTimerTenths);
   }
}

function showTime(secs){
	var dSecs=Math.floor(secs % 60);
	var dMins=Math.floor(((secs-dSecs) / 60) % 60);
	var dHrs=Math.floor(((secs-dSecs) / 3600) % 60);
	return dHrs.toString()+":"+str2(dMins)+":"+str2(dSecs);
}
function str2(x){
	return ((100+x).toString()).substr(1);
}

/*
function showTimer(){
	var p=$('timerDiv').style.left;
	if (p.substr(0,1)=='-'){
		showTimerExtend();
	}else{
		showTimerRetract();
	}
}
function showTimerExtend(){
	var curLeft=$('timerDiv').style.left;
	var curPos=curLeft.substr(0,curLeft.length-2);
	if (curPos>=210){
		timerReset();	
		$('tStart').focus();
		return;
	}
	$('timerDiv').style.left=(parseFloat(curPos)+20).toString()+'px';
	setTimeout('showTimerExtend()',10);
}
function showTimerRetract(){
	var curLeft=$('timerDiv').style.left;//alert(curLeft);
	var curPos=curLeft.substr(0,curLeft.length-2);
	if (curPos<=-450) {
		$('paceTable').style.display='none';
		return;
	}
	$('timerDiv').style.left=(parseFloat(curPos)-20).toString()+'px';
	setTimeout('showTimerRetract()',10);
}

var startTime, stopTime;
function timerStart() {
	$('tStop').disabled=false;
	$('tStart').disabled='disabled';
	var date=new Date();
	startTime=date.getTime();//alert(startTime);
}

function timerStop() {
	$('tStart').disabled=false;
	$('tStop').disabled='disabled';
	var date=new Date();
	stopTime=date.getTime();//alert(startTime+"\n"+stopTime+"\n"+diffTime(startTime,stopTime));
}

function diffTime(beginTime,endTime){
	var date=new Date();
	date=endTime - beginTime;
	return date.toString().substr(0,(date.toString().length-3));
}
*/

//window.onload=timerReset();
