//
// Title: Speech Timer for Toastmasters
// Author: Stan Birdwell 
// Date: March 26, 2001
// **************************** Copyright Notice ****************************
// Copyright (C) 2001 Stan Birdwell

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ***************************************************************************
// This timer works in conjunction with timer.html. The timer is launched when the Start/Restart button in
// timer.html is clicked. The timer increments from zero until the desired count is reached. Timer modes are 
// initialized from the timer mode radio buttons in timer.html. 
//

var Stop = 0;
var Start = 0;
var setCount = 0; // initialize counter to zero
var modeValue = 1;
var flash = false;
var flashing = 0;
//
// The getStarted() function is called by the "Start/Restart Timer" button in timer.html
//
function getStarted(count) {
	// need to set Stop = 0 here so that if Reset Timer button pressed more 
	//than once, Stop will always be reset to zero when Start button is pressed.
	Stop = 0; 
	// Test to see if Timer has already been started. If No, then proceed with count down, 
	// if Yes, then reset timer.
	if (Start == 0) { 
	  	//-- Loads the number specified (passed via count) into the form field. 
		document.myForm.Mode.value = parseInt(modeValue);	
		document.myForm.numberShown.value= parseInt(count);
		convertSec(count);
		writeMinSec();
		document.holder.src = imageoff.src;
		document.holder1.src = imageoff.src;
		document.holder2.src = imageoff.src;
	  	//-- Pauses one-second, then calls the addOne function at 1 second intervals.
	  	myTimerID=setInterval("addOne()",1000)
		// Set Start = 1 so we know that timer has been started.
		Start = 1;
		}
		// If timer already started, restart timer. 
		// Pass value of flashing so restartTimer function can determine if timer is flashing
		// restartTimer function will turn of flashing lights if they are flashing
		else restartTimer(flashing);
}

//-- This function adds one to the current value in the form field

