function showCommentsPage(id, videoId, page) {
   filterCommentsPage(id, videoId, page, 0);
}

function filterCommentsPage(id, videoId, page, rating) {
   var commentsDiv = document.getElementById(id);
   var xmlHttp = getHttpRequest();

   if (commentsDiv == null || xmlHttp == null) {
      return;
   }

   var qs = new StringBuffer();
   qs.append("v=").append(encodeURIComponent(videoId));
   qs.append("&p=").append(encodeURIComponent(page));
   qs.append("&f=").append(encodeURIComponent(rating));

   xmlHttp.open("POST", "/js-dyn/article/comments.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         commentsDiv.innerHTML = xmlHttp.responseText;
         window.location.href = '#comments';
      }
   }
   xmlHttp.send(qs.toString());
}
function rateComment(vid, cid, score) {
   var scoreboard = document.getElementById("comment_rating_score_" + cid);
   var xmlHttp = getHttpRequest();

   if (scoreboard == null || xmlHttp == null) {
      return;
   }

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(vid));
   qs.append("&");
   qs.append("ci=");
   qs.append(encodeURIComponent(cid));
   qs.append("&");
   qs.append("r=");
   qs.append(encodeURIComponent(score));

   xmlHttp.open("POST", "/js-dyn/article/ratecomment.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         var rating = Number(xmlHttp.responseText);

         if (rating > 0) {
            scoreboard.style.color = "green";
         } else if (rating < 0) {
            scoreboard.style.color = "red";
         } else {
            scoreboard.style.color = "grey";
         }

         scoreboard.innerHTML = rating;
      }
   }
   xmlHttp.send(qs.toString());
}

function LastOpenedCommentForm() {
   var instance = this;

   this.referenceToFormElement = null;

   this.set = function(e) {
      instance.referenceToFormElement = e;
   }

   this.get = function() {
      return instance.referenceToFormElement;
   }

   this.exists = function() {
      try {
         if (instance == null) {
            return false;
         }

         if (instance.referenceToFormElement == null) {
            return false;
         }

         if (instance.referenceToFormElement.nodeType != 1) {
            return false;
         }

         if (instance.referenceToFormElement.getAttribute("id") == null) {
            return false;
         }

         return document.getElementById(instance.referenceToFormElement.getAttribute("id")) != null;
      } catch(e) {
         return null;
      }
   }
}

var lastOpenedCommentForm = new LastOpenedCommentForm();
var commentOfLoggedOutMember = "";

function openCommentForm(afterNode, vid, cid, showCloseButton) {
   var xmlHttp = getHttpRequest();
   var commentsHead = document.getElementById(afterNode);

   if (xmlHttp == null || commentsHead == null) {
      return;
   }

   // close prevously opened form
   if (lastOpenedCommentForm.exists()) {
      if (lastOpenedCommentForm.get().parentNode != null) {
         lastOpenedCommentForm.get().parentNode.removeChild(lastOpenedCommentForm.get());
      } else {
         closeCommentForm(lastOpenedCommentForm.get().getAttribute("id"));
      }
   }

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(vid));
   qs.append("&");
   qs.append("c=");
   qs.append(encodeURIComponent(cid));
   qs.append("&");
   qs.append("b=");
   qs.append(encodeURIComponent(showCloseButton));

   xmlHttp.open("POST", "/js-dyn/article/opencommentform.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         var tmp = document.createElement("div");

         if (xmlHttp.responseText != null) {
            tmp.innerHTML = xmlHttp.responseText.trim();
         }

         var referenceToOpenedForm = insertAfter(tmp.firstChild, commentsHead);

         if (cid != null && cid.trim().length > 0) {
            lastOpenedCommentForm.set(referenceToOpenedForm);
         }
      }
   }
   xmlHttp.send(qs.toString());
}

