//
// Default time out interval for the ajax requests.
//
var commentsJs_defaultTimeOut = 30000;


//******************************************************************************
//
//  Function:   submitComment
//
//  Purpose:    Submits a comment to the database for the given detail type 
//              and detail id.
//
//  Parameters: textArea - the textArea where the comment is inputed 
//              
//              detailTypeIdToComment - the detail type id to comment on (this
//                                      parameter is option, the function will 
//                                      take the page's detail type id if not 
//                                      specified)
//              
//              detailIdToComment - the detail id to comment on (this parameter
//                                  is optional, the function will take the 
//                                  page's detail id if not specified).
//              
//              autorefresh - Whether to reload the comments section so the
//                            newly submitted comment will be reflected in the
//                            list.
//
//              timeout - Timeout for the submit comment ajax request (if not
//                        specified the default timeout will be applied).
//
//  Returns:    Nothing
//
//*****************************************************************************/
function submitComment(textArea, 
                       detailTypeIdToComment, 
                       detailIdToComment, 
                       autorefresh, 
                       timeout) {
                       
                       
  //
  // This method will only be executed, if a comments page element is 
  // available in the page, either directly or through an ajax page element.
  //   
  if (window.commentsElementId && window.commentsElementId > 0) {
                       
    //
    // If the detailTypeIdToComment has not been specified, 
    // use the global one for the page.
    // 
    if (!detailTypeIdToComment && 
         window.GLOBAL_DETAIL_TYPE_ID && 
         window.GLOBAL_DETAIL_TYPE_ID > 0) {
      detailTypeIdToComment = window.GLOBAL_DETAIL_TYPE_ID;
    }
  
    //
    // If the detailIdToRate has not been specified, use the global 
    // one for the page.
    //
    if (!detailIdToComment && 
        window.GLOBAL_DETAIL_ID && 
        window.GLOBAL_DETAIL_ID > 0) {
      detailIdToComment = window.GLOBAL_DETAIL_ID;
    }  

    //
    // If all the parameters have been received ok, proceed with
    // submission.
    //
    if(textArea && 
       detailTypeIdToComment && 
       detailIdToComment &&
       detailTypeIdToComment > 0 && 
       detailIdToComment > 0) {
     
      $.ajax({
        type: "POST",
        url: '/page/commentSubmission',
        data: 'detailTypeId=' + detailTypeIdToComment +            
              '&id=' + detailIdToComment + 
              '&commentText=' + encodeURI(textArea.value),
        success: function(msg){
          //
          // On submission success, if the autorefresh flag has been
          // set to true, refresh the comments listing so the newly
          // submitted comment will become visible.
          // 
          if (autorefresh && window.commentsElementId > 0) {    
            refreshCommentsList(
              detailTypeIdToComment, 
              detailIdToComment, 
              1, //first page.
              true, // after submitting get the uncached version just in case jms takes a little longer to flush.
              timeout
            );
          }
        
          //
          // On submission success, call the "onCommentSubmitted" method. This
          // method is optional and can be added to a page to customise the 
          // comment submission experience on a page by page basis.
          //
          if(window.onCommentSubmitted) {       
            onCommentSubmitted(detailTypeIdToComment, detailIdToComment);
          }
        },
        error: function(msg) {
          //
          // On submission error, call an optional function that we
          // can drop in a page to customise the user experience in 
          // a page by page basis.
          //
          if(window.onCommentSubmittedError) {       
            onCommentSubmittedError(detailTypeIdToComment, detailIdToComment);
          }     
        }   
      });
    }
  }  
  return;
}



