var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
var livesearch_submit_url = "/web/liveSearch.do";
var highlight = null;
var vars = ''; // Sono le variabili che passo insieme al comando all'azione che fa' il search
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new AJAXConnect();
}

var idLiveSearch = "";
var idResult = "";
var isInit = false;

function liveSearchInit() {
    if (!isInit) {
      if (navigator.userAgent.indexOf("Safari") > 0) {
        idLiveSearch.addEventListener("keydown",liveSearchKeyPress,false);
      } else if (navigator.product == "Gecko") {
        idLiveSearch.addEventListener("keypress",liveSearchKeyPress,false);
      } else {
        idLiveSearch.attachEvent('onkeydown',liveSearchKeyPress);
        isIE = true;
      }
      isInit = true;
    }
}

function liveSearchKeyPress(event) {
  if (event.keyCode == 40 )
  //KEY DOWN
  {
    highlight = document.getElementById("LSHighlight");
    if (!highlight) {
      highlight = idResult.firstChild.nextSibling.nextSibling.firstChild;//.firstChild;
//      alert('1: ' + highlight.nodeName);
    } else {
      highlight.removeAttribute("id");
      highlight.style.backgroundColor = '#ffffff';
      highlight = highlight.nextSibling;
    }
    if (highlight) {
      highlight.setAttribute("id","LSHighlight");
      highlight.style.backgroundColor = '#C6E03E';
    }
//    alert(highlight.innerHTML + ' --> ' + highlight.getAttribute('id'));
    if (!isIE) { event.preventDefault(); }
  }
  //KEY UP
  else if (event.keyCode == 38 ) {
    highlight = document.getElementById("LSHighlight");
    if (!highlight) {
      highlight = idResult.firstChild.nextSibling.nextSibling.firstChild;
//      highlight = idResult.firstChild.firstChild.nextSibling.nextSibling.lastChild;
    }
    else {
      highlight.removeAttribute("id");
      highlight.style.backgroundColor = '#ffffff';
      highlight = highlight.previousSibling;
    }
    if (highlight) {
      highlight.setAttribute("id","LSHighlight");
      highlight.style.backgroundColor = '#C6E03E';
    }
    if (!isIE) { event.preventDefault(); }
  }
  //ESC
  else if (event.keyCode == 27) {
    highlight = document.getElementById("LSHighlight");
    if (highlight) {
      highlight.removeAttribute("id");
    }
    idResult.style.display = "none";
    idLiveSearch.value = '';
  }
  // RETURN
  else if (event.keyCode == 13) {
    highlight = document.getElementById("LSHighlight");
    if (!highlight) {
      highlight = idResult.firstChild.nextSibling.nextSibling.firstChild;//.firstChild;
//      alert('1: ' + highlight.nodeName);
    }
    if (highlight) {
      liveSearchSubmit();
    }
//    alert(highlight.innerHTML + ' --> ' + highlight.getAttribute('id'));
    if (!isIE) { event.preventDefault(); }

  }
}

/*
 * Chiamata dal box della ricerca; genera il sottomenu'
 * @param oFormField  Riferimento al campo INPUT type="text" della form HTML nel quale scrive l'utente.
 * @param lsResult    Riferimento al campo DIV nel quale far comparire i risultati della ricerca.
 * Ad es. onclick="liveSearchStart(document.myForm.mySearchBoxField, document.getElementById('LSResultDue'));"
 */
function startLiveSearch(url, oHtmlField, lsResult, givenVars) {
        idLiveSearch = oHtmlField;
        idResult = lsResult;
        livesearch_submit_url = url;
        vars = givenVars;
        isInit = false;
        liveSearchInit();
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()",200);
}

function liveSearchProcessReqChange(oXML) {
	var res = idResult;
	res.style.display = "block";
        res.innerHTML = '<div id="searchcontrols" class="oddresult"><div class="alignleft"><small>Usare frecce e invio</small></div><div class="alignright"><small><a href="javascript://" title="Chiudi" onclick="closeResults()">chiudi (esc)</a></small></div><br /></div><div id="searchheader" style="display: none;"></div>'+oXML.responseText;
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		eval(highlight.firstChild.getAttribute('onClick'));//getAttribute("href");
		return false;
	}
	else {
		return true;
	}
}

function closeResults() {
//    var highlight = document.getElementById("LSHighlight");
//    alert(highlight);
//    alert(highlight.innerHTML);
//    if (highlight) {
//      highlight.removeAttribute("id");
//    }
    if (idResult != null) {
      idResult.style.display = "none";
    }
}

/*
*
* Chiamata dal box della ricerca; genera il sottomenu'
* @param oFormField  Riferimento al campo INPUT type="text" della form HTML nel quale scrive l'utente
* Ad es. onclick="liveSearchDoSearch()"
*/
function liveSearchDoSearch() {
  //if (idLiveSearch.value.length < 6) return;

  if (liveSearchLast != idLiveSearch.value) {
    if (idLiveSearch.value == "") {
      idResult.style.display = "none";
      highlight = document.getElementById("LSHighlight");
      if (highlight) {
        highlight.removeAttribute("id");
      }
      return false;
    }
    var ajax = new AJAXConnect();
    ajax.connect(livesearch_submit_url, 'GET', "codice=" + idLiveSearch.value + "&" + vars, liveSearchProcessReqChange);
    liveSearchLast = idLiveSearch.value;
  }
  return true;
}
