/////////////////////////////////////////////////////////////
// Methods to add/edit/delete ObjectComments

var deleteComment = function(formIdentifier, nObjectCommentId)
{
  var form = document.getElementById('deleteObjectComment'+formIdentifier);
  form.Command.value = 'DELETE';
  form.ObjectCommentId.value = nObjectCommentId;

  $.post( form.action,
          $(form).serialize(),
          function( oData, sStatus )
          {
            if(( typeof oData === 'object' ) && ( typeof oData.success === 'boolean' ))
            {
              if( oData.success === true )
              {
                var divCommentNode = $('#commentNode'+formIdentifier+'_'+nObjectCommentId)[0];
                divCommentNode.parentNode.removeChild(divCommentNode);
                var sNewLabel = updateObjectCommentCount(oData.nTotalComments, formIdentifier);
                if (form.objectType.value == 'RESOURCE')
                {
                  updateFlightboardDisplay(form.objectId.value, sNewLabel);
                }
              }
              else
              {
                alert('An error occurred in processing the request to delete the comment: ' + oData.message);
              }
            }
            else
            {
              alert('Server returned unknown status on attempt to delete the comment.');
            }
          },
          'json'
  );
}

var insertComment = function(thisForm, response)
{
  // Insert the new or editied comment into the DOM.
  var sInstanceIdentifier = response.sInstanceIdentifier;
  var nObjectCommentId = response.id;
  var sCommand = response.Command;
  var sCommentNode = 'commentNode' + sInstanceIdentifier + '_' + nObjectCommentId;

  try
  {
    var sNewLabel = 'Comments';
    var divComments = $('#comments'+sInstanceIdentifier)[0];
    if (divComments != null)
    {
      // Only do the insert if the display is shown.  We may not have a display
      // if we are in the flightboard and the comments display is not expanded.
      if (sCommand == 'EDIT')
      {
        // If it's an edited comment, remove the whole thing, then put the new one
        // at the top of the list, as it
        var divCommentNode = $('#commentNode'+sInstanceIdentifier+'_'+nObjectCommentId)[0];
        divCommentNode.parentNode.removeChild(divCommentNode);
      }

      var head = document.getElementsByTagName("head")[0];
      var divNewComment = document.createElement('div');
      divNewComment.setAttribute('id', sCommentNode);
      divNewComment.innerHTML = response.sObjectCommentDisplay;
      divComments.appendChild(divNewComment);

      // Update the count even for an edit, as there could have been
      // other changes by other users.
      sNewLabel = updateObjectCommentCount(response.nTotalComments, sInstanceIdentifier, thisForm.objectId.value);
      processScripts(response.sObjectCommentDisplay, head);
    }

    if (thisForm.objectType.value == 'RESOURCE')
    {
      updateFlightboardDisplay(thisForm.objectId.value, sNewLabel);
    }
  }
  catch (e)
  {
    alert('e:' + e.toSource() + '\n\n Data received for ObjectComment:\n' + response);
  }
}

function updateObjectCommentCount(nCount, sInstanceIdentifier)
{
  // Update the count on the display
  var sLabel = nCount + " Comment";
  if (nCount != 1)
  {
    sLabel += "s";
  }

  oObj = document.getElementById('totalComments'+sInstanceIdentifier);
  if (oObj)
  {
    oObj.innerHTML = sLabel;
  }

  return sLabel;
}

function updateFlightboardDisplay(nObjectId, sLabel)
{
  // This stuff is for comments living in a resource display on the flightboard.
  // Should we ever change that implmentation to use say microprofiles,
  // then we would be able to get rid of this section.
  var sDiv = 'viewComments' + nObjectId;
  var divCommentTab = $('#' + sDiv)[0];
  if (divCommentTab)
  {
    var childNode = divCommentTab.childNodes[0];
    if (childNode)
    {
      childNode.innerHTML = sLabel;
    }
    if (typeof removeMetaFromCache == 'function')
    {
      removeMetaFromCache(nObjectId);
    }
  }
}
