var stauts = 0;

//删除多个话题
function deleteTopic() {
	if (!checkIds('ids')) {
		alert('请至少选择一个话题')
		return ;
	}
	if(confirm("你确定删除话题吗？")){
		var frm = $('topicListFrm');
		var url = '/group2/topic/deletegrouptopic.do';
		var myAjax = new Ajax.Request( url, { method: 'post', parameters: Form.serialize(frm), onComplete:deleteTopicResp});
	}
}
function deleteTopicResp(resp) {
	if (resp.responseText == "0") {
		var ids = document.getElementsByName('ids');
		
		for (var i = 0; ids && i < ids.length; i++) {
			if (ids[i].checked) {
				$('topicDiv' + ids[i].value).style.display = 'none';				
			}
		}
		alert('删除成功');
	}else{
		alert('您不是管理员不能执行该操作！');
	}	
}
function checkIds(idsname) {
	var ids = document.getElementsByName(idsname);
	for (var i = 0; ids && i < ids.length; i++) {
		if (ids[i].checked) {
			return true;
		}
	}
	return false;
}

function uploadblogpic(str){
    if (str == "0") {
        alert("上传失败");
        return;
    }
    if (str == "1") {
        alert("文件扩展名只能是jpg或gif");
        return;
    }
	 if (str == "2") {
        alert("文件不能大于1M");
        return;
    }
    window.frames['Editor'].addhtml(str);
    closeDivDialog('insertPictureArea2');
}

function addpicId(id){
	try{
	    var newInput = document.createElement("input");
	    newInput.type = "hidden";
	    newInput.name = "groupTopicPicIds";
		newInput.value=id
	    $("topicForm").appendChild(newInput);
	}catch(e){
	}
}
function checkFile(ele){
    if (!CheckFileFormat(ele)) {
        alert("请选择扩展名为jpg或gif的图片！")
        return false;
    }
    return true;
}

function CheckFileFormat(ele){
    var fileext = ele.value.substring(ele.value.length - 4, ele.value.length)
    fileext = fileext.toLowerCase()
    if (!(fileext == '.jpg' || fileext == '.gif')) {
        return false;
    }
    else {
        return true;
    }
}

//提交话题
function topicSubmit(){
	var form = $('topicForm');
	if(checkTopicForm()){
		KeyWordFilter(form.title.value.trim());
		if(stauts==2){
			form.submit();
		}
	}	
}
//更新话题
function updateTopicSubmit(){
	var form = $('topicForm');
	if(checkTopicForm()){
		KeyWordFilter(form.title.value.trim());
		if(stauts==2){
			form.submit();
		}
		
	}	
}
function checkTopicForm(){
	var form = document.getElementById("topicForm");
	if(form.title.value.trim().size()==0){
		alert("请输入标题");
		$("title").focus();
		return false;
	}	
	var content = "";
	if(window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none"){
        content = window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML;    
    }else{
        content = window.frames["Editor"].document.getElementById("sourceEditor").value; 
    }
	if(stripContent(content).length < 10){
		alert("文章内容太短,最少10个字符");
		return false;
	}
	document.getElementById("topicForm").content.value = content; 
	return true;
}
function stripContent(str){
		return str.stripTags().replace(/&nbsp;/gi, '').replace(/\[:quote\](\n|\r|.)*\[quote:\]/gi, "").strip();
}

//回复话题
function topicCommentSubmit(groupId){
	var url = '/group2/topic/checklockedtopic.do';
	var topicId = $('groupTopicinfo').value;
	var pars = '&topicId='+topicId+'&groupId='+groupId;
	var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars,onComplete: topicCommentResp});
}

function topicCommentResp(resp){
	if (resp.responseText== 0){
		if(checkTopicCommentForm()){ 
			KeyWordFilter(document.getElementById("topiccommentForm").content.value );
			if(stauts==2){
				var frm = $('topiccommentForm');
				var url = '/group2/topic/savetopiccomment.do';
				var myAjax = new Ajax.Request( url, { method: 'post', parameters: Form.serialize(frm), onComplete:topiccommentResp});
			}
		}	
		
	}else if(resp.responseText== 1){
		alert("该话题已经锁定无法回复");
		
	}else if(resp.responseText== 2){
		alert("您不是圈子成员无法回复");
	}else{
		alert("请您登陆");
	}
}

