/*------------------------------------------------------------------------
- Module name   : lutil.js
- Author        : Paul Battersby
- Creation Date : Jul 27/02
- Description   :
-   This contains general purpose utilities
-------------------------------------------------------------------------*/

/*---------------------------- INCLUDE FILES ----------------------------*/


/*----------------------------- CONSTANTS -------------------------------*/


/*----------------------------- VARIABLES -------------------------------*/
/* determine the type of browser */
var LUTIL_isDOM = (document.getElementById ? true : false);
var LUTIL_isIE4 = ((document.all && !LUTIL_isDOM) ? true : false);
var LUTIL_isNS4 = (document.layers ? true : false);

/* the tag name or id of the last shown block of HTML */
var lutil_lastShown = "";

/*----------------------------- FUNCTIONS -------------------------------*/

//************************************************************************
// Name   : LUTIL_getRefById
// Author : Paul Battersby
// Description :
//  In a browser independant way, this returns a reference to the HTML
//  element that contains the given "id" value
//
// Pre    :
//  FORMVAL_isDOM, FORMVAL_isIE4, FORMVAL_isNS4 have all been defined
//
//  "id" = the value of the id field in an HTML element
//
// Post   :
//  (nothing)
//
// Returns:
//  reference to an HTML element
//*************************************************************************/
function LUTIL_getRefById(id)
{
  if (LUTIL_isDOM) return document.getElementById(id);
  if (LUTIL_isIE4) return document.all[id];
  if (LUTIL_isNS4) return document.layers[id];
} /* end of LUTIL_getRefById */

/*************************************************************************
* Name   : LUTIL_getArgs
* Author :
* <!-- Original:  Doug Lawson (dlawson@clark.net) -->
*
* <!-- This script and many more are available free online at -->
* <!-- The JavaScript Source!! http://javascript.internet.com -->
*
* modified by Paul Battersby
*
* Pre    :
*   (nothing)
*
* Post   :
*   a structure of arguements input from the document location string
*   has been returned to the caller.
*
*   For example, if the url is:
*
*       clicktst.htm?value1=1&value2=3
*
*   And we do this:
*
*       args = LUTIL_getArgs();
*
*   then this function will return
*
*       args.value1
*       args.value2
*
*   set to
*
*       1, 3
*
*   respectively
*
**************************************************************************/
function LUTIL_getArgs()
{
  // gets the arguments the page was loaded with - that is,
  // everything after the first '?'
  // parameters:
  //
  // values are ALWAYS case sensitive
  //
  var args  = new Object();
  var query = unescape(location.search.substring(1));
  var pairs = query.split("&");

  for (var i = 0; i< pairs.length; i++) {
		pairs[i]= unescape(pairs[i]);
		var pos=pairs[i].indexOf('=');

    if(-1 == pos) continue;

    var argname;
    argname = pairs[i].substring(0,pos);

    var value = pairs[i].substring(pos+1);
		args[argname] = value;
  }
  return args;
} /* end of LUTIL_getArgs */

//************************************************************************
// Name   : LUTIL_getBrowser
// Author : Paul Battersby
// Description :
//
// Pre :
//
// Params :
//
// Post :
//
// Returns :
//
//*************************************************************************/
function LUTIL_getBrowser()
{
  var detect = navigator.userAgent.toLowerCase();
  var OS,browser,version,total,thestring;

  function checkIt(string)
  {
    place = detect.indexOf(string) + 1;
    thestring = string;
    return place;
  }

  if (checkIt('konqueror'))
  {
    browser = "Konqueror";
    OS = "Linux";
  }
  else if (checkIt('safari')) browser = "Safari"
  else if (checkIt('omniweb')) browser = "OmniWeb"
  else if (checkIt('opera')) browser = "Opera"
  else if (checkIt('webtv')) browser = "WebTV";
  else if (checkIt('icab')) browser = "iCab"
  else if (checkIt('msie')) browser = "Internet Explorer"
  else if (!checkIt('compatible'))
  {
    browser = "Netscape Navigator"
    version = detect.charAt(8);
  }
  else browser = "An unknown browser";

  if (!version) version = detect.charAt(place + thestring.length);

  if (!OS)
  {
    if (checkIt('linux')) OS = "Linux";
    else if (checkIt('x11')) OS = "Unix";
    else if (checkIt('mac')) OS = "Mac"
    else if (checkIt('win')) OS = "Windows"
    else OS = "an unknown operating system";
  }

  return browser;

} /* end of LUTIL_getBrowser */

