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 (this.id != 'td10') {
	  if( currentCell ) {
	    changeBackground( currentCell, '#EEF6E4' );
	  }
	  changeBackground( currentCell = this, '#D5E0CE' );
	}
}

function cellOut() {
	if (this.id != 'td10') {
	  if( this != currentCell )
	    changeBackground( this, '#EEF6E4' ); 
	}   
}

function cellOver() {
	//alert(document.getElementById('td2').id);
	if (this.id != 'td10') {
	  if( this != currentCell )
	    changeBackground( this, '#D5E0CE' );
	}
}

function changeBackground( element, colour ) {
  if( element.style
    && 'undefined' != typeof element.style.backgroundColor ) {
    element.style.backgroundColor = colour;
  }
}
function getRefByTagName( tag, scope ) {
  scope = scope || document;

  if( scope.getElementsByTagName ) {
    return scope.getElementsByTagName( tag );
  } else if( scope.all && scope.all.tags ) {
    return scope.all.tags( tag );
  }
  return null;
}

function getRefById( id ) {
  if( document.getElementById ) {
    return document.getElementById( id );
  } else if( document.all ) {
    return document.all[ id ];
  }
  return null;
}