function addOne() {
	//Add 1 to current value in form field.
	var newValue = parseInt(document.myForm.numberShown.value) + 1
	//Replace the contents of form field with smaller number.
	document.myForm.numberShown.value = newValue
	document.myForm.timerID.value = myTimerID
	convertSec(newValue);
	writeMinSec();
	// write the following values for debug
	document.myForm.flashing.value = flashing
	document.myForm.flash.value = flash	
	//if RESET not clicked, do the following
	if (Stop == 0) { // modeValue 0 =5-15 sec., modeValue 1 1-2 min, modeValue 2 = 2-3 min, modeValue 3 = 3 min, modeValue 4 = 3-4 min, modeValue 5 = 3-5 minutes, modeValue 6 = 4-6 minutes, modeValue 7 = 5 minutes, modeValue 8 = 5-7 min, modeValue 9 = 6-8 min, modeValue 10 = 7-9 min, modeValue 11 = 8 min, modeValue 12 = 8-10 min, modeValue 13 = 10-12 min, modeValue 14 = 10-14 min, modeValue 15 = 12-15 min, modeValue 16 = 13-15 min, modeValue 17 = 15-20 min, modeValue 18 = 20-30 min, modeValue 19 = 20-40 min, modeValue 20 = 30-40 min, modeValue 21 = 10 min,
		//check for Greenlight
		if ((newValue == 5) && (modeValue == 0)){  
			reachedGreenLight()
		}
		if ((newValue == 60) && (modeValue == 1)){  
			reachedGreenLight()
		}
		if ((newValue == 120) && (modeValue == 2)){  
			reachedGreenLight()
		}
		if ((newValue == 150) && (modeValue == 3)){  
			reachedGreenLight()
		}
		if ((newValue == 180) && (modeValue == 4)){  
			reachedGreenLight()
		}
		if ((newValue == 180) && (modeValue == 5)){  
			reachedGreenLight()
		}
		if ((newValue == 240) && (modeValue == 6)){  
			reachedGreenLight()
		}
		if ((newValue == 270) && (modeValue == 7)){  
			reachedGreenLight()
		}
		if ((newValue == 300) && (modeValue == 8)){  
			reachedGreenLight()
		}
		if ((newValue == 360) && (modeValue == 9)){  
			reachedGreenLight()
		}
		if ((newValue == 420) && (modeValue == 10)){  
			reachedGreenLight()
		}
		if ((newValue == 450) && (modeValue == 11)){  
			reachedGreenLight()
		}
	       if ((newValue == 600) && (modeValue == 12)){  
			reachedGreenLight()
		}
		if ((newValue == 480) && (modeValue == 13)){  
			reachedGreenLight()
		}
		if ((newValue == 600) && (modeValue == 14)){  
			reachedGreenLight()
		}
		if ((newValue == 720) && (modeValue == 15)){  
			reachedGreenLight()
		}
		if ((newValue == 780) && (modeValue == 16)){  
			reachedGreenLight()
		}
		if ((newValue == 900) && (modeValue == 17)){  
			reachedGreenLight()
		}
		if ((newValue == 1200) && (modeValue == 18)){  
			reachedGreenLight()
		}
		if ((newValue == 1200) && (modeValue == 19)){  
			reachedGreenLight()
		}
		if ((newValue == 1800) && (modeValue == 20)){  
			reachedGreenLight()
		}
		if ((newValue == 570) && (modeValue == 21)){  
			reachedGreenLight()
		}
		//check for Yellowlight
        if ((newValue == 10) && (modeValue == 0)) { 
			reachedYellowLight()
		}
		if ((newValue == 90) && (modeValue == 1)) { 
			reachedYellowLight()
		}
	       if ((newValue == 150) && (modeValue == 2)) { 
			reachedYellowLight()
		}
	       if ((newValue == 165) && (modeValue == 3)) { 
			reachedYellowLight()
		}
	       if ((newValue == 210) && (modeValue == 4)) { 
			reachedYellowLight()
		}
	       if ((newValue == 240) && (modeValue == 5)) { 
			reachedYellowLight()
		}
		if ((newValue == 300) && (modeValue == 6)) { 
			reachedYellowLight()
		}
		if ((newValue == 285) && (modeValue == 7)) { 
			reachedYellowLight()
		}
		if ((newValue == 360) && (modeValue == 8)) { 
			reachedYellowLight()
		}
		if ((newValue == 420) && (modeValue == 9)) { 
			reachedYellowLight()
		}
		if ((newValue == 480) && (modeValue == 10)) { 
			reachedYellowLight()
		}
		if ((newValue == 465) && (modeValue == 11)) { 
			reachedYellowLight()
		}
	       if ((newValue == 660) && (modeValue == 12)) { 
			reachedYellowLight()
		}
		if ((newValue == 540) && (modeValue == 13)) { 
			reachedYellowLight()
		}
		if ((newValue == 720) && (modeValue == 14)) { 
			reachedYellowLight()
		}
		if ((newValue == 810) && (modeValue == 15)) { 
			reachedYellowLight()
		}
		if ((newValue == 840) && (modeValue == 16)) { 
			reachedYellowLight()
		}
		if ((newValue == 1050) && (modeValue == 17)) { 
			reachedYellowLight()
		}
		if ((newValue == 1500) && (modeValue == 18)) { 
			reachedYellowLight()
		}
		if ((newValue == 1800) && (modeValue == 19)) { 
			reachedYellowLight()
		}
		if ((newValue == 2100) && (modeValue == 20)) { 
			reachedYellowLight()
		}
		if ((newValue == 585) && (modeValue == 21)) { 
			reachedYellowLight()
		}
		//check for Redlight
		if ((newValue == 15) && (modeValue == 0)) {  
			reachedRedLight()
		}
		if ((newValue == 120) && (modeValue == 1)) {  
			reachedRedLight()
		}
		if ((newValue == 180) && (modeValue == 2)) {  
			reachedRedLight()
		}
		if ((newValue == 180) && (modeValue == 3)) {  
			reachedRedLight()
		}
		if ((newValue == 240) && (modeValue == 4)) {  
			reachedRedLight()
		}
		if ((newValue == 300) && (modeValue == 5)) {  
			reachedRedLight()
		}
		if ((newValue == 360) && (modeValue == 6)) {  
			reachedRedLight()
		}
		if ((newValue == 300) && (modeValue == 7)) {  
			reachedRedLight()
		}
		if ((newValue == 420) && (modeValue == 8)) {  
			reachedRedLight()
		}
		if ((newValue == 480) && (modeValue == 9)) {  
			reachedRedLight()
		}
		if ((newValue == 540) && (modeValue == 10)) {  
			reachedRedLight()
		}
		if ((newValue == 480) && (modeValue == 11)) {  
			reachedRedLight()
		}
	    if ((newValue == 720) && (modeValue == 12)) {  
			reachedRedLight()
		}
        if ((newValue == 600) && (modeValue == 13)) {  
			reachedRedLight()
		}
		if ((newValue == 840) && (modeValue == 14)) { 
			reachedRedLight()
		}
		if ((newValue == 900) && (modeValue == 15)) { 
			reachedRedLight()
		}
		if ((newValue == 900) && (modeValue == 16)) { 
			reachedRedLight()
		}
		if ((newValue == 1200) && (modeValue == 17)) { 
			reachedRedLight()
		}
		if ((newValue == 1800) && (modeValue == 18)) { 
			reachedRedLight()
		}
		if ((newValue == 2400) && (modeValue == 19)) { 
			reachedRedLight()
		}
		if ((newValue == 2400) && (modeValue == 20)) { 
			reachedRedLight()
		}
		if ((newValue == 600) && (modeValue == 21)) { 
			reachedRedLight()
		}
		//check for Zero time left - FLASH LIGHTS!!!!!!
		if ((newValue == 20) && (modeValue == 0)) {   
		reachedMax();
		}
		if ((newValue == 150) && (modeValue == 1)) {   
		reachedMax();
		}
		if ((newValue == 210) && (modeValue == 2)) {   
		reachedMax();
		}
		if ((newValue == 210) && (modeValue == 3)) {   
		reachedMax();
		}
		if ((newValue == 270) && (modeValue == 4)) {   
		reachedMax();
		}
		if ((newValue == 330) && (modeValue == 5)) {   
		reachedMax();
		}
		if ((newValue == 390) && (modeValue == 6)) {   
		reachedMax();
		}
		if ((newValue == 330) && (modeValue == 7)) {   
		reachedMax();
		}
		if ((newValue == 450) && (modeValue == 8)) {   
		reachedMax();
		}
		if ((newValue == 510) && (modeValue == 9)) {   
		reachedMax();
		}
		if ((newValue == 570) && (modeValue == 10)) {   
		reachedMax();
		}
		if ((newValue == 510) && (modeValue == 11)) {   
		reachedMax();
		}
	    if ((newValue == 750) && (modeValue == 12)) {   
		reachedMax();
		}
		if ((newValue == 630) && (modeValue == 13)) {   
		reachedMax();
		}
		if ((newValue == 870) && (modeValue == 14)) {   
		reachedMax();
		}
		if ((newValue == 930) && (modeValue == 15)) {   
		reachedMax();
		}
		if ((newValue == 930) && (modeValue == 16)) {   
		reachedMax();
		}
		if ((newValue == 1230) && (modeValue == 17)) {   
		reachedMax();
		}
		if ((newValue == 1830) && (modeValue == 18)) {   
		reachedMax();
		}
		if ((newValue == 2430) && (modeValue == 19)) {   
		reachedMax();
		}
		if ((newValue == 2430) && (modeValue == 20)) {   
		reachedMax();
		}
		if ((newValue == 630) && (modeValue == 21)) {   
		reachedMax();
		}
	}
	// if RESET has been clicked (Stop = 1), call reset function
	if (Stop == 1) {
	// Pass value of "flashing" so resetTimer function can determine if timer is flashing or not;
	// resetTimer function will turn of flashing lights if they are flashing
	resetTimer(flashing)
	}
}
//This function turns on the Green Light
function reachedGreenLight() {
	document.holder.src = imagegreen.src;
}
//This function turns on the Yellow Light
function reachedYellowLight() {
	document.holder.src = imageoff.src;
	document.holder1.src = imageyellow.src;
}
//This function turns on the Red Light
function reachedRedLight() {
	document.holder1.src = imageoff.src;
	document.holder2.src = imagered.src;
}
function reachedBuzzer() {
	//add code to sound buzzer here
}
function reachedMax(count) {
	//-- This is the function that is executed when the max count is reached.
	flashing = 1;
	//clearInterval(myTimerID)
	onLights();
	//document.holder.src = imageoff.src;
	//document.holder1.src = imageoff.src;
	//document.holder2.src = imageoff.src;
	flash = false;
	flashTimerID=setInterval("flashLights()",500)
}
function flashLights () {
	if (flash == false) {
		onLights();
	}
	else if (flash == true) {
		offLights();
	}
	//toggle = !toggle;
	flash = !flash;
	document.myForm.flashtimerID.value = flashTimerID
}
function onLights() {
	document.holder.src = imagegreen.src;
	document.holder1.src = imageyellow.src;
	document.holder2.src = imagered.src;
}
function offLights() {
	document.holder.src = imageoff.src;
	document.holder1.src = imageoff.src;
	document.holder2.src = imageoff.src;
}
function restartTimer(set) {
	//-- This is the function that is executed when the timer is restarted.
	clearInterval(myTimerID)
	if (set == 1){
		resetFlash();
	}
	resetImages();
	Start = 0;
	getStarted(setCount);
}
function resetTimer(set) {
	//-- This is the function that is executed when Stop = 1 (Reset Timer button is pressed).
	document.myForm.flashing.value = set
	clearInterval(myTimerID)
	if (set == 1){
	//turn flashing lights off if they are flashing
		resetFlash();
	}
	resetImages();
	resetFields();
	Stop = 0;
	Start = 0;
	flash = false;
	flashing = 0;
}
// The variable Stop is called by the RESET button. Stop is used in the addOne function to determine if
// the RESET button has been clicked. This function sets it equal to 1
function setStop(){
    Stop = 1;
	resetImages();
	resetFields();
}
function resetFlash() {
	clearInterval(flashTimerID);
}
function convertSec(sec) {
	if (sec >= 0 && sec < 60) {
	seconds = sec;
	minutes = 0;
	}
	if (sec >= 60 && sec < 120) {
	seconds = sec - 60;
	minutes = 1;
	}
	if (sec >= 120 && sec < 180) {
	seconds = sec - 120;
	minutes = 2;
	}
	if (sec >= 180 && sec < 240) {
	seconds = sec - 180;
	minutes = 3;
	}
	if (sec >= 240 && sec < 300) {
	seconds = sec - 240;
	minutes = 4;
	}
	if (sec >= 300 && sec < 360) {
	seconds = sec - 300;
	minutes = 5;
	}
	if (sec >= 360 && sec < 420) {
	seconds = sec - 360;
	minutes = 6;
	}
	if (sec >= 420 && sec < 480) {
	seconds = sec - 420;
	minutes = 7;
	}
	if (sec >= 480 && sec < 540) {
	seconds = sec - 480;
	minutes = 8;
	}
	if (sec >= 540 && sec < 600) {
	seconds = sec - 540;
	minutes = 9;
	}
	if (sec >= 600 && sec < 660) {
	seconds = sec - 600;
	minutes = 10;
	}
	if (sec >= 660 && sec < 720) {
	seconds = sec - 660;
	minutes = 11;
	}
	if (sec >= 720 && sec < 780) {
	seconds = sec - 720;
	minutes = 12;
	}
	if (sec >= 780 && sec < 840) {
	seconds = sec - 780;
	minutes = 13;
	}
	if (sec >= 840 && sec < 900) {
	seconds = sec - 840;
	minutes = 14;
	}
	if (sec >= 900 && sec < 960) {
	seconds = sec - 900;
	minutes = 15;
	}
	if (sec >= 960 && sec < 1020) {
	seconds = sec - 960;
	minutes = 16;
	}
	if (sec >= 1020 && sec < 1080) {
	seconds = sec - 1020;
	minutes = 17;
	}
	if (sec >= 1080 && sec < 1140) {
	seconds = sec - 1080;
	minutes = 18;
	}
	if (sec >= 1140 && sec < 1200) {
	seconds = sec - 1140;
	minutes = 19;
	}
	if (sec >= 1200 && sec < 1260) {
	seconds = sec - 1200;
	minutes = 20;
	}
	if (sec >= 1260 && sec < 1320) {
	seconds = sec - 1260;
	minutes = 21;
	}
	if (sec >= 1320 && sec < 1380) {
	seconds = sec - 1320;
	minutes = 22;
	}
	if (sec >= 1380 && sec < 1440) {
	seconds = sec - 1380;
	minutes = 23;
	}
	if (sec >= 1440 && sec < 1500) {
	seconds = sec - 1440;
	minutes = 24;
	}
	if (sec >= 1500 && sec < 1560) {
	seconds = sec - 1500;
	minutes = 25;
	}
	if (sec >= 1560 && sec < 1620) {
	seconds = sec - 1560;
	minutes = 26;
	}
	if (sec >= 1620 && sec < 1680) {
	seconds = sec - 1620;
	minutes = 27;
	}
	if (sec >= 1680 && sec < 1740) {
	seconds = sec - 1680;
	minutes = 28;
	}
	if (sec >= 1740 && sec < 1800) {
	seconds = sec - 1740;
	minutes = 29;
	}
	if (sec >= 1800 && sec < 1860) {
	seconds = sec - 1800;
	minutes = 30;
	}
	if (sec >= 1860 && sec < 1920) {
	seconds = sec - 1860;
	minutes = 31;
	}
	if (sec >= 1920 && sec < 1980) {
	seconds = sec - 1920;
	minutes = 32;
	}
	if (sec >= 1980 && sec < 2040) {
	seconds = sec - 1980;
	minutes = 33;
	}
	if (sec >= 2040 && sec < 2100) {
	seconds = sec - 2040;
	minutes = 34;
	}
	if (sec >= 2100 && sec < 2160) {
	seconds = sec - 2100;
	minutes = 35;
	}
	if (sec >= 2160 && sec < 2220) {
	seconds = sec - 2160;
	minutes = 36;
	}
	if (sec >= 2220 && sec < 2280) {
	seconds = sec - 2220;
	minutes = 37;
	}
	if (sec >= 2280 && sec < 2340) {
	seconds = sec - 2280;
	minutes = 38;
	}
	if (sec >= 2340 && sec < 2400) {
	seconds = sec - 2340;
	minutes = 39;
	}
	if (sec >= 2400 && sec < 2460) {
	seconds = sec - 2400;
	minutes = 40;
	}
	if (sec >= 2460 && sec < 2520) {
	seconds = sec - 2460;
	minutes = 41;
	}
	if (sec >= 2520 && sec < 2580) {
	seconds = sec - 2520;
	minutes = 42;
	}
	if (sec >= 2580 && sec < 2640) {
	seconds = sec - 2580;
	minutes = 43;
	}
	if (sec >= 2640 && sec < 2700) {
	seconds = sec - 2640;
	minutes = 44;
	}
	if (sec >= 2700 && sec < 2760) {
	seconds = sec - 2700;
	minutes = 45;
	}
}
function writeMinSec() {
	document.myForm.Minutes.value = minutes;
	document.myForm.Seconds.value = seconds;	
}
function resetImages() {
	document.holder.src = imageoff.src;
	document.holder1.src = imageoff.src;
	document.holder2.src = imageoff.src;
}
function resetFields() {
	document.myForm.numberShown.value = ""
	document.myForm.timerID.value = ""
	document.myForm.Minutes.value= "";
	document.myForm.Seconds.value= "";
	document.myForm.Mode.value= "";
}
//speechIndex passed from timer mode radio buttons in form - modeValue used to determine timer mode
function speechDuration(speechIndex) {
	var returnValue = true;
	if (Start == 1) {
	// if timer running, set index to previous value (modeValue)
	index = modeValue;
	alert("Timer still runnning! Click OK below then Reset Timer and reselect Speech Duration.");
	// set checked radio button to previous state
	document.myForm.duration[index].checked = true;
	}
	else {
	modeValue = speechIndex;
	//return;
	}
	//return returnValue;
}
//preload images to be swapped
var imagegreen = new Image();
imagegreen.src = "images/greenlight.gif";
var imageyellow = new Image();
imageyellow.src = "images/yellowlight.gif";	
var imagered = new Image();
imagered.src = "images/redlight.gif";	
var imageoff = new Image();
imageoff.src = "images/offlight.gif";