/** 
Javascript/AJAX blog app
    
Coded By: Edward Boot
Last Updated: May 20, 2009
    
This file loads the blogs and switches 
the blogs on the main part of the page.
**/

/** Background Fix File **/

function getElementHeight(Elem) {
    var mid_section = document.getElementById("mid_section");
    var height = mid_section.style.height;
    return height;
}

function getElementWidth(Elem) {
    var mid_section = document.getElementById("mid_section");
    var width = mid_section.style.width;
    return width;
}

/** ResetContainer Method
* This function will reset the size of the main content area */
function ResetContainer() {
    var mid_section = document.getElementById("mid_section");
    mid_section.style.height = 0 + "px";
}

/** Background Fix Function
* This function fixes the repeating background when too much content takes it up */
function BackgroundFix() {

    // Set variables
    var mid_section = document.getElementById("mid_section");
    var new_height = 0;
    var main_size = 366;
    var i = main_size;
    var found = false;

    // Get the scroll height
    var current_height = document.body.scrollHeight - 150;
    if (current_height <= 650) {
        current_height = current_height - 300;
    }

    // Determine if background needs fixed
    // (largest possible background is main size times 30)
    while (i <= main_size * 30) {
        if (current_height > i) {
            found = false;
            // alert("Height not matched. Current possible height: " + i + "\nCurrent absolute height: " + current_height);
        }
        else {
            found = true;
            // alert("Height matched - Current possible height: " + i + "\nCurrent absolute height: " + current_height);
            new_height = i;
        }

        if (found == true) {
            i = main_size * 30 + 8;
        }
        else {
            i = i + main_size;
        }
    }

    // Set the new background height
    mid_section.style.height = new_height + "px";
} 

// Loads the blog title
function LoadTitle(blog_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 browser does not support AJAX!");
                return false;
            }
        }
    }
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            document.getElementById("title_spacing").innerHTML = xmlHttp.responseText;
            document.title = "The Three Saints ~ " + xmlHttp.responseText;
        }
    }
    var url = "Process.aspx?section=HOME&comments=false&blog_id=" + blog_id + "&mode=title";
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

// Load the blog
function LoadBlog(blog_id, section, comments) {

    // Reset the height of the container
    // ResetContainer();

    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
            document.getElementById("mid_section").innerHTML = '<img class="loader" src="Layout/graphics/loader_2.gif" alt="" />';
        }
        if (xmlHttp.readyState == 4) {

            // Display the main page content and links for this section
            document.getElementById("mid_section").innerHTML = xmlHttp.responseText;

            // Fix the height of the container
            // BackgroundFix();

            // Re-center the page to the comments if enabled
            if (comments == 'true') {
                location.replace('#comment_center');
            }

            // Load the page title
            LoadTitle(blog_id);

            // Load the appropriate title bar
            var titleBar;
            switch (section) {
                case "HOME":
                    document.getElementById("MAIN_TITLE_BAR").className = "bar_home";
                    break;

                case "PERSONAL":
                    document.getElementById("MAIN_TITLE_BAR").className = "bar_personal";
                    break;

                case "ABOUT":
                    document.getElementById("MAIN_TITLE_BAR").className = "bar_about";
                    break;

                case "POLITICS":
                    document.getElementById("MAIN_TITLE_BAR").className = "bar_politics";
                    break;

                case "GAMES":
                    document.getElementById("MAIN_TITLE_BAR").className = "bar_games";
                    break;
            }
        }
    }
    var url = "Process.aspx?section=" + section + "&comments=" + comments + "&blog_id=" + blog_id + "&mode=null";
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

// Form validation function
function validate_required(field, alerttxt) {
    with (field) {
        if (value == null || value == "")
        { alert(alerttxt); return false; }
        else { return true }
    }
}

// Process Form function - adds the comment with no reload
function ProcessForm(blog_id, section) {
    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
            document.getElementById("main_comment_area").innerHTML = '<img class="loader" src="Layout/graphics/loader_2.gif" alt="" />';
        }
        if (xmlHttp.readyState == 4) {
            // Reload the main content area with the new comment
            LoadBlog(blog_id, '' + section + '', "true");
        }
    }
    // Collect and validate post data
    var name = document.theCommentForm.name.value;
    var email = document.theCommentForm.email.value;
    var website = document.theCommentForm.url.value;
    var comment = document.theCommentForm.comment.value;

    // Check for blank fields
    if (name == null || name == "") {
        alert("Please enter your name (a nickname is fine, too)!");
        return false;
    }
    /**
    if (email == null || email == "") {
        alert("Please enter your email address. It is not shared with anyone, only used to respond back to your comment if we choose.");
        return false;
    }
    */
    if (comment == null || comment == "") {
        alert("You forgot to enter your comment!");
        return false;
    }

    // convert line breaks with BB code line break
    comment = comment.replace(/\n/g, "[br]");

    // Send AJAX data
    var url = "Comment.aspx?blog_id=" + blog_id + "&comment=" + comment + "&name=" + name + "&email=" + email + "&url=" + website;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}