//******************************************************************************
//
//  Function:   replyComment
//
//  Purpose:    Submits a reply to the comment specified by the 
//              passed in comment id.  
//
//  Parameters: textArea - the textArea where the reply is inputed 
//      
//              commentId - The id of the comment to which the reply applies.
//        
//              commentsSourceDetailTypeId - This is the detail type of the
//                                           object that originated the comments
//                                           thread within which this reply sits.
//              
//              commentsSourceDetailId - This is the detail id of the object
//                                       that originated the comments thread
//                                       within which this reply sits.
//              
//              pageNumber - This is the page number where the comment we are
//                           replying to sits. This is needed in case we want
//                           to reload the comments section so the reply just
//                           submitted to be shown. This parameter will tell
//                           us which page within the list of comments we need
//                           to reload.
//
//              autorefresh - Whether to reload the comments section so the
//                            newly submitted reply will be reflected in the
//                            list.
//
//              timeout - Timeout for the submit reply ajax request (if not
//                        specified the default timeout will be applied).
//
//  Returns:    Nothing
//
//*****************************************************************************/
function replyComment(textArea,
                      commentId, 
                      commentsSourceDetailTypeId, 
                      commentsSourceDetailId, 
                      pageNumber, 
                      autorefresh,
                      timeout) {
  
  //
  // This method will only be executed, if a comments page element is 
  // available in the page, either directly or through an ajax page element.
  //   
  if (window.commentsElementId && window.commentsElementId > 0) {
  
    //
    // If the detailTypeIdToComment has not been specified, 
    // use the global one for the page.
    // 
    if (!commentsSourceDetailTypeId && 
        window.GLOBAL_DETAIL_TYPE_ID && 
        window.GLOBAL_DETAIL_TYPE_ID > 0) {
      commentsSourceDetailTypeId = window.GLOBAL_DETAIL_TYPE_ID;
    }
  
    //
    // If the detailIdToRate has not been specified, use the global 
    // one for the page.
    //
    if (!commentsSourceDetailId && 
        window.GLOBAL_DETAIL_ID && 
        window.GLOBAL_DETAIL_ID > 0) {
      commentsSourceDetailId = window.GLOBAL_DETAIL_ID;
    }  

    //
    // If all the parameters have been received ok, proceed with
    // submission.
    //
    if(textArea && 
       commentId > 0 &&
       commentsSourceDetailTypeId && 
       commentsSourceDetailId &&
       commentsSourceDetailTypeId > 0 && 
       commentsSourceDetailId > 0) {
  
      $.ajax({
        type: "POST",
        url: '/page/commentSubmission',
        data: 'detailTypeId=47' +
              '&id=' + commentId + 
              '&commentText=' + encodeURI(textArea.value),
        success: function(msg){
          //
          // On submission success, if the autorefresh flag has been
          // set to true, refresh the comments listing so the newly
          // submitted reply will become visible.
          // 
          if (autorefresh && window.commentsElementId) {    
            refreshCommentsList(
              commentsSourceDetailTypeId, 
              commentsSourceDetailId, 
              pageNumber,
              true, // after submitting get the uncached version just in case jms takes a little longer to flush.
              timeout
            );
          }
        
          //
          // On reply submission success, call the "onReplySubmitted" method. This
          // method is optional and can be added to a page to customise the reply 
          // submission experience on a page by page basis.
          //
          if(window.onReplySubmitted) {       
            onReplySubmitted(commentId, commentsSourceDetailTypeId, commentsSourceDetailId);
          }
        },
        error: function(msg) {
          //
          // On submission error, call an optional function that we
          // can drop in a page to customise the user experience in 
          // a page by page basis.
          //
          if(window.onReplySubmittedError) {       
            onReplySubmittedError(commentId, commentsSourceDetailTypeId, commentsSourceDetailId);
          }  
        }            
      });
    }
  } 
  return;
}


