//----------------------------------------------------------
//
// Filename    : General.js
// Description : General purpose Javascript functions
// Author      : Kevin Spencer
// Last Change : 19-May-01
//
//----------------------------------------------------------

//----------------------------------------------------------
//
// Show all the properties of an object
//
//----------------------------------------------------------

function ShowProperties( object )
{
  for ( var prop in object )
  {
    document.write( prop + "<br>" )
//    document.write( prop + " = " + document[prop] + "<br>" )
  }
}

//----------------------------------------------------------
//
// Get the value of a cookie.
//
//----------------------------------------------------------

function GetCookie( Name, Default )
{
  var search = Name + "=";

  if ( document.cookie.length > 0 )
  {
    offset = document.cookie.indexOf( search );
    if ( offset != -1 )
    {
      offset += search.length;
      end = document.cookie.indexOf( ";", offset );
      if ( end == -1 )
         end = document.cookie.length;
         return unescape( document.cookie.substring( offset, end ) );
    }
  }
  return Default
}

//----------------------------------------------------------
//
// Create or change a cookie.
//
//----------------------------------------------------------

function SetCookie( sName, sValue )
{
  var date = new Date();

  date.setTime( date.getTime() + 1000 * 60 * 60 * 24 * 365 );

  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

//----------------------------------------------------------
//
// Delete the cookie with the specified name.
//
//----------------------------------------------------------

function DelCookie( sName )
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT";
}

//----------------------------------------------------------
//
// Returns the age of a person born on the specified date, in years.
//
//----------------------------------------------------------

function GetAge( year, month, date )
{
  now = new Date();

  age = now.getYear() - year;
  if ( age < 0 )
    age += 1900;

  if
  (
    ( now.getMonth()+1 < month ) ||
    ( ( now.getMonth()+1 == month ) && ( now.getDate() < date ) )
  )
    age--;

  return age;
}

//----------------------------------------------------------
//
// Format a number in the form %02d
//
//----------------------------------------------------------

function Format( num )
{
  var str = "" + num

  if ( str.length < 2 )
    str = "0" + str

  return str
}

//----------------------------------------------------------
//
// Return the 3-character abbreviation for the specified month.
//
//----------------------------------------------------------

function MonthStr( month )
{
  var ms = new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" )

  return ms[ month ]
}

//----------------------------------------------------------
//
// Returns the current date in the form dd-mmm-yy HH:MM
//
//----------------------------------------------------------

function CurrentDate()
{
  date   = new Date();

  str = Format( date.getDate() ) + "-" + MonthStr( date.getMonth() ) + "-" + date.getYear()
  str += " " + Format( date.getHours() ) + ":" + Format( date.getMinutes() ) + ":" + Format( date.getSeconds() )

  return str

//  return date.toLocaleString();  // More portable, but not as pretty
}

//----------------------------------------------------------
//
// Pre-load a sequence of images for faster viewing.
//
//----------------------------------------------------------

function PreloadImages( length, path, type )
{
    for ( var i = 1; i <= length; i++ )
    {
        this[i] = new Image()
        this[i].src = path + i + type
    }
    return this
}

//----------------------------------------------------------
//
// Show a string, unescaped. Used for brevity in the HTML.
//
//----------------------------------------------------------

function Show( str )
{
    document.write( unescape( str ) )
}
