function GetPureBase(base) {
  uspos = base.indexOf("_");
  if (uspos == -1) return base;
  return base.substring(0,uspos);
}

function PrepareTabControl(base,tabsStr,ownerTab) {

  purebase = GetPureBase(base);
  TabNamesArray[purebase] = tabsStr;
  OwnerTabsArray[base] = ownerTab;

}

function SwitchTab(base,tabName,focus)
{

  purebase = GetPureBase(base);

  if (focus) {

    ownerTab = OwnerTabsArray[base];
    if (ownerTab && ownerTab != ",") {
      cpos = ownerTab.indexOf(",");
      if (cpos > -1) {
        bs = ownerTab.substring(0,cpos);
        tn = ownerTab.substring(cpos+1);
        SwitchTab(bs,tn,true);
      }
    }

  }

  tabsStr = TabNamesArray[purebase];

  tabs = tabsStr.split(",");

  for (i = 0; i < tabs.length; i++) {
    tab = tabs[i];
    if (tab != "") {
      div = document.getElementById(base + "_" + tab);
      if (tab == tabName)
        div.style.display = "block"; // inline does not work well for FireFox
      else
        div.style.display = "none";
    }
  }

  selSrc = TabButtonImagesArray[purebase + tabName + "sel"].src;
  for (i = 0; i < tabs.length; i++) {
    tab = tabs[i];
    if (tab != "") {
      button = document.getElementById(base + "_Button_" + tab);
      if (button) {
        if (tab == tabName)
          button.src = selSrc;
        else if (button.lowsrc != '')
          button.src = button.lowsrc;
      }
    }
  }
}

function SetupTabEvents(base,tabName,imgSrc,moSrc,mdSrc,selSrc,isel,selName)
{

  purebase = GetPureBase(base);

  s = purebase + tabName;
  if (!TabButtonImagesArray[s + "mo"]) {

    TabButtonImagesArray[s + "mo"] = new Image();
    TabButtonImagesArray[s + "mo"].src = moSrc;

    TabButtonImagesArray[s + "md"] = new Image();
    TabButtonImagesArray[s + "md"].src = mdSrc;

    TabButtonImagesArray[s + "sel"] = new Image();
    TabButtonImagesArray[s + "sel"].src = selSrc;

  }

  imgId = base + "_Button_" + tabName;

  var img = document.getElementById(imgId);
  img.lowsrc = imgSrc;

  if (moSrc != '')
    img.onmouseover = function() {if (this.src.indexOf(selName) == -1) this.src = moSrc;}

  if (mdSrc != '')
    img.onmousedown =  function() {if (this.src.indexOf(selName) == -1) this.src = mdSrc;}

  if (isel)
    img.src = selSrc;

  if (moSrc != '')
    img.onmouseup  = function() {if (this.src.indexOf(selName) == -1) this.src = moSrc;}
  else
    img.onmouseup = function() {if (this.src.indexOf(selName) == -1) this.src = imgSrc;}

  img.onmouseout = function() {if (this.src.indexOf(selName) == -1) this.src = imgSrc;}
}

