////////////////////////////////////////////////////////////////////////////////
// Encoding Utils
function escapeEx(str)
{
    var ret = '';

	var i = 0;
    for (var i=0; i<str.length; i++)
    {
        var n = str.charCodeAt(i);
        if (n >= 0x410 && n <= 0x44F)
            n -= 0x350;
        else if (n == 0x451)
            n = 0xB8;
        else if (n == 0x401)
            n = 0xA8;
        if ((n < 65 || n > 90) && (n < 97 || n > 122) && n < 256)
        {
            if (n < 16)
                ret += '%0'+n.toString(16);
            else
                ret += '%'+n.toString(16);
        }
        else
            ret += String.fromCharCode(n);
    }

    return ret;
}

function hideTooltip(obj_id)
{
	var obj = uh_get_object(obj_id);
	var obj_body = uh_get_object(obj_id + "_body");

	obj_body.innerHTML = "empty";
	obj.style.visibility = "hidden";
	obj.style.display = "none";
}

function showTooltipTextShort(obj_id, x, y, txt)
{
	var obj = uh_get_object(obj_id);
	var obj_body = uh_get_object(obj_id + "_body");

	obj.style.visibility = "visible";
	obj.style.display = "block";
	obj.style.left = x + "px";
	obj.style.top = y + "px";

	obj_body.innerHTML = txt + "<br />";

	//alert( "obj: " + obj_id + "; " + txt );
}

function selWordFromLink(val1, target_id, droptip_id)
{
	var target = uh_get_object( target_id );

	target.value=val1;
	target.focus();
	hideTooltip(droptip_id);
}

function fillSearchList(str_val, obj_id, inp_id)
{
	var ctrl_droplist = uh_get_object( obj_id );
	var ctrl_text = uh_get_object( inp_id );

	if( search_is_running )
		return;

	search_is_running = true;

	if( str_val.length > 1 )
	{
	  	if( AJAX != null )
		{
			//alert( 'Ajax OK' );
			var process_fn = function(res)
			{
				//alert( "Respoce: " + res );

				eval( 'var responce_data = ' + res );

				var div_inner_html = "";

				try
				{
					// Fill list of cities
					for(i=0; i<responce_data.words.length; i++)
					{						if( i>0 )
							div_inner_html += '<br />';
						div_inner_html += '<a href="javascript:selWordFromLink(\'' + responce_data.words[i].word + '\', \'' + inp_id + '\', \'' + obj_id + '\');" class="searchitem">' + responce_data.words[i].word + '</a>';
					}

					if( div_inner_html == "" )
						hideTooltip(obj_id);
					else
						showTooltipTextShort(obj_id, ctrl_text.offsetLeft, ctrl_text.offsetTop + 20, div_inner_html);
				}
				catch(e1)
				{
					// Some errors occure while retieving city list so do nothing
					hideTooltip(obj_id);
				}

				search_is_running = false;
			};

			var post_req_str = "rcom=uh_com_srchlist&swstart=" + unescape(escapeEx(str_val));
			//var post_req_str = "rcom=uh_com_city_start&citystart=" + str_val;
			AJAX.SendRequest('POST', '', process_fn, encode64(post_req_str));
		}
	}
	else
	{
		hideTooltip(obj_id);
		search_is_running = false;
	}
}

var search_is_running = false;