﻿/* JScript File to display the time and Date */
var todaysDate;
var dateFlag;
var preScopeState;

function displayTime() {
    var today = new Date();
    var hours = today.getHours();
    var mins = today.getMinutes();
    var secs = today.getSeconds();    
    var scope = "AM";
    var todaysTimeDate;
    
    /* Call the date function once only */
    if (!dateFlag) {
        displayDate();
        dateFlag += 1;
    }
    
    /* Out put 12 hour clock with AM and PM */
    if (hours > 12) {
        scope = " PM";
    }
    else {
        scope = " AM";       
    }
    
    /* If new day update the date */
    if (preScopeState != scope) {
        preScopeState = scope
                
        if (preScopeState == " AM") {            
            displayDate();
        }
    }

    /* Make sure at midnight it outputs 12 */
    if (hours == 0) {
        hours = 12;
    }
    else if (hours > 12) {
        hours = hours - 12;
    }

    /* Make sure there are always two
    * characters for hours, mins and secs */

    /* Converts to string */
    hours = hours + "";
    if (hours.length == 1) {
        hours = "0" + hours;
    }

    mins = mins + "";
    if (mins.length == 1) {
        mins = "0" + mins;
    }

    secs = secs + "";
    if (secs.length == 1) {
        secs = "0" + secs;
    }

    todaysTimeDate = hours + ":" + mins + ":" + secs + scope + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + todaysDate;
    document.getElementById("currentTime").innerHTML = todaysTimeDate 
    t = setTimeout("displayTime()", 500);
}

/* Script to get todays date */
function displayDate() {
    /* Create the variables */
    var currentDate = new Date();
    var day = currentDate.getDate();
    var year = currentDate.getFullYear();
    var months = new Array(13);
    var dayType = "th";

    /* Populate the array with the months starting at position 1*/
    months[1] = "Jan";
    months[2] = "Feb";
    months[3] = "March";
    months[4] = "April";
    months[5] = "May";
    months[6] = "June";
    months[7] = "July";
    months[8] = "Aug";
    months[9] = "Sept";
    months[10] = "Oct";
    months[11] = "Nov";
    months[12] = "Dec";

    /*Get the current month + 1 as the months returned start at 0 for Jan */
    var month = months[currentDate.getMonth() + 1];

    /*Get the correct end type for the day nuber*/
    if (day == 1 || day == 21 || day == 31) {
        dayType = "st";
    }
    else if (day == 2 || day == 22) {
        dayType = "nd";
    }
    else if (day == 3 || day == 23) {
        dayType = "rd";
    }
    todaysDate = day + dayType + " " + month + " " + year;  
}
