// Set some exterior variables (not in clock loop)
var blinker = ":"; // The default blinker
var alarm = "--:--:-- --"; // The default alarm
var whyalarm = "Wake Up!"; // The default reason for the alarm

// The clock loop
function show()
	{
		// The bit that decides where the clock will appear on the page
		if (!document.all&&!document.getElementById)
		return
		thelement=document.getElementById? document.getElementById("clock"): document.all.clock;
		
		// The time aquisistion process
		var newdate=new Date(); // Get the system date & time
		var dateness=newdate.getDate(); // Get the date
		var monthness=newdate.getMonth(); // Get the month number
		var hours=newdate.getHours(); // Get the hour
		var minutes=newdate.getMinutes(); // Get the minute
		var seconds=newdate.getSeconds(); // Get the second
		
		// The rough time set-up
		if (hours>5 && hours<12) var rtoday = "in the Morning"
		if (hours>=12 && hours<18) var rtoday = "in the Afternoon"
		if (hours>=18 && hours<22) var rtoday = "in the Evening"
		if (hours<=5 || hours>=22) var rtoday = "at Night"
		
		if (minutes>32) hours=hours+1
		
		if (hours==0 || hours==12 || hours==24) var rhours = "Twelve";
		if (hours==1 || hours==13) var rhours = "One";
		if (hours==2 || hours==14) var rhours = "Two";
		if (hours==3 || hours==15) var rhours = "Three";
		if (hours==4 || hours==16) var rhours = "Four";
		if (hours==5 || hours==17) var rhours = "Five";
		if (hours==6 || hours==18) var rhours = "Six";
		if (hours==7 || hours==19) var rhours = "Seven";
		if (hours==8 || hours==20) var rhours = "Eight";
		if (hours==9 || hours==21) var rhours = "Nine";
		if (hours==10 || hours==22) var rhours = "Ten";
		if (hours==11 || hours==23) var rhours = "Eleven";
		
		if (minutes<=2 || minutes>57) var rminutes = "O' Clock"
		if (minutes>2 && minutes<=7) var rminutes = "Five Past"
		if (minutes>7 && minutes<=12) var rminutes = "Ten Past"
		if (minutes>12 && minutes<=17) var rminutes = "Quarter Past"
		if (minutes>17 && minutes<=22) var rminutes = "Twenty Past"
		if (minutes>22 && minutes<=27) var rminutes = "Twenty-Five Past"
		if (minutes>27 && minutes<=32) var rminutes = "Half Past"
		if (minutes>32 && minutes<=37) var rminutes = "Twenty-Five To"
		if (minutes>37 && minutes<=42) var rminutes = "Twenty To"
		if (minutes>42 && minutes<=47) var rminutes = "Quarter To"
		if (minutes>47 && minutes<=52) var rminutes = "Ten To"
		if (minutes>52 && minutes<=57) var rminutes = "Five To"
		
		if (minutes>32) hours=hours-1
		
		// The time set-up process
		if (dateness<10) dateness="0"+dateness; // Add a '0' to the front of single digit dates
		if (monthness<10) monthness="0"+monthness; // Add a '0' to the front of single digit months
		if (hours<12)
			{
				ampm="AM";
			}
		else
			{
				ampm="PM";
			} // Set AM/PM
			
		if (minutes<=2 || minutes>57)
			{
				texttime = "<span class='texttime'>" +rhours +" " +rminutes +" " +rtoday+"</span>"
			}
		else
			{
				texttime = "<span class='texttime'>" +rminutes +" " +rhours +" " +rtoday +"</span>"
			}
			
		if (hours>12) hours=hours-12; // Change hours to 12-hour format
		if (hours==0) hours=12
		if (hours<10) hours="0"+hours; // Add a '0' to the front of single digit hours
		if (minutes<10) minutes="0"+minutes; // Add a '0' to the front of single digit minutes
		if (seconds<10) seconds="0"+seconds; // Add a '0' to the front of single digit seconds
		
		// Stops some errors
		if (blinker == null) blinker=oldblink;
		if (alarm == null) alarm=oldalarm;
		if (whyalarm == null) whyalarm=oldwhyalarm;
		
		// Sets off the alarm (if there is one set)
		var currenttime = hours +":" +minutes +":" +seconds +" " +ampm; // Get the current time in a specific format
		if (alarm == currenttime) alert('Ding Dong! It is ' +currenttime +', time for ' +whyalarm +'!'); // Check whether the current time is the same as the alarm time, if yes, set it off
		
		// The arrays for the text-date
		var weekday=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); // The weekday array
		var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); // The months array
		
		// Sets up the various parts of the clock
		minidate = "<span class='minidate'>" +dateness +"/" +monthness +"/" +newdate.getFullYear() +"</span>"; // The numerical date
		expanddate = "<span class='expanddate'>" +weekday[newdate.getDay()] +", " +dateness +" " +monthname[newdate.getMonth()] +", " +newdate.getFullYear() +"</span>"; // The text-date
		thetime = "<span class='thetime'>" +hours +blinker.blink() +minutes +blinker.blink() +seconds +" " +ampm +"</span>"; // The time
		
		// Adds the various parts of the clock together
		completedate =  /*minidate +"<br>" +*/expanddate +"<br>" +thetime +"<br>" +texttime
		
		// Sets the complete clock to appear at the location designated at the start
		thelement.innerHTML=completedate;
		
		// The clock's refresh rate
		setTimeout("show()",1000);
	}

// Tells the browser to run the script on page-load
window.onload=show;