function sendComment(vid, cid, text, close, loggedin) {
   commentOfLoggedOutMember = "";
   close = eval(close);
   loggedin = eval(loggedin);

   if (text == null || text.trim().length == 0) {
      alert("Lütfen yorum yazınız.");
      return;
   }

   var commentFormId = (cid == null || cid.trim().length == 0) ? "comment_form" : "comment_form_" + cid;
   var xmlHttp = getHttpRequest();

   if (document.getElementById(commentFormId) == null || xmlHttp == null) {
      return;
   }

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(vid));
   qs.append("&");
   qs.append("p=");
   qs.append(encodeURIComponent(cid));
   qs.append("&");
   qs.append("t=");
   qs.append(encodeURIComponent(text));
   qs.append("&");
   qs.append("b=");
   qs.append(encodeURIComponent(close));

   if (!loggedin) {
      commentOfLoggedOutMember = text;
   }

   xmlHttp.open("POST", "/js-dyn/article/addcomment.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         // insert new comment
         if (xmlHttp.responseText != null && xmlHttp.responseText.trim().length > 0) {
            var tmp = document.createElement("div");
            tmp.innerHTML = xmlHttp.responseText;

            var comment_container = document.getElementById("comment_items");
            var comment_items = [];

            for (var i = 0; comment_container != null && i < comment_container.childNodes.length; i++) {
               var comment_item = comment_container.childNodes[i];

               if (comment_item.nodeType == 1) {
                  comment_items[comment_items.length] = comment_item;
               }
            }

            for (var j = tmp.childNodes.length - 1; j >= 0; j--) {
               if (tmp.childNodes[j].nodeType != 1) {
                  tmp.childNodes[j].parentNode.removeChild(tmp.childNodes[j]);
               }
            }

            if (loggedin) {
               if (cid == null || cid.trim().length == 0) {
                  if (tmp.innerHTML.indexOf("id=\"cta\"") > 0) {
                     // in case when input form returned (for example on dublicate comment)
                     if (document.getElementById(commentFormId) != null) {
                        document.getElementById(commentFormId).parentNode.replaceChild(tmp, document.getElementById(commentFormId));
                     }
                  } else {
                     // everythign went OK, so just publish the comment
                     if (comment_items.length > 0) {
                        comment_items[0].parentNode.insertBefore(tmp, comment_items[0]);
                     } else {
                        comment_container.appendChild(tmp);
                     }
                  }
               } else {
                  // just shoe login or signup form
                  var parent = document.getElementById("comment_" + cid);

                  if (close) {
                     closeCommentForm(document.getElementById(commentFormId).getAttribute("id"));
                  }

                  if (parent != null) {
                     insertAfter(tmp, parent);
                  }
               }

               if (document.getElementById("cta") != null) {
                  document.getElementById("cta").value = "";
               }
            } else {
               if (document.getElementById(commentFormId) != null) {
                  document.getElementById(commentFormId).parentNode.replaceChild(tmp, document.getElementById(commentFormId));
               }
            }

            if (document.getElementById("comment_send_button") != null) {
               document.getElementById("comment_send_button").disabled = false;
            }

            if (document.getElementById("comments_head") != null && loggedin){
               document.getElementById("comments_head").style.display = "block";
            }
         }
      }
   }
   xmlHttp.send(qs.toString());

   if (document.getElementById("comment_send_button") != null) {
      document.getElementById("comment_send_button").disabled = true;
   }
}

function login4Comment(v, p, b) {
   b = ("true" == b);

   var xmlHttp = getHttpRequest();
   var commentForm = null;
   var username = null;
   var password = null;

   if (p == null || p.trim().length == 0) {
      commentForm = document.getElementById("comment_form");
      username = document.getElementById("username");
      password = document.getElementById("password");
   } else {
      commentForm = document.getElementById("comment_form_" + p);
      username = document.getElementById("username" + p);
      password = document.getElementById("password" + p);
   }

   if (commentForm == null || xmlHttp == null || username == null || password == null) {
      return;
   }

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(v));
   qs.append("&");
   qs.append("c=");
   qs.append(encodeURIComponent(p));
   qs.append("&");
   qs.append("b=");
   qs.append(encodeURIComponent(b));
   qs.append("&");
   qs.append("username=");
   qs.append(encodeURIComponent(username.value));
   qs.append("&");
   qs.append("password=");
   qs.append(encodeURIComponent(password.value));

   xmlHttp.open("POST", "/js-dyn/article/login4comment.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         var result = eval("(" + xmlHttp.responseText + ")");

         if (result.s == "success") {
            // save comment
            sendComment(result.v, result.p, commentOfLoggedOutMember, result.b, true);

            // replace comment form at the bottom of the comments
            closeCommentForm("comment_form");
            openCommentForm("comment_pages", result.v, '', false);
         } else {
            var msgs = new StringBuffer();

            for (var i = 0; i < result.m.length; i++) {
               msgs.append("\n- ");
               msgs.append(result.m[i]);
            }

            alert(msgs.toString());
         }
      }
   }
   xmlHttp.send(qs.toString());
}

