/*
  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright © 2003 by Gary Johnson
Copyright (&copy;) 2003 by Gary Johnson

COPYRIGHT

The information contained on this site is protected by Canadian, United States of America and international copyright laws.

All website materials, including, without limitation, design, text, graphics, photos, files, the Fast Track! logo, and 
the selection and arrangement thereof are © 2004 Gary Johnson ALL RIGHTS RESERVED.
 
Permission is granted to electronically copy and print to hard copy portions of this website for the sole purpose of 
using materials it contains for informational and non-commercial personal use only.
 
Any other use of materials in this website, including any commercial use, reproduction for purposes other than those noted above, 
modification, distribution or republication, without the prior written consent of Gary Johnson is strictly prohibited.


The Full Copyright statement is

	http://mywebpages.comcast.net/adgj/XXSoftwareTools/Copyright.html

  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/

/*
Time Format

The following characters are recognized in the format string:
  %a - abbreviated weekday name
  %A - full weekday name
  %b - abbreviated month name
  %B - full month name
  %d - day of the month (01 to 31)
  %D - same as %m/%d/%y
  %e - day of the month (1 to 31)
  %H - hour using a 24-hour clock (range 00 to 23)
  %I - hour using a 12-hour clock (range 01 to 12)
  %m - month as a number (01 to 12)
  %M - minute as a number
  %p - either "am" or "pm" according to the given time
  %R - time in 24 hour notation
  %S - second as a decimal number
  %T - current time, equal to %H:%M:%S
  %y - 2 digit year (00 to 99)
  %Y - 4 digit year
  %Z - time zone or name or abbreviation
  %% - a literal '%' character

  for short date no seconds
    var ampm = (h >= 12)?"PM":"AM";    // Is it am or pm?
    if (h > 12) h -= 12;               // Convert 24-hour format to 12-hour.
    if (h == 0) h = 12;                // Convert 0 o'clock to midnight.
    if (m < 10) m = "0" + m;           // Convert 0 minutes to 00 minutes, etc.
    var t = h + ':' + m + ' ' + ampm;  // Put it all together.


*/

if (!window.myAlarm) 
{
    var myAlarm = 
	{      
        COUNTDOWN:	1, 
        STOPWATCH:	2, 
        TIME:		3,      
        HOURLY:		4,    
        DAILY:		5
    }
} 

if (!window.FN) 
{
    var FN = 
	{      
		NUMBERALARMS:		0		
    }
} 

if (!window.myField) 
{
    var myField = 
	{      
		ALARMNUMBER:		0,		
		MALARMTIME:			1,
		SFILESPECOFCLIP:	2,
		BREPEAT:			3,
		SNAME:				4,
		IDNAME:				5,
		IALARMTYPE:			6,
		HOUR:				7,
		MIN:				8,
		SEC:				9
    }
} 


function getShortMonthName(iMonth)
{

smname = new Array (  "Jan",
					  "Feb",
					  "Mar",
					  "Apr",
					  "May",
					  "Jun",
					  "Jul",
					  "Aug",
					  "Sep",
					  "Oct",
					  "Nov",
					  "Dec", 
					  "bad"	);
      
return (smname[iMonth]);
}         

function getMonthName(iMonth)
{

mname = new Array (	  "January",
					  "February",
					  "March",
					  "April",
					  "May",
					  "June",
					  "July",
					  "August",
					  "September",
					  "October",
					  "November",
					  "December", 
					  "bad"	);
      
return (mname[iMonth]);
}  

function getShortDayName(iDay)
{

sdname = new Array(	"Sun",
					"Mon",
					"Tue",
					"Wed",
					"Thu",
					"Fri",
					"Sat",
					"bad"	); 
return (sdname[iDay]);
}		

function getDayName(iDay)
{

dname = new Array(	"Sunday",
					"Monday",
					"Tuesday",
					"Wednesday",
					"Thursday",
					"Friday",
					"Saturday",
					"bad"	); 
return (dname[iDay]);
}		


var accumulate    = new Array( 31, 59, 90,120,151,181,212,243,273,304,334);
var accumulateLY  = new Array( 31, 60, 91,121,152,182,213,244,274,305,335);


function LeapYear(year) 
{
    if ((year / 4)   != Math.floor(year / 4))   {return false;}
    if ((year / 100) != Math.floor(year / 100)) {return true;}
    if ((year / 400) != Math.floor(year / 400)) {return false;}
    return true;
}

function getJulian(day,month,year) 
{
	if(month == -1)
		return (day);
	
  if(LeapYear(year))
	{
        return (day + accumulateLY[month]);
	}
  else
	{
        return (day + accumulate[month]);
	}
}

