/*
     $Revision$
     $Id$
*/
// Add to <select>

// onKeyPress="return false;"
// onKeyUp="autoComplete(this);"
// onFocus="this.s_str='';"
var cur_i = -1;
var last_k = 0;

function autoComplete(evt, s) {
  FcltMgr.stopPropagation(evt);
  s.old_str = s.s_str;

  // Step 1. Handle key

  var k = event.keyCode;

  // 27 - Escape
  // 46 - Delete

  if( k==27 || k==46 ) {
    s.s_str='';
    cur_i = -1;
    return;
  } else

  //  8 - BackSpace... Doesn't work - it's browser's back action...

  if( k==8 ) {
    var t = s.s_str.length;
    if(t>0) {
      s.s_str = s.s_str.substring(0,t-1);
    }
    return;
  } else

  // 32 - WhiteSpace
  // 65-90 - alpha
  // 48-57 - numeric

  var c = "";
  if( k>=32&&k<=90 ) {
    c = String.fromCharCode(k);
    if( k >= 48 && k <= 57 ) {
// this code may be dependent on keyboard or locale!
      if ( event.shiftLeft ) {
        switch (k) {
         case 48:
           c = ')';
           break;
         case 49:
           c = '!';
           break;
         case 50:
           c = '@';
           break;
         case 51:
           c = '#';
           break;
         case 52:
           c = '$';
           break;
         case 53:
           c = '%';
           break;
         case 54:
           c = '^';
           break;
         case 55:
           c = '&';
           break;
         case 56:
           c = '*';
           break;
         case 57:
           c = '(';
           break;
       }
     }
   }
// end of code potentailly dependent on keyboard or locale!
 }
// this code may be dependent on keyboard or locale!
 if ( c == "" ) {
    switch ( k ) {
    case 186 :
      if ( event.shiftLeft ) c = ":";
      else c = ";";
      break;
    case 187 :
      if ( event.shiftLeft ) c = "+";
      else c = "=";
      break;
    case 188 :
      if ( event.shiftLeft ) c = "<";
      else c = ",";
      break;
    case 189 :
      if ( event.shiftLeft ) c = "_";
      else c = "-";
      break;
    case 190 :
      if ( event.shiftLeft ) c = ">";
      else c = ".";
      break;
    case 191 :
      if ( event.shiftLeft ) c = "?";
      else c = "/";
      break;
    case 219 :
      if ( event.shiftLeft ) c = "{";
      else c = "[";
      break;
    case 220 :
      if ( event.shiftLeft ) c = "|";
      else c = "\\";
      break;
    case 221 :
      if ( event.shiftLeft ) c = "}";
      else c = "]";
      break;
    case 222 :
      if ( event.shiftLeft ) c = '"';
      else c = "'";
    }
  }
  if ( c == "" ) {
// end of code potentailly dependent on keyboard or locale!
    return;
  }
  s.s_str = s.s_str + c;

  // Step 2. Search...

  var l=s.length,i,ss,i0 = s.selectedIndex;
  if( s.length <= 0 ) return;

 while( s.s_str != '' ) {

  // Check if the current item fits...

  if( i0 >= 0 ) {
    ss = s.options[i0].text.toUpperCase();
    ss = ss.substr(0,s.s_str.length)
    if( ss == s.s_str ) return;
  }

  // Search list...

  for( i=(i0+1)%l; i!=i0; i=(i+1)%l ) {
    if (i0 == -1) i0 = 0; // Voorkom oneindige loop als selectedIndex==-1
    ss = s.options[i].text.toUpperCase();
    ss = ss.substr(0,s.s_str.length)
    if( ss == s.s_str ) {
      s.selectedIndex = i;
      if( s.onchange ) s.onchange();
      cur_i = i;
      return;
    }
  }

  s.s_str = c; c=''; //s.old_str;
 }
}

// Put autocomplete on all selects on the page
// NOTE!!! function overrides handlers of OnFocus & OnKeyUp events in all
// selects
function autocompleteAllSelects()
{
  selects = document.getElementsByTagName("SELECT");

  if (selects.length != null)
  {
    for (i=0; i<selects.length; i++)
    {
      selects[i].s_str = "";
      selects[i].onfocus = autocompleteOnFocus;
      selects[i].onkeyup = autocompleteOnKeyUp;
      selects[i].onkeypress = autocompleteOnKeyPress;
    }
  }
}

function autocompleteOnFocus()
{
  cur_i = -1;
  last_k = 0;
  event.srcElement.s_str = "";
}

function autocompleteOnKeyUp()
{
  autoComplete(event.srcElement);
}

function autocompleteOnKeyPress()
{
  return false;
}

function autocompleteOnKeyDown()
{
  last_k = event.keyCode;
}

function autocompleteOnBlur()
{
    if ( last_k == 9 ) {
      s = event.srcElement;
      s.selectedIndex = cur_i;
      cur_i = -1;
      last_k = 0;
      if( s.onchange ) s.onchange();
    }

}


if (this.appendLoadSequence)
	appendLoadSequence(autocompleteAllSelects);
