var MARGINS = 10;
var TABBEDPAGE = true;

function getTop(obj)
{
  var t = 0;
  var p = obj.offsetParent;
  while (p != null)
  {
    t += p.offsetTop;
    p = p.offsetParent;
  }
  t += obj.offsetTop;
  return t;
}
function getLeft(obj)
{
  var l = 0;
  var p = obj.offsetParent;
  while (p != null)
  {
    l += p.offsetLeft;
    p = p.offsetParent;
  }
  l += obj.offsetLeft;
  return l;
}

function Tab_expand()
{
  var header = document.getElementById('TABHEADER');
  var selector = document.getElementById('SELECTORS');
  var footer = document.getElementById('TABFOOTER');
  var width = Math.max(this.parent.clientWidth - (MARGINS * 2), 0);
  this.div.style.width = width + 'px';
  var left = getLeft(this.parent);
  left += MARGINS;
  this.div.style.left = left + 'px';
  var top = getTop(this.parent);
  if (header != null) 
    top += header.offsetHeight;
  else
    top += MARGINS;
  
  top += selector.offsetHeight - 1;
  this.div.style.top = top + 'px';
  var height = this.parent.clientHeight;
  height -= selector.offsetHeight;
  if (header != null) 
    height -= header.offsetHeight;
  else
    height -= (MARGINS);
  height -= MARGINS;
  if (footer != null) height -= (footer.offsetHeight - 2);
  height = Math.max(height, 0);
  this.div.style.height = height + 'px';
}

function Tab_showChildObjects(tagname)
{
  var children = this.div.getElementsByTagName(tagname);
  for (var i = 0; i < children.length; i++)
    children[i].style.visibility = 'visible';
}

function Tab_show()
{
  this.showChildObjects('SELECT');
  this.showChildObjects('IFRAME');
  this.div.className = 'SELECTEDTAB';
  this.link.className = this.link.className.replace('INACTIVELINK', 'ACTIVELINK');
  var paddiv = this.div.firstChild;
  if (paddiv != null)
  {
      var fc = paddiv.getElementsByTagName('INPUT');
      if ((fc.length > 0) && (fc.enabled))
      {
            fc[0].focus();
      }
  }
}

function Tab_hideChildObjects(tagname)
{
  var children = this.div.getElementsByTagName(tagname);
  for (var i = 0; i < children.length; i++)
  {
    children[i].style.visibility = 'hidden';
  }
}

function Tab_hide()
{
  this.hideChildObjects('SELECT');
  this.hideChildObjects('IFRAME');
  this.div.className = 'HIDDENTAB';
  if (this.link.className.indexOf('INACTIVELINK') == -1)
     this.link.className = this.link.className.replace('ACTIVELINK', 'INACTIVELINK');
}

function Tab(parent, link, div)
{
  this.parent = parent;
  this.link = link;
  this.div = div;
  this.expand = Tab_expand;
  this.expand();
  this.showChildObjects = Tab_showChildObjects;
  this.show = Tab_show;
  this.hideChildObjects = Tab_hideChildObjects;
  this.hide = Tab_hide;
  this.hide();
  return this;
}

function TabSet_initializeTabs(cnt)
{
  this.placeSelector();
  this.expandFooter();
  for (var i = 0; i < cnt; i++)
  {
     this.tabs.add(new Tab(this.parent, document.getElementById('LINKx' + i), document.getElementById('TABx' + i)));
  }
}

function TabSet_placeSelector()
{
  var s = document.getElementById('SELECTORS');
  var width = this.parent.clientWidth;
  width -= (MARGINS * 2);
  width = Math.max(width, 0);
  s.style.width = width + 'px';
  var left = getLeft(this.parent);
  left += MARGINS;
  s.style.left = left + 'px';
  var h = document.getElementById('TABHEADER');
  var top = getTop(this.parent);
  if (h != null) 
   top += h.offsetHeight;
  else
   top += MARGINS;
  s.style.top = top + 'px';
}

function TabSet_expandHeader()
{
  var h = document.getElementById('TABHEADER');
  if (h == null) return;
  var width = this.parent.clientWidth;
  width -= (MARGINS * 2);
  width = Math.max(width, 0);
  h.style.width = width + 'px';
  var left = getLeft(this.parent);
  left += MARGINS;
  h.style.left = left + 'px';
  var top = getTop(this.parent);
  h.style.top = top + 'px';
}

function TabSet_expandFooter()
{
  var f = document.getElementById('TABFOOTER');
  if (f == null) return;
  var width = this.parent.clientWidth;
  width -= (MARGINS * 2);
  width = Math.max(width, 0);
  f.style.width = width + 'px';
  var left = getLeft(this.parent);
  left += MARGINS;
  f.style.left = left + 'px';
  var top = getTop(this.parent);
  top += this.parent.clientHeight;
  top -= f.offsetHeight;
  top -= MARGINS;
  top += 3;
  f.style.top = top + 'px';
}

function TabSet_resize()
{
  this.expandHeader();
  this.placeSelector();
  this.expandFooter();
  for (var i = 0; i < this.tabs.size(); i++)
    this.tabs.elementAt(i).expand();
}

function TabSet_selectTab(ind)
{
  if (this.selectedTab != -1) this.tabs.elementAt(this.selectedTab).hide();
  this.selectedTab = ind;
  this.tabs.elementAt(this.selectedTab).show();
  return false;
}

function TabSet_focusParentTab(e)
{
  var p = e.offsetParent;
  while (p != null)
  {
     if (p.id.indexOf('TABx') != -1) break;
     p = p.offsetParent;
  }
  if (p != null)
  {
    var ind = parseInt(p.id.replace('TABx', ''));
    this.selectTab(ind);
  }
}

function TabSet(cnt)
{
  this.parent = document.getElementById('MAIN');
  if (this.parent == null) this.parent = document.body;
  this.tabs = new Vector();
  this.initializeTabs = TabSet_initializeTabs;
  this.resize = TabSet_resize;
  this.selectedTab = -1;
  this.selectTab = TabSet_selectTab;
  this.expandHeader = TabSet_expandHeader;
  this.placeSelector = TabSet_placeSelector;
  this.expandFooter = TabSet_expandFooter;
  this.focusParentTab = TabSet_focusParentTab;
  this.initializeTabs(cnt);
  if (this.tabs.size() > 0) this.selectTab(0);
  return this;
}

function focusParentTab(e)
{
  tabset.focusParentTab(e);
}

function refreshTabs()
{
 // Nothing to do!
}