function format12HourTime(hour, min, sec)
{
	var ampm = "PM";

	if (min <= 9) 
	{
		min = "0" + min;
	}

	if (sec <= 9) 
	{
		sec = "0" + sec;
	}

	if (hour > 12) 
	{
		hour = hour - 12;
		ampm = "PM";
	}
	else 
	{
		hour = hour;
		ampm = "AM";
	}
	
	if (hour == 12) 
	{
		ampm = "PM";
	}

	if (hour == 00) 
	{
		hour = "12";
	}
	
	if(hour <= 9)
	{
		hour =  "0" + hour 
	}

	var msg1 = hour;
	msg1 += ':' + min;
	msg1 += ':' + sec;
	msg1 += ' ' + ampm + ' ';

return msg1;
}


//<a href="javascript:insertHTMLTable()">click here to insert inner HTML table into div</a>


function makeCancelTimer(myfunc, mylabel, myarg)
{
	var str0 = "<a href='javascript:";
    var str1 = myfunc;
    var str2 = "(";
    var str3 = myarg;
  	var str4 = ")'>"   
  	var str5 = mylabel;   
  	var str6 = "</a>"   
	str0 += str1 + str2 + str3 + str4 + str5 + str6;
return str0;
}



function createDisplayTable(where,inArray)
{
	changeInnerHTML(where, "");

	var tstring = new Array();

	tstring.push('<table cellpadding="4" cellspacing="4" class="LTB"><tbody>');

//	tstring.push('<table><tbody>');

	var NumberofCells = inArray.length;
	var NumberofRows = Math.ceil(NumberofCells / 3 );
	var NumberofInnerLoops = Math.ceil(NumberofCells / NumberofRows);

	tstring.push('<tr>');

	for (var i=0 ; i < NumberofCells; i++)
	{
		if((i % 2) == 0 )
		{
			tstring.push('<td class="b">' + inArray[i] + '</td>');
		}
		else
		{
			tstring.push('<td class="a">' + inArray[i] + '</td>');
		}
	}

	tstring.push('</tr>');
	
	tstring.push('</tbody></table>');
	
	var writestring = tstring.join('');
	
	changeInnerHTML(where, writestring);
}



function CAlarmTimer	(	  aObjectReference, 
							  bAutoRepeat,
							  bALLoff, 
							  bHourly,
							  bDaily,
							  iAlarmType,
							  iSoundAlarm,
							  sFileSpecofClip,
							  sNameofAlarm,
							  iIntervalWarning,
							  iRepeat
						)
{

// aObjectReference 
(bAutoRepeat )			? (this.m_bAutoRepeat = bAutoRepeat)				:  (this. m_bAutoRepeat = false);
(bALLoff )				? (this.m_bALLoff = bALLoff)						:  (this. m_bALLoff = false);
(bHourly )				? (this.m_bHourly = bHourly)						:  (this. m_bHourly = false);
(bDaily )				? (this.m_bDaily = bDaily)							:  (this. m_bDaily = false);
(iAlarmType )			? (this.m_iAlarmType = iAlarmType)					:  (this. m_iAlarmType = 0);
(iSoundAlarm )			? (this.m_iSoundAlarm = iSoundAlarm)				:  (this. m_iSoundAlarm = 0);
(sFileSpecofClip )		? (this.m_sFileSpecofClip = sFileSpecofClip)		:  (this. m_sFileSpecofClip = '');
(sNameofAlarm )			? (this.m_sNameofAlarm = sNameofAlarm)				:  (this. m_sNameofAlarm = '');
(iIntervalWarning )		? (this.m_iIntervalWarning = iIntervalWarning)		:  (this. m_iIntervalWarning = 0);
(iRepeat )				? (this.m_iRepeat = iRepeat)						:  (this. m_iRepeat = 0);

	//first element is the number of alarms

	this.alarmTimes = new Array();
	this.alarmTimes[FN.NUMBERALARMS] = 0;
}


CAlarmTimer.prototype.dumpAlarms = function(what)
{

	mymsg = '';

	mymsg  += this.alarmTimes[0]								+ ' Number of Alarms' + '<br>';
	mymsg  += this.alarmTimes[what][myField.ALARMNUMBER]		+ ' Number' + '<br>';
	mymsg  += this.alarmTimes[what][myField.MALARMTIME]			+ ' malarmTime' + '<br>';
	mymsg  += this.alarmTimes[what][myField.SFILESPECOFCLIP]	+ ' sFileSpecofClip' + '<br>';
	mymsg  += this.alarmTimes[what][myField.BREPEAT]			+ ' bRepeat' + '<br>';
	mymsg  += this.alarmTimes[what][myField.SNAME]				+ ' sName' + '<br>';
	mymsg  += this.alarmTimes[what][myField.IDNAME]				+ ' idName' + '<br>';
	mymsg  += this.alarmTimes[what][myField.IALARMTYPE]			+ ' iAlarmType' + '<br>';
	mymsg  += this.alarmTimes[what][myField.HOUR]				+ ' hour' + '<br>';
	mymsg  += this.alarmTimes[what][myField.MIN]				+ ' min' + '<br>';
	mymsg  += this.alarmTimes[what][myField.SEC]				+ ' sec' + '<br>';


	doMessage(mymsg);
}


