document.write('<link rel="stylesheet" type="text/css" href="http://www.xuetang.cn/skin/xuetang/ct/search.css" />');
document.write('<form  name="cmsa_search_show_form" AUTOCOMPLETE="off" ID="cmsa_search_show_form"  action="search.action" method="post" onsubmit="return false;">	');	
document.write('<table  border="0"  cellpadding="0" cellspacing="0">');	
document.write('<tr><td   >');	
document.write('');	
document.write('<input type="text" id="cmsa_search_show_form_txtUserInput" name="cmsa_search_show_form_txtUserInput" style="width:170px;"   value=""   maxlength="30" />');										
document.write('</td>');
document.write('<td >');
document.write('<img  id="searchBt" border="0"  name="searchBt" style="cursor:pointer; margin-left:10px;" src="http://www.xuetang.cn/skin/xuetang/ct/searchbn.gif" onclick="query_clkbt(this)"/>');

document.write('</td>');	
document.write('<td class="recommendKeyword">');

document.write('</td>');	
document.write('</tr>');	
document.write('</table>');	
document.write('</form>');

function query_clkbt(thiz){
    var url="http://cmsa.vnet.cn/search/search.action?txtUserInput="+encodeURI(document.getElementById('cmsa_search_show_form_txtUserInput').value);
    url+="&queryParams.columnId=72";
    url+="&resultTempletId=15";
    url+="&searchCss.id=64";    
    url+="&resultPageId=10"; 
    window.open(url);
	//document.cmsa_search_show_form.submit();
	//thiz.disabled=true;
}

function query2(){
var url="http://cmsa.vnet.cn/search/search.action?txtUserInput="+encodeURI(document.getElementById('cmsa_search_show_form_txtUserInput').value);
    url+="&queryParams.columnId=72";
    url+="&resultTempletId=15";
    url+="&searchCss.id=64";     
    url+="&resultPageId=10";   
    window.parent.location.href=url;
}
function query3(keyword){

	var url="http://cmsa.vnet.cn/search/search.action?txtUserInput="+encodeURI(keyword);
    url+="&queryParams.columnId=72";
    url+="&resultTempletId=15";
    url+="&searchCss.id=64";    
    url+="&resultPageId=10";   
    window.parent.location.href=url;
}
var arrOptions = new Array();

var strLastValue = "";

var theTextBox;

var currentValueSelected = -1;

var req;
var ch='0';

window.onload = function()
{
	
	var elemSpan = document.createElement("span");
	elemSpan.id = "spanOutput";
	elemSpan.className = "spanTextDropdown";
	//alert("document.getElementById('cmsa_search_show_form_txtUserInput')--"+document.getElementById('cmsa_search_show_form_txtUserInput'));
	//alert("document.cmsa_search_show_form--"+document.cmsa_search_show_form);
	
    theTextBox = document.getElementById('cmsa_search_show_form_txtUserInput');
	document.body.appendChild(elemSpan);	
	
	document.getElementById('cmsa_search_show_form_txtUserInput').onpropertychange = GiveOptions;
	document.getElementById('cmsa_search_show_form_txtUserInput').onkeyup = keyup;
		
	if(navigator.userAgent.indexOf("Firefox")>0){
		//alert('Firefox');
	    document.getElementById('cmsa_search_show_form_txtUserInput').addEventListener("input",GiveOptions,false);	  
	}
	
	document.getElementById('cmsa_search_show_form_txtUserInput').onblur = HideTheBox;
	ch='1';
}
function keyup(evt){
	//alert('keyup--event.keyCode=='+window.event.keyCode);
	 var intKey = -1;	
	 evt = (evt) ? evt : ((window.event) ? window.event : "")

    
     intKey = evt.keyCode ? evt.keyCode : (evt.which ? evt.which :evt.charCode);

	if(theTextBox.value.length == 0)
	{
		HideTheBox();
		strLastValue = "";
		return false;
	}

	if(intKey == 13)
	{
		GrabHighlighted();
		HideTheBox();
		document.getElementById('searchBt').focus();
		query2();	
		return false;
	}else if(intKey == 38){
		//alert('ok-up');
		MoveHighlight(-1);
		return false;
	}else if(intKey == 40){
		//alert('ok-down');
		MoveHighlight(1);
		return false;
	}

}
function GiveOptions()
{
		if( ch=='1' && theTextBox.value!=null && theTextBox.value!="" )
		{			
			strLastValue = theTextBox.value;
			loadContent(theTextBox.value);
		}
		
}
function loadContent(xStrText)
{

var s=document.createElement('SCRIPT');
s.src='http://cmsa.vnet.cn/search/ajax_tool.jsp?f=setDivContent&keyword='+encodeURI(xStrText)+'&charset='+'GB2312';
//alert('loadContent---s.src=='+s.src);
document.body.appendChild(s);
}

