/**For file series_Thread.inc and seriesPostList.jsp;Alex/2008-10-07**/
/************************************登录人角色的判断**************************************/
var bodyUpLmt_T = 4000;
var bodyUpLmt_P = 400;
var titleUpLmt = 50;
var bodyLowLmt = 5;
var validCount = false;
function isAdmin() {
	if (user !== null) {
		for (var i = 0; i < admins.length; i++) {
			if (user.userName == admins[i]) {
				return true;
			}
		}
	}
    return false;
}
function isAuthor() {
	if (user == null) {
		return false;
	}
	return (userId == user.userId);
}
/***************************************内容检查*****************************************/
function replaceByRegExp(elementId){
	var element = $(elementId).val();
	element = element.replace(/(^\s*)|(\s*$)/g, "");
    element = element.replace(/(^　*)|(　*$)/g, "");
    return element;
}
function monitor(obj, msg,who){
		var bodyUpLmt=0;
    if (document.getElementById(obj) != null){
        if(who==1){
        	bodyUpLmt=bodyUpLmt_T;
         var remainLen = bodyUpLmt_T - document.getElementById(obj).value.length;
        }else if(who==2){
        	bodyUpLmt=bodyUpLmt_P;
         var remainLen = bodyUpLmt_P - document.getElementById(obj).value.length;
        }
        if (remainLen >= 0){
            document.getElementById(msg).innerHTML = "&nbsp;您还可以输入<strong class=\"fred\">" + remainLen + "</strong>个字";
            validCount = true;
        }else if(remainLen < 0){
            document.getElementById(msg).innerHTML = "&nbsp;内容不能超出<strong class=\"fred\">" + bodyUpLmt + "</strong>个字";
            validCount = false;
        }
    }
}
function check(titleId,bodyId){
    var title=replaceByRegExp(titleId);
    var body=replaceByRegExp(bodyId);
    if(!validCount){alert("内容的字数不符合要求!");return false;}
	if (title.length == 0||title=="请填写评论标题") {
		//alert("请填写标题!");
		//return false;
		title='';
	}
	if (title.length > titleUpLmt) {
		alert("标题不能超过" + titleUpLmt + "个字符!");
		return false;
	}
	if (body.length == 0) {
		alert("内容不能为空!");
		return false;
	}
	if(body.length < bodyLowLmt){
	    alert("内容不能少于"+bodyLowLmt+"个字符!");
		return false;
	}
	return true;
}

/*************************************显示评论列表函数*************************************/
function showThreadList(seriesId, count, seriesThreadShowQuery) {
    seriesThreadShowQuery.count=count;
    seriesThreadShowQuery.seriesId=seriesId;
	seriesThreadShowQuery.queryUrl = "http://comic.zongheng.com/seriesThread.xml";
	seriesThreadShowQuery.isAuthor=isAuthor;
	seriesThreadShowQuery.isAuthor=isAuthor;
	if (count > 0) {seriesThreadDefaultShow();}
	function seriesThreadDefaultShow() {
		jQuery(function ($) {
			var map = new Map();
			map.put("seriesId", seriesId);
			map.put("pageNum", 1);
			if(isAdmin()) {map.put("user",2);}
			if(isAuthor()){map.put("user",3);}
			if(!isAdmin()&&!isAuthor()){map.put("user",1);}
			seriesThreadShowQuery.querySeriesThreadByParams(map.toUrlString());
		});
	}
}
/*************************************显示评论回复列表函数*********************************************/
function showPostList(threadId, count, seriesPostShowQuery) {
    seriesPostShowQuery.count=count;
    seriesPostShowQuery.threadId=threadId;
	seriesPostShowQuery.queryUrl = "http://comic.zongheng.com/seriesPost.xml";
	if (count > 0) {seriesPostDefaultShow();}
	function seriesPostDefaultShow() {
		jQuery(function ($) {
			var map = new Map();
			map.put("threadId", threadId);
			map.put("pageNum", 1);
			seriesPostShowQuery.querySeriesPostByParams(map.toUrlString());
		});
	}
}
/*************************************新增评论的函数*********************************************/
function addNewSeriesThread(remoteId,seriesId, stamp) {
	if(!check("#title","#body")) return ;
	var title = replaceByRegExp("#title");
    var body = replaceByRegExp("#body");
	$.post("/newSeriesThread.xml", {Action: "post", title: title,body: body,remoteId: remoteId, seriesId: seriesId,userId: userId,stamp: stamp}, 
	function (data) {
		if (data.statusFlag == -1) {
			alert("请先登陆");
			login();
		}
		if (data.statusFlag == -2) {
		    alert(" 提示：抱歉，您已被管理员禁言。\r\n 原因："+data.desc.reason+"\r\n 限期：禁言时间至"+data.desc.expiresTime);
		}
		if (data.statusFlag == 1) {
			alert("发表成功!");
			location.reload();
			$("#title").attr("value", "请填写评论标题");
			$("#body").attr("value", "请填写评论内容");
		}
		if (data.statusFlag == -4) {
			alert("服务器忙,请稍后再试");
		}
		if (data.statusFlag == -3) {
			alert("你已经发表了相同的评论");
		}
		if (data.statusFlag == -5) {
			alert("请间隔几秒钟再发表评论!");
		}
	}, "json");
	return 1;
}
/*************************************新增评论的回复函数*********************************************/
function addNewSeriesPost(remoteId,seriesId,threadId,parentPostId,stamp) {
   /****upLimit:回复贴上限100***/
    var upLimit=100;
    if(count>=upLimit-1) {
       alert("很抱歉,本条评论的回复数已满!"); 
       return;
    }
	if(!check("#title","#body")) return;
	var title = replaceByRegExp("#title");
    var body = replaceByRegExp("#body");
	$.post("/newSeriesPost.xml", {Action: "post", title: title,body: body,remoteId: remoteId,seriesId: seriesId,threadId: threadId,parentPostId: parentPostId,userId: userId,stamp: stamp}, 
	function (data) {
		if (data.statusFlag == -1) {
			alert("请先登陆");
			login();
		}
		if (data.statusFlag == -2) {
		   alert(" 提示：抱歉，您已被管理员禁言。\r\n 原因："+data.desc.reason+"\r\n 限期：禁言时间至"+data.desc.expiresTime);
		}
		if (data.statusFlag == 1) {
			alert("发表成功!");
			location.reload();
			$("#title").attr("value", "请填写回复标题");
			$("#body").attr("value", "请填写回复内容");
		}
		if (data.statusFlag == -4) {
			alert("服务器忙,请稍后再试");
		}
		if (data.statusFlag == -3) {
			alert("你已经发表了相同的回复");
		}
		if (data.statusFlag == -5) {
			alert("请间隔几秒钟再发表回复!");
		}
	}, "json");
	return 1;
}
