/*  
  Developed by Zeal Neo Technologies
  info@zealneotech.com 
  http://www.zealneotech.com
  You can use this script until you don't remove this comment from the script
*/ 
  
var xmlhttp;
var tEle;
var lstResultId;
var locEleId;
var inputFldEleId;

function showResult(str, oEvent, id, locElementId, locElementNameId)
{
  if(!id)
    alert('Target element Id not provided');
 
  tEle = document.getElementById(id);
  if(!tEle)
    alert('Target element not found');

  if(!locElementId)
    alert('Location element Id not provided');
   
  locEleId = locElementId;

  if(!locElementNameId)
    alert('Location element input field Id not provided');
  
  inputFldEleId = locElementNameId;
    
  var KeyID = oEvent.keyCode;
	var selObject = document.getElementById(lstResultId);
	if(KeyID==40 || KeyID==38) // Down arrow(40) / Up arrow(38)
	{
		if(selObject)
      selObject.focus();
	}
	else
	{	
		if (str.length==0)
		{
			//document.getElementById("livesearch").innerHTML="";
			//document.getElementById("livesearch").style.border="0px";
			tEle.innerHTML="";
			tEle.style.border="0px";
      return;
		}
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null)
		{
			alert ("Your browser does not support XML HTTP Request");
			return;
		}
		var url="livesearchresults.php";
		url=url+"?q="+str;
		url=url+"&sid="+Math.random();
		xmlhttp.onreadystatechange=stateChangedLiveSearch ;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function stateChangedLiveSearch()
{
	if (xmlhttp.readyState==4)
	{
		//alert(xmlhttp.responseText);
		//document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
		//document.getElementById("livesearch").style.border="1px solid #A5ACB2";
		tEle.innerHTML=xmlhttp.responseText;
		tEle.style.border="1px solid #A5ACB2"; //create border only if some response id there
	}
	else
	{
    tEle.innerHTML = '<img src="img/loading.gif" title="Processing..." alt="Processing...">' ;
  }
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


// Function will be called from list filled through ajax.
// To modify the value in textbox and transfer the control
function resultsIndexChanged(oEvent, id)
{
	lstResultId = id;
  var selObject = document.getElementById(lstResultId);   
	if(selObject)
	{
    var selectedIndex = selObject.selectedIndex;
    if( selectedIndex >= 0 )
    {
    	document.getElementById(locEleId).value = selObject.options[selectedIndex].value;
  	  //document.getElementById('hdsearchlocname').value = selObject.options[selectedIndex].value;
      document.getElementById(inputFldEleId).value = selObject.options[selectedIndex].text;
  	  document.getElementById(inputFldEleId).focus();
    }
  }
}

