// ----------------------------------------------------------------------------
// Copyright (C) 1998, ???
//
// Module Name:
// Environment:
// Description:
// Notes:
//
// Modification History
// ------------------------------------------------------------------------
//   Name                 Date        Description
//   -------------------- ----------- ------------------------------------
//
// --------------------------------------------------------------------------


// --------------------------------------------------------------------------
// Module Name:
// Description: For Version 4+ browsers, write the appropriate HTML to
//              the page for the clock, otherwise, attempt to write a
//              static date to the page.
// --------------------------------------------------------------------------
function Clock_Create()
{
  // -----------------------------
  // Netscape 6 or Internet Explorer
  // -----------------------------
  if ((this.bIE) || (this.bN6))
  {
    this.Tags = '<SPAN ID="' + this.ID + '"';
    if ((this.Style != null) && (this.Style != ""))
    {
      this.Tags += ' STYLE="' + this.Style + '"';
    }
    this.Tags += '></SPAN>';
  }
  // -----------------------------
  // Netscape (Older...Pre 6.0)
  // -----------------------------
  else if (this.bNS)
  {
    this.Tags = '<ILAYER ID="' + this.ID + 'Position"';
    if ((this.Style != null) && (this.Style != ""))
    {
      this.Tags += ' STYLE="' + this.Style + '"';
    }
    this.Tags += '><LAYER ID="' + this.ID + '"></LAYER></ILAYER>';
  }

  // -----------------------------
  // 
  // -----------------------------
  this.bCreated = true;
  if (! this.bOther) document.write(this.Tags);
}

// --------------------------------------------------------------------------
// Module Name:
// Description:
// --------------------------------------------------------------------------
function Clock_Calculate(strDateFormat, strTimeFormat)
{
  var iValue;
  var dtDate = new Date();

  // -----------------------------
  // Date
  // -----------------------------
  if ((strDateFormat != null) && (strDateFormat != 'null'))
  {
    // -----------------------------
    // Month
    // -----------------------------
    if (strDateFormat.indexOf('m') > -1)
    {
      iValue = dtDate.getMonth() ;
      strDateFormat = ((strDateFormat.indexOf('mm') > -1) ? strDateFormat.replace('mm', (iValue < 10) ? '0' + iValue : iValue) : strDateFormat.replace('m', iValue));
    }
    // -----------------------------
    // Date
    // -----------------------------
    if (strDateFormat.indexOf('d') > -1)
    {
      iValue = dtDate.getDate() + 1;
      strDateFormat = ((strDateFormat.indexOf('dd') > -1) ? strDateFormat.replace('dd', (iValue < 10) ? '0' + iValue : iValue) : strDateFormat.replace('d', iValue));
    }
    // -----------------------------
    // Year
    // -----------------------------
    if (strDateFormat.indexOf('y') > -1)
    {
      iValue = dtDate.getFullYear() < 1900 ? dtDate.getFullYear() + 1900 : dtDate.getFullYear();
      strDateFormat = ((strDateFormat.indexOf('yyyy') > -1) ? strDateFormat.replace('yyyy', iValue) : strDateFormat.replace('yy', ((iValue % 100) < 10) ? '0' + (iValue % 100) : (iValue % 100)));
    }
  }

  // -----------------------------
  // Time
  // -----------------------------
  if ((strTimeFormat != null) && (strTimeFormat != 'null'))
  {
    var bMilitaryTime = true;
    
    // -----------------------------
    // AM/PM
    // -----------------------------
    if (strTimeFormat.indexOf('AM/PM') > -1)
    {
      iValue = dtDate.getHours();
      strTimeFormat = ((iValue > 12) ? strTimeFormat.replace('AM/PM', 'PM') : strTimeFormat.replace('AM/PM', 'AM'));
      bMilitaryTime = false;
    }
    // -----------------------------
    // Hour
    // -----------------------------
    if (strTimeFormat.indexOf('h') > -1)
    {
      iValue = dtDate.getHours();
      if (! bMilitaryTime) iValue = (iValue == 0) ? 12 : (iValue > 12) ? iValue -= 12 : iValue;
      strTimeFormat = ((strTimeFormat.indexOf('hh') > -1) ? strTimeFormat.replace('hh', (iValue < 10) ? '0' + iValue : iValue) : strTimeFormat.replace('h', iValue));
    }
    // -----------------------------
    // Minutes
    // -----------------------------
    if (strTimeFormat.indexOf('m') > -1)
    {
      iValue = dtDate.getMinutes();
      strTimeFormat = ((strTimeFormat.indexOf('mm') > -1) ? strTimeFormat.replace('mm', (iValue < 10) ? '0' + iValue : iValue) : strTimeFormat.replace('m', iValue));
    }
    // -----------------------------
    // Seconds
    // -----------------------------
    if (strTimeFormat.indexOf('s') > -1)
    {
      iValue = dtDate.getSeconds();
      strTimeFormat = ((strTimeFormat.indexOf('ss') > -1) ? strTimeFormat.replace('ss', (iValue < 10) ? '0' + iValue : iValue) : strTimeFormat.replace('s', iValue));
    }
  }

  return(((strDateFormat != null) && (strDateFormat != 'null') ? strDateFormat + ' ': '') + ((strTimeFormat != null) && (strTimeFormat != 'null') ? strTimeFormat : ''));
}

