var isIE = !!document.all;
if(isIE)
document.documentElement.addBehavior("#default#userdata");
function  saveUserData(key, value){
    var ex; 
    if(isIE){
        with(document.documentElement)try {
            load(key);
            setAttribute("value", value);
            save(key);
            return  getAttribute("value");
        }catch (ex){
         //   alert(ex.message)
        }
    }else if(window.sessionStorage){//for firefox 2.0+
        try{
            sessionStorage.setItem(key,value)
        }catch (ex){
            alert(ex);
        }
    }else{
        alert("当前浏览器不支持userdata或者sessionStorage特性")
    }
}

function loadUserData(key){
    var ex; 
    if(isIE){
        with(document.documentElement)try{
            load(key);
            return getAttribute("value");
        }catch (ex){
            alert(ex.message);return null;
        }
    }else if(window.sessionStorage){//for firefox 2.0+
        try{
            return sessionStorage.getItem(key)
        }catch (ex){
            alert(ex)
        }
    }else{
        alert("当前浏览器不支持userdata或者sessionStorage特性")
    }
}
function  deleteUserData(key){
    var ex; 
    if(isIE){
        with(document.documentElement)try{
            load(key);
            expires = new Date(315532799000).toUTCString();
            save(key);
        }
        catch (ex){
            alert(ex.message);
        }
    }else if(window.sessionStorage){//for firefox 2.0+
        try{
            sessionStorage.removeItem(key)
        }catch (ex){
            alert(ex)
        }
    }else{
        alert("当前浏览器不支持userdata或者sessionStorage特性")
    }
} 
//主题帖引用
function addTopicQuote(topiId) {
     
	var followContent = FCKeditorAPI.GetInstance("followContent").GetXHTML();
	var url = "updateTopic.action";
	$.post(url, {topiId:topiId, operate:"quote"}, callbackFun, "json");
}
//主题帖编辑
function addTopicEdit(topiId) {
	var followContent = FCKeditorAPI.GetInstance("followContent").GetXHTML();
	var url = "updateTopic.action";
	$.post(url, {topiId:topiId, operate:"edit"}, callbackFun, "json");
}

//主题帖关闭
function addTopicClose(topiId) {
    var topicIds = topiId + ",";
	//var followContent = FCKeditorAPI.GetInstance("followContent").GetXHTML();
	//var url = "updateTopic.action";
	//$.post(url, {topiId:topiId, operate:"isClose"}, callbackFun, "json");
	showWin('url','../bbs/other/openOrClose.jsp?topicId='+topicIds+'&t=0','false','关闭主题帖','400px','300px');
}
//主题帖关闭
function addTopicNotClose(topiId) {
    var topicIds = topiId + ",";
	//var followContent = FCKeditorAPI.GetInstance("followContent").GetXHTML();
	//var url = "updateTopic.action";
	//$.post(url, {topiId:topiId, operate:"notClose"}, callbackFun, "json");
	showWin('url','../bbs/other/openOrClose.jsp?topicId='+topicIds+'&t=1','false','打开主题帖','400px','300px');
}
 
