var is24 = false;
var isUTC = false;

function date_update() {
	var time = new Date();
	
	var nday = time.getDay();
	var nmonth = time.getMonth();
	var ntoday = time.getDate();
	var nyear = time.getFullYear();
	
	if(isUTC) {
		nday = time.getUTCDay();
		nmonth = time.getUTCMonth();
		ntoday = time.getUTCDate();
		nyear = time.getUTCFullYear();
	}
	
	var days = new Array();
	days[0]="Sun";
	days[1]="Mon";
	days[2]="Tue";
	days[3]="Wed";
	days[4]="Thu";
	days[5]="Fri";
	days[6]="Sat";
	
	var months = new Array();
	months[0]="January";
	months[1]="February";
	months[2]="March";
	months[3]="April";
	months[4]="May";
	months[5]="June";
	months[6]="July";
	months[7]="August";
	months[8]="September";
	months[9]="October";
	months[10]="November";
	months[11]="December";
	
	var clockDiv = document.getElementById("clockDiv");
	var dateSpan = document.getElementById("dateSpan");
	
	var newSpan, clockText;
	
	// Do date
	newSpan = document.createElement("span");
	clockText = document.createTextNode(days[nday]+", "+months[nmonth]+" "+ntoday+" "+nyear);
	newSpan.appendChild(clockText);
	clockDiv.replaceChild(newSpan, dateSpan);
	newSpan.setAttribute("id","dateSpan");
	newSpan.setAttribute("onclick","isUTC=!isUTC;date_update()");
	
	var tomorrow = new Date();
	
	if(isUTC) {
		tomorrow.setUTCDate(tomorrow.getUTCDate()+1);
		tomorrow.setUTCHours(0);
		tomorrow.setUTCMinutes(0);
		tomorrow.setUTCSeconds(0);
	} else {
		tomorrow.setDate(tomorrow.getDate()+1);
		tomorrow.setHours(0);
		tomorrow.setMinutes(0);
		tomorrow.setSeconds(0);
	}
	
	var interval = tomorrow - time;
	setTimeout('date_update()',interval+1000);
}

function time_update() {
	var time = new Date();
	var nhours = time.getHours();
	var nmins = time.getMinutes();
	var nsecn = time.getSeconds();
	
	if(isUTC) {
		nhours = time.getUTCHours();
		nmins = time.getUTCMinutes();
		nsecn = time.getUTCSeconds();
	}
	
	var AorP = "";
	
	if(!is24) {
		AorP = (nhours>=12) ? "PM" : "AM";
		if (nhours>=13) nhours-=12;
		if (nhours==0) nhours=12;
	} else if(nhours < 10) nhours = "0" + nhours;
	if (nsecn < 10) nsecn = "0" + nsecn;
	if (nmins < 10) nmins = "0" + nmins;
	
	var clockDiv = document.getElementById("clockDiv");
	var timeSpan = document.getElementById("timeSpan");
	
	var newSpan, clockText;
	
	// Do time
	newSpan = document.createElement("span");
	clockText = document.createTextNode(nhours+":"+nmins+":"+nsecn+" "+AorP+" "+(isUTC?"GMT":""));
	newSpan.appendChild(clockText);
	clockDiv.replaceChild(newSpan, timeSpan);
	newSpan.setAttribute("id","timeSpan");
	newSpan.setAttribute("onclick","is24=!is24");
	
	setTimeout('time_update()',1000);
}

date_update();
time_update();
