/*
	$Id: clock.js 39405 2008-07-07 09:49:09Z echandran $
	$URL: http://nvdweb1/svn/CodeDB/branches/stable/webroot/images/js/clock.js $
*/

function init ( )
{
  timeDisplay = document.createTextNode ( "" );
  document.getElementById("clock").appendChild ( timeDisplay );
}

function arrayOfMonthNames() {
        this[0] = "January";
        this[1] = "February";
        this[2] = "March";
        this[3] = "April";
        this[4] = "May";
        this[5] = "June";
        this[6] = "July";
        this[7] = "August";
        this[8] = "September";
        this[9] = "October";
        this[10] ="November";
        this[11] ="December";
}

function getMonthName(m) {
        var monthNames = new arrayOfMonthNames();
        return monthNames[m];
}

function updateClock (dateFormat)
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentTimeString = "";
 
  // Pad the minutes with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
 
  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "am" : "pm";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  var currentDayOfMonth = currentTime.getDate()
  var currentMonth = getMonthName(currentTime.getMonth());
  var currentYear =  currentTime.getYear();
  if (currentYear < 1000) currentYear += 1900;
  
  // Compose the string for display
  if(dateFormat && dateFormat != "HH:MM:tt dd MMM YYYY")
  	currentTimeString = currentHours + ":" + currentMinutes + timeOfDay + ' ' + currentDayOfMonth + ' ' + currentMonth + ' ' + currentYear;
  else 
  	currentTimeString = currentDayOfMonth + ' ' + currentMonth + ' ' + currentYear + ': ' +currentHours + ":" + currentMinutes + timeOfDay;

  // Update the time display
  var clk = document.getElementById("clock");
  if ( clk != undefined ) 
  	clk.firstChild.nodeValue = currentTimeString;
}