CAlarmTimer.prototype.checkAlarms = function(aDate)
{
	var timeNow =  this.computeMilliseconds(aDate)

    for (i = 1; i <= this.alarmTimes[FN.NUMBERALARMS]; i++)
	{
		if(this.alarmTimes[i][myField.IALARMTYPE] == myAlarm.STOPWATCH) 
		{
			continue;
		}
		if(this.alarmTimes[i][myField.MALARMTIME] < timeNow)
		{
			if(this.alarmTimes[i][myField.MALARMTIME] != 0)
			{
				// Trigger Display of Alarm
				this.doAlarm(this.alarmTimes[i][myField.ALARMNUMBER], this.alarmTimes[i][myField.SFILESPECOFCLIP],this.alarmTimes[i][myField.SNAME] );

				// set the Alarm Time to 0 so we no longer act on it

				this.alarmTimes[i][myField.MALARMTIME] = 0;
				// it its a repeatable alarm that needs repeating, set a new one

				if(this.alarmTimes[i][myField.BREPEAT] != -1)
				{
					this.alarmTimes[i][myField.BREPEAT]--;
					if(this.alarmTimes[i][myField.BREPEAT])
					{
						this.setAlarm(this.alarmTimes[i][myField.HOUR], this.alarmTimes[i][myField.MIN],this.alarmTimes[i][myField.SEC],this.alarmTimes[i][myField.SFILESPECOFCLIP],this.alarmTimes[i][myField.BREPEAT],this.alarmTimes[i][myField.SNAME],this.alarmTimes[i][myField.IDNAME],this.alarmTimes[i][myField.IALARMTYPE] );
					}
				}
				else
				{
						this.setAlarm(this.alarmTimes[i][myField.HOUR], this.alarmTimes[i][myField.MIN],this.alarmTimes[i][myField.SEC],this.alarmTimes[i][myField.SFILESPECOFCLIP],this.alarmTimes[i][myField.BREPEAT],this.alarmTimes[i][myField.SNAME],this.alarmTimes[i][myField.IDNAME],this.alarmTimes[i][myField.IALARMTYPE] );
				}

				// clear out the display
				//document.getElementById(this.alarmTimes[i][myField.IDNAME]).firstChild.nodeValue = "";
				changeInnerHTML(this.alarmTimes[i][myField.IDNAME],"");
			}
		}
	}
};

