var timeout = 1000; // milliseconds to wait for no activity 
var countEvents = 0; 

function delayedSearchSuggestions(form,field,str,event){ 
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) { // IE	
		keynum = event.keyCode;
	} else if(event.which) { // Netscape/Firefox/Opera
		keynum = event.which;
	}

	if ((keynum >= 37) && (keynum <= 40)) // arrow keys
		return;

	if (keynum == 13) {
		location.href = "http://cerca-nel-sito.360gradi.info/?siteSearchStr=" + encodeURIComponent(str);
//		location.href = "http://cerca-nel-sito.360gradi.test/?siteSearchStr=" + encodeURIComponent(str);
		return;
	}
	
	if (str.length < 3) {
		//searchSuggestions(form,field,'[CANCEL]',event);
		return;		
	}
	
	showLoader();
	countEvents++; 
	setTimeout(function() { 
		countEvents--; 
		if(countEvents==0){ 
			searchSuggestions(form,field,str,event); 
		} 
	},timeout); 
} 
function searchSuggestions(form,field,str,event)
{	
	if (str.length < 3) {
		searchSuggestions(form,field,'[CANCEL]',event);
		return;		
	}
	var httpxml;
	try
	{
		// Firefox, Opera 8.0+, Safari
		httpxml=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			httpxml=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				httpxml=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}	
	function stateChanged() 
	{
		if(httpxml.readyState==4)
		{
			document.getElementById("siteSearchSuggestionsDiv").innerHTML=httpxml.responseText;		
		}
	}
	var url="/scripts/ajax-search-suggestions.php";
	url=url+"?txt="+encodeURIComponent(str);
	url=url+"&sid="+Math.random();
	httpxml.onreadystatechange=stateChanged;
	httpxml.open("GET",url,true);
	httpxml.send(null);
}
function clearSuggestions(form,field,event)
{	
	setTimeout(function() {	searchSuggestions(form,field,'[CANCEL]',event);	}, 200);
}
function showLoader()
{	
	var httpxml;
	try
	{
		// Firefox, Opera 8.0+, Safari
		httpxml=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			httpxml=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				httpxml=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}	
	function stateChanged() 
	{
		if(httpxml.readyState==4)
		{
			document.getElementById("siteSearchSuggestionsDiv").innerHTML=httpxml.responseText;		
		}
	}
	var url="/scripts/ajax-loading-in-progress.php";
	url=url+"?sid="+Math.random();
	httpxml.onreadystatechange=stateChanged;
	httpxml.open("GET",url,true);
	httpxml.send(null);
}
