var AjaxSearchBox =
{
	TimeOutClose : null,
	
	createXHR : function() 
	{
		var request;
		if (window.XMLHttpRequest)// Mozilla, Safari, ...
		{ 
			request = new XMLHttpRequest();
			if (request.overrideMimeType) { request.overrideMimeType('text/xml'); }
		}
		else if (window.ActiveXObject)// IE
		{ 
			try
			{
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){}
			}
		}
	
		if (!request)
		{
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
		
		return request;
	},
	Trim : function (sString) 
	{
		if ( sString != '')
		{
			while (sString.substring(0,1) == ' ')
			{
				sString = sString.substring(1, sString.length);
			}
			while (sString.substring(sString.length-1, sString.length) == ' ')
			{
				sString = sString.substring(0,sString.length-1);
			}
		}
		return sString;
	},
	Add : function()
	{
		AjaxSearchBox.close();
	},
	setInputs : function(pdisplay)
	{
		document.getElementById('keyword').value = pdisplay;
	},
	close : function(t)
	{
		var div = document.getElementById('AjaxSearchBoxDisplay');
		var objAjaxSearchBox = document.getElementById('AjaxSearchBox');
		div.style.display = 'none';
		objAjaxSearchBox.style.display = 'none';
	},
	open : function(t)
	{
		var div = document.getElementById('AjaxSearchBoxDisplay');
		var objAjaxSearchBox = document.getElementById('AjaxSearchBox');
		if (div != null && objAjaxSearchBox != null)
		{
			div.style.display = 'block';
			objAjaxSearchBox.style.display = 'block';
			
			AjaxSearchBox.setCloseTimer();
		}
	},
	setCloseTimer : function()
	{
		clearTimeout(AjaxSearchBox.TimeOutClose);
		AjaxSearchBox.TimeOutClose = setTimeout("AjaxSearchBox.close();",5000);
	},
	get : function(t)
	{
		AjaxSearchBox.setCloseTimer();
		
		var url = './inc/ajax_scripts/search.php?keyword='+t.value;
		
		var xmlHttpRequest = AjaxSearchBox.createXHR();
		
		xmlHttpRequest.onreadystatechange = function()
		{  
			if (xmlHttpRequest.readyState == 4)
			{
				if (xmlHttpRequest.status == 200)
				{
					var objAjaxSearchBox = document.getElementById('AjaxSearchBox');
					
					var div = document.getElementById('AjaxSearchBoxDisplay');
					if (div == null)
					{
					
						div = document.createElement('div');
						div.id = 'AjaxSearchBoxDisplay';
						div.style.display = 'none';
						div.style.position = 'absolute';
						div.style.top = '0px';
						div.style.left = '0px';
						div.style.width = '99%';
						div.style.border = '1px solid #ccffff';
						div.style.backgroundColor = '#ffffff';
						objAjaxSearchBox.appendChild(div);
					}
					
					try
					{
						var response = xmlHttpRequest.responseText;
						response = AjaxSearchBox.Trim(response);
						div = document.getElementById('AjaxSearchBoxDisplay');
						div.innerHTML = response;
						if ( response.length < 3 )
						{
							AjaxSearchBox.close();
						}
						else
						{
							AjaxSearchBox.open();
						}
					} 
					catch (e) 
					{ 
						//alert("Nothing");
					}
				}
				else
				{
					//alert("Nothing");
				}
			}
		}
		xmlHttpRequest.open('GET', url, true);	
		xmlHttpRequest.send(null);
	}
}

