var viewTbody;

function confirmClear() {
	if(confirm('您要删除搜索历史记录么?'))
    	clearSearchHistory('searchhistory');
}

function addSearchHistory(keyword, degree){
	keyword = encodeURIComponent(keyword);
	var xmlHttp = create_http_request();
	xmlHttp.open("GET","/searchhistory/searchhistory_add.htm?keyword=" + keyword + "&degree=" + degree + "&timestamp=" + new Date().getTime(), true);
	xmlHttp.send(null); 
}

function create_http_request(){
	var xml_http;
	if(window.ActiveXObject){
		xml_http = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xml_http = new XMLHttpRequest();
	}
	return xml_http;
}

function loadSearchHistory(tableId){
	viewTbody = document.getElementById(tableId);
	var xmlHttp = create_http_request();
	xmlHttp.open("GET","/searchhistory/searchhistory_list.htm?timestamp=" + new Date().getTime(), true);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){      
			if(xmlHttp.status == 200){
				var list = eval('(' + xmlHttp.responseText + ')'); 
				var innerHtml = "";
				if(list!=undefined && list.length!=undefined && list.length>0){
					for(var i=0; i<list.length; i++){
						var keyword = list[i].keyword;
						var degree = list[i].degree;
						innerHtml += "<li class=\"con_txt\"><a href='/search/?keyword="+encodeURIComponent(keyword)+"' target='_blank' title='"+keyword+"' class='f12_b'><center>"+ keyword + "</center></a></li><li class=\"con_txt\"><center>大约有：<font style=\"font-size:12px; color:#CC0000; \">"+degree+"</font>条结果</center></li>";
					}
					innerHtml += "<li class=\"con_txt\"><center><input type=\"button\"  value=\"清空\" onclick=\"confirmClear()\" /></center></li>";
				} else {
					innerHtml += "<li class=\"con_txt\"><center>无记录</center></li>";
				}
				viewTbody.innerHTML = innerHtml;
			}
		}
	};
	xmlHttp.send(null); 
}

function clearSearchHistory(tableId) {
	var xmlHttp = create_http_request();
	xmlHttp.open("GET","/searchhistory/searchhistory_clear.htm?timestamp=" + new Date().getTime(), true);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){      
			if(xmlHttp.status == 200){
				viewTbody = document.getElementById(tableId);
				if (viewTbody != undefined) {
					viewTbody.innerHTML = "<li class=\"con_txt\"><center>无记录</center></li>";
				}
			}
		}
	};
	xmlHttp.send(null); 	
}