// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+

var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
	
var isIE = false;

// +----------------------------------------------------------------------+
// | Init ajax object
// +----------------------------------------------------------------------+
function initliveSearchReq()
{
  if(window.XMLHttpRequest)
  {
    // Firefox et autres
  	liveSearchReq = new XMLHttpRequest();
  } 
  else if(window.ActiveXObject)
  {
    // Internet Explorer 
  	try
    {
  		liveSearchReq = new ActiveXObject("Msxml2.XMLHTTP");
  	}
    catch (e)
    {
      liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
  	}
  } 
}
// +----------------------------------------------------------------------+
// | Init - Called on page load - Adds event listeners
// +----------------------------------------------------------------------+
function liveSearchInit()
{
	if (navigator.userAgent.indexOf("Safari") > 0)
	{
		document.getElementById('livesearch1').addEventListener("keydown",liveSearchKeyPress1,false);
		document.getElementById('livesearch2').addEventListener("keydown",liveSearchKeyPress2,false);
	}
  else if (navigator.product == "Gecko")
  {
		document.getElementById('livesearch1').addEventListener("keypress",liveSearchKeyPress1,false);
		document.getElementById('livesearch2').addEventListener("keypress",liveSearchKeyPress2,false);
	}
  else
  {
    // ===> IE !
		document.getElementById('livesearch1').attachEvent('onkeydown',liveSearchKeyPress1);
		document.getElementById('livesearch2').attachEvent('onkeydown',liveSearchKeyPress2);
		isIE = true;
	}
	
	// Set autocomplete to off !
	document.getElementById('livesearch1').setAttribute("autocomplete","off");
	document.getElementById('livesearch2').setAttribute("autocomplete","off");
}
// +----------------------------------------------------------------------+
// | Hides but with a delay...
// +----------------------------------------------------------------------+
function liveSearchHideDelayed(which)
{
	window.setTimeout("liveSearchHide("+which+")",400);
}
// +----------------------------------------------------------------------+
// | Hide search. Called by liveSearchDoSearch()
// +----------------------------------------------------------------------+
function liveSearchHide(which)
{
	document.getElementById("LSResult"+which).style.display = "none";
	var highlight = document.getElementById("LSHighlight"+which);
	if (highlight)
  {
		highlight.removeAttribute("id");
	}
}
// +----------------------------------------------------------------------+
// | Called first. Only takes arrows and escape keys into account
// +----------------------------------------------------------------------+
function liveSearchKeyPress1(event)
{
	if (event.keyCode == 40)
	//KEY DOWN
	{
    if(document.getElementById("LSResult1").style.display == "none")
      return false;
      
    // Get currently highlighted row
		highlight = document.getElementById("LSHighlight1");
		if (!highlight)
    {
      // If we don't have one, select first one!
			highlight = document.getElementById("LSShadow1").firstChild.firstChild;
		}
    else
    {
      // If we have one, select next one
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight)
    {
      // Set new row to current highlighted
			highlight.setAttribute("id","LSHighlight1");
			
      // Put value in text box as we scroll    
      var  res = document.getElementById("livesearch1");
      res.value = highlight.firstChild.innerHTML;
      res.style.backgroundColor='#E8DFC7'; 			
		} 
		if(!isIE)
    {
      event.preventDefault();
    }
	}
   
	//KEY UP
	else if (event.keyCode == 38)
  {
    if(document.getElementById("LSResult1").style.display == "none")
      return false;
        
		highlight = document.getElementById("LSHighlight1");
		if (!highlight)
			highlight = document.getElementById("LSResult1").firstChild.firstChild.lastChild;
 
		else
    {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight)
		{
			highlight.setAttribute("id","LSHighlight1");
				
      // Put value in text box as we scroll    
      var  res = document.getElementById("livesearch1");
      res.value = highlight.firstChild.innerHTML;
      res.style.backgroundColor='#E8DFC7'; 					
		}
		if (!isIE)
      event.preventDefault();

	} 
	//ESC
	else if (event.keyCode == 27)
  {
		highlight = document.getElementById("LSHighlight1");
		if (highlight)
			highlight.removeAttribute("id");

		document.getElementById("LSResult1").style.display = "none";
	}
	// ENTER
  else if (event.keyCode == 13)
  {
    var html = document.getElementById("LSHighlight1");
    if(html)
    {
      // List is displayed, we copy selected value to text box and hide list
      var  res = document.getElementById("livesearch1");
      res.value = html.firstChild.innerHTML;
      res.style.backgroundColor='#E8DFC7';
      liveSearchHide(1);
    
      return false;
    }
    else
    {
      return true;
    }
  }
	// TAB
  else if (event.keyCode == 9)
  {
    liveSearchHide(1);
  }  
  
  // Call actual search here, rather than in event !
  if (event.keyCode != 13 && event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 9)
    liveSearchStart(1);

}
function liveSearchKeyPress2(event)
{
	if (event.keyCode == 40)
	//KEY DOWN
	{
    if(document.getElementById("LSResult2").style.display == "none")
      return false;
      
    // Get currently highlighted row
		highlight = document.getElementById("LSHighlight2");
		if (!highlight)
    {
      // If we don't have one, select first one!
			highlight = document.getElementById("LSShadow2").firstChild.firstChild;
		}
    else
    {
      // If we have one, select next one
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight)
    {
      // Set new row to current highlighted
			highlight.setAttribute("id","LSHighlight2");
			
      // Put value in text box as we scroll    
      var  res = document.getElementById("livesearch2");
      res.value = highlight.firstChild.innerHTML;
      res.style.backgroundColor='#E8DFC7'; 			
		} 
		if(!isIE)
    {
      event.preventDefault();
    }
	}
   
	//KEY UP
	else if (event.keyCode == 38)
  {
    if(document.getElementById("LSResult2").style.display == "none")
      return false;
        
		highlight = document.getElementById("LSHighlight2");
		if (!highlight)
			highlight = document.getElementById("LSResult2").firstChild.firstChild.lastChild;
 
		else
    {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight)
		{
			highlight.setAttribute("id","LSHighlight2");
				
      // Put value in text box as we scroll    
      var  res = document.getElementById("livesearch2");
      res.value = highlight.firstChild.innerHTML;
      res.style.backgroundColor='#E8DFC7'; 					
		}
		if (!isIE)
      event.preventDefault();

	} 
	//ESC
	else if (event.keyCode == 27)
  {
		highlight = document.getElementById("LSHighlight2");
		if (highlight)
			highlight.removeAttribute("id");

		document.getElementById("LSResult2").style.display = "none";
	}
	// ENTER
  else if (event.keyCode == 13)
  {
    var html = document.getElementById("LSHighlight2");
    if(html)
    {
      // List is displayed, we copy selected value to text box and hide list
      var  res = document.getElementById("livesearch2");
      res.value = html.firstChild.innerHTML;
      res.style.backgroundColor='#E8DFC7';
      liveSearchHide(2);
    
      return false;
    }
    else
    {
      return true;
    }
  }
	// TAB
  else if (event.keyCode == 9)
  {
    liveSearchHide(2);
  }    
  
  // Call actual search here, rather than in event !
  if (event.keyCode != 13 && event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 9)
    liveSearchStart(2);
}
// +----------------------------------------------------------------------+
// | Called second, just after liveSearchKeyPress() above...
// +----------------------------------------------------------------------+
function liveSearchStart(which)
{
	if (t)
		window.clearTimeout(t);

	t = window.setTimeout("liveSearchDoSearch("+which+")",400);
}
// +----------------------------------------------------------------------+
// | Actual search routine - called by liveSearchStart() above...
// +----------------------------------------------------------------------+
function liveSearchDoSearch(which)
{
  // Init some variables
	if (typeof liveSearchRoot == "undefined")
  {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined")
  {
		liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams == "undefined")
  {
		liveSearchParams2 = "";
	}
  else
  {
		liveSearchParams2 = "&" + liveSearchParams;
	}
	
	// Get scenariste or dessinateur control handler
	var  res = document.getElementById("livesearch"+which);
	
	// If we are not searching the same thing again
	if (liveSearchLast != res.value)
  {
    // Is the XMLHttpRequest object ready ?
  	if (liveSearchReq && liveSearchReq.readyState < 4)
  		liveSearchReq.abort();

    // If no text, hide pop-up
  	if (res.value == "")
    {
  		liveSearchHide(which);
  		return false;
  	}
  	
  	// At least 3 characters!
  	if(res.value.length < 3)
      return false
  	
    // Init Ajax object
    initliveSearchReq();
  	
  	// Set what to do once data returned
  	if(which==1)
      liveSearchReq.onreadystatechange = liveSearchProcessReqChange1;
    else
      liveSearchReq.onreadystatechange = liveSearchProcessReqChange2;
  	
  	// Run query !
  	liveSearchReq.open("GET", liveSearchRoot + "livesearch"+ which +".php?param=" + res.value + liveSearchParams2);
  	
    // Save result as last search
    liveSearchLast = res.value;
    
    // Finish transaction
  	liveSearchReq.send(null);
	}
}
// +----------------------------------------------------------------------+
// | What to do once data returned. Display on screen !
// +----------------------------------------------------------------------+
function liveSearchProcessReqChange1()
{
	if (liveSearchReq.readyState == 4)
  {
    // Change style of LSResult
		var  res = document.getElementById("LSResult1");
		res.style.display = "block";
		
		// Put result in LSShadow
		var  sh = document.getElementById("LSShadow1");
		sh.innerHTML = liveSearchReq.responseText;
	}
}
function liveSearchProcessReqChange2()
{
	if (liveSearchReq.readyState == 4)
  {
    // Change style of LSResult
		var  res = document.getElementById("LSResult2");
		res.style.display = "block";
		
		// Put result in LSShadow
		var  sh = document.getElementById("LSShadow2");
		sh.innerHTML = liveSearchReq.responseText;
	}
}
// +----------------------------------------------------------------------+
// | Process Click
// +----------------------------------------------------------------------+
function ProcessClick(obj, which)
{
  var  res = document.getElementById("livesearch"+which);
  
  res.value = obj.innerHTML;
  res.style.backgroundColor='#E8DFC7';
    
  liveSearchHide(which);
}
