/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function prod_sel_getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Votre navigateur Internet est trop ancien!\nIl est plus que temps de le mettre à jour");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var prod_sel_searchReq = prod_sel_getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function prod_sel_searchSuggest() {
	if (prod_sel_searchReq.readyState == 4 || prod_sel_searchReq.readyState == 0) {
		var str = escape(document.getElementById('selection_produit_nom').value);
		prod_sel_searchReq.open("GET", '../../../tools/ProduitSelSuggest/searchSuggest.php?search=' + str, true);
		prod_sel_searchReq.onreadystatechange = prod_sel_handleSearchSuggest; 
		prod_sel_searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function prod_sel_handleSearchSuggest() {

	// Test de détection d'IE6
   	var strChUserAgent = navigator.userAgent;
   	var intSplitStart = strChUserAgent.indexOf("(",0);
   	var intSplitEnd = strChUserAgent.indexOf(")",0);
   	var strChStart = strChUserAgent.substring(0,intSplitStart);
	var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
	var strChEnd = strChUserAgent.substring(strChEnd);
		
	if (prod_sel_searchReq.readyState == 4) {	
		var ss = document.getElementById('prod_sel_search_suggest');
		ss.innerHTML = '';
		var prods = prod_sel_searchReq.responseXML.getElementsByTagName("produit");

		for(i=0;i<prods.length;i++){
		
			
			var id  = prods[i].getElementsByTagName("id")[0].firstChild.nodeValue;
			
			if(id == -1) {
				var nom = "";
			}else
			{
				var nom = prods[i].getElementsByTagName("nom")[0].firstChild.nodeValue;
				
				if(strChEnd.indexOf("MSIE 6") != -1) {
					/*document.getElementById("departement_id").style.visibility = "hidden";
					document.getElementById("departement_id").disabled = true;*/
				}
				var escape_nom = nom.replace(/\'/g,"\\\'");
				
				var suggest = '<div onmouseover="javascript:prod_sel_suggestOver(this);" ';
				suggest += 'onmouseout="javascript:prod_sel_suggestOut(this);" ';
				suggest += 'onclick="javascript:prod_sel_setSearch(\''+escape_nom+'\', \''+id+'\');" ';
				suggest += 'class="suggest_link" ';
				
				if(strChEnd.indexOf("Firefox/2") != -1) {
					suggest += 'style="width:131px'; 
				
					if (i + 1 == prods.length)
						suggest += ';border-bottom:1px solid #999"'; 
					else
						suggest += '"'; 
				}
				
				if(strChEnd.indexOf("MSIE 7") != -1) {
					suggest += 'style="width:128px';
					
					if (i + 1 == prods.length)
						suggest += ';border-bottom:1px solid #999"';
					else
						suggest += '"';
				}
				
				suggest += '>' +nom + '</div>';
				ss.innerHTML += suggest;
			}
		}
	}
}

//Mouse over function
function prod_sel_suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function prod_sel_suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function prod_sel_setSearch(value1,value2) {
	if(strChEnd.indexOf("MSIE 7") != -1) {
		document.getElementById("prod_sel_search_suggest").style.top = "-2px";
	}
	if(strChEnd.indexOf("MSIE 6") != -1) {
		/*document.getElementById("prod_sel_search_suggest").style.top = "-2px";*/
	}
	document.getElementById('selection_produit_nom').value = value1;
	document.getElementById('prod_sel_search_suggest').innerHTML = '';
}