// --------------------------------------------------------------------------
// Module Name:
// Description:
// --------------------------------------------------------------------------
function Clock_Update(strID, strDateFormat, strTimeFormat)
{
  if (((document.all) ? 1 : 0) || ((window.sidebar) ? 1 : 0))
  {
    document.getElementById(strID).innerHTML = Clock_Calculate(strDateFormat, strTimeFormat);
  }
  // -----------------------------
  // 
  // -----------------------------
  else if ((window.sidebar) ? 1 : 0)
  {
    var oLayer = document.layers[strID + 'Position'].document.layers[strID].document;
    oLayer.open();
    oLayer.write(Clock_Calculate(strDateFormat, strTimeFormat));
    oLayer.close();
  }
}

// --------------------------------------------------------------------------
// Module Name:
// Description:
// --------------------------------------------------------------------------
function Clock_Display(strDateFormat, strTimeFormat)
{
  // -----------------------------
  // 
  // -----------------------------
  if (! this.bCreated) this.Create();
  if ((this.bIE) || (this.bN6))
  {
    document.getElementById(this.ID).innerHTML = this.Calculate(strDateFormat, strTimeFormat);
    window.setInterval('Clock_Update(\'' + this.ID + '\',\'' + strDateFormat + '\',\'' + strTimeFormat + '\')', 100);
  }
  // -----------------------------
  // 
  // -----------------------------
  else if (this.bNS)
  {
    var oLayer = document.layers[this.ID + 'Position'].document.layers[this.ID].document;
    oLayer.open();
    oLayer.write(this.Calculate(strDateFormat, strTimeFormat));
    oLayer.close();
    window.setInterval('Clock_Update(\'' + this.ID + '\',\'' + strDateFormat + '\',\'' + strTimeFormat + '\')', 100);
  }
  // -----------------------------
  // 
  // -----------------------------
  else
  {
    document.write(this.Calculate(strDateFormat, strTimeFormat));
  }
}

// --------------------------------------------------------------------------
// Module Name:
// Description:
// --------------------------------------------------------------------------
function Clock(strID, strStyle)
{
  // -------------------------------
  // Private Variables
  // Calculate Browser Type
  // -------------------------------
  this.bCreated = false;
  this.bIE    = (document.all) ? 1 : 0;
  this.bNS    = (document.layers) ? 1 : 0;
  this.bN6    = (window.sidebar) ? 1 : 0;
  this.bOther = ((! this.bIE) && (! this.bNS) && (! this.bN6)) ? 1 : 0; 

  // -------------------------------
  //
  // -------------------------------
  this.ID    = strID;
  this.Style = strStyle;

  // -------------------------------
  // Methods/Functions
  // -------------------------------
  this.Create = Clock_Create;
  this.Calculate = Clock_Calculate;
  this.Display = Clock_Display;
}


