var display = null;

/** This method displays all of the comments for the given blog. */
function DisplayComments(id, commentSubmitted) {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 1) {
            // If page is loading, display loading icon
            // Determine if the comment area needs displayed
            content = "comment_view_" + id;
            element1 = document.getElementById(content);

            // Display block
            // element1.style.display = 'block';
            element1.innerHTML = '<img class="loader" src="http://seankananent.com/layout/grafix/ajax-loader2.gif" alt="" />';
        }
        if (xmlHttp.readyState == 4)
        {
            // Reload the main content area with the ajax results with appropriate conditions
            content = "comment_view_" + id;
            element1 = document.getElementById(content);

            // Get the display state
            if (element1.style.display == 'none')
            {
                display = true;
            }
            else
            {
                display = false;
            }

            if (commentSubmitted == true)
            {
                // No matter what the display is, show the comment they added
                element1.style.display = 'block';
                element1.innerHTML = xmlHttp.responseText;
            }
            else
            {
                // Determine if the comment area needs displayed
                if (display == true)
                {
                    element1.style.display = 'block';
                    element1.innerHTML = xmlHttp.responseText;
                }
                else
                {
                    element1.style.display = 'none';
                }
            }

        }
    }

    // Send AJAX data
    var url = "process.php?action=view_comments&blog_id=" + id;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}


/** This method submits the comments via AJAX and
* enters them into the database. */
function submitComment(id) {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your web browser does not support AJAX, a dynamic form of Javascript. Please upgrade your web browser.");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 1) {
            // If page is loading, display loading icon
            document.getElementById("comment_form_" + id).innerHTML = '<img class="loader" src="http://seankananent.com/layout/grafix/ajax-loader2.gif" alt="" />';
        }
        if (xmlHttp.readyState == 4) {
            // Reload the main content area with the new comment
            var view = "comment_view_" + id;
            var blog_id = id;
            document.getElementById("comment_form_" + id).innerHTML = xmlHttp.responseText;
            DisplayComments(blog_id, true);
        }
    }

    // Collect and validate post data
    var comment = document.getElementById("comment_" + id).value;
    if (comment == null || comment == "") {
        alert("You forgot to enter your comment!");
        return false;
    }
    var blog_id = id;
    var username = document.getElementById("usernameinput_" + id).value;

    // convert line breaks with BB code line break
    comment = comment.replace(/\n/g, "[br]");

    // Send AJAX data
    var url = "process.php?action=add_comment&comment=" + comment + "&username=" + username + "&blog_id=" + blog_id;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}
