function createXMLHttp() {
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	} else if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	} 
	throw new Error("XMLHttp object could be created.");
}
function sendRequest(action, isendpage, templatepath) {
	var oForm = document.getElementById("postform");
	if (!isendpage || !isendpage == true)
	{
		oForm.submit();
		return;
	}
	var sBody = getRequestBody(oForm);
	var oXmlHttp = createXMLHttp();
	oXmlHttp.open("post", (action && action != '') ? action : oForm.action, true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				saveResult(oXmlHttp.responseXML, templatepath);
				oForm.replysubmit.disabled = false;
				if (document.getElementById("reloadvcade"))
				{
					document.getElementById("reloadvcade").click();
				}
				//bind current post;
			} else {
				alert("An error occurred: " + oXmlHttp.statusText);
			}
		}
	};
	oXmlHttp.send(sBody);
}
function getRequestBody(oForm) {
	var aParams = new Array();
	for (var i=0 ; i < oForm.elements.length; i++) {
		if (oForm.elements[i].type == "checkbox" && oForm.elements[i].checked == false)
		{
			continue;
		}
		var sParam = encodeURIComponent(oForm.elements[i].name);
		sParam += "=";
		sParam += encodeURIComponent(oForm.elements[i].value);
		aParams.push(sParam);
	}
	return aParams.join("&");
}

function ajaxctlent(event, objfrm, topicid, isendpage, templatepath) {
	if(postSubmited == false && (event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83)) {
		if (!$("postsubmit").disabled)
		{
			if (validate(objfrm, false, false)) 
			{
				sendRequest('tools/ajax.aspx?topicid=' + topicid + '&t=quickreply', isendpage, templatepath);			
			}
		}
		else
		{
			alert('正在提交, 请稍候...');
		}
		
	}
}

function getStars(n, t, path) {
	var s = '';
	for (var i = 3; i > 0; i--) {
		var level = parseInt(n / Math.pow(t, i-1));
		n = n % Math.pow(t, i-1);
		for (var j = 0; j < level; j++) {
			s += '<img src="templates/' + path + '/images/star_level' + i + '.gif" />';
		}
	}
	return s;
}

