function revealComment(self, id) {
    var commentBox = $('comment-reply-' + id);
    if (!commentBox) return false;

    showElement(commentBox);
    self.onclick = partial(hideComment, self, id);
    return false;
}

function hideComment(self, id) {
    var commentBox = $('comment-reply-' + id);
    if (!commentBox) return false;
    self.onclick = partial(revealComment, self, id);

    hideElement(commentBox);
    return false;
}    

function submitComment(klass, id) {

    var commentBox = $('comment-reply-' + id);
    if (!commentBox) return false;

    var commentForm = formContents(commentBox);
    
    // make sure defaults are removed
    var formNames = new Array();
    var formParams = new Array();
    var inputs = getElementsByTagAndClassName("input", null, commentBox);
    var textareas = getElementsByTagAndClassName("textarea", null, commentBox);
    var i = 0;
    
    for (i = 0; i < inputs.length; i++) {
        if (isNotEmpty(inputs[i].name)) {
            var param = inputs[i].value;
            if (isNotEmpty(inputs[i]['default']) && (inputs[i]['default'] == inputs[i].value)) {
                param = "";
            }
            formNames.push(inputs[i].name);
            formParams.push(param);
        }
    }
    for (i = 0; i < textareas.length; i++) {
        if (isNotEmpty(textareas[i].name)) {
            var param = textareas[i].value;
            if (isNotEmpty(textareas[i]['default']) && (textareas[i]['default'] == textareas[i].value)) {
                param = "";
            }
            formNames.push(textareas[i].name);
            formParams.push(param);
        }
    }
    
    var req_body = queryString(formNames, formParams);
    var req = getXMLHttpRequest();
    var url = '/comments/api/post/' + klass + '/' + id + '/';
    req.open('POST', url, true);

    submitButtonChange(id, "Submitting..");
    var d = sendXMLHttpRequest(req,req_body);

    var ok = function(req) {
        submitButtonChange(id, "Reply");

        var response = evalJSONRequest(req);
        if (response.error) {
            alert(response.error);
            return;
        }
        
        hideComment($('comment-reply-' + id), id);
        submitTextareaClear(id);

        // insert comment into blog-comments-new
        var insertPoint = $('comment-new-' + id);
        if (!insertPoint) return;

        var comment = DIV({}, " ");
        comment.innerHTML = response.comment_html;
        appendChildNodes(insertPoint, comment);
        return;
    };

    var err = function(req) {
        alert("Error submitting comment. Please try again later.");
        submitButtonChange(id, "Reply");
        return false;
    };

    d.addCallbacks(ok, err);

    return false;
}

function submitEditComment(id) {
    var commentBox = $('comment-' + id);
    if (!commentBox) return false;

    var commentTextarea = getElementsByTagAndClassName('textarea', null,
                                                      commentBox);
    if (!commentTextarea || (commentTextarea.length < 1)) return false;

    var req_body = queryString({'comment': commentTextarea[0].value});
    var req = getXMLHttpRequest();
    var url = '/comments/api/edit/' + id + '/';
    req.open('POST', url, true);

    var d = sendXMLHttpRequest(req, req_body);
    var ok = function(req) {   
        var response = evalJSONRequest(req);
        if (response.error) {
            alert(response.error);
            return;
        }
        commentBox.innerHTML = response.comment_html;
        return;
    };

    var err = function(req) {
        alert("Error occurred submitting edited comment.");
        return;
    };

    d.addCallbacks(ok, err);
    return false;
}

function editComment(id) {
    var commentHTML = $('comment-' + id);
    if (!commentHTML) return false;

    // get the raw contents from the server
    var d = loadJSONDoc('/comments/api/get/' + id + '/');
    var ok = function(response) {
        if (response.error) {
            alert(response.error);
            return;
        }

        var form = DIV({}, FORM({'onsubmit': partial(submitEditComment, id)}, 
                                [TEXTAREA({'cols':80, 'rows':3, 'class':'comment'}, 
                                          response.comment_text),
                                 DIV({},INPUT({'type':'submit', 
                                                   'value':'Edit'}))]));
        replaceChildNodes(commentHTML, form);
    };

    var err = function(response) {
        alert("Unable to start editing comment");
    };

    d.addCallbacks(ok, err);

    return false;
}

function deleteComment(id) {
    // confirm delete
    var really_delete = confirm("Really delete this comment?");
    if (!really_delete) return false;

    // send delete
    var d = loadJSONDoc('/comments/api/delete/' + id + '/');
    var ok = function(response) {
        if (response.error) {
            alert(response.error);
            return;
        }
        
        var comment = $('comment-block-' + id);
        hideElement(comment);
        return;
    };
    var err =  function(response) {
        alert("Error occurred when deleting this comment.");
        return;
    };

    d.addCallbacks(ok, err);
    return false;
}

function votedownComment(id) {
    // vote down (mark as spam)

    var really_vote = confirm("Report this comment as spam?");
    if (!really_vote) return false;

    // send delete
    var d = loadJSONDoc('/comments/api/votedown/' + id + '/');
    var ok = function(response) {
        if (response.error) {
            alert(response.error);
            return;
        }
        
        var comment = $('comment-block-' + id);
        hideElement(comment);
        return;
    };
    var err =  function(response) {
        alert("Error occurred when reporting this comment.");
        return;
    };

    d.addCallbacks(ok, err);
    return false;
}

function submitButtonChange(id, msg) {
    var commentBox = $('comment-entry-' + id);
    if (!commentBox) return;

    var submitButton = getElementsByTagAndClassName('input', 'reply', 
                                                    commentBox);
    if (submitButton && (submitButton.length > 0)) {
        submitButton[0].value = msg;
    }
}

function submitTextareaClear(id) {
    var commentBox = $('comment-entry-' + id);
    if (!commentBox) return;

    var commentTextarea = getElementsByTagAndClassName('textarea', null,
                                                      commentBox);
    if (!commentTextarea || (commentTextarea.length < 1)) return false;

    commentTextarea[0].value = "";
}