function setDivContent(v)
{
//alert("?");
arrOptions =v.split(',');	  
BuildList(theTextBox.value);
}

function TypeAhead2(xStrText)
{
	var url = "http://cmsa.vnet.cn/search/findKeywordList.do?txtUserInput=" + encodeURI(xStrText);
	//alert('url='+url);
	if(window.XMLHttpRequest){
          req = new XMLHttpRequest();
	}else if(window.ActiveXObject){
          try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e){
                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                }catch (e) {}
            }
	}

	if(req){
		req.open("GET", url, true);
		//alert('url='+url);
		req.onreadystatechange=callback;
		req.send(null);
	}
}


function callback()
{
		
arrOptions =AjaxCrossDomainResponse.split(',');	  
BuildList(theTextBox.value);
		
}

function parseMessage()
{
	
        var xmlDoc = req.responseXML.documentElement;
   
        var node = xmlDoc.getElementsByTagName('info');

        arrOptions = new Array();
       
	for(var i=0; i < node.length; i++)
	{	
		arrOptions[i] = node[i].firstChild.nodeValue;
		//alert('arrOptions[i]='+arrOptions[i]);
	}
	BuildList(theTextBox.value);
	strLastValue = theTextBox.value;
}

/*
@param theText 
*/
function BuildList(theText)
{
	SetElementPosition();  
	var inner = "";
	var theMatches = MakeMatches(theText); 
	for(var i = 0 ; i < theMatches.length ; i++)
	{
		inner += theMatches[i];
		//alert('inner1='+inner);
	}

	if(theMatches.length > 0)
	{
		document.getElementById("spanOutput").innerHTML = inner;
		
		document.getElementById("OptionsList_0").className = "spanHighElement";
		currentValueSelected = 0;
	}else{
		HideTheBox();
	}
	
	//alert('document.getElementById("spanOutput").innerHTML='+document.getElementById("spanOutput").innerHTML);
}


function SetElementPosition()
{
  	//alert('SetElementPosition');
	var selectedPosX = 0;
	var selectedPosY = 0;
	var theElement = document.getElementById('cmsa_search_show_form_txtUserInput');
	var theTextBoxInt = document.getElementById('cmsa_search_show_form_txtUserInput');
	if(!theElement) return;
	var theElementHeight = theElement.offsetHeight;
	var theElementWidth = theElement.offsetWidth-2;
	//alert('SetElementPosition--theElementHeight='+theElementHeight+'----theElementWidth='+theElementWidth);
	while(theElement != null){
		selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}

	xPosElement = document.getElementById("spanOutput");

	document.getElementById("spanOutput").style.left = selectedPosX+"px";
	document.getElementById("spanOutput").style.width = theElementWidth+"px";
	document.getElementById("spanOutput").style.top = selectedPosY+theElementHeight+"px";
	document.getElementById("spanOutput").style.display = "block";
	
	//alert('document.getElementById("spanOutput").className='+document.getElementById("spanOutput").className);
	//alert('selectedPosX='+selectedPosX+'----selectedPosY='+selectedPosY);
	//alert('document.getElementById("spanOutput").style='+document.getElementById("spanOutput").style);
	//alert('document.getElementById("spanOutput").style.left='+document.getElementById("spanOutput").style.left+'document.getElementById("spanOutput").style.top='+document.getElementById("spanOutput").style.top);
	//alert('left='+xPosElement.style.left+'----top='+xPosElement.style.top);
}


