    var commentoffset = 0;      // Will be set by the return from makeAJAXGetMoreComments

    YAHOO.util.Event.onDOMReady(function() {
        YAHOO.util.Event.addListener("prevpagecomments", "click", makeAJAXGetLessComments);
        YAHOO.util.Event.addListener("nextpagecomments", "click", makeAJAXGetMoreComments);

    });


    var makeAJAXGetLessComments = function() {
        makeAJAXGetComments('previous');
    }

    var makeAJAXGetMoreComments = function() {
        makeAJAXGetComments('next');
    }

    var makeAJAXGetComments = function(direction) {
        YAHOO.util.Dom.get('comments').innerHTML = '';
        var surl = ajax_post_cmthandler_url + '?op=getmorecomments&direction=' + direction + '&source=mgext&sid='+ mid + '&offset=' + commentoffset;
        var callback = {
            success: function(o) {
                var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1);
                var oResults = eval('(' + json + ')');
                if (oResults.retcode == 200) {
                    commentoffset = oResults.offset;
                    if (commentoffset ==  0){
                        YAHOO.util.Dom.setStyle('prevcomments_on','display','none');
                        YAHOO.util.Dom.setStyle('prevcomments_off','display','');
                        YAHOO.util.Dom.setStyle('nextcomments_on','display','');
                        YAHOO.util.Dom.setStyle('nextcomments_off','display','none');
                    } else if (commentoffset + 50 > numcomments || oResults.moredata == false){
                        YAHOO.util.Dom.setStyle('prevcomments_on','display','');
                        YAHOO.util.Dom.setStyle('prevcomments_off','display','none');
                        YAHOO.util.Dom.setStyle('nextcomments_on','display','none');
                        YAHOO.util.Dom.setStyle('nextcomments_off','display','');
                    } else {
                        YAHOO.util.Dom.setStyle('prevcomments_on','display','');
                        YAHOO.util.Dom.setStyle('prevcomments_off','display','none');
                        YAHOO.util.Dom.setStyle('nextcomments_on','display','');
                        YAHOO.util.Dom.setStyle('nextcomments_off','display','none');
                    }
                    YAHOO.util.Dom.get('comments').innerHTML = oResults.displayhtml;
                }
            },
            failure: function(o) {
                alert('AJAX error loading additional comments : ' + o.status);
            },
            argument: {},
            timeout:55000
        }

        YAHOO.util.Connect.asyncRequest('GET', surl, callback);

    };

   var commentRequestHandleSuccess = function(o){
    document.getElementById('pstatus').style.visibility='visible';
    document.getElementById('pstatuscontent').innerHTML=o.responseText;
    setTimeout("document.getElementById('pstatus').style.visibility='hidden';window.location.reload()", 1000);
        /*if(o.responseXML !== undefined) {
            var root = o.responseXML.documentElement;
            var error = root.getElementsByTagName('error')[0].firstChild.nodeValue;
            var op    = root.getElementsByTagName('op')[0].firstChild.nodeValue;
            var cid    = root.getElementsByTagName('cid')[0].firstChild.nodeValue;
            var cmtcount    = root.getElementsByTagName('cmtcount')[0].firstChild.nodeValue;

            if (error == 'NULL') {
                document.getElementById('pstatus').style.visibility='hidden';
                if (op == 'addreply') {
                    var form = 'document.fcomment' + cid;
                    form.value = '';
                    document.getElementById('cid'+cid).style.display='none';
                } else {
                    document.fcomment.comment.value = '';
                    document.fqcomment.comment.value = '';
                    document.getElementById('qcomment').style.display='none';
                    document.getElementById('pcommentlink').style.visibility = 'visible';
                }

                var commentpnode = root.getElementsByTagName('comments')[0];
                var commentHTML = '';
                for (var i = 0; i < commentpnode.childNodes.length; i++) {
                    commentHTML += commentpnode.childNodes[i].nodeValue;
                }
                document.getElementById('comments').innerHTML = commentHTML;
                document.getElementById('cmtcount').innerHTML = cmtcount;
            } else {
                document.getElementById('pstatuscontent').innerHTML = error;
            }


        }*/
    };
var commentRequestHandleFailure = function(o){
    document.getElementById('pstatus').style.visibility='visible';
    document.getElementById('pstatuscontent').innerHTML=o.responseText;
        
    };
    var commentRequestCallback =
    {
      success:commentRequestHandleSuccess,
      failure:commentRequestHandleFailure,
      argument: {},
      timeout:55500
    };

    function makeAJAXCommentRequest(formObject, op, cid) {
  
        //op='addcomment';
        if (op == 'addcomment') {
            document.getElementById('pstatuscontent').innerHTML=LANG_adding_comment;
            action = "/add_comment/"
        }  else if (op == 'addreply') {
            document.getElementById('pstatuscontent').innerHTML=LANG_adding_reply;
            action = "/add_comment/"
        }  else if (op == 'delcomment') {
            document.getElementById('pstatuscontent').innerHTML=LANG_deleting_comment;
            action = "/delete_comment/"+cid+"/";
        }  else if (op == 'flagcomment') {
            document.getElementById('pstatuscontent').innerHTML=LANG_flagging_comment;
            action = "/flag_comment/"
        }
        YAHOO.util.Connect.setForm(formObject, false);
        YAHOO.util.Connect.asyncRequest('POST', action, commentRequestCallback, op);
    }

    function showhide_qcomment() {
        if(document.getElementById('qcomment').style.display == 'none') {
            showElement('qcomment');
            document.getElementById('pcommentlink').style.visibility = 'hidden';
        } else {
            hideElement('qcomment');
            document.getElementById('pcommentlink').style.visibility = 'visible';
        }

    }