var totalTime = new TimeObj(0, 0);
var times = [];
var isIe = window.event ? true : false;

function isFloat(n)
{
	return n - Math.floor(n) > 0;
}

// Time Object.
// Constructor:
//	converts minutes over 60 to hours and rest minutes;
// 	converts fractions of hours to minutes.
function TimeObj(h, m, s)
{
	this.h = h > 0 ? h : 0;
	this.m = m > 0 ? m : 0;
	this.s = s > 0 ? s : 0;

	// add seconds over 60 to minutes
	if ( this.s > 59 )
	{
		this.m += Math.floor(this.s / 60);
		this.s = this.s % 60;
	}	
		
	// add minutes over 60 to hours
	if ( this.m > 59 )
	{
		this.h += Math.floor(this.m / 60);
		this.m = this.m % 60;
	}
	
	this.getHours = function()
	{
		return this.h;
	}
	
	this.getMinutes = function()
	{
		return this.m;
	}
	
	this.getSeconds = function()
	{
		return this.s;
	}
	
	this.getFormattedTime = function(format)
	{
		// format as strings.
		// mins, secs: add heading 0 if under 10
		var hStr = this.h.toString();
		var mStr = this.m < 10 ? '0' + this.m.toString() : this.m.toString();
		var sStr = this.s < 10 ? '0' + this.s.toString() : this.s.toString();

		if( format == 'table' )
		// return as html table row (without <tr></tr>)
		{
			var ret = "<td>" + hStr + ":</td>\n";
			ret += "\t<td>"+ mStr +":</td>\n";
			ret += "\t<td>"+ sStr +"</td>\n";
		}
		else
		// return as string
		{
			var ret = hStr + ':' + mStr + ':' + sStr;
		}
		return ret;
	}
}

function calculate()
{  
	var hours = document.getElementById("h").value;
	if ( isNaN(hours) )
	{
		 alert("Please enter hours as a number.");
		 return;
	}

	var minutes = document.getElementById("m").value;
	if ( isNaN(minutes) )
	{
		 alert("Please enter minutes as a number");
		 return;
	}

	var seconds = document.getElementById("s").value;
	if ( isNaN(seconds) )
	{
		 alert("Please enter seconds as a number");
		 return;
	}
	
	if ( (hours.length == 0) && (minutes.length == 0) && (seconds.length == 0) )
	{
		 alert("Please enter some hours, minutes, or seconds to add up.");
		 return;
	}

	hours = parseInt(hours);
	minutes = parseInt(minutes);
	seconds = parseInt(seconds);
	
	if ( hours < 0 )
	{
		alert("Please enter hours as a positive number (more than 0)");
		return;
	}	
	if ( minutes < 0 )
	{
		alert("Please enter minutes as a positive number (more than 0)");
		return;
	}
	if ( seconds < 0 )
	{
		alert("Please enter seconds as a positive number (more than 0)");
		return;
	}
	
	// reset input form
	document.getElementById("h").value = '';
	document.getElementById("m").value = '';
	document.getElementById("s").value = '';
	
	// create new time object
	var time = new TimeObj(hours, minutes, seconds);
	
 	// add to total
	totalTime = new TimeObj(
		totalTime.getHours() + time.getHours(),
		totalTime.getMinutes() + time.getMinutes(),
		totalTime.getSeconds() + time.getSeconds()
	);
	
	times.push(time);

	var x = document.getElementById("results");
	var results = "<table><tr><th class='right'>h</th><th class='right'>m</th><th class='right'>s</th></tr>";

	for(var i = 0; i < times.length; i++)
	{
		results += '<tr>' + times[i].getFormattedTime('table') + "</tr>\n";
	}
	
	results += "<th>Total Time</th>"
	results += '<tr class="total">' + totalTime.getFormattedTime('table') + "</tr></table>\n";

	x.innerHTML = results;
	x.style.display = "block";
}

$(document).ready(function()
{
	// attach return key listener to input elements
	$('input').bind('keydown', function(e){
		e = e || window.event;

		if(e.type=='keydown' || e.type=='onkeydown')
		{
			var code = isIe ? e.keyCode : e.which;
		 	if ( code != 13 )
				return;
			else
				calculate();
		}
		
	});
});
