/*
 * A simple abstraction class for doing AJAX
 *
 * $author = Paul Washburn
 * $date = 26 January 2008
 *
 * (C) 2008 Paul Washburn
 */

/*
 * Purpose : Instantiates a XHR object for standards compliant browsers (first try)
 *           or Internet Explorer (second and third try). This function should really
 *           only be used internally.
 *
 * Returns : XMLHttpRequest object (or its Microsoft variants)
 *
 * Errors : Alerts the user, and returns NULL, if a XHR object cannot be instantiated
 */
function ajax_createXMLHttpRequestObject() {
    var xhr;
    try {
        xhr = new XMLHttpRequest();
    }
    catch(e) {
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e2) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert('Your browser does not support AJAX. This application will not function properly');
                return null;
            }
        }
    }
    return xhr;
}

/*
 * Purpose : This is the scripts XHR that it executes on. It is instantiated when
 *           the page is loaded. It must also be used in user-defined callbacks
 *
 * SEE THE END OF THIS DOCUMENT FOR EXAMPLE USAGE !!!
 */
var ajax_xhr = ajax_createXMLHttpRequestObject();

/*
 * Purpose : Allows the user to specify a script, get parameters, and a callback function
 *           that will use the XHR object resident in this script.
 *
 * Parameters : script = The name of the script that the XHR object will submit to
 *              param_num = The number of parameters contained in the parameter array
 *              params = Get parameters to send to $script
 *              callback = The function to be executed when the XHR changes state. If
 *                          this is null, it defaults to ajax_defaultResponse
 *
 * Returns: VOID
 *
 * Exceptions : No externally relevant exception handling is present
 *
 * SEE THE END OF THIS DOCUMENT FOR EXAMPLE USAGE !!!
 */
function ajax_sendGetRequest(script,params,callback) {
    var build_url = script+'?';
    for(i = 0; i < params.length; i++){
        if(i > 0){
            build_url+='&';
        }
        build_url+=(params[i]);
    }
    ajax_xhr.open('get', build_url);
    if(callback == null){
        ajax_xhr.onreadystatechange = ajax_defaultResponse;
    }
    else {
        ajax_xhr.onreadystatechange = callback;
    }
    ajax_xhr.send(null);
}

/*
 * Purpose : The default callback function. Creates an alert with the
 *           responseText of the XHR object. This is mostly for debugging
 *           purposes and to notify developers that they forgot to define
 *           a valid callback
 *
 * Returns : VOID
 *
 * Errors : Entering in this function is an error of sorts. The alert box
 *          notifies the user that this has occured.
 */
function ajax_defaultResponse() {
    if(ajax_xhr.readyState == 4 && ajax_xhr.status == 200){
        if(ajax_xhr.responseText) {
            alert(ajax_xhr.responseText);
        }
    }
}

/*
 * Purpose : Returns the state and status of the XHR object
 *
 * Returns : TRUE if the object is both ready and has a normal status
 *           FALSE otherwise
 *
 * Errors : N/A
 */
function ajax_getXML(){
    if(ajax_xhr.readyState == 4 && ajax_xhr.status == 200){
        return ajax_xhr.responseXML;
    }
    else {
        return false;
    }
}
/*
 *****************
 * Example Usage *
 *****************

<html>
    <head>
        <script language="JavaScript" src="scripts/ajax.js"><!-- This is the path to this document! --></script>
        <script language="JavaScript">
            function user_callback(){
                if(ajax_xhr.readyState == 4 && ajax_xhr.status == 200){
                    var xmlin = ajax_xhr.responseXML
                    if(xmlin) {
                        var element_array = xmlin.getElementsByTagName('element');
                        var html_builder = "<p><ul>";
                        for(i = 0; i < element_array.length; i++){
                            html_builder+="<li>"+element_array[i].textContent+"</li>";
                        }
                        html_builder+="</ul></p>";
                        document.getElementById("returned").innerHTML = "<p>"+ajax_xhr.responseXML+"</p>"+html_builder;
                    }
                }
            }
        </script>
    </head>
    <body>
        <FORM method="GET" name="ajax" action="" onsubmit="return false;">
            <INPUT type="submit" value="Submit" ONCLICK="ajax_sendGetRequest('ajax.php',['dyn='+document.ajax.dyn.value],user_callback)">
            <INPUT type="text" name="dyn" value="">
            <LABEL id="returned"></LABEL>
        </FORM>
    </body>
</html>

 *******************************
 * XML Schema for this example *
 *******************************

<root>
    <element>TEXT</element>
    <element>TEXT</element>
    ...
    <element>TEXT</element>
</root>
 

 */