var viewTbody;

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 confirmClear() {
	if(confirm('您要删除浏览历史记录么?'))
    	clearViewHistory('viewhistory');
}

function appendClearButton() {
	var lineTr = document.createElement("tr");
	var lineTd = document.createElement("td");
	lineTd.setAttribute("align", "center");
	lineTd.setAttribute("valign", "top");
	lineTd.className = "font5";
	lineTd.innerHTML = "<input type=\"image\" src=\"\/images\/bt_clear.gif\"  onclick=\"confirmClear()\" />";
	lineTr.appendChild(lineTd);
	viewTbody.appendChild(lineTr);
} 

function clearViewHistory(tableId) {
	var xmlHttp = create_http_request();
	xmlHttp.open("GET","/book/viewhistory_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) {
					var line = viewTbody.rows.length;
					for ( var i = line - 1; i > 0; i--) {
						viewTbody.removeChild(viewTbody.rows[i]);
					}
				}
				
				appendNullRecord();
				appendLine();
			}
		}
	};
	xmlHttp.send(null); 	
}

function loadViewHistory(tableId){
	viewTbody=document.getElementById(tableId);
	var xmlHttp=createHttpRequest();
	xmlHttp.open("GET","/book/viewhistory_list.htm?timestamp=" + new Date().getTime(), true);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState == 4){      
			if(xmlHttp.status == 200){
				var list=eval('('+xmlHttp.responseText+')'); 
				appendNullLine();
				if(list!=undefined && list.length!=undefined && list.length>0){
					for(var i=0;i<list.length;i++){
						var id=list[i].id;
						var name=list[i].name;
						var itemTr = document.createElement("tr");
						var itemTd = document.createElement("td");
						itemTd.setAttribute("align", "left");
						itemTd.setAttribute("valign", "middle");
						itemTd.innerHTML = "·<a href='/product/"+ id +"/' target='_blank' title='"+name+"' class='f12_b'>"+ substring(name,13) + "</a>";
						itemTr.appendChild(itemTd);
						viewTbody.appendChild(itemTr);
						appendLine();
					}
					appendClearButton();
				} else {
					appendNullRecord();
					appendLine();
				}
			}
		}
	};
	xmlHttp.send(null); 
}

function appendNullLine() {
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.setAttribute("align", "left");
	td.setAttribute("valign", "top");
	td.innerHTML = "&nbsp";
	tr.appendChild(td);
	viewTbody.appendChild(tr);
}

function appendLine() {
	var lineTr = document.createElement("tr");
	var lineTd = document.createElement("td");
	lineTd.setAttribute("align", "left");
	lineTd.setAttribute("valign", "top");
	lineTd.className = "l_line5";
	lineTd.innerHTML = "&nbsp";
						
	lineTr.appendChild(lineTd);
	viewTbody.appendChild(lineTr);
}

function appendNullRecord() {
	var lineTr = document.createElement("tr");
	var lineTd = document.createElement("td");
	lineTd.setAttribute("align", "center");
	lineTd.setAttribute("valign", "top");
	lineTd.className = "font5";
	lineTd.innerHTML = "无记录";
	lineTr.appendChild(lineTd);
	viewTbody.appendChild(lineTr);
} 

function substring(str,length){
	if(str.length<=length)
		return str;
	else
		return str.substring(0,length)+"...";
}

function addViewHistory(id){
	var xmlHttp=createHttpRequest();
	xmlHttp.open("GET","/book/viewhistory_add.htm?id=" + id + "&timestamp=" + new Date().getTime(), true);
	xmlHttp.send(null); 
}