function g(id) {
	return document.getElementById(id);
}
function showPrompt(id, shows) {
	var prompt = g(id);
	prompt.style.display = shows;
}
function showPrompt(str) {
	try {
//		g("prompt").innerHTML = str;
//		window.setTimeout(hiddenPrompt, 4000);
	} catch (e) {
	}
}
function hiddenPrompt() {
	//g("prompt").style.display = "none";
	//g("prompt").innerHTML = "";
}
// 创建数组对象
function StringBuffer() {
	this.__strings__ = new Array();
}
// 向数组的末尾添加一个或更多元素，并返回新的长度。
StringBuffer.prototype.append = function(str) {
	this.__strings__.push(str);
}
// 删除元素，并向数组添加新元素。
StringBuffer.prototype.insert = function(pos, str) {
	this.__strings__.splice(pos, 0, str);
}
// 把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。
StringBuffer.prototype.toString = function() {
	return this.__strings__.join("");
}

// *******************************************************************************
// 去掉空格
// *******************************************************************************
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
// *******************************************************************************
// 去掉左边空格
// *******************************************************************************
String.prototype.ltrim = function() {
	return this.replace(/(^\s*)/g, "");
};
// *******************************************************************************
// 去掉右边空格
// *******************************************************************************
String.prototype.rtrim = function() {
	return this.replace(/(\s*$)/g, "");
};
var reload = true;
function showComments() {
	// alert("aaa1")
	var commentDiv = g("commentDiv");
	// alert("aaa2")
	if (!isV) {
		g("reply_validate").style.display = "none";
	}
	// commentDiv.style.display = (commentDiv.style.display ==
	// "none")?"block":"none";
	if (g("releaseDiv")) {
		g("releaseDiv").style.display = (g("releaseDiv").style.display == "none") ? "block"
				: "none";
	}
	if (reload) {
		showComment(1);
		reload = false;
	}
}

function showCommentInPage(data) {
	if (data == null || data == "null" || data.length == 0)
		return;
	var allComments = g("allComments");
	if (allComments) {
		allComments.innerHTML = "";
	}
	var strInner = "";
	for (i = 0; i < data.length - 2; i++) {
		var cmt = data[i];
		if (allComments) {
			var tempName = cmt["username"];
			if(cmt['nicename']){
				tempName = cmt["nicename"];
			}else{
				tempName = "<a target='_blank' href='http://www.dxy.cn/bbs/user/info?uname="+tempName+"'>"+tempName+"</a>";
				
			}
			var innerLi = document.createElement("LI");
			innerLi.innerHTML = '<h3 class="hh_comment">'+cmt["str_creatd"]+' 网友：' + tempName + ' 发表评论：</h3><p>'
					+ cmt["body"] + '</p>';
			allComments.appendChild(innerLi);
		}
	}
	if (allComments) {
		var allcPageBuilder = g("allcPageBuilder");
		allcPageBuilder.innerHTML = data[data.length - 2];
		if(g("comment_num"))
			g("comment_num").innerHTML = "("+data[data.length-1]+")";
	}
	var releaseDiv = g("releaseDiv");
	if(g('submitting'))
		g("submitting").style.display = "none";
	releaseDiv.style.display = "block";
	// load信息提示
	showPrompt("");
}
// IP隐藏处理
function proIp(ip) {
	return ip.replace(ip.substring(ip.indexOf(".", 4), ip.length), ".*.*");
}

// 取消对评论的回复
function cancelReply(sel) {
	try {
		sel.parentElement.style.display = "none";
	} catch (e) {
		sel.parentNode.style.display = "none";
	}
}

// 按ctrl+enter提交
function ctrlComment(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
		if (evt.keyCode == 13 && evt.shiftKey) {
			showPrompt("");
			submitComment();
		}
	}
}

function ctrlComment1(evt, txt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
		if (evt.keyCode == 13 && evt.shiftKey) {
			reply(txt);
		}
	}
}

// 匿名提交
var nicknames = "";
function anonymous(chk) {
	if (g("nickname").value.length > 0)
		nicknames = g("nickname").value;
	if (chk.checked) {
		g("nickname").value = "";
	} else {
		g("nickname").value = nicknames;
	}
}

function showReply(btn) {
	try {
		// firefox
		var parent = btn.parentNode.parentNode;
		var replyDiv = parent.childNodes[parent.childNodes.length - 1];
		replyDiv.style.display = "block";
	} catch (e) {
		// IE
		var parent = btn.parentElemente.parentElement;
		var replyDiv = parent.children[parent.children.length - 1];
		replyDiv.style.display = "block";
	}
}

function delComment(btn) {
	if (!owner) {
		alert("操作没有获得相应的权限!");
		return;
	}
	if (window.confirm("确认删除评论")) {
		var commentId = getCommentId(btn);
		CommentUtils.delComment(commentId, limit, showCommentInPage);
	}
}

