function validate(theform, previewpost, switcheditormode) {
	var message = !theform.parseurloff.checked ? parseurl(theform.message.value) : theform.message.value;

	if (theform.title.value == "" || message == "") {
		alert("请完成标题和内容栏。");
		$("postsubmit").disabled = false;
		return false;
	} else if (theform.title.value.length > 60) {
		alert("您的标题超过 60 个字符的限制。");
		theform.title.focus();
		$("postsubmit").disabled = false;
		return false;
	}

	if(!disablepostctrl && ((postminchars != 0 && mb_strlen(message) < postminchars) || (postmaxchars != 0 && mb_strlen(message) > postmaxchars))) {
		alert("您的帖子长度不符合要求。\n\n当前长度: "+mb_strlen(message)+" 字节\n系统限制: "+postminchars+" 到 "+postmaxchars+" 字节");
		return false;
	}

	if (!switcheditormode && !previewpost) {
		$("postsubmit").disabled = true;
	}

	theform.message.value = message;
	return true;

}


function gotopage(topicid,index){
	page = parseInt(document.getElementById("divInput_pagenumber_" + topicid).value);
	if (is_ie){
		curpage = parseInt(document.getElementById("div_curpage_" + topicid).innerText);
		total = parseInt(document.getElementById("div_Totalpage_" + topicid).innerText);
	}
	else{
		curpage = parseInt(document.getElementById("div_curpage_" + topicid).textContent);
		total = parseInt(document.getElementById("div_Totalpage_" + topicid).textContent);
	}
	if (!(parseInt(page)==page && page>0)){
		alert("页码无效");
		return false;
	}
	
	if (!(parseInt(curpage)==curpage && curpage>0)){
		curpage = page;
	}
	
	if (!(parseInt(total)==total && total>0)){
		alert("总页码无效");
		return false;
	}
	
	if (curpage>total || curpage < 1){
		alert("页码无效");
		return false;
	}
	
	
	switch (index){
		case 1:
			if (page-1<1){
				alert("已经是第一页");
				return false
			}
			document.getElementById("divFollow_" + topicid + "_" + curpage).style.display="none";
			document.getElementById("divFollow_" + topicid + "_" + (curpage-1)).style.display="block";
			document.getElementById("divInput_pagenumber_" + topicid).value = curpage - 1;
			break;
		case 2:
			if (page+1>total){
				alert("已经到达最后一页");
				return false
			}
			document.getElementById("divFollow_" + topicid + "_" + curpage).style.display="none";
			document.getElementById("divFollow_" + topicid + "_" + (parseInt(curpage) +1)).style.display="block";
			document.getElementById("divInput_pagenumber_" + topicid).value = parseInt(curpage) + 1;
			break;
		case 3:
			if (page>total || page<1){
				alert("页码应在 1-" + total + " 之间");
				return false
			}
			document.getElementById("divFollow_" + topicid + "_" + curpage).style.display="none";
			document.getElementById("divFollow_" + topicid + "_" + page).style.display="block";
			break;
	}
	if (is_ie){
		document.getElementById("div_curpage_" + topicid).innerText = document.getElementById("divInput_pagenumber_" + topicid).value;
	}
	else{
		document.getElementById("div_curpage_" + topicid).textContent = document.getElementById("divInput_pagenumber_" + topicid).value;
	}
}

