/*
    Bell control function
*/

function playBell() {
    soundManager.play('bell');
}

function playSmallBell() {
    soundManager.play('sbell');
}

function silentBells() {
    soundManager.stop('sbell');
    soundManager.stop('bell');
}

function containsOnlyNumeric(name) {
    if (name.match(/[^0-9]/) == null) {
        return true;
    }

    alert("Please only input digits.");

    return false;
}

/* 
    Normal Bells 
*/

var timerID=null;

function startTimer(refreshInterval) {
	if (timerID != null) {
		return;
	}

	if (refreshInterval > 0) {
		timerID = setInterval("playSmallBell()", refreshInterval);
    }
}

function stopTimer() {

	if (timerID) {
		clearInterval(timerID);
		timerID  = null;
	}
}

var timerID_b=null;

function startTimer_b(refreshInterval) {
	if (timerID_b != null) {
		return;
	}

	if (refreshInterval > 0) {
		timerID_b = setInterval("playBell()", refreshInterval);
    }
}

function stopTimer_b() {

	if (timerID_b) {
		clearInterval(timerID_b);
		timerID_b  = null;
	}
}


function setStatus(id, msg) {
    var obj = document.getElementById(id);
    obj.innerHTML=msg;
}

function startBells() {
    var inputs = document.getElementsByTagName('input');
    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].name == "interval") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            stopTimer();
            startTimer(inputs[i].value*60*1000);
        } else if (inputs[i].name == "intervalb") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            stopTimer_b();
            startTimer_b(inputs[i].value*60*1000);
        }
    }
    setStatus("status","In Progress");
}

function stopBells() {
    stopTimer();
    stopTimer_b();
    silentBells();
    setStatus("status","Stop");
}

/*
    Random Bells
*/

function getRandomTimeInterval(timeA, timeB) {
    var retval = 0;
    retval = Math.floor(Math.random()*(timeB-timeA) + timeA);
    return retval;
}

var timerID_rand=null;
var timeA = 0;
var timeB = 0;

function playRandomBell() {
    soundManager.play('bell');
    var intval = getRandomTimeInterval(timeA, timeB);

	if (intval > 0) {
		timerID_rand = setTimeout("playRandomBell()", intval);
    }
}

function stopTimer_rand() {

	if (timerID_rand) {
		clearTimeout(timerID_rand);
		timerID_rand  = null;
	}
}

function startRandomBell() {
    var inputs = document.getElementsByTagName('input');
    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].name == "interval_randA") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            timeA = inputs[i].value*60*1000;
        } else if (inputs[i].name == "interval_randB") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            timeB = inputs[i].value*60*1000;
        }
    }
    stopTimer_rand();

	timerID_rand = setTimeout("playRandomBell()", getRandomTimeInterval(timeA, timeB));

    setStatus("status_rand","In Progress");
}

function stopRandomBell() {
    stopTimer_rand();
    silentBells();
    setStatus("status_rand","Stop");
}


/*
    Reminder Bell
*/


var timerID_reminder=null;

function playReminderBell() {
	if (timerID_reminder != null) {
		return;
	}
    
    soundManager.play('bell');

    stopTimer_reminder();
    setStatus("status_reminder","Time passed");
}

function stopTimer_reminder() {

	if (timerID_reminder) {
		clearInterval(timerID_reminder);
		timerID_reminder  = null;
	}
}

function startReminderBell() {
    var inputs = document.getElementsByTagName('input');
    var hour = 0;
    var min = 0;
    var sec = 0;
    var d = new Date()

    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].name == "time_HH") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            d.setHours(inputs[i].value);
        } else if (inputs[i].name == "time_MM") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            d.setMinutes(inputs[i].value);
        } else if (inputs[i].name == "time_SS") {
            if (!containsOnlyNumeric(inputs[i].value)) {
               return; 
            }
            d.setSeconds(inputs[i].value);
        }
    }
    stopTimer_reminder();

    var now = new Date();
    var diff = d.getTime() - now.getTime();
    if (diff <= 0) {
        alert("Please input a future time.");
        return;
    }

	setTimeout("playReminderBell()", diff);

    setStatus("status_reminder","In Progress");
}

function stopReminderBell() {
    stopTimer_reminder();
    silentBells();
    setStatus("status_reminder","Stop");
}

/*
    Clock related functions
*/
function addZero(vNumber) { 
    return ((vNumber < 10) ? "0" : "") + vNumber;
} 

function dateDisplayLookup(val) {
    return addZero(val.getMonth()+1)+"/"+addZero(val.getDate())+"/"+val.getFullYear()+"  "+addZero(val.getHours())+":"+addZero(val.getMinutes())+":"+addZero(val.getSeconds());
}

var clockID = 0;

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }

   var tDate = new Date();

   document.getElementById("theTime").firstChild.data = dateDisplayLookup(tDate);
   // document.getElementById("theTime").firstChild.data = tDate.toString();

/*
   document.getElementById("theTime").firstChild.data = "" 
                                   + tDate.getHours() + ":" 
                                   + tDate.getMinutes() + ":" 
                                   + tDate.getSeconds();
*/                                   

   clockID = setTimeout("UpdateClock()", 1000);
}

function startClock() {
   clockID = setTimeout("UpdateClock()", 500);
}

function stopClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}

function init() {
    soundManagerInit();
    startClock();
}

function cleanUp() {
    stopTimer();
    stopTimer_b();
    stopClock();
}
