dojo.require("dojo.io.script");

function CommentCount(objectName){
	
	this.objectName=objectName;
	this.C_nowURL='';
	this.C_subjectid=0;
	this.C_parentid=0;
	this.C_board='';
	this.C_form='';
	
	this.clubURL="http://club.dayoo.com/service/JSON/clubCount.dy";
	this.clubURL_Post="http://club.dayoo.com/outerPost/post2.dy";

	this.getCommentCount=function(){		
			if(this.C_nowURL==null || this.C_nowURL==''){
				return false;
			}
			
			var url=this.C_nowURL;
			var regx='';
			var rs=null;
			
			if(url.indexOf(".htm")!=-1){
				regx=/read-([^-]*)-(\d*)/;
				rs=regx.exec(url);
				this.C_board=rs[1];
				this.C_subjectid=rs[2];
				this.C_parentid=rs[2];
			}else{
				regx=/b=([^\&&^\=]*)\&/;
				rs=regx.exec(url);
				this.C_board=rs[1];
			
				regx=/t=(\d*)/;
				rs=regx.exec(url);
				this.C_subjectid=rs[1];
				this.C_parentid=rs[1];
			}
			
			if(this.C_subjectid=='' || this.C_subjectid==0){
				alert('参数错误！');
				return;
			}
			
			
			var handleFun=function(data,obj){
				data=eval("("+data+")");
				dojo.byId(obj.C_form+"_commentCount").innerHTML=data.total;
			}
			
			var params=this.C_board.replace(/_/g,"#")+"_0_"+this.C_subjectid;
			postURL=this.clubURL;
			
			var obj=this;
			var kw = dojo.io.script.get( {
			  url: postURL,
			  callbackParamName: "callback",
			  content: {'params':params,"mode":1},
			  handleAs: "json",
			  preventCache: true,
			  handle: function(response, ioArgs){
				  if (response instanceof Error) {
					if (response.dojoType == "timeout") {} 
					else {}
				  }else{
					handleFun(response,obj);
				  }
			   }		  
			} );
	}

	this.C_post=function(obj){
		
		if(typeof(IsLogin)=='undefined' || IsLogin!=1){
		
			if(obj.username.value.replace(/ /g)==''){
				alert('请输入用户名！');
				obj.username.focus();
				return false;
			}
			
			if(obj.password.value.replace(/ /g)==''){
				alert('请输入用户密码！');
				obj.password.focus();
				return false;
			}
			
			 if(obj.username.value != '' && obj.password.value != ''){
				obj.anonymous.value=0;
				obj.nickname.value='';
			 }else{
				obj.anonymous.value=1;
				obj.nickname.value=obj.username.value;
			 }
		 
		}
		
		if(obj.content.value.replace(/ /g)=='' || obj.content.value=='文明上网　理性发言'){
			alert('请填写评论内容！');
			obj.content.focus();
			return false;
		}
		
		if(this.C_board=='' || this.C_subjectid==0 || this.C_parentid==0){
			alert('参数错误，请刷新页面！');
			return false;
		}
		alert('感谢您发表评论，您发表的评论将在管理员审核后发布！');		
		var clubURL_Post="http://club.dayoo.com/outerPost/post2.dy";
		obj.action=this.clubURL_Post+"?t="+this.C_subjectid+"&r="+this.C_parentid+'&b='+this.C_board+"&reply=1&showReturn=1";
		obj.submit();
		var nickname=obj.nickname.value;
		var username=obj.username.value;
		setTimeout(function(){obj.reset();obj.nickname.value=nickname;obj.username.value=username;},2000);
		return false;
	}
	
	//匿名与登录切换
	this.pp1Onclick=function(){
		dojo.byId(this.C_form+'_pp1').style.display='none';
		dojo.byId(this.C_form+'_pp2').style.display='block';
	}
	
	//登录与匿名切换
	this.pp2Onclick=function(){
		dojo.byId(this.C_form+'_pp1').style.display='block';
		dojo.byId(this.C_form+'_pp2').style.display='none';
	}
	
}