CAlarmTimer.prototype.updateAlarms = function(aDate)
{


//dear Gary - you cannot call one of your own member functions from within a switch

	var timeNow		=	this.computeMilliseconds(aDate)
	var msg5		=	'';  //for CountDown Timers
	var msg6		=	'';  //for Alarm Timers
	var msg7		=	'';  //for stopwatches
	var doCD		=	'';
	var doAT		=	'';
	var doSW		=	'';
	var activeCD	=	0;
	var activeAT	=	0;
	var activeSW	=	0;
	var CDarray		=	new Array();
	var ATarray		=	new Array();
	var SWarray		=	new Array();


    for (i = 1; i <= this.alarmTimes[FN.NUMBERALARMS]; i++)
	{
		switch (this.alarmTimes[i][myField.IALARMTYPE])
		{
		//countdowns					
		case 1:
			{
				if(this.alarmTimes[i][myField.MALARMTIME] != 0)
				{
					//create a string for each timer
					doCD = this.alarmTimes[i][myField.IDNAME];
					msg5 += "# " + this.alarmTimes[i][myField.ALARMNUMBER] + " " + this.alarmTimes[i][myField.SNAME] + " ";
					
					bigDiff = this.alarmTimes[i][myField.MALARMTIME] - timeNow;
					
					var cHoursMill		= (60 * 60 * 1000);
					var cMinutesMill	= (60 * 1000);
					var cSecondsMill	= 1000;
					
					if(bigDiff > cHoursMill)
					{
						var hrs = Math.floor(bigDiff / cHoursMill);
						bigDiff = bigDiff - (hrs * cHoursMill);
						if(hrs <= 9)
						{
							hrs =  "0" + hrs; 
						}				
						msg5 += hrs + ":";
					}
					else
					{
						msg5 += "00:";
					}

					if(bigDiff > cMinutesMill)
					{
						var mns = Math.floor(bigDiff / cMinutesMill);
						bigDiff = bigDiff - (mns * cMinutesMill);
						if (mns <= 9) 
						{
							mns = "0" + mns;
						}
						msg5 += mns + ":";
					}
					else
					{
						msg5 += "00:";
					}
					
					if(bigDiff > cSecondsMill)
					{
						var sec = Math.floor(bigDiff / cSecondsMill);
						bigDiff = bigDiff - (sec * cSecondsMill);
						if (sec <= 9) 
						{
							sec = "0" + sec;
						}
						msg5 += sec + " ";
					}
					else
					{
						msg5 += "00";
					}
					//save each time in its own cell
					CDarray[activeCD] = msg5;
 					activeCD++;
					//one more time, with feeling
					msg5 = '';
				}//if (this ..
				break;  
			} //case 1
		//2 StopWatch
		case 2:
			{
				if(this.alarmTimes[i][myField.BREPEAT] != 0)
				{
					//the reverse of a CountDown
					doSW = this.alarmTimes[i][myField.IDNAME];
					msg7 += "# " + this.alarmTimes[i][myField.ALARMNUMBER]  + " ";
					
					bigDiff = timeNow - this.alarmTimes[i][myField.MALARMTIME];
					
					var cHoursMill		= (60 * 60 * 1000);
					var cMinutesMill	= (60 * 1000);
					var cSecondsMill	= 1000;
					
					if(bigDiff > cHoursMill)
					{
						var hrs = Math.floor(bigDiff / cHoursMill);
						bigDiff = bigDiff - (hrs * cHoursMill);
						if(hrs <= 9)
						{
							hrs =  "0" + hrs; 
						}				
						msg7 += hrs + ":";
					}
					else
					{
						msg7 += "00:";
					}
					
					if(bigDiff > cMinutesMill)
					{
						var mns = Math.floor(bigDiff / cMinutesMill);
						bigDiff = bigDiff - (mns * cMinutesMill);
						if (mns <= 9) 
						{
							mns = "0" + mns;
						}
						msg7 += mns + ":";
					}
					else
					{
						msg7 += "00:";
					}
					
					if(bigDiff > cSecondsMill)
					{
						var sec = Math.floor(bigDiff / cSecondsMill);
						bigDiff = bigDiff - (sec * cSecondsMill);
						if (sec <= 9) 
						{
							sec = "0" + sec;
						}
						msg7 += sec + " ";
					}
					else
					{
						msg7 += "00: ";
					}
					//save it for display after hitting stop
					this.alarmTimes[i][myField.SNAME] = msg7;

					msg7 += makeCancelTimer("runstopStopWatch","Stop",this.alarmTimes[i][myField.ALARMNUMBER]);
					//save each time in its own cell
					SWarray[activeSW] = msg7;
					activeSW++;
					//one more time, with feeling
					msg7 = '';
				} //if
				else
				{
					SWarray[activeSW] = this.alarmTimes[i][myField.SNAME];
					activeSW++;
					//one more time, with feeling
				}
			break;
			}
		//Alarm
		case 3:
			{
				if(this.alarmTimes[i][myField.MALARMTIME] != 0)
				{
					//create a string for each timer
					doAT = this.alarmTimes[i][myField.IDNAME];
					msg6 += "# " + this.alarmTimes[i][myField.ALARMNUMBER] + " ";

//					var hrs =  this.alarmTimes[i][myField.HOUR]; 

					msg6 = format12HourTime(this.alarmTimes[i][myField.HOUR], this.alarmTimes[i][myField.MIN], '0');


//					if(hrs <= 9)
//					{
//						hrs =  "0" + hrs; 
//					}				
					
//					msg6 += hrs + ":";

//					var mns = this.alarmTimes[i][myField.MIN];

//					if (mns <= 9) 
//					{
//						var mns = "0" + mns;
//					}
					
//					msg6 += mns;
					//save each time in its own cell
					ATarray[activeAT] = msg6;
 					activeAT++;
					//one more time, with feeling
					msg6 = '';
				}//if (this ..
				break;
			}
		//Hourly
		case 4:
			{
				alert("4 Lets do it");			
				break;
			}
		//Daily
		case 5:
			{
				alert("5 Lets do it");
				break;
			}
		default:
				alert("default");
		} //switch what type
	}//for all timers

	//update the displays 
	if(doCD != "")
	{
		createDisplayTable(doCD,CDarray);
		//		document.getElementById(doCD).firstChild.nodeValue = msg5;
	}

	if(doAT != "")
	{
		createDisplayTable(doAT,ATarray);
		//		this.dumpAlarms(0);
	}

	if(doSW != "")
	{
		createDisplayTable(doSW,SWarray);
	}

};

CAlarmTimer.prototype.clearAlarms = function()
{

var msg5 = '';  //for CountDown Timers
var msg6 = '';  //for Alarm Timers
var msg7 = '';  //for stopwatches
var doCD = '';
var doAT = '';
var doSW = '';

    for (i = 1; i <= this.alarmTimes[FN.NUMBERALARMS]; i++)
	{
		switch (this.alarmTimes[i][myField.IALARMTYPE])
		{
			//countdowns					
			case 1:
				{
					if(this.alarmTimes[i][myField.MALARMTIME] != 0)
					{
						doCD = this.alarmTimes[i][myField.IDNAME];
						msg5 += "";
					}
					break;  //case 1
				} //case 1
			//StopWatch
			case 2:
				{
					if(this.alarmTimes[i][myField.MALARMTIME] != 0)
					{
						doSW = this.alarmTimes[i][myField.IDNAME];
						msg7 += "";
					}
					break;
				}
			//Alarm
			case 3:
				{
					if(this.alarmTimes[i][myField.MALARMTIME] != 0)
					{
						doAT = this.alarmTimes[i][myField.IDNAME];
						msg6 += "";
					}
					break;
				}
			//Hourly
			case 4:
				{
					alert("4 Lets do it");			
					break;
				}
			//Daily
			case 5:
				{
					alert("5 Lets do it");			
					break;
				}
			default:
					alert("default");	
			} //switch what type
	}//for all timers

	//clear the displays
	if(doCD != "")
	{
		changeInnerHTML(doCD,msg5);
	}

	if(doAT != "")
	{
		changeInnerHTML(doAT,msg6);
	}

	if(doSW != "")
	{
		changeInnerHTML(doSW,msg7);
	}


};