function topiccommentResp(resp){
	try {
		var res=resp.responseText;
		if (res.indexOf("to_comment_")>-1){
			 document.location=res;
			 return;
		}
	}catch(e){
		 alert("回复失败！")
	}
        alert("回复失败！")
}
function checkTopicCommentForm(){
	var form = document.getElementById("topiccommentForm");
	var content = "";
	if(window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none"){
        content = window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML;    
    }else{
        content = window.frames["Editor"].document.getElementById("sourceEditor").value; 
    }
	if(stripContent(content).length < 10){
		alert("文章内容太短,最少10个字符");
		return false;
	}
	if(content.trim().length > 10000){
		alert("文章内容太长了.达到了"+10000+"个字");
		return false;
	}	
	document.getElementById("topiccommentForm").content.value = content; 
	return true;
}
//加精话题
function isDigest(groupTopicId,isDigest,groupId){
    var url = '/group2/topic/isdigesttopic.do'; 
	var pars = '&groupTopicId='+groupTopicId+'&digest='+isDigest +'&groupId='+groupId
	var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars, onComplete: function(resp){
        	 	if(resp.responseText == 0){
        	 		if(isDigest){
        	 		    $('isDigestDiv').innerHTML='<a href="javascript:isDigest('+groupTopicId+', false,'+groupId+')" class="btnSmallOk">解除加精</a>'
        	 		}else{
        	 		    $('isDigestDiv').innerHTML='<a href="javascript:isDigest('+groupTopicId+', true,'+groupId+')" class="btnSmallOk">加精</a>'
        	 		}
        	 	}else if(resp.responseText == 1){
             		alert("你不是管理员不能执行该操作");
             	}else{
             		alert("请您登陆");
             	}
        	 }});
}

//置顶话题
var isTopval;
var groupIdval;
var TopicIdval
function IsTop(groupTopicId,isTop,groupId){
 	isTopval = isTop;
 	groupIdval = groupId;
 	TopicIdval = groupTopicId;
    var url = '/group2/topic/istoptopic.do'; 
	var pars = '&groupTopicId='+groupTopicId+'&isTop='+isTop +'&groupId='+groupId
	var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars, onComplete: IsTopResp});
}
function IsTopResp(resp){
 	if (resp.responseText== 0){
 		if(isTopval == 0){
 		 $('isTopDiv').innerHTML='<a href="javascript:IsTop('+TopicIdval+',1,'+groupIdval+')" id="potic_top" class="btnSmallOk">置顶</a>'
 		}else{
 		 $('isTopDiv').innerHTML='<a href="javascript:IsTop('+TopicIdval+',0,'+groupIdval+')" id="potic_top" class="btnSmallOk">解除置顶</a>'
 		}
 	}else if(resp.responseText== 1){
 		alert("你不是管理员不能执行该操作");
 	}else{
 		alert("请您登陆");
 	}
}

//锁定话题
var islockedval;
var groupIdlockedval;
var TopicIdlockedval
function IsLocked(groupTopicId,isLocked,groupId){
 	islockedval = isLocked;
 	groupIdlockedval = groupId;
 	TopicIdlockedval = groupTopicId;
    var url = '/group2/topic/islockedtopic.do'; 
	var pars = '&groupTopicId='+groupTopicId+'&isLocked='+isLocked +'&groupId='+groupId
	var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars, onComplete: IslockedResp});
} 
function IslockedResp(resp){
 	if (resp.responseText== 0){
 	 $('topiccommentForm').toggle();
 		if(islockedval == 0){
            $('isLockedDiv').innerHTML='<a href="javascript:IsLocked('+TopicIdlockedval+',1,'+groupIdlockedval+')" id="potic_lock" class="btnSmallOk">锁定</a>'
            $('Relytitle').innerHTML='我来说几句o(∩_∩)o...';
 		}else{
            $('isLockedDiv').innerHTML='<a href="javascript:IsLocked('+TopicIdlockedval+',0,'+groupIdlockedval+')" id="potic_lock" class="btnSmallOk">解除锁定</a>'
            $('Relytitle').innerHTML='该话题已被管理员锁定，无法回复';
 		}
 	}else if(resp.responseText== 1){
 		alert("你不是管理员不能执行该操作");
 	}else{
 		alert("请您登陆");
 	}
 }
