/*-------------------------AJAX Suggest Engine-------------------------------*/	
/*-------------------------Begin Suggest Code--------------------------------*/	
				
/*				if (typeof netscape != 'undefined' && typeof netscape.security != 'undefined') {
                netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
        }*/
				
var tabid=new Array();
var tabnom=new Array();
var position = -1;
var isKeyUpDownPressed = false; 
var suggestions = 0; 
var minVisiblePosition = 0;
var maxVisiblePosition = 5;
var boolTimer = false;

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Votre Navigateur ne supporte pas l'Ajax. Pour une optimisation des résultats pensez à télcharger la dernière version de ce dernier.");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest(e) {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		// get the event
		e = (!e) ? window.event : e;
		// get the event's target
		target = (!e.target) ? e.srcElement : e.target;
		if (target.nodeType == 3) 
		target = target.parentNode;
		// get the character code of the pressed button
		code = (e.charCode) ? e.charCode :
		   ((e.keyCode) ? e.keyCode :
		   ((e.which) ? e.which : 0));
		// check to see if the event was keyup
		if (e.type == "keyup") 
		{    
			isKeyUpDownPressed = false; 
			// check to see we if are interested in the current character
			if (code != 13 && code != 8 && code != 38 && code != 40) {
				tabid=new Array();
				tabnom=new Array();
				var str = escape(document.getElementById('txtSearch').value);
				if(str!=''){
					searchReq.open("GET", 'searchsuggest.php?search=' + str, true);
					searchReq.onreadystatechange = handleSearchSuggest;
					if(boolTimer == false) {
						timer = setTimeout("document.getElementById('search_suggest').style.visibility = 'hidden'", 20000);
						boolTimer = true;
					}
					searchReq.send(null);
				}
				else
				{
					document.getElementById('search_suggest').style.visibility = 'hidden';
				}
			}
			else
			/* if Enter is pressed we jump to the PHP help page of the current 
			   function */
			if(code == 13)
			{
				// check to see if any function is currently selected
				if(position>=0)
				{
					setSearch(document.getElementById('a' + position).innerHTML);
				}        
			}        
			else
			// if the down arrow is pressed we go to the next suggestion
			  if(code == 40)
			  {	
				newTR=document.getElementById('a' + (++position));
				oldTR=document.getElementById('a' + (--position));
				
				// deselect the old selected suggestion   
				if(position>=0 && position<suggestions-1)
				  oldTR.className = "suggest_link";
		 
				// select the new suggestion and update the keyword
				if(position < suggestions-1)
				{
					newTR.className = "suggest_link_over";
					if(navigator.appName == 'Microsoft Internet Explorer') {
						document.getElementById('txtSearch').value = document.getElementById('a' + (++position)).innerText;
					} else {
						document.getElementById('txtSearch').value = document.getElementById('a' + (++position)).textContent;
					}
				}     
				e.cancelBubble = true;
				e.returnValue = false;
				isKeyUpDownPressed = true;
				if(boolTimer == true) {
					clearTimeout(timer);
					boolTimer = false;
				}
				// scroll down if the current window is no longer valid
				/*if(position > maxVisiblePosition)
				{   
					oScroll = document.getElementById("scroll");
					oScroll.scrollTop += 18;
					maxVisiblePosition += 1;
					minVisiblePosition += 1;
				}*/
			}
			else
			// if the up arrow is pressed we go to the previous suggestion
			if(code == 38)
			{       
				newTR=document.getElementById('a' + (--position));
				oldTR=document.getElementById('a' + (++position));
				// deselect the old selected position
				if(position>0 && position <= suggestions-1)
				{       
				  oldTR.className = "suggest_link";
				}
				// select the new suggestion and update the keyword
				if(position > 0)
				{
					newTR.className = "suggest_link_over";
					if(navigator.appName == 'Microsoft Internet Explorer') {
						document.getElementById('txtSearch').value=document.getElementById('a' + (--position)).innerText;
					} else {
						document.getElementById('txtSearch').value=document.getElementById('a' + (--position)).textContent;
					}
					// scroll up if the current window is no longer valid
					/*if(position<minVisiblePosition)
					{
						oScroll = document.getElementById("scroll");
						oScroll.scrollTop -= 18;
						maxVisiblePosition -= 1;
						minVisiblePosition -= 1;
					}*/
				}
				e.cancelBubble = true;
				e.returnValue = false;
				isKeyUpDownPressed = true;
				if(boolTimer == true) {
					clearTimeout(timer);
					boolTimer = false;
				}
			  }     
		  }
	}
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {		
		var nm= new Array();
		var ss = document.getElementById('search_suggest');
		ss.style.visibility = 'visible';
		var str = searchReq.responseText.split("\n");
		suggestion = str.length;
		if(str!=''){
			ss.innerHTML = '<span style="color:#999; line-height:20px;vertical-align:middle"><strong>&nbsp;Nos suggestions&nbsp;:&nbsp;</strong></span><span style="float: right;"><img src="/images/searchbar-btn-close.gif" alt="Fermer" title="Fermer" style="margin-top:5px;margin-right:5px;cursor: pointer;" onClick="document.getElementById(\'search_suggest\').style.visibility = \'hidden\';" /></span>';
		}else{
			position = -1;
			ss.innerHTML = '<span style="color:#999; line-height:20px; vertical-align:middle"><strong>&nbsp;Pas de suggestions</strong></span><span style="float: right;"><img src="/images/searchbar-btn-close.gif" alt="Fermer" title="Fermer" style="margin-top:5px;margin-right:5px;cursor: pointer;" onClick="document.getElementById(\'search_suggest\').style.visibility = \'hidden\';" /></span>';
		}
		for(i=0; i < str.length - 1; i++) {
			nm = str[i].split(" -- ");
			tabid[tabid.length]=nm[1];
			var tmp=nm[0];
			tmp=tmp.replace('<strong>','');
			tmp=tmp.replace('</strong>','');
			tmp=tmp.replace('<STRONG>','');
			tmp=tmp.replace('</STRONG>','');
			document.getElementById('tempo').innerHTML=tmp;
			tabnom[tabnom.length]=document.getElementById('tempo').innerHTML;
			suggestions = tabnom.length;
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link" id="a' + i + '">' + nm[0] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	if(position >= 0 && (div_value.id != ('a' + position))) {
		document.getElementById('a' + position).className = 'suggest_link';
	}
	div_value.className = 'suggest_link_over';
	document.getElementById('txtSearch').style.color='#000';
	if(navigator.appName == 'Microsoft Internet Explorer') {
		document.getElementById('txtSearch').value = div_value.innerText;
	} else {
		document.getElementById('txtSearch').value = div_value.textContent;
	}
	if(boolTimer == true) {
		clearTimeout(timer);
		boolTimer = false;
	}
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
	document.getElementById('txtSearch').value = '';
	if (boolTimer == false) {
		timer = setTimeout("document.getElementById('search_suggest').style.visibility = 'hidden'", 20000);
		boolTimer = true;
	}
	position = -1;
}
//Click function
function setSearch(value) {
	value=value.replace('<strong>','');
	value=value.replace('</strong>','');
	value=value.replace('<STRONG>','');
	value=value.replace('</STRONG>','');
	document.getElementById('txtSearch').value = value.replace(/&amp;/, '&');
	document.getElementById('search_suggest').innerHTML = '';
	for(i=0;i<tabnom.length;i++){
		if(tabnom[i]==value){
			var place=i;
			break;
		}
	}
	document.getElementById('quick_find').action='./product_info.php';
	document.getElementById('products_id').value=tabid[place];
	document.getElementById('search_suggest').style.visibility = 'hidden';
	document.forms.quick_find.submit();
}
/*-------------------------End Suggest Code--------------------------------*/				