CAlarmTimer.prototype.cancelTimer = function(which)
{
		if(this.alarmTimes[which][myField.MALARMTIME] != 0)
		{
			this.alarmTimes[which][myField.MALARMTIME] = 0;
		}
}

CAlarmTimer.prototype.stopWatchStop = function(which)
{
		if(this.alarmTimes[which][myField.BREPEAT] != 0)
		{
			this.alarmTimes[which][myField.BREPEAT] = 0;
		}
}


CAlarmTimer.prototype.setAlarm = function(hour, min, sec,sFileSpecofClip,bRepeat,sName,idName,iAlarmType )
{
	// compute the alarm time in millseconds from the current time
	// 1 hour equals 60 (minutes) X 60 (seconds) X 1000 (milliseconds)

var newItemNum	= 0;
var now			= new Date();
var chour		= now.getHours();  //should be 24 
var cmin		= now.getMinutes();
var cdate		= now.getDate();
var cmonth		= now.getMonth();
var cyear		= now.getFullYear();
var malarmTime	= 0;

	switch (iAlarmType)
	{
		//countdowns					
		case 1:
			{
				var dhour;
				var dmin;
				var dsec;
				
				(hour - 0) ? 	(dhour = ( hour * (60 * 60 * 1000)))	: dhour = 0;
				(min - 0) ?		(dmin = ( min * (60*1000)))				: dmin = 0;
				(sec - 0)  ?	(dsec  = ( sec * 1000))					: dsec = 0;

				malarmTime = this.computeMilliseconds(now) + dhour + dmin + dsec;

				// add that time to the array that is checked in checkAlarms
				// first element always holds the number of elements

				this.alarmTimes[FN.NUMBERALARMS] =	this.alarmTimes[FN.NUMBERALARMS] +  1;
				newItemNum = this.alarmTimes[FN.NUMBERALARMS];

				this.alarmTimes[newItemNum] = new Array(10);

				this.alarmTimes[newItemNum][myField.ALARMNUMBER]		= newItemNum;
				this.alarmTimes[newItemNum][myField.MALARMTIME]			= malarmTime;
				this.alarmTimes[newItemNum][myField.SFILESPECOFCLIP]	= sFileSpecofClip;
				this.alarmTimes[newItemNum][myField.BREPEAT]			= bRepeat;
				this.alarmTimes[newItemNum][myField.SNAME]				= sName;
				this.alarmTimes[newItemNum][myField.IDNAME]				= idName;
				this.alarmTimes[newItemNum][myField.IALARMTYPE]			= iAlarmType;
				this.alarmTimes[newItemNum][myField.HOUR]				= hour;
				this.alarmTimes[newItemNum][myField.MIN]				= min;
				this.alarmTimes[newItemNum][myField.SEC]				= sec;
//				this.dumpAlarms(newItemNum);
				break;
			}
		//StopWatch					
		case 2:
			{
				//save the orginal time in malarmtime
				//use bRepeat to control (start set to 1) stop stopwatch sets it to 0
				//time now - malartime 
				// convet milliseconds to hr min sec for display

				malarmTime = this.computeMilliseconds(now);

				//var tempTime = new Date(hour, min, sec);
				// add that time to the array that is checked in checkAlarms
				// first element always holds the number of elements

				this.alarmTimes[FN.NUMBERALARMS] =	this.alarmTimes[FN.NUMBERALARMS] +  1;
				newItemNum = this.alarmTimes[FN.NUMBERALARMS];

				this.alarmTimes[newItemNum] = new Array(10);


				this.alarmTimes[newItemNum][myField.ALARMNUMBER]		= newItemNum;
				this.alarmTimes[newItemNum][myField.MALARMTIME]			= malarmTime;
				this.alarmTimes[newItemNum][myField.SFILESPECOFCLIP]	= sFileSpecofClip;
				this.alarmTimes[newItemNum][myField.BREPEAT]			= bRepeat;
				this.alarmTimes[newItemNum][myField.SNAME]				= sName;
				this.alarmTimes[newItemNum][myField.IDNAME]				= idName;
				this.alarmTimes[newItemNum][myField.IALARMTYPE]			= iAlarmType;
				this.alarmTimes[newItemNum][myField.HOUR]				= hour;
				this.alarmTimes[newItemNum][myField.MIN]				= min;
				this.alarmTimes[newItemNum][myField.SEC]				= sec;
				break;
			}
		//Alarm
		case 3:
			{
				var today	= -1;

				if(hour < chour)
				{
					today = 0;
				}
				
				if((hour == chour) && min < cmin)
				{
					today = 0;
				}
				
				if(today != -1)
				{
					var msgxyz = "We don't support alarms on different days yet." + '<br>';
//					msgxyz += " Hour " + hour + " Min " + min + " sec " + sec + '<br>';
					msgxyz += "Hour " + hour + " Min " + min + '<br>';
					msgxyz += "Alarm Sound " + sFileSpecofClip + '<br>';
//					msgxyz += "Sound " + sFileSpecofClip + " bRepeat " + bRepeat + '<br>';
//					msgxyz += "Name " + sName + " idName " + idName + " iAlarmType " + iAlarmType;
					doMessage(msgxyz);
					return false;
				}

				var tempTime = new Date(cyear, cmonth, cdate, hour, min,0);

				var malarmTime = this.computeMilliseconds(tempTime);
				// add that time to the array that is checked in checkAlarms
				// first element always holds the number of elements

				this.alarmTimes[FN.NUMBERALARMS] =	this.alarmTimes[FN.NUMBERALARMS] +  1;
				newItemNum = this.alarmTimes[FN.NUMBERALARMS];

				this.alarmTimes[newItemNum] = new Array(10);

				this.alarmTimes[newItemNum][myField.ALARMNUMBER]		= newItemNum;
				this.alarmTimes[newItemNum][myField.MALARMTIME]			= malarmTime;
				this.alarmTimes[newItemNum][myField.SFILESPECOFCLIP]	= sFileSpecofClip;
				this.alarmTimes[newItemNum][myField.BREPEAT]			= bRepeat;
				this.alarmTimes[newItemNum][myField.SNAME]				= sName;
				this.alarmTimes[newItemNum][myField.IDNAME]				= idName;
				this.alarmTimes[newItemNum][myField.IALARMTYPE]			= iAlarmType;
				this.alarmTimes[newItemNum][myField.HOUR]				= hour;
				this.alarmTimes[newItemNum][myField.MIN]				= min;
				this.alarmTimes[newItemNum][myField.SEC]				= sec;
				break;
			}
		//Hourly
		case 4:
			{
				alert("4 Lets do it");			
				break;
			}
		//Daily
		case 5:
			{
				alert("5 Lets do it");
				break;
			}
		default:
				alert("default");			
	}//end switch
};