function getInPostad(index){

	try{
		if (inpostad){
				var adstr = '';
				adstr += "<div class=\"line category\"><div style='float: left;'>[广告]&nbsp;</div><div>";
				var tempstr = inpostad[index];
				var ad = tempstr.split("\\r\\n");
				for (var i = 0; i < ad.length; i++)
				{
					adstr += ("\r\n" + ad[i]);
				}
				adstr += "\r\n</div></div>";
				return adstr;
			}
		}
	catch(e){}
	return "";
}
String.prototype.trim = function(){return this.replace(/(^[\s\t\xa0\u3000]+)|([\u3000\xa0\s\t]+$)/g, "")};
function saveResult(doc, templatepath)
{
	var err = doc.getElementsByTagName('error');
	if (err[0] != null && err[0] != undefined)
	{
		if (err[0].childNodes.length > 1) {
		    alert(err[0].childNodes[1].nodeValue);
		} else {
		    alert(err[0].firstChild.nodeValue);    		
		}
		return;
	}


	var invisible = getSingleNodeValue(doc, 'invisible');
	if (invisible == 1)
	{
		alert("回复成功，请等待审核");
		return;
	}

	var ismoder = getSingleNodeValue(doc, 'ismoder');
	var adindex = getSingleNodeValue(doc, 'adindex');
	var status = getSingleNodeValue(doc, 'status');
	var stars = getSingleNodeValue(doc, 'stars');
	var id = getSingleNodeValue(doc, 'id');
	var fid = getSingleNodeValue(doc, 'fid');
	
	var ip = getSingleNodeValue(doc, 'ip');
	var lastedit = getSingleNodeValue(doc, 'lastedit');
	var layer = getSingleNodeValue(doc, 'layer');
	var message = escape(getSingleNodeValue(doc, 'message'));
	var parentid = getSingleNodeValue(doc, 'parentid');
	var pid = getSingleNodeValue(doc, 'pid');
	var postdatetime = getSingleNodeValue(doc, 'postdatetime');
	var poster = getSingleNodeValue(doc, 'poster');
	var posterid = getSingleNodeValue(doc, 'posterid');
	var smileyoff = getSingleNodeValue(doc, 'smileyoff');
	var topicid = getSingleNodeValue(doc, 'topicid');
	var title = getSingleNodeValue(doc, 'title');
	var usesig = getSingleNodeValue(doc, 'usesig');
	var uid = getSingleNodeValue(doc, 'uid');
	var accessmasks = getSingleNodeValue(doc, 'accessmasks');
	var adminid = getSingleNodeValue(doc, 'adminid');
	var avatar = getSingleNodeValue(doc, 'avatar');
	var avatarheight = getSingleNodeValue(doc, 'avatarheight');
	var avatarshowid = getSingleNodeValue(doc, 'avatarshowid');
	var avatarwidth = getSingleNodeValue(doc, 'avatarwidth');
	var credits = getSingleNodeValue(doc, 'credits');
	var digestposts = getSingleNodeValue(doc, 'digestposts');
	var email = getSingleNodeValue(doc, 'email');
	var score1 = getSingleNodeValue(doc, 'score1');
	var score2 = getSingleNodeValue(doc, 'score2');
	var score3 = getSingleNodeValue(doc, 'score3');
	var score4 = getSingleNodeValue(doc, 'score4');
	var score5 = getSingleNodeValue(doc, 'score5');
	var score6 = getSingleNodeValue(doc, 'score6');
	var score7 = getSingleNodeValue(doc, 'score7');
	var score8 = getSingleNodeValue(doc, 'score8');
	var scoreunit1 = getSingleNodeValue(doc, 'scoreunit1');
	var scoreunit2 = getSingleNodeValue(doc, 'scoreunit2');
	var scoreunit3 = getSingleNodeValue(doc, 'scoreunit3');
	var scoreunit4 = getSingleNodeValue(doc, 'scoreunit4');
	var scoreunit5 = getSingleNodeValue(doc, 'scoreunit5');
	var scoreunit6 = getSingleNodeValue(doc, 'scoreunit6');
	var scoreunit7 = getSingleNodeValue(doc, 'scoreunit7');
	var scoreunit8 = getSingleNodeValue(doc, 'scoreunit8');
	var extcredits1 = getSingleNodeValue(doc, 'extcredits1');
	var extcredits2 = getSingleNodeValue(doc, 'extcredits2');
	var extcredits3 = getSingleNodeValue(doc, 'extcredits3');
	var extcredits4 = getSingleNodeValue(doc, 'extcredits4');
	var extcredits5 = getSingleNodeValue(doc, 'extcredits5');
	var extcredits6 = getSingleNodeValue(doc, 'extcredits6');
	var extcredits7 = getSingleNodeValue(doc, 'extcredits7');
	var extcredits8 = getSingleNodeValue(doc, 'extcredits8');
	var extgroupids = getSingleNodeValue(doc, 'extgroupids');
	var gender = getSingleNodeValue(doc, 'gender');
	var bday = getSingleNodeValue(doc, 'bday');
	var icq = getSingleNodeValue(doc, 'icq');
	var joindate = getSingleNodeValue(doc, 'joindate');
	var lastactivity = getSingleNodeValue(doc, 'lastactivity');
	var medals = getSingleNodeValue(doc, 'medals');
	var nickname = getSingleNodeValue(doc, 'nickname');
	var oltime = getSingleNodeValue(doc, 'oltime');
	var onlinestate = getSingleNodeValue(doc, 'onlinestate');
	var showemail = getSingleNodeValue(doc, 'showemail');
	var signature = getSingleNodeValue(doc, 'signature');
	var sigstatus = getSingleNodeValue(doc, 'sigstatus');
	var skype = getSingleNodeValue(doc, 'skype');
	var website = getSingleNodeValue(doc, 'website');
	var yahoo = getSingleNodeValue(doc, 'yahoo');
	var qq = getSingleNodeValue(doc, 'qq');
	var msn = getSingleNodeValue(doc, 'msn');
	var posts = getSingleNodeValue(doc, 'posts');
	var footerad = getSingleNodeValue(doc, 'ad_thread1');
	var topad = getSingleNodeValue(doc, 'ad_thread2');
	var rightad = getSingleNodeValue(doc, 'ad_thread3');

	var theLocation = getSingleNodeValue(doc, 'location');

	var showavatars = getSingleNodeValue(doc, 'showavatars');
	var userstatusby = getSingleNodeValue(doc, 'userstatusby');
	var starthreshold = getSingleNodeValue(doc, 'starthreshold');
	var forumtitle = getSingleNodeValue(doc, 'forumtitle');
	var showsignatures = getSingleNodeValue(doc, 'showsignatures');
	var maxsigrows = getSingleNodeValue(doc, 'maxsigrows');
	var enablespace = getSingleNodeValue(doc, 'enablespace');
	var enablealbum = getSingleNodeValue(doc, 'enablealbum');

	var container = document.getElementById("postsContainer");
	var divDetailnav = document.createElement("DIV");
	var divDetailnav2 = document.createElement("DIV");
	var divmenu = document.createElement("DIV");

	container.appendChild(divDetailnav);
	container.appendChild(divDetailnav2);
	container.appendChild(divmenu);

	divDetailnav.className = 'viewthread';
	divDetailnav2.className = 'forumdetailline';
	divmenu.className = 'menuwindow';
	divmenu.id = 'memberinfo_' + id + '_menu';
	divmenu.style.display = 'none';
	//divmenu.style.width = '177px';
	
	var html = '';
	html += '		<table cellpadding="0" cellspacing="0" border="0">';
	html += '			<tbody>';
	html += '			<tr>';
	html += '			<td class="postauthor" id="' + pid + '">';
	html += '				<div id="memberinfo_' + id + '" onmouseover="showMenu(this.id,false)">';
	html += '					<div class="normalname" onmouseover="this.className=\'normalnameon\'" onmouseout="this.className=\'normalname\'">';
	html += '						<img src="templates/' + templatepath + '/images/friend_dot.gif" align="absmiddle" title="在线" />';
	html +=	poster;
	html += '					</div>';
	html += '				</div>';
	html += '				<div class="avatarphoto">';
	if (avatar != '' && showavatars == '1')
	{
		html += '	<img class="avatar" src="' + avatar + '"';
			if (parseInt(avatarwidth) > 0)
			{
				html += '	width="' + avatarwidth + '"';
				html += '	height="' + avatarheight + '"';
			}
		html += '	/>';
	}
	html += '				</div>';
	if (nickname != "")
	{
		html += '			<div class="nickname">';
		html += nickname;
		html += '			</div>';
	}
	html += '				<p>';
	html += getStars(stars, starthreshold, templatepath);
	html += '				</p>';
	html += '			<div class="profile">';
	html += '				<ul>';
	if (enablespace == 1 || enablealbum == 1)
	{
		html += '						<li class="otherproduct">';
	}
	
	if (enablespace == 1)
	{
		html += '<strong><a href="space/?uid=' + posterid + '">个人空间</a></strong>';
	}
	
	if (enablealbum == 1)
	{
		html += '<em><a href="showalbumlist.aspx?uid=' + posterid + '">相册</a></em>';
	}

	if (enablespace == 1 || enablealbum == 1)
	{
		html += '</li>';
	}
	if (userstatusby == 1)
	{
		html += '				<li>组别:<span>' + status +'</span></li>';
	}
	html += '					<li>性别: <span>' + displayGender(gender) + '</span></li>';
	if (bday.trim() != '')
	{
		html += '				<li>生日: <span>' + bday + '</span></li>';
	}
	html += '					<li>来自: <span>' + theLocation+ '</span></li>';
	html += '					<li>积分: <span>' + credits + '</span></li>';
	html += '					<li>帖子: <span>' + posts + '</span></li>';
	html += '					<li>注册: <span>' + new Date(joindate.replace(/-/ig,'/')).format("yyyy-MM-dd")+ '</span></li>';
	html += '				</ul>';
	html += '			</div>';
	html += '			</td>';
	html += '			<td class="postcontent">';
	html += '			<div class="navpostinfo">';
	html += '				<div class="postinfoleft">';
	html += '					<span>' + postdatetime + '</span>';
	html += '					<em>|';
	html += '					</em>';
	html += '				</div>';
	html += '				<div class="postinfoitem">';
	html += '					<a href="showtree.aspx?topicid=' + topicid + '&postid=' + pid + '">树型</a><span class="see-line2">|</span>';
	html += '					<a href="favorites.aspx?topicid=' + topicid + '">收藏</a><span class="see-line2">|</span>';
	html += '					<a href="editpost.aspx?topicid=' + topicid + '&postid=' + pid + '">编辑</a><span class="see-line2">|</span>';
	html += '					<a href="delpost.aspx?topicid=' + topicid + '&postid=' + pid + '" onclick="return confirm(\'确定要删除吗?\');">删除</a>&nbsp;';
	if(ismoder == 1)
	{
		html += '				<input name="postid" id="postid" value="'+pid+'" type="checkbox" />';
	}
	html += '					<a href="###" class="t_number" onclick="$(\'message' + pid + '\').className=\'t_smallfont\'">小</a>';
	html += '					<a href="###" class="t_number" onclick="$(\'message' + pid + '\').className=\'t_msgfont\'">中</a>';
	html += '					<a href="###" class="t_number" onclick="$(\'message' + pid + '\').className=\'t_bigfont\'">大</a>';
	html += '					<span>' + id + '</span>';
	html += '				</div>';
	html += '			</div>';
	html += '			<div id="ad_thread2_' + id + '"></div>';
	html += '			<div class="defaultcontent">';
	html += '				<div class="forumarticle">';
	html += '					<h3>' + title + '</h3>';
	html += '					<div id="message' + pid + '" class="t_msgfont"><div id="ad_thread3_' + id + '"></div>' + unescape(message) + '</div>';
	html += '				</div>';
	html += '			</div>';
	if (usesig == 1 && signature != "" && showsignatures == 1)
	{
		html += '			<div class="postertext">';
		if (maxsigrows > 0)
		{
			var ieheight = maxsigrows*12;
			html += '			<div class="t_signature" style="overflow: hidden; max-height: ' + maxsigrows + 'em;maxHeightIE:'+ieheight+'px;">'+signature+'</div>';
		}
		else
			html += signature;
		html += '			</div>';
	}
	html += '			</td>';
	html += '			</tr>';
	html += '			</tbody>';
	html += '			<tbody>';
	html += '			<tr>';
	html += '			<td class="postauthor threadfoot">';
	html += '				<img src="templates/' + templatepath + '/images/gototop.gif" alt="gototop"  onclick="window.scrollTo(0,0)" class="cursor"/>';
	html += '			</td>';
	html += '			<td class="threadfootright threadfoot">';
	html += '				<div id="ad_thread1_' + id + '" class="forumcontentad"></div>';
	html += '				<div class="forumanswer">';
	html += '					<a href="postreply.aspx?topicid='+topicid+'&postid='+pid+'&quote=yes"><img src="templates/'+templatepath+'/images/button_use.gif" alt="引用"  style="padding:2px;"/></a>';
	html += '					<a href="###" onclick="replyToFloor(\'' + id + '\', \'' + poster + '\', \'' + pid + '\')"><img src="templates/'+templatepath+'/images/dot_replay.gif" alt="回复" style="padding:2px;"/></a>';
	html += '				</div>';
	html += '			</td>';
	html += '			</tr>';
	html += '			</tbody>';
	
	divDetailnav.innerHTML = html;

	divDetailnav2.innerHTML = '&nbsp;';

	html = '					<div class="popupmenu">';
	html += '						<div class="popupmenuitem" style="border-top:none;">';
	html += '							<p class="recivemessage">';
	html += '							<a href="usercppostpm.aspx?msgtoid=' + posterid + '" target="_blank">发送短消息</a></p>';
	html += '						</div>';
	html += '						<div class="popupmenuitem">';
	html += '							<p><a href="userinfo.aspx?userid=' + posterid + '" target="_blank">查看公共资料</a></p>';
	html += '							<p><a href="search.aspx?posterid=' + posterid + '">查找该会员全部帖子</a></p>';
	html += '						</div>';
	html += '						<div class="popupmenuitem">';
	html += '						<ul>';
	html += '						<li>UID:<span>' + posterid + '</span></li>';
	html += '						<li>精华:<span>';
	if (digestposts>0)
	{
		html += '						<a href="search.aspx?posterid=' + posterid + '&type=digest">' + digestposts + '</a>'
	}
	else
	{
		digestposts
	}
	html += '						</span></li>';
	if (score1 != "")
		html += '<li>'+score1 + ':  <span>' + extcredits1 + ' ' + scoreunit1 + '</span></li>';
	if (score2 != "")
		html += '<li>'+score2 + ':  <span>' + extcredits2 + ' ' + scoreunit2 + '</span></li>';
	if (score3 != "")
		html += '<li>'+score3 + ':  <span>' + extcredits3 + ' ' + scoreunit3 + '</span></li>';
	if (score4 != "")
		html += '<li>'+score4 + ':  <span>' + extcredits4 + ' ' + scoreunit4 + '</span></li>';
	if (score5 != "")
		html += '<li>'+score5 + ':  <span>' + extcredits5 + ' ' + scoreunit5 + '</span></li>';
	if (score6 != "")
		html += '<li>'+score6 + ':  <span>' + extcredits6 + ' ' + scoreunit6 + '</span></li>';
	if (score7 != "")
		html += '<li>'+score7 + ':  <span>' + extcredits7 + ' ' + scoreunit7 + '</span></li>';
	if (score8 != "")
		html += '<li>'+score8 + ':  <span>' + extcredits8 + ' ' + scoreunit8 + '</span></li>';
	html += '					</ul>';
	html += '					</div>';
	html += '						<div class="popupmenuitem">';
	html += '						<p>状态:<span>在线</span></p>';
	html += '						<ul class="tools">';
	if (msn !='')
	{
		html += '					<li>';
		html += '						<img src="templates/' + templatepath + '/images/tools-msn.gif" title="MSN Messenger: ' + msn + '"/>';
		html += '						<a href="mailto:' + msn + '" target="_blank">' + msn + '</a>';
		html += '					</li>';
	}
	if (skype !='')
	{
		html += '					<li>';
		html += '						<img src="templates/' + templatepath + '/images/tools-skype.gif" title="Skype: ' + skype + '"/>';
		html += '						<a href="skype:' + skype + '" target="_blank">' + skype + '</a>';
		html += '					</li>';
	}
	if (icq !='')
	{
		html += '					<li>';
		html += '						<img src="templates/' + templatepath + '/images/tools-icq.gif" title="ICQ: ' + icq + '"/>';
		html += '						<a href="http://wwp.icq.com/scripts/search.dll?to=' + icq + '" target="_blank">' + icq + '</a>';
		html += '					</li>';
	}
	if (qq !='')
	{
		html += '					<li>';
		html += '						<img src="templates/' + templatepath + '/images/tools-qq.gif" title="QQ: ' + qq + '" />';
		html += '						<a href="http://wpa.qq.com/msgrd?V=1&Uin=' + qq + '&Site=' + forumtitle + '&Menu=yes" target="_blank">' + qq + '</a>';
		html += '					</li>';
	}
	if (yahoo !='')
	{
		html += '					<li>';
		html += '						<img src="templates/' + templatepath + '/images/tools-yahoo.gif" title="Yahoo Messager: ' + yahoo + '" />';
		html += '						<a href="http://edit.yahoo.com/config/send_webmesg?.target=' + yahoo + '&.src=pg" target="_blank">' + yahoo + '</a>';
		html += '					</li>';
	}
	html += '						</ul>';
	html += '						</div>';

	divmenu.innerHTML = html;

	try
	{		
		document.getElementById("postform").reset();
	}
	catch (e)
	{
		alert(e.message);
	}
	delete doc;
}

function getSingleNodeValue(doc, tagname)
{
	try{
		var oNodes = doc.getElementsByTagName(tagname);
		if (oNodes[0] != null && oNodes[0] != undefined)
		{
			if (oNodes[0].childNodes.length > 1) {
				return oNodes[0].childNodes[1].nodeValue;
			} else {
				return oNodes[0].firstChild.nodeValue;    		
			}
		}
	}
	catch(e){}
	return '';
}
