function Form (el)
{
  this.el = el;
  this.ctrls = Array ();

  var ds = this.el.getElementsByTagName ("*");
  for (var dl = ds.length; --dl >= 0; )
  {
    var dsl = ds[dl];
    var dep = dsl.getAttribute ('dep');
   
    if (dep != null)
    {
      var dep_el = getElement (dep);
      
      if (dep_el.tagName == 'INPUT')
      {
        if (typeof (this.ctrls[dep]) == 'undefined')
          this.ctrls[dep] = new CheckBox (dep_el);
          
        this.ctrls[dep].addItem (dsl);
      } else
      {
        if (typeof (this.ctrls[dep]) == 'undefined')
          this.ctrls[dep] = new ComboBox (dep_el);
          
        this.ctrls[dep].addPane (dsl, dsl.getAttribute ('index'));
      }
    }
  }
  
  this.el.style.visibility = 'visible';
}

Form.prototype.dispose = function ()
{
  for (var $key in this.ctrls)
  {
    this.ctrls[$key].dispose ();
    this.ctrls[$key] = null;
  }
  this.ctrls = null;
  this.el = null;
}


function CheckBox (el)
{
  this.el = el;
  this.items = Array ();
  
  var oThis = this;
  
  if (el.type == 'radio')
  {
    var rs = document.getElementsByName (el.name);
    for (var rl = rs.length; --rl >= 0; )
      rs[rl].onclick = function (e) { oThis.setCheckState (oThis.el.checked); };
  } else this.el.onclick = function (e) { oThis.setCheckState (this.checked); };
}

CheckBox.prototype.addItem = function (item_el)
{
  this.items[this.items.length] = item_el;
  
  this.EnableItem (item_el, this.el.checked)
}

CheckBox.prototype.EnableItem = function (item_el, enabled)
{
  item_el.disabled = !enabled;
  
  var is = item_el.getElementsByTagName ('*');
  for (var il = is.length; --il >= 0; )
  {
    var isl = is[il];
    
    if (isl.tagName == 'IFRAME')
    {
      if (enabled)
        isl.style.display = "block"; else isl.style.display = "none";
    } else is[il].disabled = !enabled;
  }
}

CheckBox.prototype.setCheckState = function (checked)
{
  for (var $key in this.items)
    this.EnableItem (this.items[$key], checked)
}

CheckBox.prototype.dispose = function ()
{
  for (var $key in this.items)
    this.items[$key] = null;

  this.el.onclick = null;
  this.items = null;
  this.el = null;
}


function ComboBox (el)
{
  this.el = el;
  this.panes = Array ();
  this.activePane = 0;
  
  var oThis = this;
  this.el.onchange = function (e) { oThis.setActivePane (this.value); };
}

ComboBox.prototype.addPane = function (pane_el, index)
{
  this.panes[index] = pane_el;
  
  if (this.el.value == index)
    this.setActivePane (index); else pane_el.style.display = "none";
}

ComboBox.prototype.setActivePane = function (pane)
{
  if (this.activePane != pane)
  {
    if (this.panes[this.activePane] != null)
      this.panes[this.activePane].style.display = "none";
      
    this.activePane = pane;

    if (this.panes[this.activePane] != null)
      this.panes[this.activePane].style.display = "block";
  }
}

ComboBox.prototype.dispose = function ()
{
  for (var $key in this.panes)
    this.panes[$key] = null;

  this.el.onchange = null;
  this.panes = null;
  this.el = null;
}


/* TabPane */

function TabPane (el)
{
  this.el = el;
  this.pages = Array ();
  
  this.tabRow = document.createElement ("div");
  this.tabRow.className = "ctrlTabPaneRow";
  el.insertBefore( this.tabRow, el.firstChild );

  this.activePage = Number (getCookie ("tabpane_" + this.el.id));
  if (isNaN (this.activePage))
    this.activePage = 0;

  var cs = el.childNodes;
  for (var i = 0; i < cs.length; i++)
  {
    var csi = cs[i];
    
    if (csi.nodeType == 1 && csi.className == "ctrlTabPanePage" && csi.tabPane != this)
      this.addTabPage (csi);
  }
  
  if (this.pages.length - 1 < this.activePage)
    this.setActivePage (0);
}

TabPane.prototype.addTabPage = function (page_el)
{
  var pl = this.pages.length;
  var pi;
    
  pi = this.pages[pl] = new TabPanePage (page_el, this, pl);
  
  this.tabRow.appendChild (pi.tab);
      
  if (pl == this.activePage)
    pi.show(); else pi.hide();
}
  
TabPane.prototype.setActivePage = function (page)
{
  if (this.activePage != page)
  {
    if (this.activePage != null && this.pages[this.activePage] != null)
      this.pages[this.activePage].hide ();
      
    this.activePage = page;
    this.pages[this.activePage].show ();
    
    setCookie ("tabpane_" + this.el.id, page);
  }
}

