
var m_TypeAheadList_position = 0;
var m_TypeAheadList_length = 0;
var m_TypeAheadKeyArray = "";
var m_SearchTextField = ""

function checkTypeAhead(item,id,mode,event,itemClassName)
{
   m_SearchTextField = item.id;
   var Res = noenter();
   if(!Res)
   {
      hideTypeAheadMenu();
      return;
   }
   
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode == 38||keyCode == 40){
		selectTypeAheadMenuItem(keyCode,m_SearchTextField,0)
		return;
	}  
	
	
	m_TypeAheadList_position = 0;
	
	var _itemClassName = 'QuickSearch_TypeAheadMenu';
	var _args = checkTypeAhead.arguments;
	for(var i=0;i<_args.length;i++){
	   if(i==4 && typeof(checkTypeAhead.arguments[i])=='string'){
	      _itemClassName = checkTypeAhead.arguments[i];
	   }
	}
	
	var menu = document.getElementById(id);
	if(item.value.length >0 && mode == 0){
	
		menu.className = _itemClassName;
		menu.innerHTML = getQuickSearchTypeAhead(item.value);
	}else{
		menu.className = _itemClassName + ' Hide';
	}
	if(menu.innerHTML==''){ 
	   menu.style.display='none'; 
	}else{
	   menu.style.display='';
	}
}
function getQuickSearchTypeAhead(str)
{
	var _arrList = "";
	_arrList = getQuickSearchArray();
	
	
	
	//_arrList.sort();
	
	var _txt = "";
	var _tmp1, _tmp2;
	var _nFound = 1;
	
	
	try{
	
	   for(var i = 0; i < _arrList.length; i++){
		   _tmp1 = str.toLowerCase();
		   _tmp2 = _arrList[i].toLowerCase();
   		
		   if(_tmp2.indexOf(_tmp1) != -1 && _tmp2.indexOf(_tmp1) == 0 && _tmp2.charAt(0) == _tmp1.charAt(0)){ 
			   _txt = _txt + '<div id="tahm_'+ _nFound +'" class="TypeAhead_ElementNotSelected" onMouseOver="selectTypeAheadMenuItem(this,\'' + m_SearchTextField +'\',1);">' + _arrList[i] + '</div>'; 
			   _nFound = _nFound + 1;			
		   }
		   if(_nFound > 11){
			   _txt = _txt + '<div class="TypeAhead_MoreResults"></div>'; 
			   break
		   }		
	   }
	 
	   _txt = _txt + '<div class="TypeAhead_FooterResults"></div>'; 
	   
	   m_TypeAheadList_length = _nFound;
	   if(_nFound==1) { _txt = ""; }
	   return _txt;
	
	}catch(e){
	   return "";
	}
	
	
}
function hideTypeAheadMenu()
{
   document.getElementById('TypeAheadMenu').style.display='none';
}
function selectTypeAheadMenuItem(key,textfield,mode)
{
   if(mode==0){
	   if(m_TypeAheadList_length < 2){ m_TypeAheadList_position = 0; return; }
	   if(m_TypeAheadList_position > m_TypeAheadList_length - 1) { m_TypeAheadList_position = 1; }
	   if(m_TypeAheadList_position > 0){ 
		   if(document.getElementById("tahm_" + m_TypeAheadList_position)!=null){ 
			   document.getElementById("tahm_" + m_TypeAheadList_position).className='TypeAhead_ElementNotSelected'; 
		   }
	   }
	   if(key == 38){
		   if(m_TypeAheadList_position == 0||m_TypeAheadList_position == 1){ 
			   m_TypeAheadList_position = m_TypeAheadList_length - 1;
		   }else{
			   m_TypeAheadList_position = m_TypeAheadList_position - 1;
		   }
	   }else if(key == 40){
		   if(m_TypeAheadList_position == m_TypeAheadList_length - 1){ 
			   m_TypeAheadList_position = 1;
		   }else{
			   m_TypeAheadList_position = m_TypeAheadList_position + 1;
		   }
	   }
	   if(document.getElementById("tahm_" + m_TypeAheadList_position)!=null){ 
	      document.getElementById("tahm_" + m_TypeAheadList_position).className='TypeAhead_ElementSelected';
	      var _textField = document.getElementById(textfield);
	      _textField.value = document.getElementById("tahm_" + m_TypeAheadList_position).innerHTML;
	      _textField.focus();
	      //textfield.select();
	   }
	}else{
      var _m_TypeAheadList_position = key.id.substring(5, key.id.length);
      if(m_TypeAheadList_position > 0){ 
         if(document.getElementById("tahm_" + m_TypeAheadList_position)!=null){ 
	         document.getElementById("tahm_" + m_TypeAheadList_position).className='TypeAhead_ElementNotSelected'; 
         }
      }
      m_TypeAheadList_position = _m_TypeAheadList_position;
      
      var input = document.getElementById(textfield);
      key.style.cursor='pointer';
      key.className='TypeAhead_ElementSelected';
      input.value = key.innerHTML;
      input.focus();
      //input.select();
	  }
}
function getQuickSearchArray()
{
	return m_TypeAheadKeyArray;
}