function getCommentId(btn) {
	try {
		// firefox
		var parent = btn.parentNode.parentNode;
		return parent.childNodes[0].value;
	} catch (e) {
		// IE
		var parent = btn.parentElement.parentElement;
		return parent.children[0].value;
	}
}
// *******************************************************************************
// cas返回值设置userName
// *******************************************************************************
function initUserLongin(user, userEncode) {
	userName = user;
	userNameEncode = userEncode;
}
// *******************************************************************************
// topline显示控制
// *******************************************************************************
function showMenu() {
	var out = new StringBuffer();
	if (userName == "0") {
		out.append("<a href=\"" + ssoUrl + "/login?service=" + root
				+ "/portal/user/index.do?done=" + url
				+ "\" title=\"用户登陆\" class=\"butt2\">登录</a> | ");
		out
				.append("<a href=\""
						+ ssoUrl
						+ "/account/register.do?service="
						+ root
						+ "/portal/user/index.do\" title=\"新用户注册\" class=\"butt2\">注册</a>");
	} else {
		out.append("您好，");
		out.append("" + userName + "");
		out.append("<a title=\"退出\"  href=\"" + ssoUrl + "/logout?service="
				+ root + "/portal/user/logout?done=" + url + "\">退出</a> | ");
		out.append("<a title=\"我的帐户\" target=\"_blank\" href=\"" + ssoUrl
				+ "/account/viewUser.do?username=" + userName
				+ "\">我的帐户</a> | ");
		out
				.append('<a href="http://www.dxy.cn/bbs/user/myforum">我的论坛</a> | <a href="http://www.dxy.cn/bbs/user/edit">设置</a> | <a href="http://www.dxy.cn/bbs/user/message">站内短信</a>');
	}
	document.getElementById("foma").innerHTML = out;
}
// *******************************************************************************
// topline显示控制
// *******************************************************************************
function showWelecom() {
	var out = new StringBuffer();
	if (userName == "0") {
		out.append("<span id='nichen'>昵称:"+userName+"</span>");
		out.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		out.append("请<a href=\"" + ssoUrl + "/login?service=" + root
				+ "/portal/user/index.do?done=" + done
				+ "\" title=\"登陆\" >登录</a>");
		out.append("或<a href=\"" + ssoUrl + "/account/register.do?service="
				+ root + "/portal/user/index.do?done=" + done
				+ "\" title=\"注册\" class=\"butt2\">注册</a>后，发表留言");
		if(g('welcome'))
			document.getElementById("welcome").innerHTML = out;
		displayNiceInput();
	} else {
		out.append("<span id='nichen'>昵称:"+userName+"</span>");
		out.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		out.append("<a title=\"匿名发表评论\"  href=\"javascript:void(0)\" onclick=\"displayNiceInput()\">匿名发表评论</a>");
		out.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		out.append("<a title=\"退出\"  href=\"" + ssoUrl + "/logout?service="
				+ root + "/portal/user/logout?done=" + done + "\">退出</a>");
		displayValidator();//隐藏验证码；
		if(g('welcome'))
			document.getElementById("welcome").innerHTML = out;
	}
}
// *******************************************************************************
// 评论控制
// *******************************************************************************
function showComment(pge) {
	showPrompt("prompt", "block");

	CommentUtils.showComment(objectId, objectType, pge, limit, approved,
			showCommentInPage);
	if (userName != "0") {
		DWRUtil.setValue("nickname", userName);
	}
}
function submitComment() {
	if ((DWRUtil.getValue("body")).trim().length == 0) {
		alert('请输入评论信息');
		return;
	}
	if (DWRUtil.getValue("nickname").length > 32) {
		alert("昵称长度不能超过20个字符");
		return;
	}
	if (DWRUtil.getValue("body").length > 1000) {
		alert("评论长度不能超过1000个字符");
		return;
	}
	var isV = userName == "0" || userName.length == 0 ? true : false;// 是否需要验证码
	if (isV && DWRUtil.getValue("validateCode").length == 0) {
		alert("请输入验证码");
		return;
	}
	showPrompt("submitting", "block");
	isV = false;//取消教研；
	CommentUtils.isValidateCodeRight(DWRUtil.getValue("validateCode"), isV,
			function(isRight) {
				if (isRight) {
					try {
						CommentUtils.submitComment2(objectId, objectType, DWRUtil.getValue("body"), userName, DWRUtil.getValue("nickname"),
					limit, title, url, approved, showCommentInPage);
		} catch (e) {
			alert("评论发送失败!");
			return;
		}
		alert("您的评论将于近期审核！感谢您的支持与理解！");
		DWRUtil.setValue("validateCode", "");
		DWRUtil.setValue("body", "");
		changeValidateImage();
		showPrompt("submitting", "none");
	} else {
		alert("验证码不正确!");
		changeValidateImage();
		showPrompt("prompt", "none");
		showPrompt("submitting", "none");
	}
}	);
}

function saveNewFriend(friendId, friendName) {
	MessageUtils.isLogin( function(data) {
		if (data) {
			alert("您还没有登录.请先登录");
		} else {
			MessageUtils.isExists(friendId, function(data2) {
				if (data2) {
					alert("此用户已经是您的联系人");
				} else {
					MessageUtils.saveNewFriend(friendId, friendName, function(
							data) {
						alert('添加成功');
					});
				}
			});
		}
	});
}

window.onload = function() {
	javascript: dwr.engine.setAsync(true);
}
var count = 1;
function changeValidateImage() {
	document.getElementById("validatePic").src = root + "/jcaptcha?v=" + count;
	count += 1;
}
//隐藏验证码；
function displayValidator(){
	if(g('validator'))
		g('validator').style.display="none";
}
//显示匿名输入框
function displayNiceInput(){
	if(g('nichen'))
		g('nichen').innerHTML = "昵称:<input type='text' id='nickname' name='nickname' value='游客' size=10 maxsize=10/>";
}