TabPane.prototype.dispose = function ()
{
  this.el = null;		
  this.tabRow = null;
	
  for (var pl = this.pages.length; --pl >= 0; )
  {
    this.pages[pl].dispose ();
    this.pages[pl] = null;
  }
  this.pages = null;
}


function TabPanePage (el, tabPane, nIndex)
{
  this.el = el;
  this.tabPane = tabPane;
  this.index = nIndex;
  
  var cs = el.childNodes;
  for (var cl = cs.length; --cl >= 0; )
  {
    var csl = cs[cl];

    if (csl.nodeType == 1 && csl.className == "tab")
    {
      this.tab = csl;
      break;
    }
  }
  
  var lnk = document.createElement ("a");
  this.link = lnk;
  lnk.href = "#";
  lnk.onclick = function (e) { return false; };
  
  while (this.tab.hasChildNodes ())
    lnk.appendChild (this.tab.firstChild);
  this.tab.appendChild (lnk);

  var oThis = this;
  this.tab.onclick = function (e) { oThis.select (); };
  this.tab.onmouseover = function (e) { TabPanePage.tabOver (oThis); };
  this.tab.onmouseout = function (e) { TabPanePage.tabOut (oThis); };
}

TabPanePage.prototype.show = function ()
{
  var s = this.tab.className + " selected";
  s = s.replace(/ +/g, " ");
  this.tab.className = s;
  
  this.el.style.display = "block";
}

TabPanePage.prototype.hide = function ()
{
  var s = this.tab.className;
  s = s.replace(/ selected/g, "");
  this.tab.className = s;

  this.el.style.display = "none";
}
  
TabPanePage.prototype.select = function ()
{
  this.tabPane.setActivePage (this.index);
}

TabPanePage.prototype.dispose = function ()
{
	this.link.onclick = null;
	this.link = null;
	this.tab.onclick = null;
	this.tab.onmouseover = null;
	this.tab.onmouseout = null;
	this.tab = null;
	this.tabPane = null;
	this.el = null;
}

TabPanePage.tabOver = function (tabpage)
{
  var el = tabpage.tab;
  var s = el.className + " hover";
  s = s.replace(/ +/g, " ");
  el.className = s;
}

TabPanePage.tabOut = function (tabpage)
{
  var el = tabpage.tab;
  var s = el.className;
  s = s.replace(/ hover/g, "");
  el.className = s;
}


/* Table */

function Table (el)
{
  this.el = el;
  this.capt = null;
  this.head = null;
  this.main = null;
  this.foot = null;

  var ds = this.el.getElementsByTagName ("div");
  for (var dl = ds.length; --dl >= 0; )
  {
    var dsl = ds[dl];
    
    if (dsl.className == 'ctrlTableCaption')
    {
      this.capt = dsl;
    }
    if (dsl.className == 'ctrlTableFooter')
    {
      this.foot = dsl;
    }
    if (dsl.className == 'ctrlTableMain')
    {
      this.main = dsl;

      var ts = dsl.getElementsByTagName ("table");
      for (var tl = ts.length; --tl >= 0; )
      {
        var tsl = ts[tl];
        
        if (tsl.className == 'ctrlTableHeader')
        {
          this.head = tsl;
        } else
        {
          var rs = tsl.getElementsByTagName ("TR");
          for (var rl = rs.length; --rl >= 0; )
          {
            var rsl = rs[rl];
    
            rsl.onmouseover = function (e) { this.style.backgroundColor = '#aaeeaa'/*'#ffeec2'*/; };
            rsl.onmouseout = function (e) { this.style.backgroundColor = '#f6f6f6'/*'transparent'*/; };
          }
        }
      }
      this.main.head = this.head;
      
      dsl.onscroll = function (e) { this.head.style.top = this.scrollTop; };
    }
  }
  var fs = this.el.getElementsByTagName ("form");
  for (var fl = fs.length; --fl >= 0; )
  {
    var fsl = fs[fl];

    var is = fsl.parentNode.getElementsByTagName ("input");
    for (var il = is.length; --il >= 0; )
    {
      var isl = is[il];

      isl.frm = fsl;
      isl.onkeypress = function (e)
      {
        if (!e) var e = window.event;

        if (e.keyCode == 13) this.frm.submit ();
      }
    }
  }

  this.onresize ();
}

Table.prototype.onresize = function (e)
{
  var captionHeight = (this.capt == null ? 0 : this.capt.offsetHeight);
 
  this.el.style.paddingTop = captionHeight;
  this.el.style.paddingBottom = (this.foot == null ? 0 : this.foot.offsetHeight);
  this.el.style.width = '100%';

  if (this.capt != null) this.capt.style.top = -captionHeight;
  if (this.foot != null) this.foot.style.top = -captionHeight;

  this.main.style.top = -captionHeight;
  this.main.style.paddingTop = (this.head == null ? 0 : this.head.offsetHeight);
}