function writetree(obj,topicid,ppp){
	var td = document.getElementById("tdTopicFollow" + topicid);

	var err = obj.getElementsByTagName('error');
	if (err[0] != null && err[0] != undefined)
	{
		var errdiv = document.createElement("DIV");
		errdiv.className='errcontext';

	    if (err[0].childNodes.length > 1) {
		    errdiv.innerHTML = '<div style="margin: 0px 15px 0px 40px;">' + err[0].childNodes[1].nodeValue + '</div>';
		} else {
		    errdiv.innerHTML = '<div style="margin: 0px 15px 0px 40px;">' + err[0].firstChild.nodeValue + '</div>';    		
		}

		td.appendChild(errdiv);
		document.getElementById("trTopicFollow" + topicid).style.display = "";
		document.getElementById("divShowContext_" + topicid).style.display = "none";
		return;
	}

	var dataArray = obj.getElementsByTagName('post');
	var dataArrayLen = dataArray.length;
	
		var page = 0;
		var divIsShow = "block";
		for(i=0;i<dataArrayLen;i++){
			page ++;
			div = document.createElement("DIV");
			div.id = "divFollow_" + topicid + "_" + page;
			div.style.display = divIsShow;
			div.style.lineHeight = "180%";
			var list = "";
			for (j=0;j<10;j++){
				list += "<li><a href=\"showtopic-";
				list += topicid;
				if (ppp>0){
					list += "-";
					list += Math.ceil((i+2)/ppp);
				}
				list += ".aspx#";
				list += dataArray[i].getAttribute("pid");
				list +="\">";
				list += dataArray[i].getAttribute("message").substring(0,50);
				list += "</a>&nbsp; --- &nbsp; (";
				if (dataArray[i].getAttribute("poster")!="游客")
				{
					list += "<a target=\"_blank\" href=\"userinfo.aspx?userid=";
					list += dataArray[i].getAttribute("posterid");
					list += "\">";
					list += dataArray[i].getAttribute("poster");
					list += "</a> 发表于 ";
				}
				else
				{
					list += "游客 发表于";
				}
				
				list += dataArray[i].getAttribute("postdatetime");
				list += ")</li>";
				i++;
				if (i>=dataArrayLen){
					break;
				}
			}
			if ( list != "" )
			{
			
				i--;
				
			}
			div.innerHTML = list;
			divIsShow = "none";
			td.appendChild(div);
		}
		div = document.createElement("DIV");
		div.id = "divFollow_page_" + topicid;
		div.style.marginTop = "5px"
		div.innerHTML = "页数:<span id=\"div_curpage_" + topicid + "\">1</span>/<span id=\"div_Totalpage_" + topicid + "\">" + page + "</span>&nbsp;&nbsp;&nbsp;<span onclick=\"gotopage(" + topicid + ",1);\" style=\"cursor:pointer;\" title=\"上一页\"><img src=\"templates/" + templatepath + "/images/prev.gif\" /></span>&nbsp;<input type=\"text\" id=\"divInput_pagenumber_" + topicid + "\" value=\"1\" size=\"3\" style=\"text-align:center\" class=\"colorblur\" onKeyDown=\"if(event.keyCode==13) { gotopage(" + topicid + ",3); return false; }\">&nbsp;<span onclick=\"gotopage(" + topicid + ",2);\" style=\"cursor:pointer;\" title=\"下一页\"><img src=\"templates/" + templatepath + "/images/next.gif\" /></span>"
		td.appendChild(div);
		document.getElementById("trTopicFollow" + topicid).style.display = "";
		document.getElementById("divShowContext_" + topicid).style.display = "none";

}

function showtree(topicid,ppp){
	var imgsrcCol = "templates/" + templatepath + "/images/topItem_col.gif";
	var imgsrcExp = "templates/" + templatepath + "/images/topItem_exp.gif"

	if (!document.getElementById("trTopicFollow" + topicid)){
		var tr = document.getElementById("trTopic" + topicid);
		var table = document.getElementById("category_" + fid);
			tr = table.insertRow(tr.rowIndex+1);
			tr.id="trTopicFollow" + topicid;
			tr.className = "row"
			//tr.style
			td = tr.insertCell(-1);
			td.innerHTML = "&nbsp;";
			td = tr.insertCell(-1);
			td.innerHTML = "&nbsp;";
			td =  tr.insertCell(-1);
			td.id = "tdTopicFollow" + topicid;
			td.colSpan = 5;
			div = document.createElement("DIV");
			div.id = "divShowContext_" + topicid;
			div.innerHTML = "正在生成回复列表,请稍等...";
			td.appendChild(div);
			ajaxRead("tools/ajax.aspx?t=topictree&topicid=" + topicid, "writetree(obj," + topicid + "," + ppp + ");");
	}
	
	if (document.getElementById("imgButton_" + topicid).src.indexOf(imgsrcCol)!=-1){	
		document.getElementById("imgButton_" + topicid).src = imgsrcExp;
		document.getElementById("imgButton_" + topicid).alt = "展开帖子列表";
		document.getElementById("imgButton_" + topicid).title = "展开帖子列表";

		document.getElementById("trTopicFollow" + topicid).style.display = "none";
	}
	else{
		document.getElementById("imgButton_" + topicid).src = imgsrcCol;
		document.getElementById("imgButton_" + topicid).alt = "关闭帖子列表";
		document.getElementById("imgButton_" + topicid).title = "关闭帖子列表";
		if (document.getElementById("trTopicFollow" + topicid)){
			document.getElementById("trTopicFollow" + topicid).style.display = "";
		}
	}
	
}


//计算指定时间与当前时间的小时差 ---- 胡尊杰 2007-11-8 15:18
function DiffNow(fctNow){
	if(fctNow==""){
		return 0;
	}else{
		var varYY = fctNow.split("-")[0];
		var varMN = parseInt(fctNow.split("-")[1],10)-1;
		var varDA = fctNow.split("-")[2].split(" ")[0];
		var varHH = fctNow.split(" ")[1].split(":")[0];
		var varMU = fctNow.split(":")[1];
		var varSS = fctNow.split(":")[2];
		var varNow=new Date(varYY,varMN,varDA,varHH,varMU,varSS);
		return ((new Date()-varNow)/1000/60/60); //返回当前时间-fctNow 的差
	}
}

//判断是否显示最近更新图标 ---- 胡尊杰 2007-11-8 15:18
function CheckShowFirst(fctTopicId,fctLastPostTime){
	if(DiffNow(fctLastPostTime)<24&&fctLastPostTime!=""){ //如果最近24小时内回复过则显示
		document.write('<a href="showtopic.aspx?topicid='+fctTopicId+'&amp;page=end#bottom"><img src="templates/default/images/firstnew.gif" align="absmiddle" border=0 title="24小时内回复"/></a>');
	}
}