CAlarmTimer.prototype.computeAlarmTime = function(aDate)
{
	//compute the alarm time in millseconds from the current time
	//add that time to the array that is checked in checkAlarms
	
	alert("computeAlarmTime");
};

CAlarmTimer.prototype.computeMilliseconds = function(aDate)
{
	var tstring = aDate.toUTCString();
//	alert(tstring);
	var nowMilliseconds = Date.parse(tstring ) + (aDate.getTimezoneOffset() * (60*1000));

return nowMilliseconds;
};

CAlarmTimer.prototype.doAlarm = function(AlarmNum , myFileSpec, sName )
{

//	var win = window.open('javascript:"<h1>BOUNCE!</h1>"', "", "width=" + w + ",height=" + h);
//			if(!alarmWind)
//		{
//			alert('we are hosed');
//			return;
//		}


//	if(isGecko())
//	{
		   // netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); not allowd on Web server
//			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
//	}



	try
	{
		if(isGecko())
		{
			//Test of QT/QT.html approach 
			PlayIt(document.wav1);
		}
		else
		{
			
			var wintxt = "<html><body bgcolor='#000000'>";
			wintxt += "<input type=button value='Close' onClick='self.close()'><br><br>";
			wintxt += "<div style='text-align: center; border:20pt outset red;width:10em; margin-left: auto; margin-right: auto; background-color:white;'> ";
			wintxt += "<p style=' font-weight:bold; font-size:16.0pt; color:black;  font-family:'Courier New', Courier, sans-serif;  text-align:center;   padding-right:7.5pt;  padding-left:7.5pt; padding-top:2.5pt;  padding-bottom:2.5pt;'>";
			wintxt += "Alarm";
			wintxt += "</p>";
			wintxt += "</div>";
			wintxt += "<p style='font-size: 16pt; color:white;'>";
			wintxt += "&nbsp; Alarm Number :" + AlarmNum + "<br>";
			wintxt += "&nbsp; Alarm Name   :" + sName + "<br>";
			wintxt += "</p>";
			wintxt +=   "<embed src=" + myFileSpec + " autostart='true' loop='false' controller='false'>";
			wintxt += "</body></html>";
			
			var alarmWind = window.open('','Alarm','HEIGHT=300,WIDTH=400');
			if(!alarmWind)
			{
				// try opening it another way
				//
				var tryIt = 1;
				alarmWind = window.open('javascript:wintxt',"","width=400,height=300");
				if(!alarmWind)
				{
					tryIt = 2;
					alert('Unable to Open Pop Up Window and Sound Alarm for Alarm');
					return;
				}
				
				alarmWind.document.close();
				
				alert('Call gary at 617 242 8721');
				return;
			}
			
			alarmWind.document.write(wintxt);
			alarmWind.document.close();
		}
	}
	catch(error)
	{
		onerror( error, " doAlarm ");
	}
	
	
	return false;
};

