

/*
Ajax AutoSuggest
===========
This file contains the functions which will do the autosuggest on client end
@version: 1.0
@author:  Waseem Khan
@blog:    http://blog.pakcoders.com
*/

var ajaxObj = getAjaxObject();
var targetID = new Array() ;
var searchID = new Array() ;
var inputID = new Array() ;
var searchInputHighlight ;
var ulsearchId='';

var sPath = window.location.protocol;
if(sPath=="https:")
{
	SITE_PATH=SITE_PATH_SSL;
}
else
{
	SITE_PATH=SITE_PATH.replace("https","http");
}

function autoSuggest(id, targetid, searchid, inputid, e)
{
   
   var keyCode = getKeyCode(e, 'keyup');
   if (keyCode == 40 || keyCode == 38)
   {
	   return false;   
   }
   
   autoSugPointer[id] = 0;
   
   targetID[id] = targetid;
   searchID[id] = searchid;
   inputID[id] = inputid;
   countSuggestions[id] = 0;
   ulsearchId= searchid;
   
   var searchInput = getElemId(id).value;
   //alert(searchInput);
   searchInput=searchInput.replace("'","");
   searchInput=searchInput.replace("1=","");
   searchInputHighlight=searchInput;
    
   var url = SITE_PATH+"ajax/suggesionAjax.php";
   var params = "input=" + searchInput;
   if (trim(searchInput) !== "")
   {
	  if(searchInput.length >=3)
	  {
		  
		  sendRequest(ajaxObj, url, params, handleSuggestResponse, id);
	  }
	  
	  else
	  {
	  		hideSuggestions(id);
	  }
	  
   }
   else
   {
	  hideSuggestions(id);   
   }
}

function handleSuggestResponse(id)
{
   if (ajaxObj.readyState == 4)
   {
      if (ajaxObj.status == 200)
      {
		  try
		  { 
			  var XMLResponse = ajaxObj.responseText;
			  var suggestions = new Array();
			  if(XMLResponse!='')
			  suggestions=XMLResponse.split("|");
			  	
			  if(suggestions.length > 0)
			  {
					document.getElementById(ulsearchId).style.display='';
					showSuggestions(suggestions, id);
			  }
			  else
			  {
				   document.getElementById(ulsearchId).style.display='none';
				   hideSuggestions(id);
			  }
			  
		  }
		  catch(e)
		  {
			  hideSuggestions(id);
			  if (trim(ajaxObj.responseText) !== "")
			  alert(ajaxObj.responseText);  
		  }
	  }
   }
}


var countSuggestions = new Array();
//var count;

function showSuggestions(suggestions, id)
{
//	alert(searchInputHighlight) ;
  // count=suggestions.length ;
   var listWrapID = getElemId(targetID[id]);
   listWrapID.style.visibility = "visible";
   
   var listID = getElemId(searchID[id]);
   listID.innerHTML = "";
   var high=searchInputHighlight;
   if(suggestions.length >0)
   {
	   for(var i = 0; i < suggestions.length; i++)
	   {
		  if(suggestions[i]!='')
		  {
			 listID.innerHTML += "<li><a id='"+id + "-" +(i+1)+"' href=\"javascript:void(0);\" onclick=\"insertKeyword('"+suggestions[i]+"', '"+id+"');\" onMouseover=\"insertwordOnMouseHover('"+suggestions[i]+"', '"+id+"');changeAutoSuggestKeyNav('"+(i+1)+"', '"+id+"');\" onmouseout=\"revertAutoSuggestKeyNav('"+(i+1)+"', '"+id+"');;\">" +  makeHighlight(searchInputHighlight,suggestions[i])  + "</a></li>";    
		  }
	   }
   }
   
     
   
   countSuggestions[id] = i;
   countSuggestions[id]--;
}