var countForId = 0;


function MakeMatches(xCompareStr)
{
	countForId = 0;
	var matchArray = new Array();
	var regExp = new RegExp(xCompareStr,"ig");
	for(var i=0 ; i < arrOptions.length ; i++)
	{
		if(arrOptions[i].search(regExp) >= 0 )
		{
			matchArray[matchArray.length] = CreateUnderline(arrOptions[i],xCompareStr,i);
		}
		else
		{
			continue;
		}
	}

	return matchArray;
}



var undeStart = "<span class='spanMatchText' >";
var undeEnd  = "</span>";

var selectSpanStart = "<span style='width:100%;display:block;'  class='spanNormalElement'  onMouseover='SetHighColor(this)' ";
var selectSpanEnd = "</span>";


function CreateUnderline(xStr,xTextMatch,xVal)
{
	//selectSpanMid = "onmousedown='SetTextAndSearch("+ xVal +")'" + " id='OptionsList_" + countForId + "' theArrayNumber='"+ xVal +"'>";
	selectSpanMid = "onmousedown='SetTextAndSearch("+ xVal +")'  " +" id='OptionsList_" + countForId + "' theArrayNumber='"+ xVal +"'>";
	countForId++;
	var regExp = new RegExp(xTextMatch,"ig");
	var start = xStr.search(regExp);
	var matchedText = xStr.substring(start,start + xTextMatch.length);
	var Replacestr = xStr.replace(regExp,undeStart + matchedText + undeEnd);

	return selectSpanStart + selectSpanMid + Replacestr + selectSpanEnd;
}

function SetTextAndSearch(xVal){
//alert('xVal='+xVal);
theTextBox = document.getElementById('cmsa_search_show_form_txtUserInput');
	theTextBox.value = arrOptions[xVal];
	document.getElementById("spanOutput").style.display = "none";
	currentValueSelected = -1;	
	document.getElementById('searchBt').focus();
	query2();	
	
}


function SetHighColor(theTextBox)
{
//alert('document.getElementById("spanOutput").innerHTML='+document.getElementById("spanOutput").innerHTML);
//alert('document.getElementById("OptionsList_0")='+document.getElementById("OptionsList_0"));
	if(theTextBox)
	{
		currentValueSelected = theTextBox.id.slice(theTextBox.id.indexOf("_") + 1,theTextBox.id.length);
	}

	for(var i = 0 ; i<countForId ; i++)
	{
		document.getElementById('OptionsList_' + i).className = 'spanNormalElement';
	}
	document.getElementById('OptionsList_' + currentValueSelected).className = 'spanHighElement';
	//theTextBox = document.getElementById('cmsa_search_show_form_txtUserInput');
	//theTextBox.value = arrOptions[currentValueSelected];
}


function SetText(xVal)
{
	theTextBox = document.getElementById('cmsa_search_show_form_txtUserInput');
	theTextBox.value = arrOptions[xVal];
	document.getElementById("spanOutput").style.display = "none";
	currentValueSelected = -1;
}

function GrabHighlighted()
{
	if(currentValueSelected >=0 )
	{
		xVal = document.getElementById("OptionsList_" + currentValueSelected).getAttribute("theArrayNumber");
		SetText(xVal);
		HideTheBox();
	}
}


function HideTheBox()
{
	document.getElementById("spanOutput").style.display = "none";		
	currentValueSelected = -1;
}


function MoveHighlight(xDir)
{
	if(currentValueSelected >= 0)
	{
		newValue = parseInt(currentValueSelected) + parseInt(xDir);
		if(newValue > -1 && newValue < countForId)
		{
			currentValueSelected = newValue;
			SetHighColor(null);
		}

	}
}

function ReDraw()
{
	BuildList(document.getElementById('cmsa_search_show_form_txtUserInput').value);
}

















