var DOM = (typeof(document.getElementById) != 'undefined');
function CheckTR(Element) {  // from www.yandex.ru
	if (DOM) {
		thisCheckbox = document.getElementById(Element.id.replace('rl','cb'));
		if (thisCheckbox) {
			thisCheckbox.checked = !thisCheckbox.checked;
		}
	}
}



function fdb_highlightrow(row, color)
{
  if (typeof(row.style) != 'undefined') 
  {
    row.oldcolor = row.style.backgroundColor;    
    row.style.backgroundColor = color;
  }
}

function fdb_resetrow(row)
{  
  row.style.backgroundColor = row.oldcolor;
}

function fdb_selectrow(row, rlId, rownum)
{
  table = document.getElementById(rlId);  
  if (table.listener && table.listener.setRow(rownum, row.oldcolor))
  {
    row.oldcolor = row.style.backgroundColor;    
  }
}

function fdb_do(rlId, rownum, action, confirmtext)
{
  extra="";
  if (confirmtext)
  {
    confirmed = confirm(confirmtext);
    if (confirmed) extra = "&confirm=1";
  }
  if (fdb_a[rlId][rownum][action] && (!confirmtext || confirmed))
  {
    if (!fdb_a[rlId]['embed'])
    {
      document.location.href = fdb_a[rlId][rownum][action]+'&'+fdb_a[rlId]['base']+extra;
    }
    else
    {
      fdbSubmit(fdb_a[rlId][rownum][action]+'&'+fdb_a[rlId]['base']+extra);
    }
  }
}

function fdb_next(rlId)
{
  if (fdb_a[rlId]['next'])
  {
    document.location.href = fdb_a[rlId]['next'];
  }
  return false
}

function fdb_previous(rlId)
{
  if (fdb_a[rlId]['previous'])
  {
    document.location.href = fdb_a[rlId]['previous'];
    return true;
  }
  return false;
}

fdb_a = new Array();



KEY_DOWN  = 40;
KEY_RIGHT = 39;
KEY_UP    = 38;
KEY_LEFT  = 37;
KEY_DEL   = 46;
KEY_E     = 69;
KEY_V     = 86;

// Directions
DIR_DOWN = 1;
DIR_UP   = 2;
DIR_NONE = 3;

// Detect if we are dealing with netscape/mozilla.
var ver = navigator.appVersion; 
var len = ver.length;
for (var iln = 0; iln < len; iln++) 
{
  if (ver.charAt(iln) == "(") break;
}

var isNetscape = (ver.charAt(iln+1).toUpperCase() != "C");

var keyListeners = new Array();
if (!document.getElementById('<%=getFocusElement%>') || !document.getElementById('<%=getFocusElement%>').focus())
{
  var focussedListener = -1;
}

/**
 * The generic keylistener base class.
 */
function fdbGKeyListener()
{  
}

fdbGKeyListener.prototype.handleKey = function(key, ctrl, shift)
{
  alert("you pressed "+key+" ctrl: "+ctrl+" shift: "+shift);
}

fdbGKeyListener.prototype.focus = function(direction)
{
  // Default listener does not know how to receive focus.
}

fdbGKeyListener.prototype.blur = function()
{
  // Default listener does not know how to lose focus.
}

function fdbFEKeyListener(elementId, onUp, onDown, onLeft, onRight, onCtrl)
{
  this.elementId = elementId;
  this.onUp = onUp;
  this.onDown = onDown;
  this.onLeft = onLeft;
  this.onRight = onRight;
  this.onCtrl = onCtrl;
  this.id = -1;
  
  // Hook ourselves an onfocus event to keep track of focus, when using the mouse.
  var el = document.getElementById(this.elementId);
  el.listener = this; // Give the element a pointer to the listener, so we can always access it. 
  el.onfocus = function() 
  { 
    if (this.listener.id != focussedListener)
    {
      kb_focus(this.listener.id, DIR_DOWN);
    }
    focussedListener = this.listener.id; 
  }
}