//回调函数，多出调用到该函数
function callbackFun(data) {
	var oEditor = FCKeditorAPI.GetInstance("followContent");
	oEditor.Focus();
	 oEditor.SetHTML(data.msg);  
	 
 //  alert(data);
}
//主题帖锁定
function addTopicLock(topiId) {
	var topicIds = topiId + ",";
	 showWin('url','../bbs/other/lockOrNot.jsp?topicId='+topicIds+'&t=0','false','锁定主题帖','400px','300px');
	//var url = "updateTopic.action";
	//$.post(url, {topicIds:topicIds, operate:"lock"}, callBack, "json");
}
//回调函数
function callBack(data) {
	alert(data.msg);
}
//主题帖解锁
function addTopicUnLock(topiId) {
	var topicIds = topiId + ",";
	showWin('url','../bbs/other/lockOrNot.jsp?topicId='+topicIds+'&t=1','false','锁定主题帖','400px','300px');
 	//var url = "updateTopic.action";
 	//$.post(url, {topicIds:topicIds, operate:"unLock"}, callBack, "json");
}
//主题帖加精
function addTopicBest(topiId) {
	var topicIds = topiId + ",";
	 showWin('url','../bbs/other/bestOrNot.jsp?topicId='+topicIds+'&t=1','false','设为精华帖','400px','300px');
//	var url = "updateTopic.action";
	//$.post(url, {topicIds:topicIds, operate:"isBest"}, callBack, "json");
}
//主题帖取消加精
function addTopicNotBest(topiId) {
	var topicIds = topiId + ",";
	 showWin('url','../bbs/other/bestOrNot.jsp?topicId='+topicIds+'&t=0','false','取消设置精华帖','400px','300px');
	//var url = "updateTopic.action";
//	$.post(url, {topicIds:topicIds, operate:"notBest"}, callBack, "json");
}
//删除主题帖
function deleteTopic(topiId){
   var topicIds = topiId + ",";
   showWin('url','../bbs/other/delTopic.jsp?topicId='+topicIds,'false','删除设置','400px','300px');
}
function moveTopic(topiId){
  var topicIds = topiId + ",";
  showWin('url','../bbs/other/moveTopic.jsp?topicId='+topicIds,'false','移动其他版块','400px','300px');
}

function updateTopicType(topiId,kindId){
    var topicIds = topiId + ",";
    showWin('url','other/updateTopicType.jsp?topicId='+topicIds+"&kindId="+kindId,'false','更改主题帖类型','400px','300px');
}

//引用回复贴
function addQuote(follId) {
	var followContent = FCKeditorAPI.GetInstance("followContent").GetXHTML();
	var url = "updateFollow.action";
	$.post(url, {follId:follId, operate:"quote"}, callbackFun, "json");
}
 


function quoteFollowArgu(follId,displayPlace) {
	 
	var url = "updateFollow.action?operate=quote&follId=" + follId;
	$.ajax({url:url, type:"POST", dataType:"json", error:function () {
		alert("Error loading XML document");
	}, success:function (data, textStatus) {
	        if(data.msg=='login'){
	           alert('登录已超时，请重新登录');
	         showWin('url','../pass/login.html','flase','用户登录','350px','250px');
	        }else{
               document.getElementById(displayPlace).value=data.msg;
            }
         
	}});
} 
//回复贴屏蔽
function shieldPost(follId) {
 
   showWin('url','../bbs/other/shieldOrNot.jsp?t=1&follId='+follId,'false','屏蔽回复贴','400px','300px');
  
}
//回复贴屏蔽
function notShieldPost(follId) {
	 
	showWin('url','../bbs/other/shieldOrNot.jsp?t=0&follId='+follId,'false','解除屏蔽回复贴','400px','300px');
}

//删除回复贴
function deletePost(follId) {
	 
	showWin('url','../bbs/other/delFollow.jsp?t=0&follId='+follId,'false','删除回复贴','400px','300px');
}


//编辑回复贴

function editPost(follId) {
	var followContent = FCKeditorAPI.GetInstance("followContent").GetXHTML();
	var url = "updateFollow.action?operate=edit&follId=" + follId;
	$.ajax({url:url, type:"POST", dataType:"json", error:function () {
		alert("Error loading XML document");
	}, success:function (data, textStatus) {
  
	  var oEditor = FCKeditorAPI.GetInstance("followContent");
	 oEditor.Focus();
	oEditor.SetHTML(data.msg); 
	
	document.getElementById("edit").value="true";
	document.getElementById("follId").value=follId;
	}});
}
 
