/**
 * -----------------------------------------------------
 * The Log singleton object accepts messages from
 * other JavaScript components when its add()
 * method is invoked.
 * At a later time, all accumulated messages can
 * be displayed in an alert by invoking its alert()
 * method.
 */
Log = {
  messages: [],
  /**
   * Log.add(message) adds message to Log's list of
   * messages.
   */
  add: function(message) {
    this.messages.push(message);
  },
  /**
   * Log.showException(exception) adds a message from
   * exception to Log's list of messages and then
   * invokes Log.alert().
   * On a Firefox browser the added message will show
   * a stack trace.
   */
  showException: function(exception) {
    var message = "Exception thrown: " + exception.message;
    if (exception.stack) {
      message += "\nStack trace follows:\n";
      message += exception.stack;
    } else {
      message += "\nNo stack trace"
    }
    this.messages.push(message);
    this.alert();
  },
  /**
   * Log.alert() pops up an alert that displays all
   * messages that have been added to Log since the
   * previous time its alert() method was invoked.
   * After the alert is dismissed all messages are
   * removed from Log.
   * 
   * Each message has a newline character appended
   * automatically.
   */
  alert: function() {
    if (this.messages.length == 0) {
      return;
    }
    var alertText = "";
    for (var i = 0; i < this.messages.length; i++) {
      alertText += this.messages[i] + "\n";
    }
    alertText += "\n";
    alert(alertText);
    this.messages.length = 0;
  },
  /**
   * Log.getCount() the number of messages that have
   * been added to Log since the previous time its
   * alert() method was invoked.
   */
  getCount: function() {
    return this.messages.length;
  }
};

