var core_stocks_cache = new Object();
var core_oid = null;

var core_search_result = null;

if (core_search_result==null)
{
	core_search_result = document.createElement("select");	
	core_search_result.id = "core_search_result";
	core_search_result.multiple = true;
	core_search_result.style.position = "absolute";
	core_search_result.style.width = "200px";
	document.body.getElementsByTagName("div")[0].appendChild(core_search_result);
	hide_core_search_result();
}

function core_do_search(oid, kword)
{
	if (kword)
	{
		var fchar = kword.charAt(0).toLowerCase();
		var rs = core_stocks_cache[fchar].split(";;");
		for(i=core_search_result.options.length-1 ; i>= 0 ; i--)
		{
			core_search_result.options[i] = null;
		}
		for (var i=0;i<rs.length;i++)
		{
			if (("::"+rs[i].toLowerCase()).indexOf("::"+kword.toLowerCase())>-1)
			{
				var datas = rs[i].split("::");
				var cname = datas[0];
				var code = datas[1];
				var ename = datas[2];
				if (!cname)
				{
					cname = ename;
				}
				var opt = new Option(cname, code);
				core_search_result.options.add(opt);
			}
		}
		var o = document.getElementById(oid);
		var left = getPosLeft(o);
		var top = getPosTop(o)+o.offsetHeight+5;
		core_search_result.style.top = top+"px";
		core_search_result.style.left = left+"px";
		core_search_result.style.width = (o.offsetWidth+18)+"px";
		core_search_result.style.fontSize = "12px";
		var csrheight = core_search_result.options.length * 18 + 5;
		if (csrheight>200)
		{
			csrheight = 200;
		}
		core_search_result.style.height = (csrheight) + "px";
		core_oid = oid;
		bind(core_search_result, "click", core_select);
		bind(core_search_result, "keydown", core_select);
		bind(core_search_result, "blur", hide_core_search_result);
		bind(core_search_result, "keyup", show_core_search_result);
		bind(core_search_result, "focus", show_core_search_result);
		core_search_result.style.display = "";
	}
}

function core_search(e)
{
	if (e.keyCode == 40)
	{
		core_search_result.focus();
		core_search_result.selectedIndex=0;
		show_core_search_result();
		return;
	}
	else
	{}
	var kword = this.value;
	var fchar = kword.toLowerCase().charAt(0);
	if (kword)
	{	
		if (!core_stocks_cache[fchar])
		{
			loadJs("http://hq.imeigu.com/search.jsp?fchar="+encodeURIComponent(fchar)+"&jsobj=core_stocks_cache", core_do_search, "'"+this.id+"','"+kword+"'");
		}
		else
		{
			core_do_search(this.id, kword);
		}
	}
}

function core_select(e)
{
	if (e&&(e.keyCode==13||e.keyCode==39||e.keyCode==0))
	{
		if (core_search_result.selectedIndex>-1)
		{
			document.getElementById(core_oid).value = core_search_result.value;
			hide_core_search_result();
		}
	}
	else
	{
		show_core_search_result();	
	}
}

function core_bind(oid)
{
	bind(document.getElementById(oid), "input", core_search);
	bind(document.getElementById(oid), "propertychange", core_search);
	bind(document.getElementById(oid), "keydown", core_search);
	bind(document.getElementById(oid), "click", core_search);
	bind(document.getElementById(oid), "blur", delayed_hide_core_search_result);
}

var hqitv;
function delayed_hide_core_search_result(e)
{
	hqitv = window.setInterval('hide_core_search_result();window.clearInterval(hqitv)', 405);
}

function hide_core_search_result(e)
{
	try{core_search_result.style.display = "none";}catch(e){};
}

function show_core_search_result(e)
{
	if (e&&(e.keyCode==13||e.keyCode==39))
	{
	}
	else
	{
		try{window.clearInterval(hqitv);}catch(e){}
		try{core_search_result.style.display = "";}catch(e){};
	}
}
