
function _gel(id){return document.getElementById(id);}

function XHConn(){
  var xmlhttp;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4) {
        fnDone(xmlhttp); }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}



var buff = null;
var timer = null;
function rpcSearch(str) {
 if(buff == str){ return; }
 buff = str;
 if(str.length < 3){
  _gel("resultat").style.display = "none";
  return;
 }
 if(timer != null){
  clearTimeout(timer);
  timer = null;
 }
 timer = setTimeout("doSearch(\""+str+"\")",500);
}

function doSearch(str){
 var myConn = new XHConn();
 if (!myConn) {
   alert('Fel');
 }
 else {
   var fnWhenDone = function (oXML) { showresults(oXML.responseText); };
   myConn.connect("search.rpc.php", "POST", "name="+str, fnWhenDone);
 }
}



function showresults(resultat) {
	
	res = _gel("resultat");
	
	if(resultat.length < 2){
		_gel("resultat").style.display = "none";
		return;
	}
	res.innerHTML = "";
	_gel("resultat").style.display = "block";
	resultat = resultat.split("|");

	for(x=0;x<resultat.length;x++){
		arr = resultat[x].split(";");
		res.innerHTML += "<div style='border-bottom:1px solid #ccc;padding:2px;cursor:pointer;' onmouseover='this.style.background=\"#ff9\";' onmouseout='this.style.background= \"none\";' onclick='top.location = \"http://www.undertexter.se/?p=undertext&id="+arr[3]+"\";'><div style='float:right'>"+arr[2]+"</div>"+arr[0]+" - "+arr[1]+"</div>";
	}
	
}