/*------------------------------------------------------------------------
- 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);

/*----------------------------- 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 */
