﻿// Depends: Jquery.js, common.js

var limit = 200;

//提交评论表单
function commentSubmit()
{
    if($("#wordsLim").val())
        limit = Number($("#wordsLim").val());
    var length = win_strlen($("#commentText").val());
    if(length>0)
    {
        if(length<=limit)
        {   
            $("#commentBttn").attr("disabled","disabled");
            $.post("/Comment/CreateComment",{uid:$("#uid").val(),
                                               type:$("#type").val(),
                                               objId:$("#objId").val(),
                                               text:$("#commentText").val(),
                                               count:limit
                                               },
                     function(){
                        getComment();
                        var commentCount = Number($("#count"+$("#objId").val()).html())+1;
                        $("#count"+$("#objId").val()).html(commentCount);
                     }
                   );
        }
        else
        {
            alert("评论字数限制为"+limit+"字");
        }
    }
    else
    {
        alert("你还没有写评论内容呢");
    }
}
//提交回复表单
function replySubmit()
{
    if($("#wordsLim").val())
        limit = Number($("#wordsLim").val());
    var length =  win_strlen($("#replyText").val());
    if(length>0)
    {
        if(length<=limit)
        {   
            $("#replybutton").attr("disabled","disabled");
            $.post("/Comment/CommentReply",{  cid:$("#cid").val(),
                                                text:$("#replyText").val(),
                                                count:limit
                                                },
                     function(){
                        getComment();
                     }
                  );
        }
        else
        {
            alert("回复字数限制为"+limit+"字");
        }
    }
    else
    {
        alert("你还没有写回复内容呢");
    }
}

function KeyupComment(obj)
{
    if($("#wordsLim").val())
        limit = Number($("#wordsLim").val());
    var content = $("#commentText").val();
    var length = win_strlen(content);
    if(length>limit)
    {
        var maxlength = limit;
        content = content.substring(0,limit);
        length = win_strlen(content);
        
        while(length > limit)
        {
            content = content.substring(0,--maxlength);
            length = win_strlen(content);
        }
        $("#commentText").val(content.substring(0,maxlength));
        alert("内容长度超过限制,内容将自动裁剪为"+limit+"个字符!");
        $("#commentText").focus();
        
        $("#CommentRestCharCount").html("0");
    }
    else if(length == limit)
    {
        $("#CommentRestCharCount").html("0");
    }
    else $("#CommentRestCharCount").html(limit - length);
}

function KeyupReply(obj)
{
    if($("#wordsLim").val())
        limit = Number($("#wordsLim").val());
    var content = $("#replyText").val();
    var length = win_strlen(content);
    if(length>limit)
    {
        var maxlength = limit;
        content = content.substring(0,limit);
        length = win_strlen(content);
        
        while(length > limit)
        {
            content = content.substring(0,--maxlength);
            length = win_strlen(content);
        }
        $("#replyText").val(content.substring(0,maxlength));
        alert("内容长度超过限制,内容将自动裁剪为"+limit+"个字符!");
        $("#replyText").focus();
        
        $("#ReplyRestCharCount").html("0");
    }
    else if(length == limit)
    {
        $("#ReplyRestCharCount").html("0");
    }
    else $("#ReplyRestCharCount").html(limit - length);
}

function getComment()
{
    showComment($("#objId").val(),
                                    $("#uid").val(),
                                    $("#index").val(),
                                    $("#type").val(),
                                    limit,
                                    $("#pageSize").val(),
                                    $("#pageIndex").val(),
                                    $("#noPostBox").val()
                                    );
}

//构造评论对话框
//objId：对象ID 
//uid：对象拥有者ID
//index：对象在页面上的顺序
//type：评论的类型
//len：字数限制
//pagesize：
//pageindex：
function showComment(objId,uid,index,type,len,pagesize,pageindex,nopostBoxPara)
{    
    var nopostBox;
    if(nopostBoxPara==undefined)
        nopostBox = "false";
    else
        nopostBox = nopostBoxPara;
        
    var layer = $("#comment"+index);
    var number = Math.random(); 
    number = number * 1000000000;
    number = Math.ceil(number);
    
    var strNoPager = $("#noPager").val();
    
    if(len!=null)
        limit = len;
    
    $.post("/Comment/Layer_GetCommentList?t="+number,{type:type,objId:objId,uid:uid,index:index,count:limit,pageIndex:pageindex,pageSize:pagesize,noPager:strNoPager,noPostBox:nopostBox},
      function(data)
      {                    
        var pSize = 15;
        if(!isNaN(pagesize))
            pSize = Number(pagesize);
        for(var i=0;i<pSize;i++)
        {
           if(i!=index) 
           {
              $("#comment"+i).hide();
              $("#comment"+i).html("");
           }
        }
        $("#comment"+index).html(data);
        $("#comment"+index).show();
        if(contains(location.href,"#comDiv",false))
        {
            var hrefStrIndex = location.href.indexOf("#comDiv",0);
            location=location.href.substring(hrefStrIndex,location.href.length);
        }
      }
    );    
}

//关闭/隐藏评论框
function hideComment(index)
{
    $("#comment"+index).hide();
    $("#comment"+index).html("");
}

