/*                                                                          dates.js
------------------------------------------------------------------------------------
Copyright Tim Brook 1997-2009

Purpose:
~~~~~~~
Provide miscellaneous date, time and status line routines for webpages.

Designed: 1 November 1997
~~~~~~~~
---------------------------------------------------------------------------------- */
// Constants
// ~~~~~~~~~

// var documentDate = new Date(document.lastModified) ;
var documentDate = new Date(Date.parse(document.lastModified)) ;
var documentYear = documentDate.getFullYear() ;

var weekday = new Array(
    'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'
) ;
var month = new Array(
    'January','February','March','April','May','June','July',
    'August','September','October','November','December'
) ;

var statusDivider = ' ||   ' ;

//---------------------------------
// Variables
// ~~~~~~~~~
var lastTime = '' ;         // the last time the clock was updated
var message = '' ;          // current status line message (without time)
var statusText = message ;  // default status line text (without time)

//---------------------------------
// Subroutines
// ~~~~~~~~~~~

// Make every date object capable of returning the time as a string
//
function toTime() {
    var h = this.getHours() ;
    var m = this.getMinutes() ;
    var time ;

    time = h % 12 ;    // -1 % 12 = -1! so usual construct doesn't work
    time = ((time==0) ? 12 : time) ;
    time += '.' + ( (m < 10) ? '0' : '') + m ;
    time += ' ' + ( (h > 12) ? 'pm' : 'am') ;

    return time
}
Date.prototype.toTime = toTime ;

// So now() can return the current time
//
function now() {
    var temp = new Date() ;    // Keep temp local
    return (typeof(temp.toTime) != 'function') ? '' : temp.toTime()
}

// Make every date object capable of returning the short date as a string
//
function toShort() {
    var m = this.getMonth() + 1
    var y = this.getYear() % 100

    return this.getDate() + '/' + ((m < 10) ? '0' : '') + m + '/' + ((y < 10) ? '0' : '') + y
}
Date.prototype.toShort = toShort

// Make every date object capable of returning the year as a string
//
function toFullYear() {
    var yyyy    = this.getYear()

    if (yyyy < 1000)  yyyy = 2000 + yyyy % 100

    return yyyy
}
Date.prototype.toFullYear = toFullYear ;
function toYYYY() { return this.toFullYear().toString() }
Date.prototype.toYYYY = toYYYY ;
function toYY() { return this.toYYYY().substr(-2) }
Date.prototype.toYY = toYY ;

// Make every date object capable of returning the medium date as a string
//
function toMedium() {
    return this.getDate() + ' ' + month[this.getMonth()] + ' ' + this.toFullYear()
}
Date.prototype.toMedium = toMedium ;

// Make every date object capable of returning the long date as a string
//
function toLong() {
    return weekday[this.getDay()] + ', ' + this.toMedium()
}
Date.prototype.toLong = toLong ;

// Now jDate() can return the specified date on the current page.
// 0 = today, -1 = yesterday, +1 = tomorrow &c.
// optionally with format specified by second parameter
//
function jDate(n) {
    var format     = new String ;
    var dateObj    = new Date() ;
    var dateString = new String ;        // return value

    dateObj.setTime(dateObj.getTime() + n*24*60*60*1000)

    if (jDate.arguments.length == 1) {
        format = 'm' ;                  // default to medium format
    } else {
        format = jDate.arguments[1] ;
        format = format.toLowerCase() ;
        format = format.substring(0,1) ;
    }

    if (typeof(dateObj.toShort) != 'function') return '' ; // NS4 bug workaround

    if (format =='y') dateString = dateObj.toFullYear() ;
    if (format =='s') dateString = dateObj.toShort() ;
    if (format =='m') dateString = dateObj.toMedium() ;
    if (format =='l') dateString = dateObj.toLong() ;

    return dateString
}

// today() returns the current date, optionally with format specified by parameter
//
function today() {
    var format = new String ;

    if (today.arguments.length==0) {
        format = 'medium' ;             // default to medium format
    } else {
        format = today.arguments[0] ;
    }

    return jDate(0,format)
}

//----------------------------------------------------
// Main functions
// ~~~~~~~~~~~~~~

// Force display message on status bar (until reset)
// with time displayed 1st unless there is a 2nd parameter e.g. 'only'
//
function showMessage() {
    var msgText

    msgText = (arguments.length < 2) ? (lastTime + statusDivider) : '               ' ;
    msgText += (arguments.length==0) ? message : arguments[0] ;

    status = msgText ;
    defaultStatus = msgText ;

    return true  // so can be used in events as "return showMessage(<text>)"
}

// Display the time & date on the status line (in front of current message)
//
function showTime() {
    var msgText = defaultStatus

    if (msgText=='') {
        msgText = statusText
    } else {
        if (msgText.indexOf(lastTime)==0) {
            msgText = msgText.substring((lastTime+statusDivider).length,msgText.length)
        }
    }
    if (msgText.lastIndexOf(documentYear) != (msgText.length-4)) {
        if (msgText.indexOf('©')==0)  msgText += '  ' + documentYear
    }
    lastTime = now() + '  ' + today('short')

    showMessage(msgText) ;
    return true  // so can be used in events as "return showTime()"
}

// Display default text on status bar
// (optionally change the default)
//
function statusReset() {
    if (arguments.length > 0)  statusText = arguments[0] ;
    showMessage(statusText) ;

    return true  // so can be used in events as "return statusReset()"
}

// Write the specified date on the current page.
// 0 = today, -1 = yesterday, +1 = tomorrow &c.
// optionally with format specified by second parameter
//
function writeDate(n) {
    var format     = new String ;

    if (writeDate.arguments.length==1) {
        format = 'long' ;                  // default to long date format
    } else {
        format = writeDate.arguments[1] ;
    }

    document.write(jDate(n,format)) ;

    return jDate(n,format)
}

//-----------------------------------------------------------------------------

