function set_focus() {
	document.getElementById("entry").focus();
}
function clear_entry() {
	var entry = document.getElementById("entry");
	entry.value = "";
	entry.focus();
}
var xmlHttp;
function wlist_click(div) {
	xmlHttp = createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleDefineStateChange;
	xmlHttp.open("GET", "bs.php?q=" + encodeURIComponent(div.childNodes[0].nodeValue), true);
	xmlHttp.send(null);
}
function handleDefineStateChange() {
	if (xmlHttp.readyState == 4) {
		document.getElementById("definition").innerHTML = xmlHttp.responseText;
	}
}
function doPreviousWord() {
	var pword = document.getElementById("wordlist").childNodes[1].childNodes[0].nodeValue;
	if (pword) {
		xmlHttp = createXMLHttpRequest();
		xmlHttp.onreadystatechange = handlePWordStateChange;
		xmlHttp.open("GET", "previous-backend.php?q=" + encodeURIComponent(pword), true);
		xmlHttp.send(null);
	}
}
function doNextWord() {
	var wordlist = document.getElementById("wordlist");
	var nword = wordlist.childNodes[wordlist.childNodes.length-2].childNodes[0].nodeValue;
	if (nword) {
		xmlHttp = createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleNWordStateChange;
		xmlHttp.open("GET", "next-backend.php?q=" + encodeURIComponent(nword), true);
		xmlHttp.send(null);
	}
}
function handlePWordStateChange() {
	if (xmlHttp.readyState == 4) {
		var wordList = xmlHttp.responseText.split('\n');
		var wordlist = document.getElementById("wordlist");
		var old_elmcount = wordlist.childNodes.length-2;
		for (var w in wordList) {
			var divElmt = document.createElement("div");
			divElmt.onmouseover = function(){this.className='son'};
			divElmt.onmouseout = function(){this.className=''};
			divElmt.onclick = function(){wlist_click(this)};
			divElmt.innerHTML = escapeHTML(wordList[w]);
			wordlist.insertBefore(divElmt, wordlist.childNodes[1]);
		}
		if (old_elmcount > wordList.length) {
			for (var i=0; i< wordList.length; i++)
				wordlist.removeChild(wordlist.childNodes[wordlist.childNodes.length-2]);
		}
	}
}
function handleNWordStateChange() {
	if (xmlHttp.readyState == 4) {
		pcontent = '<div onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="doPreviousWord()"><font color="blue">Previous Words</font></div>';
		var wordList = xmlHttp.responseText.split('\n');
		for (var w in wordList) {
			pcontent += '<div onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="wlist_click(this)">' + escapeHTML(wordList[w]) + "</div>";
		}
		pcontent += '<div onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="doNextWord()"><font color="blue">Next Words</font></div>';
		var wordlist = document.getElementById("wordlist");
		wordlist.innerHTML = pcontent;
	}
}
function analyse_query_type(searchword) {
	if (searchword.charAt(0)=="/") {
		return 1;
	}
	if (searchword.charAt(0)=="|") {
		return 2;
	}
	for (var i=0; i< searchword.length; i++) {
		if (searchword.charAt(i)=="\\") {
			i++;
		} else if (searchword.charAt(i) == "*" || searchword.charAt(i) == "?") {
			return 3;
		}
	}
	return 0;
}
function sendLookupRequest() {
	var searchword = document.getElementById("entry").value;
	xmlHttp = createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", "/zi/zi.php?q=" + encodeURIComponent(searchword), true);
	xmlHttp.send(null);
}
function do_fuzzy_query() {
	var searchword = document.getElementById("entry").value;
	if (!searchword)
		return;
	var type = analyse_query_type(searchword);
	if (type == 0) {
		xmlHttp = createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.open("GET", "/zi/zi.php?q=" + encodeURIComponent(searchword), true);
		xmlHttp.send(null);
	} else {
		sendLookupRequest();
	}
}

<!--zhanjun-->



function dobs(bsid) {
	var searchword =bsid
	if (!searchword)
		return;

		xmlHttp = createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleBsStateChange;
		xmlHttp.open("GET", "/zi/bs.php?q=" + encodeURIComponent(searchword), true);
		xmlHttp.send(null);

}


<!--end-->


