var searchReq=createAjaxObj();
var searchStr="";
var iKeyNum=-1;
var iKeyCount=0,isIE= (document.all) ? true:false;

function createAjaxObj()
{
	var httprequest=false;
	if(window.XMLHttpRequest)
	{
		httprequest=new XMLHttpRequest();
		if(httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject)
	{
		//IE
		try
		{
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	return httprequest
}

function searchSuggest(e)
{
	if(isIE)
	{
		key=event.keyCode;		
	}
	else
	{
		key=e.which;
	}
	if(key==38)
		{
			keyup();
			return;
		}
		if(key==40)
		{
			keydown();
			return;
		}
	
	iKeyNum=-1;
	if(searchStr!=document.getElementById('txtSearch').value)
	{
	if (document.getElementById('txtSearch').value.length>0)
	{
		searchStr=document.getElementById('txtSearch').value;
		var str=escape(document.getElementById('txtSearch').value);
		url="/action/?s=kw&k="+str+ "&t=" +  new Date().getTime();
		searchReq.open("get",url);
		searchReq.onreadystatechange=handleSearchSuggest;
		searchReq.send(null);	
	}
	else
	{
		document.getElementById("search_suggest").innerHTML="";
		document.getElementById("search_suggest").style.display="none";
	}
}
	
}

function handleSearchSuggest()
{
	if(searchReq.readyState==4)
	{		
			var ss=document.getElementById("search_suggest");		
			ss.innerHTML="";
			s0=searchReq.responseText.length;		
			if (s0>0)
			{
				xmldoc=searchReq.responseXML;	
				var message_nodes=xmldoc.getElementsByTagName("message");
				var n_messages=message_nodes.length;
				iKeyCount=n_messages
				if (n_messages<=0)
				{
					document.getElementById("search_suggest").innerHTML="";
					document.getElementById("search_suggest").style.display="none";
				}
			    else
				{ 
					document.getElementById("search_suggest").style.display="block";
					for (i=0;i<n_messages;i++ )
					{
						var suggest='<div onmouseover="javascript:suggestOver(this);"';	
						suggest+='onmouseout="javascript:sugggestOut(this);"';
						suggest+='onclick="javascript:setSearch(this.innerHTML);"';
						suggest +='class="suggest_link">'+message_nodes[i].getElementsByTagName("text")[0].firstChild.data+'</div>';
						ss.innerHTML +=suggest;
					}				
				}
			}
			else
			{
				document.getElementById("search_suggest").innerHTML="";
				document.getElementById("search_suggest").style.display="none";
			}		
	}
	else
	{
		//alert('网络连接失败');
	}
}

function keyup()
{
	iKeyNum--;
	if(iKeyNum<0)
	{
		iKeyNum=iKeyCount-1;
	}
	fSetKeyOption(iKeyNum);
}
function keydown()
{
	iKeyNum++;
	if(iKeyNum>=iKeyCount)
	{
		iKeyNum=0;
	}
	fSetKeyOption(iKeyNum);
}

function fSetKeyOption(fKeyNum)
{
	objDivList=document.getElementById("search_suggest").getElementsByTagName("div");
	for(fi=0;fi<objDivList.length;fi++)
	{
			objDivList[fi].className='suggest_link';
	}
	objDivList[fKeyNum].className='suggest_link_over';
	document.getElementById("txtSearch").value=objDivList[fKeyNum].innerHTML;
}

function suggestOver(div_value)
{
	objDivList=document.getElementById("search_suggest").getElementsByTagName("div");
	for(fi=0;fi<objDivList.length;fi++)
	{
			objDivList[fi].className='suggest_link';
			if(div_value.innerHTML==objDivList[fi].innerHTML)
			{
				iKeyNum=fi;
			}
	}
	div_value.className='suggest_link_over';
}

function sugggestOut(div_value)
{
  div_value.className='suggest_link';
}

function setSearch(div_value)
{
	if(div_value!="")
	{
   document.getElementById("txtSearch").value=div_value;
   document.getElementById("search_suggest").innerHTML="";
   document.getElementById("search_suggest").style.display="none";
   document.seacrh.submit();
	}
	else
	{
		
   document.getElementById("search_suggest").innerHTML="";
   document.getElementById("search_suggest").style.display="none";
	}
}

function action_search()
{
    if(document.seacrh.w.value=="")
    {
        alert("请输入关键字！");
        return false;
    }
	return true;
}

function check_search()
{
	if(document.getElementById("searchCheck").style.display!="block")
	{
		document.getElementById("searchCheck").style.display="block";
	}
	else
	{
		document.getElementById("searchCheck").style.display="none";
	}
	var obj=document.getElementById("searchCheck").getElementsByTagName("li");
	for(var i=0;i<obj.length;i++)
	{
		obj[i].className="";
	}
}

function searchOverCheck(objs)
{
	var obj=document.getElementById("searchCheck").getElementsByTagName("li");
	for(var i=0;i<obj.length;i++)
	{
		obj[i].className="";
	}
	objs.className="over";
}

function searchCheckSort(s)
{
	if(s==1)
	{
		document.getElementById("searchSort").innerHTML="信息";
		document.getElementById("searchS").value="1"
	}
	if(s==2)
	{
		document.getElementById("searchSort").innerHTML="商家";
		document.getElementById("searchS").value="2"
	}
	if(s==3)
	{
		document.getElementById("searchSort").innerHTML="文章";
		document.getElementById("searchS").value="3"
	}
	document.getElementById("searchCheck").style.display="none";
}
