var currentCell = null;

function addListeners( table ) {
  var cells = getRefByTagName( 'td', getRefById( table ));

  for( var i = 0; i < cells.length; ++i ) {
    if( cells[ i ].addEventListener ) {
      cells[ i ].addEventListener( 'click', cellClick, false );
      cells[ i ].addEventListener( 'mouseout', cellOut, false );
      cells[ i ].addEventListener( 'mouseover', cellOver, false );
    } else {
      cells[ i ].onclick = cellClick;
      cells[ i ].onmouseout = cellOut;
      cells[ i ].onmouseover = cellOver;
    }
  }
}

function cellClick() {
  if( currentCell ) {
    changeImg( currentCell, 'images/buttons/firm_active.gif' );
  }
  changeIMG( currentCell = this, 'images/buttons/firm_active.gif' );
}

function cellOut() {
  if( this != currentCell )
    changeBackground( this, '#ffffff' );    
}

function cellOver() {
  if( this != currentCell )
    changeBackground( this, '#3a6ea5' );
}

function changeBackground( element, colour ) {
  if( element.style
    && 'undefined' != typeof element.style.backgroundColor ) {
    element.style.backgroundColor = colour;
  }
}