//构造回复对话框
function showReply(cid)
{
    if($("#wordsLim").val())
        limit = Number($("#wordsLim").val());
    $("#reply").remove();
    
    var html = '<div class="mb20" id="reply">';    
    html += '<textarea class="textarea" name="text" onkeyup="javascript:KeyupReply()" id="replyText" cols="" rows="" onblur="this.className=\'textarea\'" style="width: 400px; height: 80px;line-height:14px;" onfocus="this.className=\'textarea2\'" ></textarea>';
    html += '<input type="hidden" id="cid" value="'+cid+'" />';
    html += '<div style="height: 10px;"></div>';
    html += '<span class="l lh25">总共字数：<span class="c36c">'+limit+'</span>　　剩余字数：<span id="ReplyRestCharCount" class="c36c">'+limit+'</span></span>'
    html += '<span class="l ml15">';
    html +='<input type="button" class="botton" id="replybutton" value="回复" onclick="replySubmit();" onMouseOut="this.className=\'botton\';" onMouseOver="this.className=\'botton2\';" /> ';
    html += '<input type="button" class="botton_close1" value="取消" onclick="javascript:hideReply();" onMouseOut="this.className=\'botton_close1\';" onMouseOver="this.className=\'botton_close2\';" />';
    html += '</span>';
    html += '<div class="c"></div>';
    html += '</div>';
       
    for(var i=0;i<50;i++)
    {
     if($("#comment"+i).html().length>0) 
     {       
        $("#comDiv"+cid).append(html);
        $("#replyText").focus();
        return;
        }
    }
}

//关闭/隐藏回复框
function hideReply()
{
    $("#reply").remove();
}

//删除评论
function deleteComment(commentId)
{
    if(confirm("确定要删除评论么？"))
    {
        $.post("/Comment/DeleteComment",
                {commentId:commentId},
                function(){
                        getComment();
                    var commentCount = Number($("#count"+$("#objId").val()).html())-1;
                    $("#count"+$("#objId").val()).html(commentCount);
                }
              );        
    }
}

//删除回复
function deleteReply(replyId)
{
    if(confirm("确定删除回复么？"))
    {
        $.post("/Comment/DeleteReply",
                {replyId:replyId},
               function(){
                    showComment($("#objId").val(),$("#uid").val(),$("#index").val(),$("#type").val(),limit,$("#pageSize").val(),$("#pageIndex").val(),
                                    $("#noPostBox").val());
               });
    }
}

function contains(string,substr,isIgnoreCase) 
{
    if(isIgnoreCase)
    {
     string=string.toLowerCase();
     substr=substr.toLowerCase();
    }
     var startChar=substr.substring(0,1);
     var strLen=substr.length;
     for(var j=0;j<string.length-strLen+1;j++)
     {
         if(string.charAt(j)==startChar)//如果匹配起始字符,开始查找
         {
               if(string.substring(j,j+strLen)==substr)//如果从j开始的字符与str匹配，那ok
               {
                     return true;
               }
         }
     }
     return false;
}

//****************************************************

function contentFocus()
{
    if($("#commentText").val() == "我来说两句......")
        $("#commentText").val("");               
    document.getElementById("commentText").className = "textarea2";
}

function setTwitter()
{
    if($("#twitterCB").attr("checked")==true)
        $("#twitter").val("yes");
    else
        $("#twitter").val("");
}

function deleteMiniComment(commentId)
{
    if(confirm("确定要删除评论么？"))
    {
        $.post("/Comment/DeleteComment",
                {commentId:commentId},
                function(){
                        $.post("/Comment/Layer_GetMiniCommentList",{type:$("#type").val(),objId:$("#objId").val()},
                                function(data)
                                {
                                    $("#comment").html(data);
                                }
                            );
                }
              );        
    }
}

function commentMore()
{
    $.post("/Comment/Layer_GetMiniCommentList",{type:$("#type").val(),objId:$("#objId").val(),newPage:"true",count:50},
                                    function(data)
                                    {
                                        OpenAlertBoxByText(data,"更多...",680);
                                        //$("#comment").html(data);
                                    }
                                );
}

//提交评论表单
function miniCommentSubmit()
{
    var length = win_strlen($("#commentText").val());
    if(length>0 && $("#commentText").val() != "我来说两句......")
    {
        if(length<=100)
        {   
            $.post("/Comment/CreateComment",{uid:$("#uid").val(),
                                               type:$("#type").val(),
                                               objId:$("#objId").val(),
                                               text:$("#commentText").val(),
                                               count:limit,
                                               twitter:$("#twitter").val()
                                               },
                     function(){
                        $.post("/Comment/Layer_GetMiniCommentList",{type:$("#type").val(),objId:$("#objId").val()},
                                function(data)
                                {
                                    $("#comment").html(data);
                                }
                            );
                     }
                   );
        }
        else
        {
            alert("评论字数限制为"+limit+"字");
        }
    }
    else
    {
        alert("你还没有写评论内容呢");
    }
}

//提交证券评论表单
function SecuritiesCommentSubmit()
{
    var length = win_strlen($("#commentText").val());
    if (length > 0 && $("#commentText").val() != "我来说两句......") {
        if (length <= 100) {
            $.post("/twitter/createtwitter", { ObjectId: $("#objId").val(), text: $("#commentText").val() },
                     function() {
                         $.post("/Comment/Layer_GetSecuritiesComment", { type: $("#type").val(), objId: $("#objId").val() },
                                function(data) {
                                    $("#comment").html(data);
                                }
                            );
                     }
                   );
        }
        else {
            alert("评论字数限制为" + limit + "字");
        }
    }
    else {
        alert("你还没有写评论内容呢");
    }
}

//删除证券评论
function deleteSecuritiesComment(commentId)
{
    if(confirm("确定要删除评论么？"))
    {
        $.post("/Comment/DeleteComment",
                {commentId:commentId},
                function(){
                        $.post("/Comment/Layer_GetSecuritiesComment",{type:$("#type").val(),objId:$("#objId").val()},
                                function(data)
                                {
                                    $("#comment").html(data);
                                }
                            );
                }
              );        
    }
}
