// JavaScript functions to run the timer controls.

var timerID = null;
var tStart  = null;
var time = null;

function updateWorkTimer() {
    if(timerID != null) {
        clearTimeout(timerID);
        timerID = null;
    }

    if(!tStart)
        tStart = new Date().getTime();

    var tDiff = new Date().getTime() - tStart;
    var displayTime  = null;
    var hrs = 0;
    var min = 0;
    var sec = 0;

    tDiff = Math.floor(tDiff/1000);
    sec = tDiff % 60;

    tDiff = Math.floor(tDiff / 60);
    min = tDiff % 60;

    hrs = Math.floor(tDiff / 60);
    
    if (hrs > 9)
        displayTime = hrs;
    else 
        displayTime = "0" + hrs;

    displayTime = displayTime + ":";
    if (min > 9)
        displayTime = displayTime + min;
    else 
        displayTime = displayTime + "0" + min;

    displayTime = displayTime + ":";
    if (sec > 9)
        displayTime = displayTime + sec;
    else 
        displayTime = displayTime + "0" + sec;

    document.wiForm.workTime.value = displayTime;
    timerID = setTimeout("updateWorkTimer()", 1000);
}

function isNotInt(num) {
    var result = false;
    var strNum = "" + num;
    var idx = strNum.indexOf(".");
    if (!isNaN(num)) {
        if (idx >= 0) {
            result = true;
        }
    }
    else {
        result = false;
    }
    return result;
}

function startWorkTimer() {
    var curWorkTime = document.wiForm.workTime.value;
    if (curWorkTime != "00:00:00") {
        var idx = curWorkTime.indexOf(":");
        var hrs = 0;
        var min = 0;
        var sec = 0;
        if (idx == -1) {
            hrs = curWorkTime;
        }
        else {
            hrs = curWorkTime.substring(0,idx);
            curWorkTime = curWorkTime.substr(idx+1);
            idx = curWorkTime.indexOf(":");
            if (idx == -1) {
                min = curWorkTime;
            }
            else {
                min = curWorkTime.substring(0,idx);
                sec = curWorkTime.substr(idx+1);
            }
        }
        if (isNotInt(hrs) | isNotInt(min) | isNotInt(sec)) {
            alert("The time needs to be entered in HH:mm:ss format");
            return;
        }

        var time = 0 + (hrs*60*60*1000) + (min*60*1000) + (sec*1000);
        tStart = 0 + new Date().getTime() - time;
    }
    else {
        tStart   = new Date().getTime();
    }
    document.wiForm.workTimerControl.value = " Stop  ";
    timerID = setTimeout("updateWorkTimer()", 1000);
}

function stopWorkTimer() {
    if (timerID != null) {
        clearTimeout(timerID);
        document.wiForm.workTimerControl.value = " Start  ";
    }
    timerID = null;
    tStart = null;
}

function toggleWorkTimer() {
    if(timerID != null) {
        stopWorkTimer();
    }
    else {
        startWorkTimer();
    }
}

function resetWorkTimer() {
   tStart = null;
   document.wiForm.workTime.value = "00:00:00";
}

function getElement(name)
{
  for (i = 0; i < document.wiForm.elements.length; i++) {
      if (document.wiForm.elements[i].name == name) {
          return document.wiForm.elements[i];
      }
  }
  return null;
}

function createList(selBox,vals){
selBox.options.length = 0;
    for(i=0; i<vals.length; i++){
        selBox.options[i] = new Option(vals[i],vals[i]);
    }
}


// SCRIPTS FOR THE TIMER.
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

function getthedate()
{
	var mydate=new Date();
	var year=mydate.getYear();
	if (year < 1000)
		year+=1900;
	var day=mydate.getDay();
	var month=mydate.getMonth();
	var daym=mydate.getDate();
	if (daym<10)
		daym="0"+daym;
	var hours=mydate.getHours();
	var minutes=mydate.getMinutes();
	var seconds=mydate.getSeconds();
	var dn="AM";
	if (hours>=12)
		dn="PM";
	if (hours>12)
		hours=hours-12;
	if (hours==0)
		hours=12;
	if (minutes<=9)
		minutes="0"+minutes;
	if (seconds<=9)
		seconds="0"+seconds;
// var cdate=dayarray[day]+", "+montharray[month]+" "+daym+", "+year+" "+hours+":"+minutes+":"+seconds+" "+dn
	var cdate=dayarray[day]+", "+montharray[month]+" "+daym+", "+year+" "+hours+":"+minutes+" "+dn;
	if (document.all)
		document.all.clock.innerHTML=cdate;
	else if (document.getElementById)
		document.getElementById("clock").innerHTML=cdate;
	else
	document.write(cdate);
}
if (!document.all&&!document.getElementById)
	getthedate();
function goforit()
{
	if (document.all||document.getElementById)
		setInterval("getthedate()",1000);
}
