// AJAX Javascript Document for refreshing web pages w/out actual browser refreshes 
//   Author: Michael Jackson (from http://www.ajaxon.com/michael/?p=6), modifications by Jeff Ludwig
//   Feel free to glean inspiration, as much inspiration as this document shows
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


// Function changePage:
//     We will use this function to change specific portions of a webpage
//     Specifically, a webpage (url) will be loaded into a specific div (container)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

function changePage(url, container) {
    var req = false;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your browser does not support Ajax.");
        return false;
    }

    req.onreadystatechange = function() {
        loadPage(req, container);
    }
	
    req.open('GET', url, true);
    req.send(null);
}


// Function changePageVal:
//     We will use this function to change specific portions of a webpage
//       Specifically, a webpage (url) will be loaded into a specific div (container)
//     Additionally, (val) is used to create dynamic php output
//       Specifically, it tacks [ ?(variable)=(val) ] onto the url when it loads
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

function changePageVal(url, container, variable, val) {
    var req = false;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your browser does not support Ajax.");
        return false;
    }

    req.onreadystatechange = function() {
        loadPage(req, container);
    }
	
    req.open('GET', url + "?" + variable + "=" + val, true);
    req.send(null);
}

// Function loadPage:
//     This function is designed to fill a div (container) with text from an HttpRequest (req)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

function loadPage(req, container) {
    if (req.readyState == 4 && req.status == 200) {
        document.getElementById(container).innerHTML = req.responseText;
    }
}
