
/*
	This is the JavaScript file for the osCommerce AJAX Search Suggest

	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.
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.

*/
//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("Pour utiliser la recherche intelligente il faut mettre à jours votre browser");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest() {

	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('txtSearch').value);
		searchReq.open("GET", 'SearchSuggest.php?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest;
		searchReq.send(null);
	}
}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = "";
}


//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		showsearch_suggest();
		var ss = document.getElementById('search_suggest')
		var str = searchReq.responseText.split("\n");
		if (str.length > 1) {
			ss.innerHTML = '<div align="right"><a  href="javascript:hidesearch_suggest();"><img border="0" src="/images/close.png"></a></div>';
			for(i=0; i < str.length - 1; i++) {
				$strtab = str[i].split('|');
				//Build our element string.  This is cleaner using the DOM, but
				//IE doesn't support dynamically added attributes.


				var suggest = '';
				suggest += '<div class="suggestsearchlign" onclick="javascript:clickProduct(\'' + $strtab[0]  + '\');" onmouseover="javascript:suggestOver(this);" onmouseout="javascript:suggestOut(this);">';
				suggest += '<table class="suggestsearchlign" border="0" width="100%" style="text-align:center;"><tr><td style="text-align:left;" width="65px" >' + $strtab[1]  + '</td><td style="color=black;" width="390px">&nbsp;'  + $strtab[3] + ' </td><td width="100px" style="color=orange;text-align:right;"><a href="' + $strtab[0] + '" ><big><b>' + $strtab[2] + '</b></big></a></td></tr></table></div>';
				suggest += '</div>';
				ss.innerHTML += suggest;
			}
		} else {
			hidesearch_suggest();
		}
	}
}
function clickProduct($address) {
	$cmd = "document.location='" + $address + "';";
	eval($cmd);

}
//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

function hidesearch_suggest() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('search_suggest').style.visibility = 'hidden';
//document.getElementById('search_suggest').style.border-width = '0px';

}
else {
if (document.layers) { // Netscape 4
document.search_suggest.visibility = 'hidden';
}
else { // IE 4
document.all.search_suggest.style.visibility = 'hidden';
}
}
}

function showsearch_suggest() {

if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('search_suggest').style.visibility = 'visible';
//document.getElementById('search_suggest').style.border-width = '1px';
}
else {
if (document.layers) { // Netscape 4
document.search_suggest.visibility = 'visible';
}
else { // IE 4
document.all.search_suggest.style.visibility = 'visible';
}
}
}

//Click function
//function setSearch(value) {
//	document.getElementById('txtSearch').value = value.split('|')[2];
//	document.getElementById('search_suggest').innerHTML = '';
//}
