﻿

var commenthandler = function(){
    var projectid;
    var templateid;
    var objectid;    
    var isrecomment;
    var username;
    var password;
    var content;   
    var commentcheck;
    
    this.post = function(){
     
        commentinfo[commentinfo.length] = {UserName:this.username,Content:this.content,CommentID:this.commentid};
        $("#commenturl").html("");
        this.init();
    }

    this.init = function(){
        
         $.ajax({
           type: "POST",
           url: "/commentsolution/CommentData.aspx",
           data: "ProjectID="+this.projectid+"&Templateid="+this.templateid+"&NewsID="+this.objectid+"&ReFlag="+this.isrecomment+"&username="+this.username+"&password="+this.password+"&CommentCheck="+this.commentcheck,
           success: function(msg){
        
               var str = msg;             

              //  alert(msg);
               $("#commenturl").html(str);
              
        
                                }
           });     
     
    }

    this.repost = function(){
         
    };
    
    
    
}

 $(document).ready(function(){

   var c = new commenthandler();
   var projectid= $("#ProjectID").val();
   var templateid= $("#TemplateID").val();
   var newsid= $("#NewsID").val();


   c.projectid=projectid;
   c.templateid=templateid;
   c.objectid=newsid;
   c.init();

   getCommentcount(projectid,templateid,newsid);
   
    $("#spupcount").click(function(){ DoAssess( -1,'spupcount','upcount',projectid,templateid,newsid)});
    $("#spdowncount").click(function(){ DoAssess( -1,'spdowncount','downcount',projectid,templateid,newsid)});
    
    $("#divupcount").click(function(){ DoAssess( -1,'divupcount','upcount',projectid,templateid,newsid)});
    $("#divdowncount").click(function(){ DoAssess( -1,'divdowncount','downcount',projectid,templateid,newsid)});
    

   
    
 });



var commentinfo;


function postcomment()
{ 

    var projectid= $("#ProjectID").val();
   var templateid= $("#TemplateID").val();
   var newsid= $("#NewsID").val();
   var cookiename = "Comment_"+projectid+"_" +templateid +"_"+newsid
   //cookie
   var cookieValue= $.cookie(cookiename) ;
   //var cookieValue= "" ;
   var content= $("#txtContent").val();     
   
   var m = new MessageBox();    
   
   var flag = false;
                                
   if(validate(content))
    {  
        
       if(cookieValue)
       {
     
          if(cookieValue.indexOf(content) ==-1)
          {
              //$.cookie(cookiename, cookieValue + " " + content); 
              flag = true;
          }
          else
          {
             flag=false;
             m.Type="alertbox";
             m.Content="请不要重复发表相同的内容!!";
             m.ShowMe();
          }
       }
       else
       {
          //$.cookie(cookiename, content); 
          flag = true;
       }
   }
   else
   {
         m.Type="alertbox";
         m.Content="请确认您发表的内容!!";
         m.ShowMe(); 
   }
  
  if(flag)
  {   
     if(cookieValue)
     {
    	 if(cookieValue.indexOf(content) ==-1)
    	 {
              $.cookie(cookiename, cookieValue + " " + content); 
           
    	 } 
      }
   else
	{
         $.cookie(cookiename, content); 
	}

     if(!validatetimer(projectid,templateid,newsid))
   {
      m.Type="alertbox";
      m.Content="您发表的速度有些频繁，请稍后再发!!";
 m.ShowMe();
      return false;
   }
     document.getElementById('formcomment').submit();   
    //formcomment.submit();
  }
}


function AddFaceToInput(faceImgID) 
{	
    var faceInput = $("#txtContent").val();
   
    var faceImgShortCut;	
    if(faceImgID != '') 
    {
    	faceImgShortCut = $("#face"+faceImgID)[0].name;	
    	
    }
    else
    {	
        return;	
    }
    $("#txtContent")[0].value = faceInput.toString() + " " + faceImgShortCut;
}

function validate(content)
{
content= $.trim(content);
    if(content !='')
    {
       if(content.length<1000)
               return true;        
    }
    return false;
}

function validatetimer(projectid,templateid,newsid)
{
   var cookietimer="Comment_"+projectid+"_" +templateid +"_"+newsid+"_timer";
      
   var cookietimerValue =$.cookie(cookietimer) ;
   var currentDate =new Date();
  

   if(cookietimerValue)
   {
      
      
      if(cookietimerValue > currentDate.setSeconds(currentDate.getSeconds()+1))
     {
       
        
        return false;
     }
     else
     {
      $.cookie(cookietimer,  currentDate.setSeconds(currentDate.getSeconds()+5)); 
      
       return true; 
     }
   }
   else
   {
      $.cookie(cookietimer,  currentDate.setSeconds(currentDate.getSeconds()+5)); 
      return true;
   }
}



//回复评论
function replyComment(commentid)
{
  $("#reflag").attr("value","true"); 
  $("#recommentid").attr("value",commentid); 
  $("#txtContent").focus();
 
}


function getCommentcount(projectid,templateid,objectid)
{
      $.ajax({
           type: "POST",
           url: "/commentsolution/Counter.aspx",
           data: "ProjectID="+projectid+"&Templateid="+templateid+"&NewsID="+objectid,
           success: function(msg){
           var commentcount="0";
           var upcount1="0";
           var downcount1="0";           

           eval(msg);                                    
            $("#spcommentcount").html(commentcount);  
              $("#commentcount").html(commentcount);          
            $("#spupcount").html(upcount1);           
            $("#spdowncount").html(downcount1);
            $("#divcommentcount").html(commentcount);
            $("#divupcount").html(upcount1);
            $("#divdowncount").html(downcount1); 
         
               }
           });     
}