CAlarmTimer.prototype.cancel = function()
{

    this.clearAlarms();
	// clear all running events
    this.alarmTimes[FN.NUMBERALARMS] = 0;
	// to do delete all new objects;
//	alert('cancel');
};


CAlarmTimer.prototype.SuperKludgewithJimmies = function(miAlarmType)
{
var kludgeLimit = 5;
var icounter = 0;

    for (i = 1; i <= this.alarmTimes[FN.NUMBERALARMS]; i++)
	{
		if( (this.alarmTimes[i][myField.IALARMTYPE]) == miAlarmType)
		{
				if((this.alarmTimes[i][myField.MALARMTIME]) != 0)
				{
					icounter++;
					if(icounter > kludgeLimit)
					{
						doMessage('Too Many Timers, Send Cash now');
						return -1;
					}
				}
		}
	}
return 0;
};

function CTimerOptions	( aObjectReference, 
						  bWindowIsTopmost,
						  bClock24Hour,
						  bDisplayClockSecond,
						  bAlarmActive,
						  bSpeakClock,
						  iSpeakIntervals,
						  iUpdateInterval,
						  bShowTime,
						  bShowDate,
						  bShowJulian,
						  bShowBigScr
						)
{

	//get the stored options and then let them be overidden if specified in the constructor
	// should I declare them as vars here?

	this.getit();

/*	I probably need to check undefined or '' here
	//	  this.aObjectReference		= ; 
	(bWindowIsTopmost)		? (this.m_bWindowIsTopmost = bWindowIsTopmost)		:  (this.m_bWindowIsTopmost = -1);
	(bClock24Hour)			? (this.m_bClock24Hour = bClock24Hour)				:  (this.m_bClock24Hour	= 0);
	(bDisplayClockSecond)	? (this.m_bDisplayClockSecond = bDisplayClockSecond) : (this.m_bDisplayClockSecond = -1);
	(bAlarmActive)			? (this.m_bAlarmActive = bAlarmActive)				:  (this.m_bAlarmActive = -1);
	(bSpeakClock)			? (this.m_bSpeakClock = bSpeakClock)				:  (this.m_bSpeakClock = 0);
	(iSpeakIntervals)		? (this.m_iSpeakIntervals = iSpeakIntervals)		:  (this.m_iSpeakIntervals = 0);
	(iUpdateInterval)		? (this.m_iUpdateInterval = iUpdateInterval)		:  (this.m_iUpdateInterval = 1);
	(bShowTime)				? (this.m_bShowTime = bShowTime)					:  (this.m_bShowTime = -1);
	(bShowDate)				? (this.m_bShowDate = bShowDate)					:  (this.m_bShowDate = -1);
	(bShowJulian)			? (this.m_bShowJulian = bShowJulian)				:  (this.m_bShowJulian = -1);
	(bShowBigScr)			? (this.m_bShowBigScr = bShowBigScr)				:  (this.m_bShowBigScr = 0);
*/

}

CTimerOptions.prototype.setit = function()
{
	//write to cookie
	//m_bWindowIsTopmost%3D-1%26m_bClock24Hour%3D0%26m_bDisplayClockSecond%3D-1%26m_bAlarmActive%3D-1%26m_bSpeakClock%3D0%26m_iSpeakIntervals%3D0%26m_iUpdateInterval%3D1%26m_bShowTime%3D0%26m_bShowDate%3D-1%26m_bShowJulian%3D-1%26m_bShowBigScr%3D0
	var cv = "";

	cv += 'm_bWindowIsTopmost='				+ this.m_bWindowIsTopmost;
	cv += '&' +'m_bClock24Hour='			+  this.m_bClock24Hour;
	cv += '&' + 'm_bDisplayClockSecond='	+  this.m_bDisplayClockSecond;
	cv += '&' + 'm_bAlarmActive='			+  this.m_bAlarmActive;
	cv += '&' + 'm_bSpeakClock='			+  this.m_bSpeakClock;
	cv += '&' + 'm_iSpeakIntervals='		+  this.m_iSpeakIntervals;
	cv += '&' + 'm_iUpdateInterval='		+  this.m_iUpdateInterval;
	cv += '&' + 'm_bShowTime='				+  this.m_bShowTime;
	cv += '&' + 'm_bShowDate='				+  this.m_bShowDate;
	cv += '&' + 'm_bShowJulian='			+  this.m_bShowJulian;
	cv += '&' + 'm_bShowBigScr='			+  this.m_bShowBigScr;

	var expdate = new Date();

	expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365)); 
	setCookie("TimerOptions", cv, expdate, "/", null, false);
};


