var ms = 0;
var state = 0;

function stop() {
	state = 0;
	if(state == 1) {
		now = new Date();
		ms = now.getTime() - then.getTime();
		document.getElementById('stopwatchDisplay').innerHTML = time(ms);
	}
}

function start() {
	state = 1;
	then = new Date();
	then.setTime(then.getTime() - ms);
}

function startstop() {
	if (state == 0) {
		state = 1;
		then = new Date();
		then.setTime(then.getTime() - ms);
	} else {
		state = 0;
		now = new Date();
		ms = now.getTime() - then.getTime();
		document.getElementById('stopwatchDisplay').innerHTML = time(ms);
	}
}

function swreset() {
	state = 0;
	ms = 0;
	document.getElementById('stopwatchDisplay').innerHTML = time(ms);
}

function display() {
	setTimeout('display();', 50);
	if (state == 1) {
		now = new Date();
		ms = now.getTime() - then.getTime();
		document.getElementById('stopwatchDisplay').innerHTML = time(ms);
	}
}

function two(x) {
	return ((x>9)?"":"0")+x
}

function three(x) {
	return ((x>99)?"":"0")+((x>9)?"":"0")+x
}

function time(ms) {
	var sec = Math.floor(ms/1000)
	ms = ms % 1000
	t = two(ms.toString().substring(0,2))
	
	var min = Math.floor(sec/60)
	sec = sec % 60
	t = two(sec) + ":" + t
	
	var hr = Math.floor(min/60)
	min = min % 60
	t = two(min) + ":" + t
	
	var day = Math.floor(hr/60)
	hr = hr % 60
	//t = two(hr) + ":" + t
	//t = day + ":" + t
	
	return t
}