HM = {}

// Time Object.
// Constructor:
//	converts minutes over 60 to hours and rest minutes;
// 	converts fractions of hours to minutes.
HM.TimeObj = function(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;
	}
}

/* Our Member Variables */
HM.totalTime = new HM.TimeObj(0, 0);
HM.times = [];
/* ******************** */


HM.calculate = function() {
	var hours = document.getElementById("dt_hours_h").value;
	if ( isNaN(hours) )
	{
		 alert("Please enter hours as a number.");
		 return;
	}

	var minutes = document.getElementById("dt_hours_m").value;
	if ( isNaN(minutes) )
	{
		 alert("Please enter minutes as a number");
		 return;
	}

	var seconds = document.getElementById("dt_hours_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("dt_hours_h").value = '';
	document.getElementById("dt_hours_m").value = '';
	document.getElementById("dt_hours_s").value = '';
	
	// create new time object
	HM.time = new HM.TimeObj(hours, minutes, seconds);
	
 	// add to total
	HM.totalTime = new HM.TimeObj(
		HM.totalTime.getHours() + HM.time.getHours(),
		HM.totalTime.getMinutes() + HM.time.getMinutes(),
		HM.totalTime.getSeconds() + HM.time.getSeconds()
	);
	
	HM.times.push(HM.time);

	var x = document.getElementById("hours_dt_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 < HM.times.length; i++)
	{
		results += '<tr>' + HM.times[i].getFormattedTime('table') + "</tr>\n";
	}
	
	results += "<th>Total Time</th>"
	results += '<tr class="total">' + HM.totalTime.getFormattedTime('table') + "</tr></table>\n";

	x.innerHTML = results;
	x.style.display = "block";
}