CTimerOptions.prototype.setOpt = function(myin)
{
//	(m_bWindowIsTopmost)		TDTop	'TDTop:1'	'TDTop:0' 
//	(m_bClock24Hour)			TDH		'TDH:1'		'TDH:0' '
//	(m_bDisplayClockSecond)		TDS		'TDS:1'		'TDS:0' 
//	(m_bAlarmActive)			TDA		'TDA:1'		'TDA:0'
//	(m_bSpeakClock)				TDsc	'TDsc:1'	'TDsc:0'
//	(m_iSpeakIntervals)			TDsi	'TDsi' + this.value
//	(m_iUpdateInterval)			TDI		'TDI' + this.value
//	(m_iNoticeInterval)			TDN		'TDN' + this.value 
//	(m_bShowTime)				TDT		'TDT:1'		'TDT:0'
//	(m_bShowDate)				TDD		'TDD:1'		'TDD:0'
//	(m_bShowJulian)				TDJ		'TDJ:1'		'TDJ:0'
//	(m_bShowBigScr)				TDM		'TDM:1'		'TDM:0'
//	m_TimeFormat				TDF		'TDF:HMS'	'TDF:SMH'
//								HMS
//								SMH


	myRe=/:/g;
	myArray = myin.split(/:/)

		switch (myArray[0])
		{
			case 'TDTop':
				{
					this.m_bWindowIsTopmost= myArray[1];	
					break;
				}
			case 'TDH':
				{
					this.m_bClock24Hour= myArray[1];		
					break;
				}
			case 'TDS':
				{
					this.m_bDisplayClockSecond= myArray[1];
					break;
				}
			case 'TDA':
				{
					this.m_bAlarmActive= myArray[1];
					break;
				}
			case 'TDsc':
				{
					this.m_bSpeakClock= myArray[1];		
					break;
				}
			case 'TDsi':
				{
					this.m_iSpeakIntervals= myArray[1];
					break;
				}
			case 'TDI':
				{
					this.m_iUpdateInterval= myArray[1];	
					break;
				}
			case 'TDN':
				{
					this.m_iNoticeInterval= myArray[1];	
					break;
				}

			case 'TDT':
				{
					this.m_bShowTime= myArray[1];			
					break;
				}
			case 'TDD':
				{
//					alert('setOpt ' + myin + ' ' + myArray[0] + ' ' + myArray[1]);
					this.m_bShowDate= myArray[1];			
					break;
				}
			case 'TDJ':
				{
					this.m_bShowJulian= myArray[1];			
					break;
				}
			case 'TDM':
				{
					this.m_bShowBigScr= myArray[1];		
					break;
				}
			case 'TDF':
				{
					if(myArray[1] == 'HMS')
					{
						// set it SMH
					}
				}
			default:
		} //switch 

//	alert('setOpt ' + myin + ' ' + myArray[0] + ' ' + myArray[1]);
};


CTimerOptions.prototype.getit = function()
{
	//read from cookie
	var sTimerOptions;

	sTimerOptions = getCookie("TimerOptions");

	if(sTimerOptions == undefined || sTimerOptions == "" )
	{ 
		//set the defaults
		this.m_bWindowIsTopmost = -1;
		this.m_bClock24Hour	= 0;
		this.m_bDisplayClockSecond = -1;
		this.m_bAlarmActive = -1;
		this.m_bSpeakClock = 0;
		this.m_iSpeakIntervals = 0;
		this.m_iUpdateInterval = 1;
		this.m_bShowTime = -1;
		this.m_bShowDate = -1;
		this.m_bShowJulian = -1;
		this.m_bShowBigScr = 0;
		//save the defaults
		this.setit();
		//I should probably popup help and go to it
	}
	else
	{
		myRe=/&/g;
		myArray = sTimerOptions.split(/&/)

		for(i=0; i < myArray.length; i++)
		{
			try
			{
				newstr = 'this.' + myArray[i];
				eval(newstr);
			}
			catch(e)
			{
				alert(e);
			}
		}
	//	alert(' getit m_bShowDate = ' +		this.m_bShowDate);			
	}
};

CTimerOptions.prototype.setForm = function(form)
{
	if(this.m_bShowTime != 0)
	{
		getElement(form).fbShowTime.checked = true;
	}
	else
	{
		getElement(form).fbShowTime.checked = false;
	}

	if(this.m_bShowDate != 0)
	{
		getElement(form).fbShowDate.checked = true;
	}
	else
	{
		getElement(form).fbShowDate.checked = false;
	}

	if(this.m_bShowJulian != 0)
	{
		getElement(form).fbShowJulian.checked = true;
	}
	else
	{
		getElement(form).fbShowJulian.checked = false;
	}
};