//************************************************************************
// Name   : LUTIL_copyHtml
// Author : Paul Battersby
// Description :
//  This will copy the inner HTML from one tag to another. The inner HTML can
//  be anything at all, including more HTML. This is useful for dynamically
//  changing the content of a page without needing to reload it.
//
//  NOTE the use of   style="display:none"  to hide the text that is to be
//       dynamically displayed
//
//  example:
//
//      <a href="javascript:LUTIL_copyHtml('src1','dst')">[expand src 1]</a>
//      <a href="javascript:LUTIL_copyHtml('src2','dst')">[expand src 2]</a>
//      <a href="javascript:LUTIL_copyHtml('src3','dst')">[expand src 3]</a>
//
//      <div id="dst">
//        New text goes here
//      </div>
//
//      <div id="src1" style="display:none">
//      I am some text from source 1
//      </div>
//
//      <div id="src2"  style="display:none">
//      <table border=1>
//        <tr>
//          <td>I</td>
//          <td>am</td>
//        </tr>
//        <tr>
//          <td>a</td>
//          <td>table</td>
//        </tr>
//        <tr>
//          <td>from</td>
//          <td>src 2</td>
//        </tr>
//      </table>
//
//      </div>
//
//      <div name="src3" id="src3"  style="display:none">
//        <OL>
//          <LI>I
//          <LI>am
//          <LI>a
//          <LI>list
//          <LI>from
//          <LI>source 3
//        </OL>
//
//      </div>
//
// Pre :
//  (nothing)
//
// Params :
//  src - the source name or id of the HTML to be copied
//  dst - the destination name or id for where the HTML is to be copied
//
// Post :
//  the source HTML has been copied to the destination HTML
//
// Returns :
//  (nothing)
//*************************************************************************/
function LUTIL_copyHtml(src,dst)
{

  var srcId = LUTIL_getRefById(src);
  var dstId = LUTIL_getRefById(dst);

  dstId.innerHTML = srcId.innerHTML;
} /* end of LUTIL_copyHtml */


//************************************************************************
// Name   : LUTIL_hideShow
// Author : Paul Battersby
// Description :
//  This hides or reveals the HTML within the specified tag. Call this once
//  with a given tagName and the HTML will be revealed. Call this a second
//  time in a row with the same tagName and the HTML will be hidden
//
//  Example:
//
//      <a href="javascript:LUTIL_hideShow('src1')">[show src 1]</a>
//      <a href="javascript:LUTIL_hideShow('src2')">[show src 2]</a>
//      <a href="javascript:LUTIL_hideShow('src3')">[show src 3]</a>
//
//      <div id="src1" style="display:none">
//      I am some text from source 1
//      </div>
//
//      <div id="src2"  style="display:none">
//      <table border=1>
//        <tr>
//          <td>I</td>
//          <td>am</td>
//        </tr>
//        <tr>
//          <td>a</td>
//          <td>table</td>
//        </tr>
//        <tr>
//          <td>from</td>
//          <td>src 2</td>
//        </tr>
//      </table>
//
//      </div>
//
//      <div id="src3"  style="display:none">
//        <OL>
//          <LI>I
//          <LI>am
//          <LI>a
//          <LI>list
//          <LI>from
//          <LI>source 3
//        </OL>
//
//      </div>
//
// Pre :
//  (nothing)
//
// Params :
//  tagName - the source name or id of the HTML to be hidden or revealed
//
// Post :
//  the HTML within the specified tag has been revealed or hidden as described
//  above
//
// Returns :
//  (nothing)
//*************************************************************************/
function LUTIL_hideShow(tagName)
{
  /* get an obj reference to the given tag name */
  var tagObj = LUTIL_getRefById(tagName);

  /* if this is not the same tag as was previously displayed */
  /* then unhide a new tag and hide the previous tag */
  if ( tagName != lutil_lastShown ) {
    tagObj.style.display = "";

    /* if there was a previously displayed tag, then hide it */
    if (lutil_lastShown != "") {
      LUTIL_getRefById(lutil_lastShown).style.display = "none";
    } /* endif */

    /* store this for later */
    lutil_lastShown = tagName;

  /* this is the same tag, then hide it */
  } else {

    /* hide the current tag */
    tagObj.style.display = "none";

    /* indicate that nothing is currently visible */
    lutil_lastShown = "";
  } /* endif */

} /* end of LUTIL_hideShow */