fdbFEKeyListener.prototype = new fdbGKeyListener();
fdbFEKeyListener.superclass = fdbGKeyListener.prototype;

fdbFEKeyListener.prototype.handleKey = function(key, ctrl, shift)
{  
  if (!ctrl)
  {   
    if(key==KEY_DOWN && this.onDown) kb_focusNext();
    if(key==KEY_UP && this.onUp) kb_focusPrevious();
    if(key==KEY_LEFT && this.onLeft) kb_focusPrevious();
    if(key==KEY_RIGHT && this.onRight) kb_focusNext();
  }
  else
  {  
    if ((key==KEY_DOWN||key==KEY_RIGHT) && this.onCtrl) kb_focusNext();
    if ((key==KEY_UP  ||key==KEY_LEFT)  && this.onCtrl) kb_focusPrevious();
  }
}

fdbFEKeyListener.prototype.blur = function()
{
  var el = document.getElementById(this.elementId);
  if (el) el.blur();
}

fdbFEKeyListener.prototype.focus = function(direction)
{
  var el = document.getElementById(this.elementId);
  if (el)
  {
    el.focus();
  }
  else
  {
    alert('listener not attached to an element!');
  }
}

/**
 The keyboard handler
 **/
function kb_addListener(listener)
{
  listener.id = keyListeners.length; // Let the listener know it's position in the array.
  keyListeners[keyListeners.length] = listener;  
}

function kb_init()
{
  document.onkeydown = kb_handleKey;
}

function kb_handleKey(e)
{ // handles a keypress
  var k = 0;
  var ctrl = false;
  var shift = false;  
  
  if (focussedListener<0)
  {
    kb_focusFirst();
  }
  
  if (focussedListener>=0)    // check if it's valid after the focusFirst check.
  {
    if (isNetscape)
    {
      k = e.which;
      var mod = parseInt(e.modifiers)
      ctrl = (mod & 2)==2;
      shift = (mod & 4)==4;  
    }
    else
    {
      k = window.event.keyCode;
      ctrl = window.event.ctrlKey;
      shift = window.event.shiftKey;
    }
    
    keyListeners[focussedListener].handleKey(k, ctrl, shift);  
  }
}

function kb_focusFirst()
{
  kb_defocus();
  if (keyListeners.length>0) focussedListener = 0;
  keyListeners[focussedListener].focus(DIR_DOWN);
}

function kb_focusLast()
{
  kb_defocus();
  if (keyListeners.length>0) focussedListener = keyListeners.length-1;
  keyListeners[focussedListener].focus(DIR_UP);
}

function kb_focusPrevious()
{
  if (focussedListener>0) 
  {
    kb_defocus(); // blur the previous one (we must do this, because we cannot
                  // assume that it is done automatically by the browser, 
                  // since we focus things that the browser cannot focus.
    focussedListener--;
    keyListeners[focussedListener].focus(DIR_UP);
  }
  else kb_focusLast();  
}

function kb_focusNext()
{
  if (focussedListener<keyListeners.length-1) 
  {
    kb_defocus();
    focussedListener++;
    keyListeners[focussedListener].focus(DIR_DOWN);
  }
  else kb_focusFirst();  
}

function kb_focus(num, direction)
{  
  if (num<keyListeners.length)
  {
    focussedListener=num;
    keyListeners[focussedListener].focus(direction);
  }  
}

function kb_defocus()
{
  if (focussedListener>-1 && focussedListener<keyListeners.length)
  {
    keyListeners[focussedListener].blur(); // blur the currently focussed element.    
  }
}

var focussedRecordlist = null;

/**
 * The keylistener class for recordlists.
 */
function fdbRLKeyListener(rlId, highlight, reccount)
{
  this.recordlistId = rlId;  
  this.currentrec = -1;
  this.prevcolor = '';
  this.highlight = highlight;
  this.reccount = reccount;
  
  var el = document.getElementById(this.recordlistId);
  el.listener = this; // Give the element a pointer to the listener, so we can always access it. 
}

