// Copyright (C) 2008 - Matt Brophy
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 1, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc.

/******************************************************************************
* org.brophy.js
* 
* Collection of useful javascript functions based off the MochiKit javasacript 
* library (http://www.mochikit.com).
******************************************************************************/

/*
  Initialize the org.brophy.jslib.* namespaces
*/
if(!org) 
  { var org = {}; } 
if(isUndefinedOrNull(org.brophy)) 
  { org.brophy = {}; } 
if(isUndefinedOrNull(org.brophy.jslib)) 
  { org.brophy.jslib = {}; } 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// org.brophy.jslib.Utils
// 
// Javascript utility library
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(isUndefinedOrNull(org.brophy.jslib.Utils)) 
  { org.brophy.jslib.Utils = {}; } 

org.brophy.jslib.Utils = 
{
  
  "submitOnEnter": function(evt,form) 
  /*
    Checks the keycode of the passed event, or the window event to see if
    the 'Enter' key was pressed.  If so, the form indicated by 'form' is 
    submitted.

    Returns: TRUE if the enter eky was pressed and the form submitted, 
             FALSE otherwise
  */
  {
    if(isUndefinedOrnull(form))
    {
      return false; 
    }
    else if ( (evt && evt.which == 13) || 
              (window.event && window.event.keyCode == 13) )
    { 
      form.submit();
      return true;
    }
    else
    {
      return false;  
    }
  },

  "encodeXMLEntities": function(str)
  /*
    Function to encode all XML entities in a string of text:
      &  -> &amp; 
      <  -> $lt;  
      >  -> $gt;  
      "  -> &quot;
      '  -> $apos;

    Returns: the passed string with the entities encoded
  */
  {
    if(isUndefinedOrNull(str))
      { return ""; }
  
     str = str.replace(/&/g, "&amp;");
     str = str.replace(/</g, "&lt;");
     str = str.replace(/>/g, "&gt;");
     str = str.replace(/"/g, "&quot;");
     str = str.replace(/'/g, "&apos;");

     return str;
  },

  "decodeXMLEntities": function(str)
  /*
    Function to decode all XML entities in a string of text:
      &amp;  ->  &
      $lt;   ->  <
      $gt;   ->  >
      &quot; ->  "
      $apos; ->  '

    Returns: the passed string with the entities decoded
  */
  {
    if(isUndefinedOrNull(str))
      { return ""; }
  
     str = str.replace(/\&amp;/g, "&");
     str = str.replace(/\&lt;/g, "<");
     str = str.replace(/\&gt;/g, ">");
     str = str.replace(/\&quot;/g, '"');
     str = str.replace(/\&apos;/g, "'");

     return str;
  }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// org.brophy.jslib.Exception
// 
// Javascript library to handle exceptions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(isUndefinedOrNull(org.brophy.jslib.Exception)) 
  { org.brophy.jslib.Exception = {}; } 

org.brophy.jslib.Exception = 
{
  "Exception": function(intId, strMsg, strSrc)
  {
    this.id = intId;
    this.message = strMsg;
    this.source = strSrc;
  },
 
  "errorHandler": function(err)
  {
    alert("An error has occured");
    alert(err.message);
  }
}


