if (!Bs_Objects)
{
 var Bs_Objects = [];
};

function Bs_TabSet(outerElmId)
{
 var a = arguments;
 this._outerElmId = (a.length>1) ? a[1] :  a[0];
 this._id;
 this._objectId;
 this.tabs = new Array;
 this.defaultTab = 0;
 this._activeTabIdx = 0;
 this._onTabSelectEvent
 this.drawStyle = 'css';
 this.imgPath;
 this.imgBorderSize;
 this._constructor =
  function()
  {
   this._id = Bs_Objects.length;
   Bs_Objects[this._id] = this;
   this._objectId = "Bs_TabSet_" + this._id;
  }
 this.addTab =
  function(caption, container, onTabSelect)
  {
   if (typeof(caption) == 'object')
   {
    var o = caption;
   }
   else
   {
    var o = new Object;
    o.caption   = caption;
    if ((typeof(container) != 'undefined') && (container != null) && (container)) 
    {
     if (typeof(container) == 'string') 
     {
      o.container = document.getElementById(container);
      o.id        = container;
     }
     else
     {
      o.container = container;
      o.id        = container.id
     }
    }
    if (typeof(onTabSelect) != 'undefined') 
    {
     o.onTabSelect = onTabSelect;
    }
   }
   o.tabIdx = this.tabs.length;
   this.tabs[o.tabIdx] = o;
  }
 this.render = 
  function() 
  {
   var ret = new Array;
   ret[ret.length] = '<div class="tabsetTabsDiv">';
   if (this.drawStyle == 'img') 
   {
    var contentDiv = document.getElementById(this._outerElmId + '_content');
    if (contentDiv)
    {
     var preHtml = '';
     preHtml += '<table border="0" cellspacing="0" cellpadding="0">';
     preHtml += '<tr>';
     preHtml += '<td width="' + this.imgBorderSize + '" background="' + this.imgPath + 'line_left.gif"><img src="' + this.imgPath + 'spacer.gif" width="' + this.imgBorderSize + '"></td>';
     preHtml += '<td>';
     var postHtml = '';
     postHtml += '</td>';
     postHtml += '<td width="' + this.imgBorderSize + '"  background="' + this.imgPath + 'line_right.gif"><img src="' + this.imgPath + 'spacer.gif" width="' + this.imgBorderSize + '"></td>';
     postHtml += '</tr>';
     postHtml += '<tr>';
     postHtml += '<td height="' + this.imgBorderSize + '" width="' + this.imgBorderSize + '"><img src="' + this.imgPath + 'line_cornerBottomLeft.gif"></td>';
     postHtml += '<td height="' + this.imgBorderSize + '" background="' + this.imgPath + 'line_bottom.gif"><img src="' + this.imgPath + 'spacer.gif" height="' + this.imgBorderSize + '"></td>';
     postHtml += '<td height="' + this.imgBorderSize + '" width="' + this.imgBorderSize + '"><img src="' + this.imgPath + 'line_cornerBottomRight.gif"></td>';
     postHtml += '</tr>';
     postHtml += '</table>';
     contentDiv.innerHTML = preHtml + contentDiv.innerHTML + postHtml;
     for (var i=0; i<this.tabs.length; i++) 
     {
      this.tabs[i].container = document.getElementById(this.tabs[i].id);
     }
    }
    ret[ret.length] = '<table border="0" cellspacing="0" cellpadding="0" id="' + this._objectId + '_outerTable"><tr>';
    ret[ret.length] = '<td valign="bottom"><img src="' + this.imgPath + 'line_cornerTopLeft.gif"></td>';
    ret[ret.length] = '<td valign="bottom"><img src="' + this.imgPath + 'line_bottom.gif" width="5" height="' + this.imgBorderSize + '"></td>';
    for (var i=0; i<this.tabs.length; i++) 
    {
     var actOrPasString = (i == this.defaultTab) ? 'open' : 'closed';
     if (i == this.defaultTab) 
     {
      var cls = 'bsTabsetActive';
     }
     else
     {
      var cls = 'bsTabsetInactive';
      if (this.tabs[i].container) this.tabs[i].container.style.display = 'none';
     }
     ret[ret.length] = '<td>';
     ret[ret.length] = '<table border="0" cellspacing="0" cellpadding="0">';
     ret[ret.length] = '<tr>';
     if (i == 0) ret[ret.length] = '<td width="' + this.imgBorderSize + '" rowspan="100%"><img src="' + this.imgPath + 'left_' + actOrPasString + '.gif"></td>';
     ret[ret.length] = '<td height="' + this.imgBorderSize + '" background="' + this.imgPath + 'top.gif"><img src="' + this.imgPath + 'spacer.gif" width="1" height="' + this.imgBorderSize + '"></td>';
     if (i == (this.tabs.length -1))
     {
      ret[ret.length] = '<td width="' + this.imgBorderSize + '" rowspan="100%"><img src="' + this.imgPath + 'right_' + actOrPasString + '.gif"></td>';
     }
     else
     {
      ret[ret.length] = '<td width="' + this.imgBorderSize + '" rowspan="100%"><img src="' + this.imgPath + 'middle.gif"></td>';
     }
     ret[ret.length] = '</tr>';
     ret[ret.length] = '<tr>';
     ret[ret.length] = '<td id="' + this._objectId + '_tdBg_' + i + '" background="' + this.imgPath + 'bg_' + actOrPasString + '.gif">';
     ret[ret.length] = '<div unselectable="On" id="' + this._objectId + '_tabCap_' + i + '" class="bsTabset ' + cls + '" style="display:inline;" onclick="Bs_Objects['+this._id+'].switchTo(' + i + ');">';
     ret[ret.length] = '<nobr>' + this.tabs[i].caption + '</nobr>';
     ret[ret.length] = '</div>';
     ret[ret.length] = '</td>';
     ret[ret.length] = '</tr>';
     ret[ret.length] = '<tr>';
     ret[ret.length] = '<td id="' + this._objectId + '_tdLineBottom_' + i + '" height="' + this.imgBorderSize + '" background="' + this.imgPath + 'bottom_' + actOrPasString + '.gif"><img src="' + this.imgPath + 'spacer.gif" width="1" height="' + this.imgBorderSize + '"></td>';
     ret[ret.length] = '</tr>';
     ret[ret.length] = '</table>';
     ret[ret.length] = '</td>';
    }
    ret[ret.length] = '<td id="' + this._objectId + '_tdLineBottom" valign="bottom" width="10"><img src="' + this.imgPath + 'line_bottom.gif" height="' + this.imgBorderSize + '" width="100%"></td>';
    ret[ret.length] = '<td valign="bottom"><img src="' + this.imgPath + 'line_cornerTopRight.gif" height="' + this.imgBorderSize + '" width="' + this.imgBorderSize + '"></td>';
    ret[ret.length] = '</tr></table>';
   }
   else
   {
    ret[ret.length] = '<div style="width:2px; min-width:2px; display:inline;"></div>';
    for (var i=0; i<this.tabs.length; i++)
    {
     if (i == this.defaultTab)
     {
      var cls = 'bsTabsetActive';
     }
     else
     {
      var cls = 'bsTabsetInactive';
      if (this.tabs[i].container) this.tabs[i].container.style.display = 'none';
     }
     ret[ret.length] = '<div unselectable="On" id="' + this._objectId + '_tabCap_' + i + '" class="bsTabset ' + cls + '" style="display:inline;" onclick="Bs_Objects['+this._id+'].switchTo(' + i + ');">' + this.tabs[i].caption + '</div>';
    }
   }
   ret[ret.length] = '</div>';
   return ret.join('');
  }
 this.draw = 
  function()
  {
   var elem = document.getElementById(this._outerElmId + '_tabs');
   if (elem) elem.innerHTML = this.render();
   if (this.drawStyle == 'img')
   {
    var tdLine     = document.getElementById(this._objectId + '_tdLineBottom');
    var outerTable = document.getElementById(this._objectId + '_outerTable');
    var tds = outerTable.firstChild.firstChild.childNodes;
    var allTdWidth = 0;
    for (var i=0; i<tds.length; i++)
    {
     if (tds[i] == tdLine) continue;
     allTdWidth += tds[i].offsetWidth;
    }
    var outerDiv  = document.getElementById(this._outerElmId);
    var lineWidth = outerDiv.offsetWidth - allTdWidth;
    tdLine.width  = lineWidth;
   }
  }
 this.switchTo =
  function(theReg)
  {
   newRegIdx = -1;
   if (theReg == '') theReg = '0';
   if (isNaN(parseInt(theReg)))
   {
    for (var i=0; i<this.tabs.length; i++)
    {
     if (this.tabs[i].caption == theReg) (newRegIdx = i);
    }
   }
   else
   {
    newRegIdx = theReg;
   }
   if (newRegIdx < 0) return;
   for (var i=0; i<this.tabs.length; i++)
   {
    var elem = document.getElementById(this._objectId + '_tabCap_' + i);
    if (!elem) continue;if (newRegIdx == i) 
    {
     this._activeTabIdx = i;
     elem.className = 'bsTabset bsTabsetActive';
     if (typeof(this.tabs[i].container) != 'undefined') 
     {
      this.tabs[i].container.style.display = 'block';
     }
     if (this.drawStyle == 'img') 
     {
      var td = document.getElementById(this._objectId + '_tdLineBottom_' + i);
      td.background = this.imgPath + 'bottom_open.gif';
      var td = document.getElementById(this._objectId + '_tdBg_' + i);
      td.background = this.imgPath + 'bg_open.gif';
     }
     if (typeof(this.tabs[i].onFocus) != 'undefined') 
     {
      this._triggerFunction(this.tabs[i].onFocus);
     }
     this.fireOnTabSelect();
    }
    else
    {
     elem.className = 'bsTabset bsTabsetInactive';
     if (typeof(this.tabs[i].container) != 'undefined') 
     {
      this.tabs[i].container.style.display = 'none';
     }
     if (this.drawStyle == 'img') 
     {
      var td = document.getElementById(this._objectId + '_tdLineBottom_' + i);
      td.background = this.imgPath + 'bottom_closed.gif';
      var td = document.getElementById(this._objectId + '_tdBg_' + i);
      td.background = this.imgPath + 'bg_closed.gif';
     }
     if (typeof(this.tabs[i].onBlur) != 'undefined') 
     {
      this._triggerFunction(this.tabs[i].onBlur);
     }
    }
   }
  }
 this.getActiveTab = 
  function() 
  {
   return this.tabs[this._activeTabIdx];
  }
 this.onTabSelect = 
  function(yourEvent) 
  {
   this._onTabSelectEvent = yourEvent;
  }
 this.fireOnTabSelect = 
  function() 
  {
   if (this._onTabSelectEvent) 
   {
    func = this._onTabSelectEvent;
    if (typeof(func) == 'string') 
    {
     eval(func);
    }
    else
    {
     func(this);
    }
   }
   var tab = this.getActiveTab();
   if (tab.onTabSelect) 
   {
    func = tab.onTabSelect;
    if (typeof(func) == 'string') 
    {
     eval(func);
    }
    else
    {
     func(this);
    }
   }
   return true;
  }
 this._triggerFunction =
   function(func) 
   {
    if (typeof(func) == 'function') 
    {
     func();
    }
    else if (typeof(func) == 'string') 
    {
     eval(func);
    }
   }
 this._constructor();
}

