//发布话题
var topic = new Object();
//回复话题
var reply = new Object();

function checkVerifyCode(vercode,verifyurl,errorid)
{	
	if(vercode == "")
	{
		$('#'+errorid).html("请输入验证码");
		return false;
	}
	else
	{
		$('#'+errorid).html("");
	}
	
	return $.ajax({
		type: 'POST',
		url: verifyurl,
		async: false,
		data: "vercode="+vercode,
		success: function(msg){
			if(msg != "")
			{
				$('#'+errorid).html("验证码输入错误");
			}
			else
			{
				$('#'+errorid).html("");
			}
		}
	});
}
topic.post_topic = function(url,verifyurl){
	var c_id = $('#category').val();
	var report_c_id = $('#report_cateogory').val();
	var topic_title = $('#topic_title').val();
	var topic_content = $('#topic_content').val();
	if(topic_title=='' && (report_c_id=='all' || c_id=='all') && topic_content==''){
		$('#post-error').html('话题标题不能为空，未选择分类，话题内容不能为空');
		return false;
	}else if((report_c_id=='all' || c_id=='all') && topic_title!='' && topic_content!=''){
		$('#post-error').html('未选择分类');
		return false;
	}else if(topic_content=='' && topic_title!='' && (report_c_id!=0 || c_id!=0)){
		$('#post-error').html('话题内容不能为空');
		return false;
	}else if(topic_title=='' && (report_c_id!=0 || c_id!=0) && topic_content!=''){
		$('#post-error').html('话题标题不能为空');
		return false;
	}else if((report_c_id=='all' || c_id=='all') && topic_title!='' && topic_content==''){
		$('#post-error').html('未选择分类，话题内容不能为空');
		return false;
	}else if(topic_title=='' && (report_c_id!=0 || c_id!=0) && topic_content==''){
		$('#post-error').html('话题标题不能为空,话题内容不能为空');
		return false;
	}else{
		$('#post-error').html('');
	}
	if(topic_content.length > 1000){
		$('#post-error').html('话题内容不能为空且字数不能超过1000字');
		return false;
	}
	
	var vercode = $('#vercode').val();	
	checkVerifyCode(vercode,verifyurl,'verify-error');	
	if($('#verify-error').html() != "")
	{
		return false;
	}
	
	$('#post-error').html('正在加载请稍后....');
	$('.b-button').hide();
	var category_id = report_c_id.indexOf('_1')>0 ? 1 : 0;
	report_c_id = report_c_id.indexOf('_1')>0 ? report_c_id.replace('_1','') : report_c_id;
	var filepath = $('#filepath').val().replace(/\s/ig,'');
	$.ajax({
		type: 'POST',
		url: url,
		data: "report_c_id="+report_c_id+"&c_id="+c_id+"&topic_title="+topic_title+"&topic_content="+topic_content+"&category_id="+category_id+"&filepath="+filepath,
		dataType:'text',
		success: function(msg){		
			if(msg==1){
				document.location = 'user.php';
			}else{
				$('#post-error').html('发布失败请重试');
				$('.b-button').show();
				return false;
			}
		}
	});	
	return false;
}
/**
*
*  回复当前文章
*/
reply.send = function(url,verifyurl,topic_id, page, page_size, page_last_num){
	var content = $('#message_content').val();
	var filepath = $('#filepath').val();
	if(content.length<10){
		$('#post-error').html('评论内容不能少于十个字符!<br />');
		return false;
	}else if(content.length > 1000){
		$('#post-error').html('回复内容不能为空且字数不能超过1000个字');
		return false;
	}
	else
	{
		$('#post-error').html("");
	}
	
	var vercode = $('#vercode').val();	
	checkVerifyCode(vercode,verifyurl,'verify-error');	
	if($('#verify-error').html() != "")
	{
		return false;
	}
	
	$('.b-s-button').remove();
	$('#post-error').html('正在加载，请稍后.....');
	//$('.b-s-button').addClass('loading');
	$.ajax({
		type: 'POST',
		url: url,
		data: "content="+content+"&topic_id="+topic_id+"&page="+page+"&page_size="+page_size+"&page_last_num="+page_last_num+"&filepath="+filepath,
		dataType: 'json',
		success: function(json){
			var url = document.location.href;
			var redirect = url.replace(/&page=[0-9]*/i, '');
			redirect = redirect.replace(/#new_reply/i, '');
			var page = json.page_last_num == 0 ? parseInt(json.page) + 1 : json.page;
			//$('#loading').html('');
			//$('#loading').removeClass('loading');
			document.location = redirect+'&page='+page;
		}
	});
	return false;

}
/**
*
*  回复评论
*
*/
reply.comment = function(reply_id, link_url, user_id, topic_id, url,codeurl,verifyurl){
	if($('#comment_'+reply_id).length ==0){
		var comment_id = 'comment_'+reply_id;
		var scomment_id = 'commentbox_'+reply_id;
		var comment_click =  'a_'+reply_id;
		var cancel = 'c_'+reply_id;
		$('#reply_'+reply_id).append(
			'<div class="new-reply-box" id="commentbox_'+reply_id+'">'+
			'<textarea rows="4" cols="45" id="'+comment_id+'"></textarea>'+
			'<div class="verify-line"> 验证码:'+
				'<img src="'+codeurl+'"/>'+
				'<input type="text" maxlength="5" id="vercode'+reply_id+'" />'+
				'<span class="errormsg" id="verify-error'+reply_id+'"></span>'+
			'</div>'+
			'<input type="button" class="b-button" id = "'+comment_click+'" value="回复留言" />'+
			'<input type="button" class="b-s-button" onclick="javascript:;" id="'+cancel+'" value="取 消"/>'+
			'</div>');
		//$('#reply_'+reply_id).append('<div><div class="commentbox" id="commentbox_'+reply_id+'"><textarea id="'+comment_id+'" rows="4" cols="40"></textarea><input id = "'+comment_click+'" type="button" value="回复留言"  src="'+link_url+'/images/color_button_bg_4.gif"><input type="button" class="bt-cancle" onclick="javascript:;" id="'+cancel+'" value=" 取 消"/></div> <div class="hackbox"> </div>');

	}
	$('#'+comment_click).click(function(){
		var vercode = $('#vercode'+reply_id).val();	
		checkVerifyCode(vercode,verifyurl,'verify-error'+reply_id);	
		if($('#verify-error'+reply_id).html() != "")
		{
			return false;
		}		
		reply.comment.process(reply_id, user_id, topic_id, url, getcomment(comment_id));
	});
	$('#'+cancel).click(function(){
		reply.cancel(reply_id);
	});
	return false;
}
function getcomment(comment_id){
	return $('#'+comment_id).val();
}
reply.cancel = function(reply_id){
	$('#commentbox_'+reply_id).remove();
	return false;
}
/**
*
* 处理回复评论
*/
reply.comment.process = function(reply_id, user_id, topic_id, url, comment){
	if(comment.length > 240){
		alert('回复内容不能为空且字数不能超过240个字');
		return false;
	}
	$('#commentbox_'+reply_id).remove();
	$.ajax({
		type: 'POST',
		url: url,
		data: "content="+comment+"&topic_id="+topic_id+"&reply_id="+reply_id,
		dataType:'json',
		success: function(json){
			var username = json.user_name;
			$('#reply_comment_'+json.reply_id).append('<div class="rp2" id="commentbox_'+json.reply_comment_id+'"><div class="rp-list-l"><div class="user-head image-outer"><div class="image-inner"><img width="50"  height="50" src="'+json.image+'" /></div></div></div><div class="rp-list-r"><div class="line"><a href="">'+username+'</a> 在 '+json.date+' 回复：</div><div class="reply-con"><p>'+json.content+'<span class="gl"><button title="删除回复" class="di" onclick="if(confirm(\'确定要删除吗？\'))return moderator.del_reply_comment('+json.reply_comment_id+')"></button></span></p></div></div><div class="hackbox"></div></div>');
		}
	});
	return false;
}
function check_page(){
	var patrn  =/^[0-9]/;
	var page_num = $('#page_num').val();
	if(patrn.test(page_num)==false){
		alert('页码为数字，请输入正确的页码！');
		return false;
	}


}
