function Suggestion(form,input,t,l){
var text = ""; //临时保存文本值, 值变化了才去请求接口
var index = 0; //提示层被选中的项
var i,f=document.forms[form];
if(!f){return;}
i=f[input];
if(!i){return;}
i.setAttribute("autocomplete","off");
var ip=i.parentNode;
var sm=new StrMaker();
sm.add("<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" bgcolor=\"#ffffff\" style=\"border: 1px solid rgb(102, 102, 102); position: absolute; z-index: 100;top:-1;width: 235px;display: none;font-weight:normal;\">");
sm.add("<tr><td height=\"16\" align=\"center\" style=\"color: rgb(153, 153, 153); padding-left: 3px; background-image: url(http://img.soufun.com/secondhouse/image/toptip-bg.gif); background-repeat: repeat-x; background-position: center center;\">输入中文/拼音/拼音首字母或上下键选择</td></tr>");
sm.add("</table>");
var d=document.createElement("span");
d.innerHTML=sm.str();
insertAfter(d,i);
var n=GetNode(d,"TABLE",0);
this.i=i;
this.f=f;
this.div=n;
var me=this;
Suggestion.List.push(me);

__IsInput = function(ev)
{
	return __IsSelect(ev) || __IsMove(ev) || ev.keyCode == 9 || ev.keyCode == 13;
}
__IsSelect = function(ev)
{
	if (ev.shiftKey || (ev.ctrlKey && ev.shiftKey))
	{
		return (ev.keyCode >=35 && ev.keyCode <=40);
	}
	else if (ev.ctrlKey && !ev.shiftKey)
	{
		return ev.keyCode == 65;
	}
	else
	{
		return false;
	}
}
function SetPos()
{
	var pos=new GetPos(i);
	n.style.top=(pos.top+t)+"px";
	n.style.left=(pos.left+l)+"px";
}
function __IsMove(ev){return (ev.keyCode >=35 && ev.keyCode <=40);}

i.onfocus = function()
{
    clear(i);
	if (this.value == this.defaultValue)
	{
		this.value = '';
		this.style.color = '#000000';
        n.style.display = '';
        SetPos();
	}
}
i.onblur = function()
{
    //clear(i);
	if (this.value == '')
	{
		this.value = this.defaultValue;
		this.style.color = '#999999';
		n.style.display = 'none';
	}
}
i.oncontextmenu = function(){clear(i);}
i.onkeypress = function(ev)
{
    clear(i);
	ev = ev || window.event;	
	if (n.style.display == '' && ev.keyCode == 13)
	{
		return false;
	}
}
i.onkeyup=Hint;
i.onchange=GetSuggestion;
n.onclick = function(ev)
{
	ev = ev || window.event;
	var target = ev.target || ev.srcElement;
	//clear(target);
	var index = parseInt(target.parentNode.getAttribute('index'));
	if (index > 0)
	{
		i.value=text=target.parentNode.cells[0].innerHTML;
	}
	n.style.display = 'none';
}
function Hint(ev)
{
	ev = ev || window.event;
	if (__IsInput(ev))
	{
		if (n.style.display == '')
		{

			if (ev.keyCode == 13)
			{
				//enter
				if(n.rows.length < 2)
				{
					//this.value=text= "";
					this.value=text= this.value;
				}
				else
				{
					this.value=text=n.rows[index].cells[0].innerHTML;
				}
				
				n.style.display = "none";
				return false;
			}
			else if (ev.keyCode == 40)
			{
				//down
				if (n.rows.length > 1)
				{
					n.rows[index].className = 'item';					
					index ++;
					if (index == n.rows.length) index = 1;					
					n.rows[index].className = 'itemFocus';
				}	
			}
			else if (ev.keyCode == 38)
			{
				//up
				if (n.rows.length > 1)
				{				
					n.rows[index].className = 'item';
					index --;
					if (index < 1) index = n.rows.length - 1;					
					n.rows[index].className = 'itemFocus';
				}
			}
			else if (ev.keyCode == 27)
			{
				//esc
				n.style.display = 'none';
			}
	
		}
		else
		{
		 	if (evkeyCode == 40)
			{
				//down
				n.style.display = '';
				SetPos();
			}	
		}
	}
	else
	{
		if (this.value != text)
		{
			text = this.value;
			if (text != '')
			{
				//Xml.Request('Xml.aspx?key=' + escape(this.value), null, ShowHint);
				Suggestion.GetAjax().OpenXml('soufun_forum/Interface/ExtendWord.aspx?q=' + escape(this.value),ShowHit,n);
			}
			else
			{
				for (var i=n.rows.length-1; i>0; i--)
				{
					n.tBodies[0].removeChild(n.rows[i]);
				}
			}
		}
	}
}

function GetSuggestion(ev)
{
	//debugger;
	ev = ev || window.event;
	if (this.value != text)
		{
			text = this.value;
			if (text != '')
			{
				//Xml.Request('Xml.aspx?key=' + escape(this.value), null, ShowHint);
				Suggestion.GetAjax().OpenXml('soufun_forum/Interface/ExtendWord.aspx?q=' + escape(this.value),ShowHit,n);
			}
			else
			{
				for (var i=n.rows.length-1; i>0; i--)
				{
					n.tBodies[0].removeChild(n.rows[i]);
				}
			}
		}
}

function AddRow(text)
{
	var tr = document.createElement('TR');
	tr.setAttribute('index', n.rows.length);
	tr.onmouseover = function()
	{
		var i = parseInt(this.getAttribute('index'));
		if (i != index)
		{
			if (index != -1) n.rows[index].className = 'item';
			this.className = 'itemFocus';
			index = i;
		}
	}
	var td = document.createElement('TD');
	td.innerHTML = text;
	td.style.color = '#333333';
	tr.appendChild(td);
	n.tBodies[0].appendChild(tr);
}
function ShowHint()
{
    //debugger;	
	for (var i=n.rows.length-1; i>0; i--)
	{
		n.tBodies[0].removeChild(n.rows[i]);
	}
	var xml = new XmlSource(this, 'hit');
	for (var i=0; i<xml.hit.length; i++)
	{
		AddRow(xml.hit[i].text.data);
	}
		
	if (n.rows.length > 1)
	{
		n.style.display = '';
		SetPos();
		index = 1;
		n.rows[1].className = 'itemFocus';
	}
}
function ShowHit(state,node,n)
{
if(state)
{
	for (var i=n.rows.length-1; i>0; i--)
	{
		n.tBodies[0].removeChild(n.rows[i]);
	}
	var hits=node.getElementsByTagName("hit")
	for (var i=0; i<hits.length; i++)
	{
		AddRow(NodeRead(hits[i],"text"));
	}
		
	if (n.rows.length > 1)
	{
		n.style.display = '';
		SetPos();
		index = 1;
		n.rows[1].className = 'itemFocus';
	}
}
}

var bodies = [document, document.body, document.documentElement];
for (var k=0; k<bodies.length; k++)
{
	if (bodies[k].onclick == null)
	{
		bodies[k].onclick = function(ev)
		{
			ev = ev || window.event;
			var target = ev.target || ev.srcElement;
			clear(target);
//			var list=Suggestion.List;
//			var len=list.length;
//			for(var j=0;j<len;j++)
//			{
//			    var div=list[j].div;
//			    var input=list[j].i;
//			    if (div.style.display == '' && target != input)
//			    {				
//				    div.style.display = 'none';
//			    }
//			}			
		}
		break;
	}
}
function clear(i)
{
    var list=Suggestion.List;
    var len=list.length;
    for(var j=0;j<len;j++)
    {
        var div=list[j].div;
        var input=list[j].i;
        if (div.style.display == '' && input != i)
        {				
	        div.style.display = 'none';
        }
    }
}
//拼接
function StrMaker(){
this.arr = new Array();
this.str = function(){return this.arr.join('');}
this.add = function(){this.arr.push.apply(this.arr, arguments);}
this.size = function(w,h){if(w>0){this.add(' width="',w,'"');if(h>0){this.add(' height="',h,'"');}}}
this.csssize = function(w,h){if(w>0){this.add('width:',w,'px;');if(h>0){this.add('height:',h,'px;');}}}
this.iframe = function(name,w,h){	
	this.add('<iframe name="',name,'" id="',name,'" frameborder="0" style="');
	this.csssize(w,h);
	this.add('padding:0;margin:0;border:0;"></iframe>');
}
}
//插入元素
function insertAfter(newEl, targetEl){
var parentEl = targetEl.parentNode;
if(parentEl.lastChild == targetEl){
    parentEl.appendChild(newEl);
}else{
    parentEl.insertBefore(newEl,targetEl.nextSibling);
}            
}

//获得对象位置
function GetPos(node){
var t=0,l=0;
var obj = node;
if(typeof(node)==String){obj=document.getElementById(node);}
do
{
	t+= top(obj);
	l+= left(obj);
	obj=eval("obj.offsetParent");
}while (obj.tagName != "BODY");
this.left=l;
this.top=t;

//左
function left(obj){
return obj.offsetLeft;
}
//上
function top(obj){
return obj.offsetTop;
}
}
}
Suggestion.List=new Array();
Suggestion.Ajax=null;
Suggestion.GetAjax=function(){
var ajax=Suggestion.Ajax;
if(ajax==null){Suggestion.Ajax=ajax=new AjaxRequest();}
return ajax;
}