function signup4Comment(v, p, b) {
   var xmlHttp = getHttpRequest();
   var commentForm = null;
   var username = null;
   var email = null;
   var birthYear = null;
   var birthMonth = null;
   var birthDay = null;
   var password1 = null;
   var password2 = null;
   var eula = null;

   if (p == null || p.trim().length == 0) {
      commentForm = document.getElementById("comment_form");
      username = document.getElementById("username");
      email = document.getElementById("email");
      birthYear = document.getElementById("birthYear");
      birthMonth = document.getElementById("birthMonth");
      birthDay = document.getElementById("birthDay");
      password1 = document.getElementById("password1");
      password2 = document.getElementById("password2");
      eula = document.getElementById("eula");
   } else {
      commentForm = document.getElementById("comment_form_" + p);
      username = document.getElementById("username" + p);
      email = document.getElementById("email" + p);
      birthYear = document.getElementById("birthYear" + p);
      birthMonth = document.getElementById("birthMonth" + p);
      birthDay = document.getElementById("birthDay" + p);
      password1 = document.getElementById("password1" + p);
      password2 = document.getElementById("password2" + p);
      eula = document.getElementById("eula" + p);
   }

   if (commentForm == null || xmlHttp == null || username == null || email == null || password1 == null || password2 == null || eula == null) {
      return;
   }

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(v));
   qs.append("&");
   qs.append("c=");
   qs.append(encodeURIComponent(p));
   qs.append("&");
   qs.append("b=");
   qs.append(encodeURIComponent(b));
   qs.append("&");
   qs.append("username=");
   qs.append(encodeURIComponent(username.value));
   qs.append("&");
   qs.append("email=");
   qs.append(encodeURIComponent(email.value));
   qs.append("&");
   qs.append("password1=");
   qs.append(encodeURIComponent(password1.value));
   qs.append("&");
   qs.append("password2=");
   qs.append(encodeURIComponent(password2.value));
   qs.append("&");
   qs.append("eula=");
   qs.append(encodeURIComponent((eula.checked)));

   xmlHttp.open("POST", "/js-dyn/article/signup4comment.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         var result = eval("(" + xmlHttp.responseText + ")");
         var success = (result.s != null && result.s.trim() == "success");

         if (success) {
            // show status
            showStatusMessage("comments_status_success", result.m);

            // save comment
            sendComment(result.v, result.p, commentOfLoggedOutMember, result.b, true);

            // replace comment form at the bottom of the comments
            closeCommentForm("comment_form");
            openCommentForm("comment_pages", result.v, '', false);

            // move page to comments block head
            window.location.href = "#comments";
         } else {
            var msgs = new StringBuffer();

            for (var i = 0; i < result.m.length; i++) {
               msgs.append("\n- ");
               msgs.append(result.m[i]);
            }

            alert(msgs.toString());
         }
      }
   }
   xmlHttp.send(qs.toString());
}

function showStatusMessage(divId, mesasge) {
   var statusBarExist = (document.getElementById(divId) != null);
   var statusBar = (statusBarExist) ? document.getElementById(divId) : document.createElement("div");

   statusBar.innerHTML = mesasge;

   if (!statusBarExist) {
      statusBar.setAttribute("id", divId);

      var commentsHead = document.getElementById("comments_head");

      if (commentsHead == null) {
         alert(mesasge);
         return;
      }

      commentsHead.parentNode.insertBefore(statusBar, commentsHead);
   }
}

function closeCommentForm(id) {
   var form = document.getElementById(id);

   if (form != null) {
      form.parentNode.removeChild(form);
   }
}

function openLoginForm(v, p, b) {
   var xmlHttp = getHttpRequest();
   var commentForm = (p == null || p.trim().length == 0) ? document.getElementById("comment_form") : document.getElementById("comment_form_" + p);

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(v));
   qs.append("&");
   qs.append("c=");
   qs.append(encodeURIComponent(p));
   qs.append("&");
   qs.append("b=");
   qs.append(encodeURIComponent(b));

   xmlHttp.open("POST", "/js-dyn/article/loginform4comment.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.responseText != null && xmlHttp.responseText.trim().length > 0) {
            var tmp = document.createElement("div");
            tmp.innerHTML = xmlHttp.responseText;

            if (commentForm != null) {
               commentForm.parentNode.replaceChild(tmp, commentForm);
            }
         }
      }
   }
   xmlHttp.send(qs.toString());
}


function openSignupForm(v, p, b) {
   b = ("true" == b);

   var xmlHttp = getHttpRequest();
   var commentForm = (p == null || p.trim().length == 0) ? document.getElementById("comment_form") : document.getElementById("comment_form_" + p);

   var qs = new StringBuffer();
   qs.append("v=");
   qs.append(encodeURIComponent(v));
   qs.append("&");
   qs.append("c=");
   qs.append(encodeURIComponent(p));
   qs.append("&");
   qs.append("b=");
   qs.append(encodeURIComponent(b));

   xmlHttp.open("POST", "/js-dyn/article/signupform4comment.js", true);
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.responseText != null && xmlHttp.responseText.trim().length > 0) {
            var tmp = document.createElement("div");
            tmp.innerHTML = xmlHttp.responseText;

            if (commentForm != null) {
               commentForm.parentNode.replaceChild(tmp, commentForm);
            }
         }
      }
   }
   xmlHttp.send(qs.toString());
}

function plus(id, activate) {
   var plus = document.getElementById(id);

   if (plus != null) {
      if (activate) {
         plus.setAttribute("src", "/img/ikon_arti_h.gif");
      } else {
         plus.setAttribute("src", "/img/ikon_arti.gif");
      }
   }

   return true;
}

function minus(id, activate) {
   var plus = document.getElementById(id);

   if (plus != null) {
      if (activate) {
         plus.setAttribute("src", "/img/ikon_eksi_h.gif");
      } else {
         plus.setAttribute("src", "/img/ikon_eksi.gif");
      }
   }

   return true;
}

function checkChars(ta, L) {
   var len = ta.value.length;
   if (len > L) {
      ta.value = ta.value.substring(0, L);
      document.getElementById(ta.id + "chars").innerHTML = "0";
      return;
   }
   document.getElementById(ta.id + "chars").innerHTML = L - len;
}
