// http://tkhere.blogspot.com/2006/02/howto-ajax-for-blogger-peek-boo.html
function ahah(serverPage, obj, objID) {
  obj.innerHTML = 'Fetching comments for this post. Please wait a moment...';
  if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); }
  else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  if (xmlhttp) {
    xmlhttp.onreadystatechange = function() { ahahDone(obj, objID); };
    xmlhttp.open("GET", serverPage, true);
    if (window.XMLHttpRequest) { xmlhttp.send(null); }
    else if (window.ActiveXObject) { xmlhttp.send(); }
  }
}

function ahahDone(obj, objID) {
  if (xmlhttp.readyState == 4) {
    if (xmlhttp.status == 200 || req.status == 304) { 
      if (objID.indexOf("post") != -1) { getPost(obj); }
      else if (objID.indexOf("blinks") != -1) { getBlinks(obj); }
      else if (objID.indexOf("comments") != -1) { getComments(obj); }
    }
    else { obj.innerHTML="Status:\n" + xmlhttp.statusText; }
  }
}

function getPost(obj) {
  resText = xmlhttp.responseText;
  start = resText.indexOf("Begin .post");
  start1 = resText.indexOf('<h3 class="post-title">', start);
  start2 = resText.indexOf('</h3>', start1) + 5;
  end = resText.indexOf('<p class="post-footer">', start2);
  obj.innerHTML = resText.substring(start2, end);
}

function getBlinks(obj) {
  resText = xmlhttp.responseText;
  start = resText.indexOf("<!-- Begin #comments -->");
  start1 = resText.indexOf('<a name="links">', start);
  end = resText.indexOf('<p class="comment-timestamp">', start1);
  obj.innerHTML = resText.substring(start1, end);
}

function getComments(obj) {
  resText = xmlhttp.responseText;
  start = resText.indexOf("<!-- Begin #comments -->");
  end = resText.indexOf('<a name="links">', start);
  obj.innerHTML = resText.substring(start, end);
}

function toggler(serverPage, linkobj, objID) {
  var obj = getElement(objID);
  if (isShown(objID)) {
    obj.style.display = "none";
    obj.innerHTML = "";
    if (objID.indexOf("post") != -1) { linkobj.innerHTML = "Click to view"; }
    else if (objID.indexOf("blinks") != -1) { linkobj.innerHTML = "Show Backlinks"; }
    else if (objID.indexOf("comments") != -1) {
      if (linkobj.innerHTML == "0 comments") {
        window.open("http://www.blogger.com/comment.g?blogID=21524530&postID=" + objID.substring(0, objID.indexOf("comments")));
      }
    }
  }
  else {
    if (objID.indexOf("post") != -1) { 
      obj.style.display = "block";
      linkobj.innerHTML = "Click to hide";
      ahah(serverPage, obj, objID);
    }
    else if (objID.indexOf("blinks") != -1) {
      obj.style.display = "block";
      linkobj.innerHTML = "Hide Backlinks";
      ahah(serverPage, obj, objID);
    }
    else if (objID.indexOf("comments") != -1) {
      if (linkobj.innerHTML == "0 comments") {
        window.open("http://www.blogger.com/comment.g?blogID=21524530&postID=" + objID.substring(0, objID.indexOf("comments")));
      }
      else { 
        obj.style.display = "block";
        ahah(serverPage, obj, objID);
      }
    }
  }
}

function getElement(id) { return document.getElementById(id); }

function isShown(id) { return getElement(id).style.display != "none"; }


