/* Script for Li Fanxi's Blog */
/* For AJAX process           */
/* March 2007                 */

/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
var xmlHttp2 = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}
if (!xmlHttp2 && typeof XMLHttpRequest != 'undefined') {
  xmlHttp2 = new XMLHttpRequest();
}

// Get Latest Comments
function GetLatestComments()
{
    if (typeof xmlHttp2 == 'undefined')
        return;
    var url = "/blog/common/latest.asp";
    xmlHttp2.open("GET", url, true);
    xmlHttp2.onreadystatechange = FillLatestComments;
    xmlHttp2.send(null);
    return true;
}

// Fill latest comments
function FillLatestComments()
{
    if (xmlHttp2.readyState == 4)
    {
        if (xmlHttp2.status == 200)
        {
            var response = xmlHttp2.responseXML;
            var id = response.getElementsByTagName("id").item(0).firstChild.data; 
            // Update page
            document.getElementById("LatestComments").innerHTML =  response.getElementsByTagName("content").item(0).firstChild.data;
        }
    }
}
// Make a comment div
function CommentDiv(id)
{
    var url = "http://www.freemindworld.com/blog/";
    var year = "20" + id.substring(0,2);
    var page = escape(document.location.href);
    page = page.replace('/', '%2F');
    document.write( "<div class=\"commentdiv\" id=\"commentdiv_" + id +"\"><a href=\"/blog/common/comments.asp?ID=" + id + "&Page=" + page +"\" onclick=\"return CallServer('" + id + "','" + page + "');\">&#35780;&#35770;</a> | <a href=\"" + url + year + "/" + id + ".shtml\">&#22266;&#23450;&#38142;&#25509;</a><div id=\"commentdiv_ajax_" + id + "\"></div></div>");

    if ((document.getElementById("IndexDev") == null) && 
        (document.location.href.indexOf("archive.shtml") == -1))
        CallServer(id, page);
}

// Call server
function CallServer(id,page)
{
    if (document.getElementById("commentdiv_ajax_" + id).innerHTML != "")
    {
        document.getElementById("commentdiv_ajax_" + id).innerHTML = "";
        return false;
    }
    document.getElementById("commentdiv_ajax_" + id).innerHTML = "&#27491;&#22312;&#21152;&#36733;&#35780;&#35770;...";
    if (typeof xmlHttp == 'undefined')
       return;
    var url = "/blog/common/comments.asp?ID=" + id + "&Page=" + page;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = UpdatePage;
    xmlHttp.send(null);
    return false;
}

// Process server response
function UpdatePage()
{
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            var response = xmlHttp.responseXML;
            var id = response.getElementsByTagName("id").item(0).firstChild.data; 
            // Update page
            document.getElementById("commentdiv_ajax_" + id).innerHTML =  response.getElementsByTagName("content").item(0).firstChild.data;
        }
        else
        {
            document.getElementById("commentdiv_ajax_" + id).innerHTML = "&#27491;&#22312;&#21152;&#36733;&#35780;&#35770;... &#22833;&#36133;";
        }
    }
}
