﻿var timer = "";
var timerID = null
var timerRunning = false

function startTimer(){
    // Stop the clock (in case it's running), then make it go.
    stopTimer()
    runClock()
}

function stopTimer(){
	//stop the clock
	if(timerRunning){
		clearTimeout(timerID)
		timerRunning = false
	}
}

function runClock(){
	timer = timeNow24();
	$("#timer").html(timer);
	//Notice how setTimeout() calls its own calling function, runClock().
	timerID = setTimeout("runClock()",1000);
	timerRunning = true;
}

function timeNow24(){
	//Grabs the current time and formats it into hh:mm:ss am/pm format.
	now = new Date()
	hours = now.getHours()
	minutes = now.getMinutes()
	seconds = now.getSeconds()
	timeStr = hours;
	timeStr  += ((minutes < 10) ? ":0" : ":") + minutes
	timeStr  += ((seconds < 10) ? ":0" : ":") + seconds
	return timeStr
}
function timeNow12(){
	//Grabs the current time and formats it into hh:mm:ss am/pm format.
	now = new Date()
	hours = now.getHours()
	minutes = now.getMinutes()
	seconds = now.getSeconds()
	timeStr = "" + ((hours > 12) ? hours - 12 : hours)
	timeStr  += ((minutes < 10) ? ":0" : ":") + minutes
	timeStr  += ((seconds < 10) ? ":0" : ":") + seconds
	timeStr  += (hours >= 12) ? " PM" : " AM"
	return timeStr
}


