// AJAX search
////var url = "http://bb.webworx.hu/web_szerver/www/info/get_search_words.php"; // The server-side script 
var my_focus=0;
var listadoboz_on=0;

if(window.addEventListener) { // Standard
	document.addEventListener('click', set_lista_off,false);
} else if(window.attachEvent) { // IE
	document.attachEvent('onclick', set_lista_off);
}

function set_lista_off()
{
	listadoboz_on=0;
	if (get_my_object('listadoboz') != null) get_my_object('listadoboz').style.display = 'none';
	if (http != null) {
		http.onreadystatechange = handleHttpResponse;
//		http.send(null);
	}
}


function handleHttpResponse() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText;

			if (results.length > 0)
			{
				get_my_object('listadoboz').innerHTML = results;
				if (my_focus<1) {
					search_tag();
				}
				set_cursors('');
			}

		} 
	} 
} 

function my_start()
{
	if (my_focus<1) {
		set_cursors('wait');
		var my_input_tag = get_my_object('tag_input').value;
		my_input_tag = escape(my_input_tag);
			
		http.open("GET", url+'?search_string='+my_input_tag, true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
	}
}
function getHTTPObject() 
{ 
	var xmlhttp; 
	
	if(window.XMLHttpRequest)
	{ 
		xmlhttp = new XMLHttpRequest(); 
	} 
	else if (window.ActiveXObject)
	{ 
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		if (!xmlhttp)
		{ 
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
		}
	} 
	return xmlhttp; 
}

var http = getHTTPObject(); // We create the HTTP Object 
var submited_form=false;


function get_my_object(nev)
{
	if (document.getElementById) 
	{
		obj = document.getElementById(nev); 
	}
	else if (document.all) 
	{
	// IE4 
		obj = document.all[nev];
	}
	return obj;
}

function set_cursors(cur)
{
	document.body.style.cursor = cur;
}

/////////////////////////////////////////////
// search
var wordlist_selected_index=-1;



/*
searchword debug 2011.02.18.
*/

function strdecode(str) {
	var new_search_string = str;
	new_search_string = unescape(new_search_string);
	new_search_string = new_search_string.replace(/\+/, " ");
	
	return new_search_string;
}



function select_tag(sel)
{
	//document.getElementById('tag_input').value = document.getElementById('word_list_'+wordlist_selected_index).value.toString();
	//document.getElementById('hidden_tag_input').value = document.getElementById('hidden_word_list_'+wordlist_selected_index).value.toString();
	
	var myNewSearchString;
	myNewSearchString = document.getElementById('word_list_'+wordlist_selected_index).value.toString();
	myNewSearchString = strdecode(myNewSearchString);
	//alert(myNewSearchString);
	
	document.getElementById('tag_input').value = myNewSearchString;
	document.getElementById('hidden_tag_input').value = document.getElementById('hidden_word_list_'+wordlist_selected_index).value.toString();
}
function add_tag()
{
//	start_search();
	document.searchForm.submit();
}
function select_and_add(irany)
{
	document.getElementById('listadoboz').style.display='';
	if (document.getElementById('sel_row_'+wordlist_selected_index)) document.getElementById('sel_row_'+wordlist_selected_index).style.background = "#FFFFFF";
	if ( (irany==1) && (wordlist_selected_index < document.getElementById('word_list_count').value-1) ) {wordlist_selected_index++;}
	if ( (irany==-1) && (wordlist_selected_index>0) ) {wordlist_selected_index--;}
	document.getElementById('sel_row_'+wordlist_selected_index).style.background = "#E3E3E3";
	
	select_tag(wordlist_selected_index);
}
function over_select(index, width_search)
{
	document.getElementById('listadoboz').style.display='';
	if (document.getElementById('sel_row_'+wordlist_selected_index)) document.getElementById('sel_row_'+wordlist_selected_index).style.background = "#FFFFFF";
	wordlist_selected_index = index;
	document.getElementById('sel_row_'+wordlist_selected_index).style.background = "#E3E3E3";
	
	if (width_search==true) {
		select_tag(wordlist_selected_index);
	}
}

function search_tag()
{
	document.getElementById('listadoboz').style.display='';
	var my_input_tag =  get_my_object('tag_input').value.toLowerCase();
	if (my_input_tag.length>0) {
		wordlist_selected_index=-1;
		for (i=0; i<document.getElementById('word_list_count').value; i++) {
			var my_option = document.getElementById('word_list_'+i).value.toString();
			var strpos = my_option.indexOf(my_input_tag);
			if ((strpos>-1) && (strpos<1)) {
				wordlist_selected_index=i;
				break;
			}
		}
	}
}

// esemenykezelok
	function kb_inputsor(evt) {
		var key = evt.keyCode;

		switch (key) {
			case 40:
				my_focus=1;
				select_and_add(1);
				break;
			case 38: //felnyil
				if (my_focus==1) select_and_add(-1);
				break;

			case 13:  // ENTER
				add_tag();
				break;
			default: my_focus=0;
				break;
		};
		if (document.getElementById('tag_input').value.length<1) document.getElementById('listadoboz').style.display='none';

	}
	function kb_listadoboz(evt) {
		var key = evt.keyCode;

		switch (key) {
			case 40: // lenyil
//				select_and_add(1);
				break;
			case 38: //felnyil
//				select_and_add(-1)
				break;

			case 13: // ENTER
				add_tag();
				break;
		};
	}


