
    /*
     * Javascript Toolbar code - for my use only - (c) 2006 Ron Phillips, aka Xyloid
     */

    function CreateToolbar(id, left, top, caption) {
      // Create a new div section with the given id.
      var div;
      if ( document.getElementById(id) ) div = document.getElementById(id);
      else div = document.createElement('div');
      div.id = id;
      div.tb = this;
      div.style.backgroundColor = '#777777';
      div.style.position = 'absolute';
      div.style.top = top + 'px';
      div.style.left = left + 'px';

      // Attach the div to the web page.
      document.body.appendChild(div);

      // Create the required elements and add them to the div section.
      // -- A Main table element.
      var t1 = document.createElement('table');
      t1.border = '1px';
      t1.cellSpacing = '0px';
      t1.cellPadding = '0px';

      // DOM tables require the tbody element also.
      var t1body = document.createElement('tbody');

      // -- 1 row for the title bar.
      var titlebar = document.createElement('tr');
      titlebar.style.backgroundColor = '#40609c';
      titlebar.style.color = '#ffffff';

      var titlerow = document.createElement('td');
      titlerow.setAttribute('width', '100%');
      titlerow.style.paddingLeft = '2px';
      titlerow.style.paddingRight = '2px';
      titlerow.style.borderStyle = 'ridge';

      // The titlebar has a table itself.
      var ttable = document.createElement('table');
      ttable.setAttribute('width', '100%');
      ttable.cellSpacing = '0px';
      ttable.cellPadding = '0px';
      var ttbody = document.createElement('tbody');
      var ttr = document.createElement('tr');
      var ttd1 = document.createElement('td');
      var ttd2 = document.createElement('td');

      // Title caption.
      var title = document.createElement('div');
      title.innerHTML = caption;
      title.style.fontFamily = 'verdana';
      title.style.fontSize = '8pt';
      title.style.color = '#ffffff';
      ttd1.style.cursor = 'default';
      ttd2.setAttribute('align', 'right');
      title.div = div;

      // Min/Max 'button'
      var minmaxdiv = document.createElement('div');
      var minmaxa = document.createElement('a');
      minmaxa.href = '';
      minmaxa.title = 'Minimize';
      minmaxa.setAttribute('tbid', id);
      minmaxa.setAttribute('onclick', 'blur(); TBMinMax(this); return false;');
      minmaxa.min = '-';//'&or;';
      minmaxa.max = '+';//'&and;';
      minmaxa.innerHTML = minmaxa.min;

      // -- 1 row for the body of the toolbar.
      var bodybar = document.createElement('tr');
      var bodyrow = document.createElement('td');
      bodyrow.style.width = '100%';
      bodyrow.style.border = '0';
      bodyrow.style.padding = '1px';

      // Add a div section to the body - this is where the user will add things.
      var bodydiv = document.createElement('div');

      // Put together the different sections for our toolbar.
      div.appendChild(t1);
        t1.appendChild(t1body);
          t1body.appendChild(titlebar);
            titlebar.appendChild(titlerow);
              titlerow.appendChild(ttable);
                ttable.appendChild(ttbody);
                  ttbody.appendChild(ttr);
                    ttr.appendChild(ttd1);
                      ttd1.appendChild(title);
                    ttr.appendChild(ttd2);
                      ttd2.appendChild(minmaxdiv);
                        minmaxdiv.appendChild(minmaxa);
          t1body.appendChild(bodybar);
            bodybar.appendChild(bodyrow);
              bodyrow.appendChild(bodydiv);

      // Set the functions for the titlebar.
      this.caption = function TBSetTitle(title) { if ( title ) this.titletext.innerHTML = title; return this.titletext.innerHTML; };
      this.titletext = title;
      this.innerHTML = function TBSetInnerHTML(text) { if ( text ) this.bodydiv.innerHTML = text; return this.bodydiv.innerHTML; };
      this.minimized = false;
      this.minimize = function TBMinimize(a) { this.bodydiv.style.display = 'none'; this.minimized = true; this.minmax.innerHTML = this.minmax.max; this.minmax.title = 'Restore'; };
      this.maximize = function TBMaximize(a) { this.bodydiv.style.display = ''; this.minimized = false; this.minmax.innerHTML = this.minmax.min; this.minmax.title = 'Minimize'; };
      this.SetMinButton = function TBMinButton(a) { this.minmax.min = a; if ( !this.minimized ) this.minmax.innerHTML = this.minmax.min; }
      this.SetMaxButton = function TBMaxButton(a) { this.minmax.max = a; if ( this.minimized ) this.minmax.innerHTML = this.minmax.max; }
      this.minmax = minmaxa;
      this.t1body = t1body;
      this.bodybar = bodybar;
      this.bodydiv = bodydiv;
      this.title = ttd1;
      this.div = div;

      this.attachElement = function TBAttachElement(id) { this.bodydiv.appendChild(document.getElementById(id)); };

      // Hook events for the titlebar (cursor/drag/etc)
      ttd1.onmousedown = TBMouseDown;
      ttd1.ondblclick = function TBDblClick(e) { var div = TBGetToolbar(e); if ( div.tb.minimized ) div.tb.maximize(); else div.tb.minimize(); div.tb.caption(div.tb.caption()); };
      ttd1.div = div;
      ttd2.onmouseover = function TBMinMaxEnter() { document.body.style.cursor = 'pointer'; };
      ttd2.onmouseout = function TBMinMaxExit()  { document.body.style.cursor = ''; };
    }

    function TBMinMax(e) {
      blur();
      var tb = e.attributes.getNamedItem('tbid').value;
      tb = document.getElementById(tb).tb;
      if ( tb.minimized ) tb.maximize();
      else tb.minimize();
    }

    /*
     * Kills an event's propagation and default action
     */
    function CancelEvent(eventObject) {
      if (eventObject && eventObject.stopPropagation) eventObject.stopPropagation();
      if (window.event && window.event.cancelBubble ) window.event.cancelBubble = true;
      if (eventObject && eventObject.preventDefault) eventObject.preventDefault();
      if (window.event) window.event.returnValue = false;
    }

    function TBGetX(e) {
      if (e.pageX) return e.pageX;
      return e.clientX;
    }

    function TBGetY(e) {
      if (e.pageY) return e.pageY;
      return e.clientY;
    }

    function TBGetToolbar(e) {
      if ( !e ) e = window.event;
      // Get the toolbar.
      var title;
      if (e.target) title = e.target
      else if (e.srcElement) title = e.srcElement
      if (title.nodeType == 3) title = title.parentNode; // defeat Safari bug
      return title.div;
    }

    function TBMax(a, b) {
      if ( a > b ) return a;
      return b;
    }

    function TBMouseMove(e, snap) {
      if ( !e ) e = window.event;
      // Get the toolbar from the document object.
      var toolbar = document.TBToolbar;
      // If we're not moving, exit.
      if ( !toolbar.moving ) return;
      var dx = TBGetX(e) - toolbar.oldX;
      var dy = TBGetY(e) - toolbar.oldY;
      if ( snap ) {
        toolbar.style.left = (TBMax(parseInt(toolbar.style.left) + dx, 0)) + "px";
        toolbar.style.top = (TBMax(parseInt(toolbar.style.top) + dy, 0)) + "px";
      } else {
        toolbar.style.left = (parseInt(toolbar.style.left) + dx) + "px";
        toolbar.style.top = (parseInt(toolbar.style.top) + dy) + "px";
      }
      toolbar.oldX = TBGetX(e);
      toolbar.oldY = TBGetY(e);
    }

    function TBMouseDown(e) {
      if ( !e ) e = window.event;
      var toolbar = TBGetToolbar(e);
      document.TBToolbar = toolbar;
      toolbar.moving = true;
      toolbar.oldX = TBGetX(e);
      toolbar.oldY = TBGetY(e);
      toolbar.tb.title.style.cursor='move';
      document.onmousemove = TBMouseMove;
      document.onmouseup = TBMouseUp;
    }

    function TBMouseUp(e) {
      if ( !e ) e = window.event;
      CancelEvent(e);
      // Snap this back into the window if needed.
      TBMouseMove(e, true);
      // And remove the drag operation.
      var toolbar = document.TBToolbar;
      document.onmousemove=null;
      document.onmouseup=null;
      toolbar.moving = null;
      toolbar.tb.title.style.cursor='default';
    }


