var liSelect = null;
var aSelect = null;
var predClassName = "liAltOneStyle";
var predIndex = 0;
var index = 0;
  
function autoCompOff(id) {
	document.getElementById(id).setAttribute("autocomplete","off");
}

// JavaScript Document
function getXhr() {
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	} 
	return xhr;
}

function go_chercher(recherche) { 
	var xhr = getXhr();

	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function() {
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if (xhr.readyState == 4 && xhr.status == 200){
			leselect = xhr.responseText;
			document.getElementById('moteur_recherche').style.display = "block";
			document.getElementById('moteur_recherche').innerHTML = leselect;
			drawSelectedItemInResultSet ();
		}
	}

	// Ici on va voir comment faire du post
	xhr.open("POST",'moteur_recherche.php',true);
	// ne pas oublier ça pour le post
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	// ne pas oublier de poster les arguments
	if(recherche!='null' || recherche!=""){ 
		xhr.send("rechercher="+recherche);
	}
}

function key_action_listener(value, _event_) {
	if (value.length >= 2) {
		var	prov;
		var winObj = checkEventObj (_event_);
		
		// on enlève les overflox
		var container = document.getElementById('container');
		var maincontent = document.getElementById('mainContent');
		
		if(container) {
			container.style.overflow = 'visible';
		}
		if(maincontent) {
			maincontent.style.overflow = 'visible';
		}
		
		aSelect = document.getElementById("a_" + index);
		
		key = winObj.keyCode;
		if (!key) key = winObj.wich;
		
		predIndex = index;
		
		switch (key) {
			// Action lorsqu'on appuie sur la touche entrée
			case 13:
				if (liSelect != null) window.location.href = aSelect.href;
			break;
			// Action lorsqu'on appuie sur la flèche du bas
			case 40:
				prov = parseInt(index) + 1;
				liSelect = document.getElementById("li_" + prov);
				if (liSelect != null) {
					index++;
					drawSelectedItemInResultSet ();
				}
			break;
			// Action lorsqu'on appuie sur la flèche du haut
			case 38:
				prov = parseInt(index) - 1;
				liSelect = document.getElementById("li_" + prov);
				if (liSelect != null) {
					index--;
					drawSelectedItemInResultSet();
				}
			break;
			default:
				index = 0;
				predIndex = 0;
				predClassName = "liAltOneStyle";
				go_chercher(value);
			break;
		}
	} else document.getElementById('moteur_recherche').style.display = "none";
}

function drawSelectedItemInResultSet () {
	document.getElementById("li_" + predIndex).className = predClassName;
	liSelect = document.getElementById("li_" + index);
	predClassName = liSelect.className;
	liSelect.className = "liSelectStyle";
}

function checkEventObj ( _event_ ) {
	// --- IE explorer
	if ( window.event ) return window.event;
	// --- Netscape and other explorers
	else return _event_;
}

