function zsxz(ta, zs, maxl){
	if(ta.value.length > maxl)
	{
		ta.value = ta.value.substring(0,maxl);
	}
	else
	{
		zs.value = maxl - ta.value.length;
	}
}
function strLen(key){
	var l=escape(key),len
	len=l.length-(l.length-l.replace(/\%u/g,"u").length)*4
	l=l.replace(/\%u/g,"uu")
	len=len-(l.length-l.replace(/\%/g,"").length)*2
	return len
}
//处理浏览器，用来选择不同的XML读取函数
var agt = navigator.userAgent.toLowerCase();
var is_opera = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_opera;
var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all;
var uniqnum_counter = (new Date).getTime();
var is_regexp = (window.RegExp) ? true : false;
var xmlhttp = null;
function UniqueNum() {  
	++uniqnum_counter;  
	return uniqnum_counter;
}
//创建Get请求
function StartGETRequest(url, handler){
	xmlhttp = null;
	if (is_ie) {    
		var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
		try {      
			xmlhttp = new ActiveXObject(control);
		} catch(e) {
			alert("You need to enable active scripting and activeX controls");
			DumpException(e);
		}
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function() {handler();}
	if (url.indexOf("?") != -1){
		var urltemp = url + "&rand=" + UniqueNum();
	} else {
		var urltemp = url + "?rand=" + UniqueNum();
	}
	//alert(urltemp);
	xmlhttp.open('GET', urltemp, true);
	
	xmlhttp.send(null);	
}
//处理方法
function GetObjValue(objName){
	if(document.getElementById){
		return eval('document.getElementById("' + objName + '")');
	}else{
		return eval('document.all.' + objName);
	}
}
function fetchObject(idname){
	if (document.getElementById) {
		return document.getElementById(idname);
	} else if (document.all) {
		return document.all[idname];
	} else if (document.layers) {
		return document.layers[idname];
	} else {
		return null;
	}
}
function fetchTags(parentobj, tag){
	if (typeof parentobj.getElementsByTagName != 'undefined') {
		return parentobj.getElementsByTagName(tag);
	} else if (parentobj.all && parentobj.all.tags) {
		return parentobj.all.tags(tag);
	} else {
		return null;
	}
}
function fetchXmlValue(parentobj, tag){
	try{
		var tags = fetchTags(parentobj, tag);
		return tags[0].firstChild.nodeValue;
	} catch(e) {
		return null;
	}
}
function getCommentHtmlList(qid, aid){
	var strText;
	var fError = false;
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){			
			strText = fetchXmlValue(xmlhttp.responseXML,'body');
			if (null == strText) {
				fError = true;
			} else {
				fetchObject("commentsec" + qid + aid).innerHTML = strText;
			}
		}else{
			fError = true;
		}
	}

	if (fError) {
		fetchObject("commentsec" + qid + aid).innerHTML = "";			
		fetchObject("comment" + qid + aid).style.display = "none";
	} else {
		fetchObject("comment" + qid + aid).scrollIntoView(true);
	}
}
function submitComment(qid, aid)
{
	var strText;
	var fnServerError = DisplayPostCommentError(qid, aid);
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var res = fetchXmlValue(xmlhttp.responseXML, 'result');
			strText = fetchXmlValue(xmlhttp.responseXML, 'body');
			if (null == strText) {
				fnServerError();
			} else {
				if (res.toLowerCase() == 'ok') {				
					fetchObject("commentsec" + qid + aid).innerHTML = strText;
					var t_total = fetchXmlValue(xmlhttp.responseXML, 'total');
					if (t_total > 0)
					{
						fetchObject("commenttotal" + qid + aid).innerHTML = "评论<b>"+t_total+"</b><img id=\"commentimg"+qid+aid+"\"src=\"../img/wd/img2_jj.gif\" border=\"0\" hspace=\"3\">";
					}
					else
					{
						fetchObject("commenttotal" + qid + aid).innerHTML = "评论";
					}					
				} else {
					fetchObject("Err" + qid + aid).innerHTML = "<table cellspacing=3 class='bvError'><tr><td width=15 valign=top><img src='../img/wd/error_icon.gif' width=15 height=16 border=0></td><td height=20 width=100%>"+ strText +"</td></tr></table>";
				}
			}
		}else{
			fnServerError();
		}
	}
}
function OpenSection(qid, aid, pages){
	var sectionTitleObj = fetchObject("comment" + qid + aid);
	if (pages >= 0)
	{
	}
	else
	{
		if (sectionTitleObj.style.display == "block")
		{
			Close(qid, aid, pages);
			return ;
		}
	}
	if (sectionTitleObj.style.display == 'none' || sectionTitleObj.style.display == '') {
		sectionTitleObj.style.display = "block";
		try{
			fetchObject("commentimg" + qid + aid).src = "../img/wd/img2_jj.gif";
		}catch(e){};
	}
	if (pages >= 0) {
		var strA = "questionid="+ qid;
		strA += "&answerid=" + aid;
		strA += "&pages=" + pages;
		StartGETRequest(web_url+'Hxask/Comment.asp?'+strA, function() {getCommentHtmlList(qid, aid)});
	} else {		
		if(fetchObject("commentsec" + qid + aid).innerHTML == "") {
			var strA = "questionid="+ qid;
			strA += "&answerid=" + aid;
			StartGETRequest(web_url+'Hxask/Comment.asp?'+strA, function() {getCommentHtmlList(qid, aid)});
		} 
	}
}
function Close(qid, aid, pages){
	fetchObject("comment" + qid + aid).style.display = "none";
	try{
		fetchObject("commentimg" + qid + aid).src="../img/wd/img2_jh.gif";
	}catch(e){};
	if (pages > 0) {
		fetchObject("commentsec" + qid + aid).innerHTML = "";
	}
}
function CloseComment(qid, aid)
{
	if (fetchObject("err"+ qid + aid) != null) {
		fetchObject("Err"+qid + aid).innerHTML = "";
		fetchObject("txtComment"+qid + aid).value = "";
	}
	Close(qid, aid);
}
function DisplayPostCommentError(qid, aid){
	return function(){
		document.getElementById("Err"+qid+aid).innerHTML = "<table cellspacing=3 class='bvError'><tr><td width=15 valign=top><img src='../img/wd/error_icon.gif' width=15 height=16 border=0></td><td height=20 width=100%>服务器出现问题。请重试。</td></tr></table>";
	}
}
function PostComment(qid, aid){
	var strComment = fetchObject("txtComment"+qid + aid);
	var strError = "";
	nCommentLength = strComment.value.length;
	if (nCommentLength == 0 || nCommentLength > 200 ) {
		if (strError.length == 0) {
			strComment.focus();
		}
	}
	if (nCommentLength == 0) {
		strError += "<tr><td width=15><img src='../img/wd/error_icon.gif' width=15 height=16 border=0></td><td height=20 width=100%>评论不能为空。请输入评论，然后单击“发表评论”。</td></tr>";
	} else if (nCommentLength > 200) {
		strError += "<tr><td width=15><img src='../img/wd/error_icon.gif' width=15 height=16 border=0></td><td height=20 width=100%>您输入的评论太长。请缩短您的评论，然后单击“确定”。</td></tr>";
	}
	if (strError.length > 0)
	{
		document.getElementById("Err" + qid + aid).innerHTML = "<table cellspacing=0 border=0 class='bvError'>" + strError + "</table>";
		return false;
	}
	var newStrComment = strComment.value;
	var strA = "questionid="+ qid;
	strA += "&answerid=" + aid;
	strA += "&do=add";
	strA += "&answer_flag=5";
	strA += "&txtcomment="+newStrComment;
	StartGETRequest(web_url+'HxAsk/Comment.asp?'+strA, function() {submitComment(qid, aid)});
}
