var xmlHttp = createXmlHttpRequestObject();
var serverAddress = "search_str.php?search_str=";
var query_str = '';
var position=0;
var min_position=0;
var max_position=0;
var min_visible_position=0;
var max_visible_position=8;
window.onload = init;

function result_click(id)
    {
	document.getElementById('query2').value=document.getElementById(id).innerHTML;
	document.getElementById('list_sapros').className='unselect';
	document.getElementById('result_form').submit();
    }

function LoginClick()
    {
	login_div = document.getElementById('autorisation_div');
	login_div.style.display='block';
	document.getElementById('login_input').focus();
    }
    
function CloseLoginDiv()
    {
	document.getElementById('autorisation_div').style.display='none';
    }

function init()
    {
	var oKeyword = document.getElementById('query');
	oKeyword.setAttribute('autocomplete', 'off');
    }
    
function createXmlHttpRequestObject()
    {
	var xmlHttp;
	try
	    {
		xmlHttp = new XMLHttpRequest();
	    }
	catch(e)
	    {
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
						"MSXML2.XMLHTTP.5.0",
						"MSXML2.XMLHTTP.4.0",
						"MSXML2.XMLHTTP.3.0",
						"MSXML2.XMLHTTP",
						"Microsoft.XMLHTTP");
		for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		    {
			try
			    {
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			    }
			catch(e) {}
		    }
	    }
	if (!xmlHttp)
	    alert("Ошибка создания объекта XMLHttpRequest.");
	else
	    return xmlHttp;
    }
    
function process()
    {
	if (xmlHttp)
	    {
		try
		    {
			xmlHttp.open("GET", serverAddress+encode(query_str), true);
			xmlHttp.onreadystatechange = handleGettingQuery;
			xmlHttp.send(null);
		    }
		catch(e)
		    { 
			document.getElementById('list_sapros').className='unselect';
			//alert("Не возможно соединится с сервером!"+e.toString());
		    }
	    }
    } 

function handleGettingQuery()
    {
	if (xmlHttp.readyState == 4)
	    {
		if (xmlHttp.status == 200)
		    {
			try
			    {
				getQuery();
			    }
			catch(e)
			    {
				alert("Ошибка получения данных!");
			    }
		    }
		else
		    {
			alert("Ошибка получения данных2!");
		    }
	    }
    }

function getQuery()
    {
	var response = xmlHttp.responseText;
	min_position = 0;
	position = 0;
	max_position = response.substring(response.search('___')+3);
	response = response.substring(0, response.length-3-max_position.length);
	document.getElementById('list_sapros').innerHTML = response;
	if (response)
	    {
		document.getElementById('list_sapros').className='select';
		
	    }
	else
	    {
		document.getElementById('list_sapros').className='unselect';
	    }
    }

function ChangeQueryStr(e)
    {
	e = (!e) ? window.event : e;
	target = (!e.target) ? e.srcElement : e.target;
	if (target.nodeType == 3)
	    target = target.parentNode;
	code = (e.charCode) ? e.charCode :
		((e.keyCode) ? e.keyCode :
		((e.which) ? e.which : 0));
	if (code == 40 || code == 13 || code == 38 || code == 27)
	    {
		if (code == 40)
		    {
			ChangePosition(position+1);
			e.cancelBubble = true;
			e.returnValue = false;
		    }
		if (code == 38)
		    {
			ChangePosition(position-1);
			e.cancelBubble = true;
			e.returnValue = false;
		    }
		if (code == 27)
		    {
			document.getElementById('list_sapros').className='unselect';
			e.cancelBubble = true;
			e.returnValue = false;
		    }
		if (code == 13)
		    {
			if (document.getElementById('list_sapros').className!='unselect')
			    {
				tmp_id = 'result_div_'+position;
				result_click(tmp_id);
			    }
			else
			    {
				document.getElementById('query2').value=document.getElementById('query').value;
				document.getElementById('result_form').submit();
			    }
		    }
	    }
	else
	    {
		query_str = document.getElementById('query').value;
		if (query_str.length > 1)
		    {
			process();
		    }
		else
		    {
			document.getElementById('list_sapros').className='unselect';
		    }
	    }
    }
function encode(url)
    {
	if (encodeURIComponent)
	    {
		return encodeURIComponent(url);
	    }
	if (escape)
	    {
		return escape(url);
	    }
    }
    
function MouseOverResult(tmp_res)
    {
	tmp_res.className='result_select';
    }

function MouseOutResult(tmp_res)
    {
	tmp_res.className='result';
    }
    
function ChangePosition(new_position)
    {
	if (new_position >= min_position && new_position <= max_position)
	    {

		if (position > min_position)
			document.getElementById('result_div_'+position).className='result';
		if (new_position > 0)
			document.getElementById('result_div_'+new_position).className='result_select';

		position = new_position;
		if (position >= max_visible_position)
		    {
			document.getElementById('list_sapros').scrollTop += 18;
			max_visible_position +=1;
			min_visible_position +=1;
		    }
		if (position < min_visible_position)
		    {
			document.getElementById('list_sapros').scrollTop -= 18;
			max_visible_position -=1;
			min_visible_position -=1;
		    }
		
	    }
    }