//******************************************************************************
//
//  Function:   refreshCommentsList
//
//  Purpose:    Gets the comments list associated to the specified detail type
//              and instance id, and refreshes it in the page.  
//
//  Parameters: detailTypeId - The detail type id of the object whose comments
//                             list to refresh. 
//      
//              detailInstanceId - The instance id of the object whose comments
//                                 list to refresh.
//
//              pageNumber - This is the page number to display within the
//                           paginated list of comments associated to the 
//                           object.
//              uncached - Boolean to set whether we should bypass the plugin
//                         cache and go directly to the tomcats to get the comments. 
//
//              timeoutMs - Timeout for the ajax request (if not specified the 
//                          default timeout will be applied).
//
//  Returns:    Nothing
//
//*****************************************************************************/
function refreshCommentsList(detailTypeId, 
                             detailInstanceId, 
                             pageNumber,
                             uncached,
                             timeoutMs) {
  //
  // This method will only be executed, if a comments page element is 
  // available in the page, either directly or through an ajax page element.
  //   
  if (window.commentsElementId && window.commentsElementId > 0) {
    //
    // If the detailTypeId has not been specified, 
    // use the global one for the page.
    // 
    if (!detailTypeId && 
        window.GLOBAL_DETAIL_TYPE_ID && 
        window.GLOBAL_DETAIL_TYPE_ID > 0) {
      detailTypeId = window.GLOBAL_DETAIL_TYPE_ID;
    }
  
    //
    // If the detailInstanceId has not been specified, use the global 
    // one for the page.
    //
    if (!detailInstanceId && 
        window.GLOBAL_DETAIL_ID && 
        window.GLOBAL_DETAIL_ID > 0) {
      detailInstanceId = window.GLOBAL_DETAIL_ID;
    }  
    
    //
    // If timeout has not been specified, use the default timeout.
    //
    if (!timeoutMs) {
      timeoutMs = commentsJs_defaultTimeOut;
    }  
    
    //
    // Make an ajax call to page element server servlet, to get the html
    // associated to the comments page element (234 is the id of the
    // comments page element).      
    //               
    var url = "/page/pageelementservlet" +
              ((uncached == true)?"/uncached_y":"") +  
              "/pageelementtypeid_234" +
              "/instanceid_" + commentsElementId  + 
              "/0,," + (window.GLOBAL_SITE_ID?GLOBAL_SITE_ID:0) +
              "~" + ((detailInstanceId>0)?detailInstanceId:0) +
              "~" + ((detailTypeId>0)?detailTypeId:0) +                             
              "~" + (window.GLOBAL_PAGE_ID?GLOBAL_PAGE_ID:0) +
              "~" + pageNumber + 
              ",00.html";
    $.ajax({
      url: url,
      type: 'GET',
      dataType: 'html',
      cache: false,
      timeout: timeoutMs,
      success: function(html){
        if ($('#commentsListing_' + detailTypeId + '_' + detailInstanceId)) {
          $('#commentsListing_' + detailTypeId + '_' + detailInstanceId).replaceWith(html);
          if (html=="") {
            $('#commentsListing_' + detailTypeId + '_' + detailInstanceId).css("display","none");  
          } else {
            $('#commentsListing_' + detailTypeId + '_' + detailInstanceId).css("display","block");
          }
        }  
      },
      error: function() {
        //
        // retry refresh with a longer timeout.
        //
        refreshCommentsList(detailTypeId, 
                            detailInstanceId,                            
                            pageNumber,
                            timeoutMs*4); 
      }      
    });
  }
}  


//******************************************************************************
//
//  Function:   reportComment
//
//  Purpose:    Sends the user to the report abuse page  
//
//  Parameters: detailTypeId - The detail type id of the object whose comments
//                             list to refresh. 
//      
//              detailInstanceId - The instance id of the object whose comments
//                                 list to refresh.
//
//              pageNumber - This is the page number to display within the
//                           paginated list of comments associated to the 
//                           object.
//
//              timeoutMs - Timeout for the ajax request (if not specified the 
//                          default timeout will be applied).
//
//  Returns:    Nothing
//
//*****************************************************************************/
function reportComment(reportAbusePagePath, commentId) {
  
  if (commentId && commentId > 0) {
    //
    // Load the report abuse page.
    //
    document.location.href = 
      reportAbusePagePath + 
      "?detailTypeId=47" +  
      "&detailId=" + commentId +
      "&pageId=" + (window.GLOBAL_PAGE_ID?window.GLOBAL_PAGE_ID:-1) +
      "&pageDetailId=" + (window.GLOBAL_DETAIL_ID?window.GLOBAL_DETAIL_ID:-1);
  }      
}

