//
// Main Javascript for site
//
// Included by all pages
//

var debugEnabled = false;

// Browser detection object
function Is()
{
  var agent = navigator.userAgent.toLowerCase();
  this.major = parseInt(navigator.appVersion);
  this.minor = parseFloat(navigator.appVersion);
  this.mac = (agent.indexOf("mac") != -1);
  this.ns = ((agent.indexOf('mozilla')!=-1) &&
    ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
  this.ns2 = (this.ns && (this.major == 3));
  this.ns3 = (this.ns && (this.major == 3));
  this.ns4 = (this.ns && (this.major >= 4));
  this.ns406 = (this.ns && (this.minor == 4.06));
  this.ns407 = (this.ns && (this.minor == 4.07));
  this.ns408 = (this.ns && (this.minor == 4.08));
  this.ns45 = (this.ns && (this.minor == 4.5));
  this.ie  = (agent.indexOf("msie") != -1);
  this.ie3 = (this.ie && (this.major == 2));
  this.ie4 = (this.ie && (this.major >= 4));
  this.op3 = (agent.indexOf("opera") != -1);
  this.dynamicBrowser = (this.ns4 || this.ie4);
}

// Global variables
var is = new Is();
var siteName = 'TNTWEB';
var siteURL = 'http://www.tntweb.f9.co.uk/tntweb';

// Null debug function - will get replace with code from 'debug.js' if debug is enabled
function debug() {}

if (location.protocol == 'file:')
{
  // Check if we are locally accessing this site
  var re = /^(.*tntweb)\/.*$/i
  siteURL = location.href.replace(re, '$1');
}

if (window.name == null || window.name == '')
{
  window.name = 'untitled';
}

/*
if (window.name.indexOf('Evaluator') != 0)
{
  if (debugEnabled == false && location.protocol == 'file:')
  {
    debugEnabled = confirm('Do you wish to enable debugging for window "' +
      window.name + '\" ?');
  }

  if (debugEnabled)
  {
    document.writeln('<scr' + 'ipt language="javascript" src="' + siteURL + '/scripts/debug.js"></scr' + 'ipt>');
  }
}
*/

document.loaded = false;

// Function called when any page is loaded.
function onLoad()
{
  debug('onLoad()');

  debug('name=' + name);
  debug('href=' + location.href);
  debug('siteURL=' + siteURL);
  debug('referrer=' + document.referrer);

  document.loaded = true;
}

// Function called when any page is unloaded.
function onUnLoad()
{
  debug('onUnLoad()');

  document.loaded = false;
}

function popupWindow(url)
{
  window.open(url,'popupWindow','toolbar=no,width=266,height=266,resizable=1,scrollbars=no')
}

function ImageButton(id, imgPath)
{
  debug('ImageButton(' + id + ',' + imgPath + ')');

  if (!document.links || !document.images || !Image)
  {
    debug('Browser doesn\'t support required DOM');
    return 0;
  }

  this.id = id;
  this.enabled = true;

  // Create a global object called '<id>Object' which refers to us
  this.obj = id + 'Object';
  eval(id + 'Object=this');

  this.lnkObj = null;
  this.imgObj = null;

  // All buttons are with in a layer (mainly for netscape else it can't
  // reference the link object. The layer will therefore contain one link
  // and one image object
  if (document.all)
  {
    this.divObj = document.all[id + 'But'];
    if (this.divObj)
    {
      this.lnkObj = this.divObj.all[id + 'Lnk'];
      this.imgObj = this.divObj.all[id + 'Img'];
      this.divObj.style.visibility = 'visible';
    }
  }
  else if (document.layers)
  {
    this.divObj = document.layers[id + 'But'];
    if (this.divObj)
    {
      this.lnkObj = this.divObj.document.links[0];
      this.imgObj = this.divObj.document.images[0];
      this.divObj.visibility = 'show';
    }
  }

  if (!this.divObj)
  {
    debug('No DOM object found for button layer "' + id + 'But"');
    return 0;
  }


  if (!this.lnkObj)
  {
    debug('No DOM object found for button link "' + id + 'Lnk"');
    return 0;
  }

  if (!this.imgObj)
  {
    debug('No DOM object found for button image "' + id + 'Img"');
    return 0;
  }

  // Set the event handlers
  this.lnkObj.onClick = new Function('return ' + this.obj + '.click();');
  this.lnkObj.onmouseover = new Function('return ' + this.obj + '.over();');
  this.lnkObj.onmouseout= new Function('return ' + this.obj + '.out();');
  this.lnkObj.onmouseup = new Function('return ' + this.obj + '.up();');
  this.lnkObj.onmousedown = new Function('return ' + this.obj + '.down();');

  // Preload the images
  this.imgUp = new Image();
  this.imgUp.src = imgPath + 'up.gif';
  this.imgDn = new Image();
  this.imgDn.src = imgPath + 'dn.gif';
  this.imgIn = new Image();
  this.imgIn.src = imgPath + 'in.gif';
  this.imgGr = new Image();
  this.imgGr.src = imgPath + 'gr.gif';

  // default handlers for the button
  this.onHide = new Function('return true;');
  this.onShow = new Function('return true;');
  this.onEnable = new Function('return true;');
  this.onDisable = new Function('return true;');
  this.onClick = new Function('return true;');
  this.onMouseOver = new Function('return true;');
  this.onMouseOut = new Function('return true;');
  this.onMouseUp = new Function('return true;');
  this.onMouseDown = new Function('return true;');

  // enable the button
  this.enable();
}

ImageButton.prototype.hide = function()
{
  debug('ImageButton.hide()');

  if (this.divObj.style)
    this.divObj.style.visibility = 'hidden';
  else
    this.divObj.visibility = 'hide';

  return this.onHide();
}

ImageButton.prototype.show = function()
{
  debug('ImageButton.show()');

  if (this.divObj.style)
    this.divObj.style.visibility = 'visible';
  else
    this.divObj.visibility = 'show';

  return this.onShow();
}

ImageButton.prototype.disable = function()
{
  debug('ImageButton.disable()');

  this.imgObj.src = this.imgGr.src;

  this.enabled = false;
  return this.onDisable();
}

ImageButton.prototype.enable = function()
{
  debug('ImageButton.enable()');

  this.imgObj.src = this.imgUp.src;

  this.enabled = true;
  return this.onEnable();
}

ImageButton.prototype.up = function()
{
  debug('ImageButton.up()');

  if (!document.loaded)
    return;

  this.imgObj.src = this.imgIn.src;
  return this.onMouseUp();
}

ImageButton.prototype.down = function()
{
  debug('ImageButton.down()');

  if (!document.loaded)
    return;

  this.imgObj.src = this.imgDn.src;
  return this.onMouseDown();
}

ImageButton.prototype.over = function()
{
  debug('ImageButton.over()');

  if (!document.loaded)
    return;

  this.imgObj.src = this.imgIn.src;
  return this.onMouseOver();
}

ImageButton.prototype.out = function()
{
  debug('ImageButton.out()');

  if (!document.loaded)
    return;

  this.imgObj.src = this.imgUp.src;
  return this.onMouseOut();
}

ImageButton.prototype.click = function()
{
  debug('ImageButton.click()');

  if (!document.loaded)
    return;

  this.imgObj.src = this.imgUp.src;
  this.onClick();
}

