if (window.fontSizer) { fontSizer.init(); }

if (window.collapseSections ) {
  collapseSections.config = {
    'div.attrgrp' : {
      handle  : 'attrgrp',
      headSel : 'h3'
    }
  };
  collapseSections.init();
}

if (window.initSafeResetButtons) {
  initAutoValidation(); 
  initSafeResetButtons();
}

if (window.pagebodyHeight) {
  Event.add(window, 'load', pagebodyHeight.fix);
}


(function(){
if (window.Ajax)
{
  
  var doCloseButton = true;
  var closeButtonText = 'Loka';
  var hideSourceRow = false;

  var ajaxToggler = function (e)
  {
    // open or closed?
    if (/(^| )(open)( |$)/i.test(this.className)) 
    {
      // the row is open
      DOM.removeClass(this, 'open');
      this._mySubRow.style.display = 'none';
    } 
    else 
    {
      // the row is closed
      if (!!this._mySubRow) // has child
      { 
        DOM.addClass(this, 'open');
        this._mySubRow.style.display = '';
      }
      else // we need to request it
      {
        var subRow = document.createElement('tr');
        subRow.className = 'subrow polling';
        if (DOM.hasClass(this, 'alt')) {
          subRow.className += ' alt';
        }
        var td = document.createElement('td');
        td.colSpan = this.cells.length;
        DOM.appendChild(td, subRow);
        DOM.insertAfter(subRow, this);
        this._mySubRow = subRow;
        subRow._myRow = this;

        var ajxOk = function (r) {
          var cell = DOM.get('td', subRow)[0];
          if (cell) 
          {
            cell.innerHTML = r;
            DOM.removeClass(subRow, 'polling');
            DOM.addClass(subRow._myRow, 'open');
            // add a close button if needed
            if (doCloseButton) 
            {
              var _closeBtn = document.createElement('a');
              _closeBtn.href = '#';
              _closeBtn.className = 'close';
              _closeBtn.innerHTML = closeButtonText;
              Event.add(_closeBtn, 'click', ajaxToggler, subRow._myRow);
              DOM.prependChild(_closeBtn, cell);
            }
/*            if (hideSourceRow) 
            {
              subRow._myRow.style.display = 'none';
            }*/
          }
        }
        var ajxFail = function (r) {
          DOM.removeClass(subRow, 'polling');
        }
        var url = document.location.protocol + '//' + document.location.host +
                  '/bitar/personas/EmployeeByID.jsp?pid=' + this.__myEmpId;
        Ajax.request(url, ajxOk, null, ajxFail);
      }
      
    }
    return false;
    
  };


  // alpha test for employee ajax requests
  var tlinks = DOM.get('.deptppl table a');  // deptppl people
  if (tlinks)
  {
    for (var i=0,l=tlinks.length; i<l; i++)
    {
      var row;
      if (/(persona\/)([0-9]+)/i.test(tlinks[i].href)) 
      {
        // find my row
        row = tlinks[i].parentNode;
        while (row && (row.tagName.toLowerCase() !== 'tr')) { row = row.parentNode; }
        if (row) 
        {
          row.__myEmpId = RegExp.$2;
          Event.add(tlinks[i], 'click', ajaxToggler, row);
        }
        
      }
    }
  }
  
} 
})();