/**
File: countdown_timer.js
Description: Common javascript functions used in the HR online application
Created by William Haun, August 8, 2005
Last Updated:
----------------------------------------------------
FUNCTION LIST
----------------------------------------------------
startCountdown(t_min,r_min=2,r_msg="",s_msg=""):Void
countDown(tick:Number):Void
----------------------------------------------------
**/

var reminder_min=2;
var reminder_msg;
var status_msg="Time left to finish - ";

function startCountdown(t_min) {
	if(!reminder_msg) {
		if(reminder_min==1) reminder_msg = "You have "+reminder_min+" minute left";
		else reminder_msg = "You have "+reminder_min+" minutes left";
	}
	countDown((t_min*60));	
}

function countDown(tick) {
	var time = status_msg;
	var minute = Math.floor(tick / 60);
	if (minute < 10) time += "0";
	time += minute + ":";
	var second = tick % 60;
	if (second < 10) time += "0";
	time += second;
	window.status = time;
	--tick;
	if (tick == (reminder_min*60)) {
		var beforeDate = new Date()
		var beforeTime=Math.floor(beforeDate.getTime()/1000);
		window.alert(reminder_msg);
		var afterDate = new Date()
		var afterTime=Math.floor(afterDate.getTime()/1000);
		tick = tick-(afterTime-beforeTime);
		if (tick == 0) {
			window.status = "Time is up";
			return;
		}
	} else if (tick == 0) {
		window.status = "Time is up";
		tick = 0;
		return;
	}
	var command = "countDown(" + tick + ")";
	window.setTimeout(command,1000);
}