function editFollowArgu(follId,displayPlace) {
	 
	var url = "updateFollow.action?operate=edit&follId=" + follId;
	$.ajax({url:url, type:"POST", dataType:"json", error:function () {
		alert("Error loading XML document");
	}, success:function (data, textStatus) {
           document.getElementById(displayPlace).value=data.msg;
           
           if(displayPlace=="pFollContent"){
              document.getElementById("pEdit").value="true";
	          document.getElementById("pFollId").value=follId;
	          document.getElementById("pSend").value="修改";
           }else{
              document.getElementById("nEdit").value="true";
	          document.getElementById("nFollId").value=follId;
	          document.getElementById("nSend").value="修改";
           } 
	}});
} 
 
 
//添加投票日志
function addVoteLog(topicId) {
	var items = document.getElementsByName("voteItem");
	var voteItem = "";
	var checkLength = 0;
	if (items.length > 0) {
		for (var i = 0; i < items.length; i++) {
			if (items[i].checked) {
				checkLength += 1;
				voteItem += items[i].value + ",";
			}
		}
	}
	if (checkLength > 0) {
		var url = "addVoteLog.action?item=" + voteItem + "&topiId=" + topicId;
		$.ajax({url:url, type:"POST", dataType:"json", error:function () {
			alert("Error loading XML document");
		}, success:function (xml, textStatus) {
			document.getElementById("totalBallot").innerHTML = xml.totalBallot;
			var voteInnerHTML=String(document.getElementById("toupiao").innerHTML);
		    
			
			
			var itemInnerHTML="";
			var itemType="radio";
			if(voteInnerHTML.indexOf("radio")!=-1){
			   itemType="radio";
			}else{
			   itemType="checkBox";
			}
		 	for(var i=0;i<xml.itemBallots.length;i++){
		 	 
		 	   var item=xml.itemBallots[i];
		 	   var votePicWidth=item.ballot/100.0*500;
		 	   itemInnerHTML+="<input type="+itemType+"  value="+item.itemId+"   name=voteItem>"+item.itemTheme+"   <img style='width:"+votePicWidth+"px;zoom;1;height:10px' src='../images/bbs/vote.gif'>  "+item.ballot+"票<br/>";
		 	   
		 	}
		 	itemInnerHTML+="<input onclick=addVoteLog("+topicId+") type='button'  class='butto' value='投票'><input type='button' onclick=javascript:window.open('voteChart.jsp?topicId="+topicId+"','new','height=530,width=700,top=100,left=200,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no') value='查看' class='butto'/><hr>";
		 	document.getElementById("toupiao").innerHTML=itemInnerHTML;
			  alert(xml.msg);
		}});
	} else {
		alert("\u8bf7\u9009\u62e9\u6295\u7968\u9879");
	}
}
//添加评论投票日志
function addArgumentVoteLog(topicId, voteItem) {
	var url = "addArgumentVoteLog.action?item=" + voteItem + "&topiId=" + topicId;
	$.ajax({url:url, type:"POST", dataType:"json", error:function () {
		alert("Error loading XML document");
	}, success:function (xml, textStatus) {
		document.getElementById("positiveSidePer").innerHTML = xml.positiveSideTotal;
		document.getElementById("negativeSidePer").innerHTML = xml.negativeSideTotal;
		alert(xml.msg);
	}});
}
//添加收藏贴
function addFavorite(topiId) {
	var url = "addFavorite.action?topiId=" + topiId;
	$.ajax({url:url, type:"POST", dataType:"json", beforeSend:function (XMLHttpRequest) {
	}, complete:function (XMLHttpRequest, textStatus) {
	}, error:function () {
		alert("Error loading XML document");
	}, success:function (xml, textStatus) {
		 
			alert(xml.msg);
	 
		 
	}});
}

/*function addFollow(){
      var followContent=FCKeditorAPI.GetInstance("followContent").GetXHTML();
     
      var follCon=String(followContent);
     var str2=String(followContent);
   var  str= follCon.replace(/<.*?>/gi,"");
   if($.trim(str.replace(/\&nbsp;/gi,"")).length!=0){
        if(str2.indexOf("<img")!=-1 || ($.trim(str.replace(/\&nbsp;/gi,"?")).length>=10 && $.trim(str.replace(/\&nbsp;/gi,"?")).length<=5000)){
            $("#addFollow").submit();
        }else{
          alert("回复内容长度必须控制在10至5000个之间");
      }
   }else if(str2.indexOf("<img")!=-1){
        $("#addFollow").submit();
      }else{
       alert("请输入跟帖内容");
   }
     
   
}*/

function getLength(){ 
       var oEditor = FCKeditorAPI.GetInstance("followContent"); 
       var oDOM = oEditor.EditorDocument; 
       var iLength ; 
       if(document.all){ 
         iLength = $.trim(oDOM.body.innerText).length; 
        } 
        else 
        { 
        var r = oDOM.createRange(); 
         r.selectNodeContents(oDOM.body); 
         iLength = $.trim(r.toString()).length; 
        } 

       return iLength;
     } 