//删除单个话题
var delgroupIdval
function deleteSimpleTopic(groupTopicId,groupId){
	if(confirm("你确定删除该话题吗？")){
	  delgroupIdval = groupId;
 	  var url = '/group2/topic/deletesimplegrouptopic.do'; 
	  var pars = '&groupTopicId='+groupTopicId 
	  var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars, onComplete:  deleteSimpleTopicResp});
	}
}
function deleteSimpleTopicResp(resp){
 	if (resp.responseText== 0){
 		window.location.href ="/group2/topic/grouptopic.do?groupId="+delgroupIdval
 	}else{
 		alert("您不是管理员不能执行该操作！")
 	}
}

//屏蔽话题
function isolateGroupTopic(groupTopicId){

	if (confirm('您确认屏蔽该话题吗?屏蔽后无法还原')) {
		var url = '/group2/topic/isolategrouptopic.do'; 
		var pars = '&groupTopicId='+groupTopicId
		var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars, onComplete: isolateResp});
	}
}
function isolateResp(resp){
	if (resp.responseText==0){
		$('topiccontent').innerHTML='您把该话题屏蔽掉了!';
		$('editTopic').innerHTML = '';
	}else if(resp.responseText== 1){
		alert("您不是管理员不能执行该操作");
	}else if(resp.responseText== 2){
		alert("请您登陆")
	}else{
		alert("未知异常");
	}
}
//屏蔽回复
var CommentId
function isolateTopicComment(topicCommentId,groupId){
	CommentId = topicCommentId;
	if (confirm('您确认屏蔽这条回复吗?屏蔽后无法还原')) {
		var url = '/group2/topic/isolatetopiccomment.do'; 
		var pars = '&topicCommentId='+topicCommentId+'&groupId='+groupId
		var myAjax = new Ajax.Request( url, { method: 'post',parameters: pars, onComplete: isolatecommentResp});
	}
}
function isolatecommentResp(resp){
	if (resp.responseText== 0){
		$('comment_'+CommentId).innerHTML='您把该回复屏蔽掉了!';
	}else if(resp.responseText== 1){
		alert("您不是管理员不能执行该操作");
	}else if(resp.responseText== 2){
		alert("请您登陆")
	}else{
		alert("未知异常");
	}
}
// 引用
function referenceComment(divid, nickname, floor){
    var str = floor < 1 ? "楼主" : "" + floor + "楼的";
    var content = '[:quote]' + '<b>最初由' + str + nickname + '发表：</b><br>' + $("" + divid).innerHTML + '[quote:]';
    replyComment(content);
}
function replyComment(content){
    if (window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none") {
        window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML = content;
    } else {
        window.frames["Editor"].document.getElementById("sourceEditor").value = content;
    }
    document.location.href = "#this"
}
// 修改回复
function topicReplyCommentSubmit(){
	var form = $('updatetopicForm');
	var content = "";
	if(window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none"){
        content = window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML;    
    }else{
        content = window.frames["Editor"].document.getElementById("sourceEditor").value; 
    }
	document.getElementById("updatetopicForm").content.value = content; 
	form.submit();
}

//搜索话题
function searchGroupTopic(groupId){
	if($('title').value.trim().size()==0){
		alert("请输入要搜索的话题!");
		$('title').focus();
		return;
	}	
	window.location= '/group2/topic/grouptopic.do?groupId=' + groupId + "&title=" + encodeURIComponent($('title').value);
}

