var numMatches=0;
var matchIndex=0;
var backgroundColor='#66CCFF';
var itemColor='#6699FF';
var matchArr = new Array();
var actObj=false;
var actArr=false;
var menuObj=false;
var KeyID=0;

/*
function selectItem()
{
    actObj.value=matchArr[matchIndex];
    actObj.focus();
    menuObj.style.display='none';
    document.getElementById('theVar').value=namesAndEmailsArr[matchIndex][1];
}*/

backgroundColor='#FFFFFF';

function selectItem(f)
{
    actObj.value=matchArr[matchIndex];
    //if (!f) actObj.focus();
    menuObj.style.display='none';
    document.getElementById('mit_fag').value=namesAndEmailsArr[matchIndex][0];
    document.getElementById('mit_fag_url').value="http://"+namesAndEmailsArr[matchIndex][1];
    //alert ( 'selecting item: '+ matchIndex + " "  +  namesAndEmailsArr[matchIndex][2] );
    document.getElementById('mit_fag_newwin').value=namesAndEmailsArr[matchIndex][2];
}

function populateMenu(obj,arr,menuName)
{
    actObj=obj;
    actArr=arr;
    menuObj=document.getElementById(menuName);
    
    menuContentString="";
    numMatches=0;
    for(key in arr)
    {
        matchArr[key]=arr[key][0];
        menuContentString+='<div class="item" id="'+key+'" onmouseover="backColor('+key+');" >'+arr[key][0]+'</div>';
        numMatches++;
    }
    matchIndex=0;
    /* menuObj.onmousedown=selectItem; */
    menuObj.innerHTML=menuContentString;
}

function autoFill(obj,arr,menuName,ev)
{
  actObj=obj;
  actArr=arr;
  menuObj=document.getElementById(menuName);
  KeyID = ev.keyCode;

  switch(KeyID)
  {
      case 27:
      { // ESC
        if(empty(actObj.value)){
          menuObj.style.display='none';
        }
      } break;
    
      case 8:
      { // backspace
        if( empty( actObj.value ) ){
          populateMenu(actObj,actArr,menuObj.id);
          backColor(0);
        }
      } break;
    
      case 13:
      { // enter
        selectItem();
        return false;
      } break;
    
      case 9:
      { // tab
        selectItem();
      } break;
    
      case 38:
      { // up
        backColor(matchIndex-1);
      }break;
    
      case 40:
      { // down
        if(empty(actObj.value) && menuObj.style.display=='none')
        {
          menuObj.style.display='block';
          backColor(0);
        }
        else
        {
          backColor(parseInt(matchIndex)+1);
        }
      }break;
    
  }

  if(empty(obj.value)){
    menuObj.style.display="none";
    return;
  }

  if(KeyID==38 || KeyID==40 || KeyID==13) return;

  var value=ltrim(obj.value.toLowerCase());
/*  value=strReplace( value , 'ø' , '&oslash;' );
  value=strReplace( value , 'å' , '&aring;'  );
  value=strReplace( value , 'æ' , '&aelig;'  );*/

  menuObj.innerHTML="";
  var regexp = new RegExp( "^"+value );

  matchArr = new Array();
  var tmpStr="";

  for(key in arr){
    tmpStr=arr[key][0].toLowerCase();
    if (tmpStr.match(regexp)) {
      matchArr[key]=arr[key][0];
    }
  }

  numMatches=0;

  firstKey = false;
  if(matchArr.length){
    for(key in matchArr){
      menuObj.innerHTML+='<div class="item" id="'+key+'" onmouseover="backColor('+key+');" >'+matchArr[key]+'</div>';
      numMatches++;
      if(typeof(firstKey)=="boolean")
        firstKey=key;
    }
/*    menuObj.onmousedown=selectItem; */
    menuObj.style.display="block";
    matchIndex=firstKey;
    backColor(firstKey);
    return;
  }
  menuObj.style.display="none";

    return false;
}

function cleanMenu()
{
    if (omo)
    {
        selectItem();
        omo=false;
    }
    menuObj.innerHTML="";
    menuObj.style.display="none";
    matchIndex=0;
    numMatches=0;
}

function backColor(id){
  if( isObject( document.getElementById( id ) ) ){
    for(key in matchArr){
      document.getElementById(key).style.backgroundColor=backgroundColor;
    }
//    if(id>=numMatches) id=0;
//    else if(id<0) id=numMatches-1;
    matchIndex=id;
    document.getElementById(id).style.backgroundColor=itemColor;
  }
}

/*function start(){
  namesArr = namesArr.sort();
  emailsArr = emailsArr.sort();
}*/

function debug(value,line,file){
  document.getElementById('debug').style.display='block';
  document.getElementById('debug').innerHTML=line+"&nbsp;"+file+"<br><b>"+value+"</b><hr>"+document.getElementById('debug').innerHTML;
}

//start();