function addFollow(){
     
     var topiId=document.getElementById("topiId").value;
     // var topiContInnerText=FCKeditorAPI.GetInstance("followContent").EditorDocument.body.innerText;
      var followContent=FCKeditorAPI.GetInstance("followContent").GetXHTML();
      
     // alert(followContent.indexOf("<img"));
      if(getLength()<5 || getLength()>100000){
         if(followContent.indexOf("<img")!=-1  || followContent.indexOf("<embed")!=-1){
            deleteUserData("addFollow"+topiId);
             $("#addFollow").submit();
         }else{
             alert("回复内容长度必须控制在5至100000个之间");
         }
      }else{
          
           deleteUserData("addFollow"+topiId);
          $("#addFollow").submit();
      }
      
    //  var topiContInnerText=FCKeditorAPI.GetInstance("followContent")
    //  topiContInnerText.Commands.GetCommand('Image').Execute();  
   
   
}

function checkFollowArgu(t){
  if(t=="p"){
      var content=document.getElementById("pFollContent").value;
      if($.trim(content).length<=0){
         alert("请输入正方评论观点");
      }else{
         document.getElementById("pSubmit").submit();
      }
  }
   if(t=="n"){
      var content=document.getElementById("nFollContent").value;
      if($.trim(content).length<=0){
         alert("请输入反方评论观点");
      }else{
         document.getElementById("nSubmit").submit();
      }
  }

}


 
                  
               var contentLength="";
         function saveToAddTopicTemp(topicId){
		      var followContent=String(FCKeditorAPI.GetInstance("followContent").GetXHTML());
		        if(followContent!=contentLength){
		            contentLength=followContent;
		            
                    saveUserData("addFollow"+topicId,followContent);
                 }
            }
            
            
//版主推荐主题帖
function recommendTopic(topiId) {
	var topicIds = topiId + ",";
	 showWin('url','../bbs/other/recommend.jsp?topicId='+topicIds+'&t=0','false','推荐主题帖','400px','300px');
	//var url = "updateTopic.action";
//	$.post(url, {topicIds:topicIds, operate:"notBest"}, callBack, "json");
}

function showUserLis(v){
    if(v==1){
      document.getElementById("zcUs").style.left=window.event.screenX;
      document.getElementById("zcUs").style.display="block";
   }else if(v==0){
      document.getElementById("fdUs").style.left=window.event.screenX;
     document.getElementById("fdUs").style.display="block";
   } 
   
}

function hiddenUserLis(v){
  if(v==1){
     document.getElementById("zcUs").style.display="none";
  }else if(v==0){
     document.getElementById("fdUs").style.display="none";
  }
  
}
var flag=false;
function addEssayView(topicId,viewValue) {
		if(flag==false){
		var url = "addEssayView.action?topiId="+topicId+"&viewValue="+viewValue;
		$.ajax({url:url, type:"POST", dataType:"json", error:function () {
		}, success:function (xml, textStatus) {
		   if(xml.memberLis==null){
		       alert(xml.msg);
		   }else{
			if(xml.viewValue!=null){
			   flag=true;
			if(xml.viewValue==1){
			   var str="";
			   for(var i=0;i<xml.memberLis.length;i++){
			       str+="<a href=''>"+xml.memberLis[i][1]+"</a><br/>";
			    }
			  var c=xml.memberLis.length;
			  document.getElementById("zcV").innerHTML=c;
			   document.getElementById("zcV2").innerHTML=c;
			   document.getElementById("zcUs").innerHTML=str;
			}else if(xml.viewValue==0){
			     var str="";
			   for(var i=0;i<xml.memberLis.length;i++){
			       str+="<a href=''>"+xml.memberLis[i][1]+"</a><br/>";
			   }
			  var c=xml.memberLis.length;
			  document.getElementById("fdV").innerHTML=c;
			   document.getElementById("fdUs").innerHTML=str;
			  
			}
			}
			}
		}});
		}else{
		   alert("已参与该投票");
		}
		 
	 
}