fdbRLKeyListener.prototype = new fdbGKeyListener();
fdbRLKeyListener.superclass = fdbGKeyListener.prototype;

fdbRLKeyListener.prototype.handleKey = function(key, ctrl, shift)
{  
  if (key==KEY_DOWN) this.down();
  else if (key==KEY_UP) this.up();
  else if (key==KEY_RIGHT) this.next();
  else if (key==KEY_LEFT) this.previous();
  else if (key==KEY_DEL) this.do_action('delete');
  else if (key==KEY_E) this.do_action('edit');
  else if (key==KEY_V) this.do_action('view');
  else
  {
    // Other key, which we ignore.
    // alert('key '+key+' pressed...');
  }
}

fdbRLKeyListener.prototype.do_action = function(action)
{
  if (this.currentrec>-1)
  {
    fdb_do(this.recordlistId, this.currentrec, action,false);
  }
}

fdbRLKeyListener.prototype.next = function()
{
  return fdb_next(this.recordlistId);  
}

fdbRLKeyListener.prototype.previous = function()
{
  return fdb_previous(this.recordlistId);
}

fdbRLKeyListener.prototype.focus = function(direction)
{    
  focussedRecordlist = this.recordlistId;
  if (direction==DIR_UP)   // We come from below.
  {  
    this.last();
  }
  else if (direction==DIR_DOWN) // We come from above
  {
    this.first(); // move to first record.
  }
  else // Clicked in the middle
  {    
  }
}

fdbRLKeyListener.prototype.blur = function()
{
  this.deselectRow();
  focussedRecordlist = null;  
}

fdbRLKeyListener.prototype.first = function()
{
  // We can implement a jump to the first record by setting the pointer to -1 and 
  // moving one down.
  this.currentrec=-1;
  this.down();
}

fdbRLKeyListener.prototype.setRow = function(rownum, oldcolor)
{
  if (this.currentrec!=rownum) // check if not already selected
  {
    kb_focus(this.id, DIR_NONE);
  
    this.deselectRow();
    this.currentrec=rownum;
    this.selectRow();
    this.prevcolor=oldcolor;
    return true;
  }
  return false;
}

fdbRLKeyListener.prototype.deselectRow = function()
{
  if (this.currentrec>=0)
  {
    curRow = document.getElementById(this.recordlistId+'_'+this.currentrec);
    curRow.style.backgroundColor = this.prevcolor;  
  }
}

fdbRLKeyListener.prototype.selectRow = function()
{
  if (this.currentrec>=0)
  {
    newRow = document.getElementById(this.recordlistId+'_'+this.currentrec);
    this.prevcolor = newRow.style.backgroundColor;
    newRow.style.backgroundColor = this.highlight;
  }
}

fdbRLKeyListener.prototype.last = function()
{
  this.currentrec=-1; // put the pointer to nothing.
  this.up();
}

fdbRLKeyListener.prototype.down = function()
{
  if (this.currentrec>=0) // a record was already selected  
  {
    this.deselectRow();
    this.currentrec++;
  }
  else  
  {
    this.currentrec=0;
  }
  
  if (this.currentrec>=this.reccount) // pointer has moved beyond last record
  {
    this.currentrec = -1; // reset pointer to nothing.
    if (!this.next())
    {
      // There's no next page
      kb_focusNext(); // pass onto next element.
    }
  }
  else
  {
    //alert('naam: '+this.recordlistId+'_'+this.currentrec);
    this.selectRow();
  }
}

fdbRLKeyListener.prototype.up = function()
{
  if (this.currentrec>=0) // a record was already selected  
  {
    this.deselectRow();
    this.currentrec--;
  }  
  else
  {
    this.currentrec=this.reccount-1; 
  }
  
  if (this.currentrec<0) // pointer moved before first record
  {
    this.currentrec = -1; // reset pointer to nothing.
    if (!this.previous()) // there is no previous page
    {
      kb_focusPrevious(); // pass onto previous element.
    }
  }
  else
  {
    this.selectRow();
  }  
}