function makeHighlight(str,string)
{
	
	var tempstr="";
	var aviationStr='';
	
	
	
	for(i=0 ; i<string.length;i++)
	{
	   if(i<str.length)
	   {
		 if(str.charAt(i)!=' ')
         tempstr+="<b>"+ string.charAt(i) +"</b>";
		 else
		 tempstr+=string.charAt(i);
	   }
	   else
       {
	   tempstr+=string.charAt(i);
	   }
	}
	for(i=(tempstr.length-8) ; i<tempstr.length;i++)
	{
		if(str.charAt(i)!=' ')
		 aviationStr+="<span class=\'lightBlueText\'>"+ tempstr.charAt(i) +"</span>";
	}
	tempstr=tempstr.replace(" - Aviation","-"+aviationStr);
	
	return tempstr ;
}

var autoSugPointer = new Array();

function keyBoardNav(e, id)
{
   var keyCode = getKeyCode(e, 'keydown');
   if (keyCode == 40)
   {
      if (autoSugPointer[id] >= 0 && autoSugPointer[id] < countSuggestions[id])
	  {
		 if (autoSugPointer[id] != 0 && autoSugPointer[id] != countSuggestions[id])
		 {
		     revertAutoSuggestKeyNav(autoSugPointer[id], id);
		 }
		 autoSugPointer[id] ++;
		
		 changeAutoSuggestKeyNav(autoSugPointer[id], id);
		 if (autoSugPointer[id] > 6)
		 {
			getElemId(searchID[id]).scrollTop = 30;
		 }
	  }
	  	 var 	str2 = getElemId(id + "-" + autoSugPointer[id]).innerHTML;
		             if(str2!=undefined)
                        //document.getElementById(id).value=str2;
					 	  document.getElementById(id).value=  stripHtml(str2);

   }
   else if (keyCode == 38)
   {
	  if (autoSugPointer[id] > 1)
	  {
		 revertAutoSuggestKeyNav(autoSugPointer[id], id);
		 autoSugPointer[id] --;
		 changeAutoSuggestKeyNav(autoSugPointer[id], id);
		 if (autoSugPointer[id] <= 2)
		 {
			getElemId(searchID[id]).scrollTop = 0;
		 }
	  }
	 var 	  str1= getElemId(id + "-" + autoSugPointer[id]).innerHTML;
      if(str1!=undefined){
		
	  document.getElementById(id).value=  stripHtml(str1);

	  }
   }
   else if (keyCode == 13 && autoSugPointer[id]  )
   {
	   var str = getElemId(id + "-" + autoSugPointer[id]).innerHTML;
 	   insertKeyword(stripHtml(str), id);

   }

}

function changeAutoSuggestKeyNav(id, ID)
{
	if(document.getElementById(ID + "-" + id)){
	   getElemId(ID + "-" + id).style.backgroundColor = "#316AC5";
	   getElemId(ID + "-" + id).style.color = "#FFF";  
	}
}

function revertAutoSuggestKeyNav(id, ID)
{
	if(document.getElementById(ID + "-" + id)){

	   getElemId(ID + "-" + id).style.backgroundColor = "#F1F8FE";
	   getElemId(ID + "-" + id).style.color = "#666666";   	
	}
}


function hideSuggestions(id)
{
   try
   {
   var listWrapID = getElemId(targetID[id]);
   listWrapID.style.visibility = "hidden";	
   }catch(e){}
}

function insertKeyword(str, id)
{
	hideSuggestions(id);
	getElemId(inputID[id]).value = str;
	getElemId(inputID[id]).focus();
}

function insertwordOnMouseHover(str, id)
{
	getElemId(inputID[id]).value = str;
}

 function stripHtml(htmlString){
        if(htmlString){
          var mydiv = document.createElement("div");
           mydiv.innerHTML = htmlString;
 
            if (document.all) // IE Stuff
            {
                return mydiv.innerText;
                
            }    
            else // Mozilla does not work with innerText
            {
                return mydiv.textContent;
            }                            
      }
   } 


