

function addCommentPageInit()
{
    document.addC0mm3ntForm.formType.value = 'addBCommA';
}


function submitComment()
{
    document.getElementById('submit_message').innerHTML = 'Submitting Comment -- Please Wait.';
    document.getElementById('form_bottom').className = 'submitting';

    return (new FormPayjacksAjax(document.addC0mm3ntForm, document.URL, submittedComment)).submitForm();
}


function submittedComment(in_responseObject)
{
    if (in_responseObject.ResponseCode == 'OK')
    {
        addAndShowComment(in_responseObject.CommentID, in_responseObject.CommentDivElement);
        document.addC0mm3ntForm.reset();
        document.getElementById('message_area').innerHTML = '';
    }
    else
    {
        var el = document.getElementById('message_area');
        el.style.display = 'none';

        switch (in_responseObject.ResponseCode)
        {
            case 'NO_ID':
                el.innerHTML = 'Sorry, you must specify either a name or a URL';
                new Effect.BlindDown('message_area', {duration: 1});
                document.addC0mm3ntForm.ttell.select();
                document.addC0mm3ntForm.ttell.focus();
                break;

            case 'NO_BODY':
                el.innerHTML = 'Sorry, you have to type at least something in the body.';
                new Effect.BlindDown('message_area', {duration: 1});
                break;
            default:
                el.innerHTML = 'An unexpected error has occurred.  Please try again later.';
                new Effect.BlindDown('message_area', {duration: 1});
                break;
        }                
    }

    document.getElementById('form_bottom').className = 'editing';
        
}

function addAndShowComment(in_commentID, in_commentDiv)
{
    var commentList = document.getElementById('commentHolderArea');
    var updated = commentList.innerHTML;
    commentList.innerHTML = updated + in_commentDiv;

    document.getElementById('comm' + in_commentID).style.display = 'none';
    new Effect.Grow('comm' + in_commentID);
}


function selectMessageState(in_commentid)
{
    document.getElementById('approvingmsg' + in_commentid).innerHTML = "Updating -- Please Wait";

    /**
     * Remember the value in case of error.
     */
    var action = document.getElementById('messageState' + in_commentid).value;
    
    var baseUrl = document.URL.split('#')[0];
    var baseUrl = baseUrl.split('?')[0];
    var urlExtra = '?' + action + '=' + in_commentid;

    return (new FormPayjacksAjax(document.c0mm3ntFiddleForm, baseUrl + urlExtra, visibleStateChanged)).submitForm();
}


function visibleStateChanged(in_responseObject)
{
    if (in_responseObject.ResponseCode == 'OK')
    {
        document.getElementById('approvingmsg' + in_responseObject.CommentID).innerHTML = "Update Succeeded";
    }
    else
    {
        if (in_responseObject.CommentID != null)
        {
            var oldVal = document.getElementById('oldState' + in_responseObject.CommentID).value;
            document.getElementById('messageState' + in_responseObject.CommentID).value = oldVal;
        }
        
        switch (in_responseObject.ResponseCode)
        {
            case 'PERMS':
                document.getElementById('approvingmsg' + in_responseObject.CommentID).innerHTML = "You don't have enough permissions for this.  Which is weird, because you're viewing the page ... (¡¡aiee!!)";
                break;
            case 'BOGUS':
                document.getElementById('approvingmsg' + in_responseObject.CommentID).innerHTML = "Bogus Form Data submitted.";
                break;
            default:
                document.getElementById('approvingmsg' + in_responseObject.CommentID).innerHTML = "Unexpected Error (probably database problem or internal error). Please reload the page and try again.";
                break;
        }
    }
}


