var xmlHttp
var response


//triggered by search box's onkey up
function showHint(str)
{

//empties out the auto complete if input is empty
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  
  document.getElementById("txtHint").style.visibility="hidden";
  return;
  }

//creates XmlHttpObject and sends a request to 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
  
  try {
//var url="http://10.50.40.116:9000/a=query&print=all&text=*";
//url=url+"&databasematch=productpages&FieldText=WILD{" + str + "*}:RELATED_PROD_GROUP_NM";
//url=url+"&sid="+Math.random();

//queries IDOL with "autoinput" parameter
var url="/USGSearch/clienthint_idolquery.jsp";
url=url+"?autoinput=" + str;
xmlHttp.onreadystatechange=stateChanged;
//xmlHttp.open("GET",url,true);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
	} 
	catch (e) {
		}
} 


//when server response is 4, fill in auto complete box
function stateChanged() 
{ 
  if (xmlHttp.readyState==4)
  { 
    var response=xmlHttp.responseXML;
    //var response=xmlHttp.responseText;

//uses responseXML to parse and retrieve group name data from results
  var x=response.getElementsByTagName("RELATED_PROD_GROUP_NM");

  var returnText = "";
  var idname = "";
  docValue="";

  if (x.length == 0) {

  document.getElementById("txtHint").style.visibility="hidden";

  }
  else {


  for (var i=0;i<x.length;i++)
    { 
      idname = "id" + i;
      docValue = x[i].childNodes[0].nodeValue;
      docValue = TrimString(docValue);
      //fills in each suggestion
      returnText = returnText+ "<div style=\"border-top-width:0.1cm;border-top-style:solid;border-top-color:#FFFFFF;color:black;text-align:left;width:300px\" id=\"" + idname +  "\"   onmouseover=\"mouseOver(this.id)\" onmouseout=\"mouseOut(this.id)\" onclick=\"itemClick(this.id)\" value=\"" + docValue + "\" >" + docValue + "</div>";
    }
  
  //color:black;text-align:left
  
  document.getElementById("txtHint").innerHTML=returnText;
  //document.getElementById("txtHint").innerHTML="<div style=\"color:black;text-align:left\">" + xmlHttp.responseText + "</div>";
  document.getElementById("txtHint").style.visibility="visible";

  }
  }
}



/////////////////////////////////////////////////////////////

//when auto complete result is clicked, sends command to search_results
function itemClick(id) {

document.getElementById("searchValue").value= document.getElementById(id).innerHTML;
document.getElementById("search").submit();


}

//when mouse hovers over the suggested result, result is highlighted
function mouseOver(id){

 var el = document.getElementById(id); 
 
 try{
    //for firefox
    el.style.setProperty("background-color", "#8080A0");
 } 
 catch (e) {
   try {
   // for IE
   el.style.backgroundColor = "#8080A0"; 
   } 
   catch (e) {
   }
 } 
 

}

//when mouse leaves suggested result, highlighting is turned off
function mouseOut(id){
 var el = document.getElementById(id); 
   try {  
     // for firefox  
     el.style.setProperty("background-color", "#ffffff"); 
   }
   catch (e) {   
     // for IE
     try {
     el.style.backgroundColor = "#ffffff"; 
     }
     catch (e) {
     }
   } 
 
}

// creates XmlHttpObject to send/receive requests to server
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

//trimstring function
function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}