var previousWord;
function do_go_back() {
	if (previousWord) {
		var entry = document.getElementById("entry");
		if (entry.value != previousWord) {
			entry.value = previousWord;
			sendLookupRequest();
			previousWord = "";
		}
	}
}
var timeId;
var oldEntryValue;
function on_entry_changed(e) {
	var searchword = document.getElementById("entry").value;
	if (!searchword)
		return;
	if (e.keyCode == 27) {
		previousWord = searchword;
		clear_entry();
		return;
	}
	if (e.keyCode == 13) {
		if (timeId) {
			window.clearTimeout(timeId);
			timeId = false;
		}
		previousWord = searchword;
		sendLookupRequest();
		return;
	}
	if (oldEntryValue == searchword)
		return;
	oldEntryValue = searchword;
	var type = analyse_query_type(searchword);
	if (type == 1) {
		if (searchword.length == 1)
			document.getElementById("definition").innerHTML = "Fuzzy query...";
	} else if (type == 2) {
		if (searchword.length == 1)
			document.getElementById("definition").innerHTML = "Full-text search...";
	} else if (type == 3) {
		document.getElementById("definition").innerHTML = "Press Enter to list the words that match the pattern.";
	} else {
		if (timeId)
			window.clearTimeout(timeId);
		timeId = window.setTimeout(sendLookupRequest, 500);
	}
}
function handleStateChange() {
	if (xmlHttp.readyState == 4) {
		parseResult();
	} else if (xmlHttp.readyState == 1) {
		document.getElementById("definition").innerHTML = "Loading.....";
	}
}






function handleBsStateChange() {
	if (xmlHttp.readyState == 4) {
		document.getElementById("bslist").innerHTML = xmlHttp.responseText;
	} else if (xmlHttp.readyState == 1) {
		document.getElementById("bslist").innerHTML = "Loading.....";
	}
}





function  escapeHTML(str) {
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
}
function parseResult() {
	var i;
	i = xmlHttp.responseText.indexOf("");
	var definition = document.getElementById("definition");
	if (i == -1) {
		definition.innerHTML = xmlHttp.responseText;
	} else {
		var result_definition = xmlHttp.responseText.slice(0, i);
		definition.innerHTML = result_definition;
		if (xmlHttp.responseText.charAt(i+1) == "d" && xmlHttp.responseText.charAt(i+2) == "\n") {
			var result_list = xmlHttp.responseText.slice(i+3, xmlHttp.responseText.length);
			var pcontent = "<ul class=\"fulltext_list\" style='margin-left:0px; padding-left:0px;'>";
			var dictList = result_list.split('\n');
			for (var d in dictList) {
				var wordList = dictList[d].split('\t');
				pcontent += '<li id="pw';
				pcontent += d;
				pcontent += '"><img src="images/minus.gif" OnClick="showHide(\'pw';
				pcontent += d;
				pcontent += '\')"><font color=\"blue\">';
				pcontent += wordList[0];
				pcontent += '</font><ul>';
				for (var w=1; w<wordList.length; w++) {
					pcontent += '<li onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="wlist_click(this)">' + escapeHTML(wordList[w]) + "</li>";
				}
				pcontent += '</ul></li>';
			}
			pcontent += '</ul>';
			var wordlist = document.getElementById("wordlist");
			wordlist.innerHTML = pcontent;
		} else {
			var islist = false;
			if (xmlHttp.responseText.charAt(i+1) == "l" && xmlHttp.responseText.charAt(i+2) == "\n")
				islist = true;
			var pcontent = "";
			var result_list = xmlHttp.responseText.slice(i+3, xmlHttp.responseText.length);
			var wordList = result_list.split('\n');
			if (islist)
				pcontent += '<div onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="doPreviousWord()"><font color="blue">Previous Words</font></div>';
			for (var w in wordList) {
				pcontent += '<div onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="wlist_click(this)">' + escapeHTML(wordList[w]) + "</div>";
			}
			if (islist)
				pcontent += '<div onmouseover="this.className=\'son\'" onmouseout="this.className=\'\'" onclick="doNextWord()"><font color="blue">Next Words</font></div>';
			var wordlist = document.getElementById("wordlist");
			wordlist.innerHTML = pcontent;
		}
	}
}

function openZi(zi){
	
	window.open("/allzi.php?q="+zi);
	
	
	}