﻿
function appendCommentDiv(comment,allowDelete) {
  var div = document.createElement("DIV");
  div.className = "pictureComment";
  
  var divName = document.createElement("DIV");
  divName.className = "name";
  divName.innerHTML = comment.username+"<span class='date'>"+comment.date.toLocaleString()+"</span>";
  div.appendChild(divName);
  
  var divText = document.createElement("DIV");
  divText.className = "text";
  divText.innerHTML = comment.comment;
  div.appendChild(divText);
  
  if(allowDelete) {
    var temp = comment;
    var divDel = document.createElement("DIV");
    divDel.className = "delete";
    var a = document.createElement("A");
    a.href = "#";
    a.onclick = function(){deleteComment(div,comment);}
    a.innerHTML = "Delete";
    divDel.appendChild(a);
    
    div.appendChild(divDel);
  }
  
  pnlComments.appendChild(div);
}

function postComment() {
  if(txtUserName!=null){
    errUserName.style.display = (txtUserName.value=="")?"":"none";
  }
  errComment.style.display = (txtComment.value=="")?"":"none";
  var username = "";
  if(txtUserName!=null) username = txtUserName.value;
  PictureServices.PostComment(currPicID, username, txtComment.value, finishPostComment, handleAjaxError);
}

function finishPostComment(comment) {
  txtComment.value="";
  if(comment != null){
    appendCommentDiv(comment);
  }
}

function deleteComment(div, comment) {
  if(confirm("Are you sure you want to delete the comment by \""+comment.username+"\"?")){
    PictureServices.DeleteComment(comment.id, finishDeleteComment,handleAjaxError,div);
  }
}
function finishDeleteComment(result,div) {
  if(div != null){
    div.parentNode.removeChild(div);
  }
}

function toggleComments(bar) {
  if(pnlAllComments.style.display == "none"){
    pnlAllComments.style.display = "";
    imgShowComments.style.display = "none";
    imgHideComments.style.display = "";
    bar.title = "Hide Comments";
  }
  else
  {
    pnlAllComments.style.display = "none";
    imgShowComments.style.display = "";
    imgHideComments.style.display = "none";
    bar.title = "View/Add Comments";
  }
}
