﻿
var cartoon_ajax_url = "/ajaxserver/CartoonActions.ashx";
var reply_html = "<div class=\"hs_cmt_quick_reply\" id=\"comment_reply\"><textarea name=\"textarea\" id=\"textarea\" cols=\"45\" rows=\"1\" style=\"width:340px;padding:4px;background-color:#fff;\" class=\"textarea_eff\" onfocus=\"this.rows=4\" onblur=\"if(this.value==''){this.rows=1;}\" ></textarea><p><input name=\"button\" type=\"button\" class=\"yellow-button\" id=\"btn_reply\" value=\"Reply\" onclick=\"AddComment(this,{0},{1},{2})\"/>  <input name=\"button\" type=\"button\" class=\"da-button\" id=\"btn_cancel\" value=\"cancel\" onclick=\"$(this).parent().parent().remove().find('input').val('')\" /> </p></div>";

function ShowReply(id, commentid, cartoonid) {
    var rootDOM = $("#comment"+commentid);
    var accountid = rootDOM.find(".strong a").attr("rel"); 
    $(".hs_cmt_quick_reply").remove();
    var new_html = String.format(reply_html, commentid, cartoonid, accountid);
    rootDOM.find(".info").append(new_html);
}

function AddComment(id, commentid, cartoonid, accountid,strcontents)
{
    if (cartoonid == null || cartoonid <= 0)
    { alert("Error."); return; }
    if (accountid == null)
    { alert("Error."); return; }
    var rootDOM = $("#comment" + commentid);
    var contents = (!strcontents || strcontents == null) ? $.trim(rootDOM.find(".hs_cmt_quick_reply textarea").val()) : strcontents;
    if (contents.length > 1000) { return; }
    var url_data="action=addcomment&commentid=" + commentid + "&cartoonid="
        + cartoonid + "&accountid=" + accountid + "&contents=" + escape(contents);
    $(id).attr("disabled", "disabled");
    $.getJSON(cartoon_ajax_url, url_data,
    function(data) {
        if (data.flag == true) {
            window.location.href = "/cartoon/cartoondetail.aspx?cid=" + cartoonid;
        }
        else {
            $(id).attr("disabled", "");
        }
    });
}

function DeleteComment(id, commentid,cartoonid) {
    if (commentid == null || commentid <= 0 || cartoonid == null || cartoonid <= 0)
    { alert("Error."); return; }
    if (confirm("Are you sure delete this comment ?")==false) 
    { return; }
    var rootDOM = $("#comment" + commentid);

    var url_data = "action=cartoonaction&mode=delete_comment&cid=" + commentid;
    $(id).attr("disabled", "disabled");
    $.getJSON(cartoon_ajax_url, url_data,
    function(data) {
        if (data==1) {
            rootDOM.parent().remove();
        }
        else {
            $(id).attr("disabled", "");
        }
    });
}
