var dt_hours_tRows = 1; //total
var dt_hours_vRows = 1; //visible

dt_hours_addRow = function()
{
	if ( dt_hours_tRows == dt_hours_vRows )
	{
		dt_hours_tRows++;
		dt_hours_vRows++;

		var row = document.createElement('div');
		row.id = "dt_hours_r" + dt_hours_tRows;
		row.innerHTML = "<div class='x'><input type='text' id='dt_hours_h" + dt_hours_tRows + "'></div><div class='x'><input type='text' id='dt_hours_m" + dt_hours_tRows + "'></div><div class='x'><input type='text' id='dt_hours_s" + dt_hours_tRows + "'></div>";
		row.className = "row datarow";
		document.getElementById("dt_hours_moreRows").appendChild ( row );
		return;
	}
	dt_hours_vRows++;
	document.getElementById("dt_hours_r" + dt_hours_vRows).style.display = "block";
};

dt_hours_removeRow = function()
{
	if ( dt_hours_vRows == 1 ) {
		alert("Can't remove the first row");
		return;
	}
	document.getElementById("dt_hours_r" + dt_hours_vRows).style.display = "none";
	dt_hours_vRows--;
};

dt_hours_getFormVal = function( el, err )
{
	var sVal = document.getElementById(el).value;
	sVal = sVal.DT_trim();
	if ( sVal.length == 0 ) {
		return 0;
	}
	sVal = DT_F.fixNumber(sVal);
	fVal = parseFloat(sVal);
	if ( isNaN(fVal) ) {
		throw err;
	}
	return fVal;
};

dt_hours_calculate = function()
{
	var html = [];

	var totalTime = 0;
	var h, m, s, d;
	for ( var i=1; i<=dt_hours_vRows; i++ )
	{
		try
		{
			h = dt_hours_getFormVal( "dt_hours_h" + i, "Please enter a valid number of hours" );
			m = dt_hours_getFormVal( "dt_hours_m" + i, "Please enter a valid number of minutes" );
			s = dt_hours_getFormVal( "dt_hours_s" + i, "Please enter a valid number of seconds" );
		}
		catch ( ex )
		{
			alert( "Error on row " + i + ": " + ex );
		}

		var sum = h*3600 + m*60 + s;
		totalTime += sum;
	}

	if ( totalTime < 0 ) {
		s = totalTime * -1;
	}
	else {
		s = totalTime;
	}
	
	h = Math.floor(s/3600);
	s -= h*3600;
	m = Math.floor(s/60);
	s -= m*60;

	if ( totalTime < 0 )
	{
		h = h*-1;
		m = m*-1;
		s = s*-1;
	}
	
	html.push("The total time is equal to the following: ");
	html.push("<ul>");

	var z = "";
	if ( h != 0 ) {
		z += h + " hours, ";
	}
	if ( m != 0 ) {
		z += m + " minutes, ";
	}
	z += DT_F.round(s,4) + " seconds";

	html.push("<li><b>" + z + "</b></li>");
	html.push("<li>" +  DT_F.addCommas( DT_F.round(totalTime,4) ) + " seconds</li>");
	minutes = totalTime / 60;
	html.push("<li>" +  DT_F.addCommas( DT_F.round(minutes,4) ) + " minutes</li>");
	hours = totalTime / 3600;
	html.push("<li>" +  DT_F.addCommas( DT_F.round(hours,4) ) + " hours</li>");
	html.push("</ul>");

	html.push("<div class=\"note\">(All results rounded to the nearest 0.0001)</div>");
	
	var x = document.getElementById("dt_hours_results");
	x.innerHTML = html.join('');
	x.style.display = "block";
};
