
function getDiv(id) {
  d = document.getElementById('comment_'+id);
  return d;
}

function editPost(id) {
  d = getDiv(id);
  html = HttpGetFile('/ajax.php?action=geteditcommentform&id='+id);
  d.innerHTML = html;
}

function cancelEditPost(id) {
  d = getDiv(id);
  if (id==0) {
    d.style.display = 'none';
    d.innerHTML = '';
  } else {
    html = HttpGetFile('/ajax.php?action=getcomment&showcomments=true&id='+id);
    d.innerHTML = html;
  }
}

function commentPost(id) {
  d = getDiv(id);
  html = HttpGetFile('/ajax.php?action=getcomment&showcomments=true&addone=true&id='+id);
//   html += HttpGetFile('/ajax.php?action=geteditcommentform&id=-1&id_parent_item='+id);
  d.innerHTML = html;
}

function newArticle() {
  d = getDiv(0);
  html = HttpGetFile('/ajax.php?action=geteditcommentform&id=0');
  d.innerHTML = html;
  d.style.display = 'block';
}

function showComments(id) {
  d = getDiv(id);
  html = HttpGetFile('/ajax.php?action=getcomment&showcomments=true&id='+id);
  d.innerHTML = html;
}

function hideComments(id) {
  d = getDiv(id);
  html = HttpGetFile('/ajax.php?action=getcomment&showcomments=false&id='+id);
  d.innerHTML = html;
}

function saveComment(f) {
  if (!f) return false;

  if (f.content.value == '') {
    alert('Sorry, you cannot post a comment without content');
    return false;
  }
  qs = '';
  qs += 'id='+f.id_comment.value+'&';
  qs += 'title='+escape(f.title.value)+'&';
  qs += 'content='+escape(f.content.value);
    if (f.id_parent_item) qs += '&id_parent_item='+escape(f.id_parent_item.value);
  if (f.type) qs += '&type='+escape(f.type.value);
  result = HttpGetFile('/ajax.php?action=savecomment&'+qs);
  if (result != 'OK') alert(result);

  return_id = f.id_comment.value;
  if (f.id_parent_item) return_id = f.id_parent_item.value;
  if (return_id == 0) {
    // New comments needs reload of the page
    window.location = 'welcome.php?';
    return;
  }
  d = getDiv(return_id);
  html = HttpGetFile('/ajax.php?action=getcomment&showcomments=true&id='+return_id);
  d.innerHTML = html;
  return false;
}

function deleteComment(id, return_id) {
  if (confirm('Do you realy want to delete this comment ?\n\nPress OK to confirm')) {
    result = HttpGetFile('/ajax.php?action=deletecomment&id='+id);
    if (result != 'OK') alert(result);

    if (return_id > 0) {
      html = HttpGetFile('/ajax.php?action=getcomment&showcomments=true&id='+return_id);
    } else {
      html = '';
    }
    d.innerHTML = html;
  }
}


