/*********************************************************************************

	Copyright 2006 - Jonathan Bourque Olivegren - Dreams Come True
	This program may not be used without written permission from the author.

**********************************************************************************/

tot_hours = 0;
tot_minutes = 0;
tot_seconds = 0;

function string_pad(string) {
	if (string.length == 1) {
		return "0" + string
	} else {
		return string
	}
}

function convert_to_seconds(hours, minutes, seconds) {
	return hours * 3600 + minutes * 60 + seconds;
}

function convert_from_seconds(seconds) {
	var hours, minutes, seconds, remainder = 0;
	remainder = seconds % 3600;
	hours = (seconds - remainder) / 3600;
	seconds = remainder;
	remainder = seconds % 60;
	minutes = (seconds - remainder) / 60;
	seconds = remainder;
	return new Array(hours, minutes, seconds);
}

function calc_time(operator) {
	var hours = document.getElementById("hours");
	var minutes = document.getElementById("minutes");
	var seconds = document.getElementById("seconds");
	
	var clock_hours = document.getElementById("clock_hours");
	var clock_minutes = document.getElementById("clock_minutes");
	var clock_seconds = document.getElementById("clock_seconds");
	
	if (operator == ".") { alert((tot_seconds / 3600) + " timmar "); } else {
	
		just_seconds = convert_to_seconds(new Number(hours.value), new Number(minutes.value), new Number(seconds.value));
		
		tot_seconds = eval("tot_seconds" + operator + "just_seconds");
	
		time_array = convert_from_seconds(tot_seconds);
	
		clock_hours.value = string_pad(time_array[0].toString());
		clock_minutes.value = string_pad(time_array[1].toString());
		clock_seconds.value = string_pad(time_array[2].toString());
	
		hours.value = "0";
		minutes.value = "0";
		seconds.value = "0";
		
		hours.focus();
		hours.select();
	
	}
}

