//  2010-05-28

function submitAjaxComment(){

    isValid = true;
    msg = "";
    var tco = document.getElementById("id_commentformComment").value.trim();
    var tnm = document.getElementById("id_commentformName").value.trim();

    if (tco.length == 0) {
        msg = "Please enter a comment.\n";
        isValid = false;
    }
    if (tnm.length == 0) {
        msg += "Please enter your name.\n";
        isValid = false;
    }

    if (!(isValid)) {
        alert(msg);
        return false;
    }


    // Define various event handlers for content comment form
    var handleEvent = {
        start: function(eventType, args) {
            document.getElementById("submitCommentButton").disabled = "disabled";

            document.getElementById("ajaxFapImg").style.visibility = 'visible';
        },

        complete: function(eventType, args) {
            document.getElementById("ajaxFapImg").style.visibility = 'hidden';

            document.getElementById("submitCommentButton").disabled = false;
        }

    };

    var handleSuccess = function(o){

        var r = YAHOO.lang.JSON.parse(o.responseText);

        if (r.replyCode) {

            if (r.replyCode == 200) {
                //  success
                //  note: we now return json data about inserted comment so this is not used
                alert("Thank you for your comment.");
            }
            else if (r.replyCode == 201) {
                //  success
                //  todo: display new comment with ajax, without window reload
                alert("Thank you for your comment.");

                ////addListItem('contentCommentsList', r.data);

                window.location.reload();
            }
            else if (r.replyCode > 299) {
                alert("Oops. Cannot submit the comment. " + r.replyText);
                return {};
            }
            else {
                alert("Oops. Cannot submit the comment. Something unexpected happened. " + r.replyText);
                return {};
            }
        } else {
            alert("Oops. Cannot submit the comment. " + o);
        }
        return o;

        var response = o.responseText;
        response = response.split("<!")[0];
    };

    var handleFailure = function(o){

        if(o.responseText !== undefined){
            alert("Oops. We experienced a problem while entering the comment: " + o.status + ' ' + o.responseText.substr(0,200) );
        } else {
            alert("Oops. We experienced a problem while entering the comment. No more information is available." );
        }
    };

    var callback =
    {
        customevents: {
            onStart: handleEvent.start,
            onComplete: handleEvent.complete
            ////onSuccess: handleEvent.success,
            ////onFailure: handleEvent.failure,
            ////onAbort: handleEvent.abort
        },
        scope:handleEvent,

        success:handleSuccess,
        failure:handleFailure,
        argument:['foo','bar']
    };

    var formObject = document.getElementById('commentform');
    YAHOO.util.Connect.setForm(formObject);

    // The POST data (form) are initialized when calling setForm() and is automatically
    // included when calling asyncRequest
    var cObj = YAHOO.util.Connect.asyncRequest('POST', '../_services/content-comment/submitContentComment.php', callback);
}


function submitAjaxCommentLike(commentId) {

    var commentIdTxt = "commentLike-" + commentId;
    var commentLikeImgId = "ajaxCommentLikeImg-" + commentId;
    var commentLikeMsg = "commentLikeMsg-" + commentId;

    var handleEvent = {
        start: function(eventType, args) {
            document.getElementById(commentIdTxt).style.display = 'none';
            document.getElementById(commentIdTxt).disabled = 'disabled';

            document.getElementById(commentLikeImgId).style.display = 'inline';
            document.getElementById(commentLikeImgId).style.visibility = 'visible';
        },

        complete: function(eventType, args) {
            document.getElementById(commentLikeImgId).style.visibility = 'hidden';
        }

    };

    var handleSuccess = function(o){

        var r = YAHOO.lang.JSON.parse(o.responseText);

        if (r.replyCode) {

            if (r.replyCode == 200) {
                //  success
                //  note: we now return json data about inserted comment so this is not used
                document.getElementById(commentLikeMsg).innerHTML = 'You like this';
                document.getElementById(commentLikeMsg).style.display = 'inline';
            }
            else if (r.replyCode == 201) {
                //  success
                document.getElementById(commentLikeMsg).innerHTML = 'You like this';
                document.getElementById(commentLikeMsg).style.display = 'inline';
            }
            else if (r.replyCode > 299) {
                alert("Oops. Cannot tally your opinion. " + r.replyText);
                return {};
            }
            else {
                alert("Oops. Cannot tally your opinion. Something unexpected happened. " + r.replyText);
                return {};
            }
        } else {
            alert("Oops. Cannot tally your opinion. " + o);
        }
        return o;

        var response = o.responseText;
        response = response.split("<!")[0];
    };

    var handleFailure = function(o){

        if(o.responseText !== undefined){
            alert("Oops. We experienced a problem while tallying your opinion: " + o.status + ' ' + o.responseText.substr(0,200) );
        } else {
            alert("Oops. We experienced a problem while tallying your opinion. No more information is available." );
        }

        document.getElementById(commentLikeMsg).innerHTML = 'error';
        document.getElementById(commentLikeMsg).style.display = 'inline';
    };

    var callback =
    {
        customevents: {
            onStart: handleEvent.start,
            onComplete: handleEvent.complete
            ////onSuccess: handleEvent.success,
            ////onFailure: handleEvent.failure,
            ////onAbort: handleEvent.abort
        },
        scope:handleEvent,

        success:handleSuccess,
        failure:handleFailure,
        argument:['foo','bar']
    };

    var cObj = YAHOO.util.Connect.asyncRequest('POST', '../_services/content-comment/submitCommentLike.php', callback, "commentid="+commentId);

}
