/*
 * Asychronous HTML and HTTP Library
 * Written by Jonathan Barnes.
 *
 * Original script taken from crackajax.net
 * Made improvements based on Thomas Rabaix's scripts on rabaix.net
 * Completely rewrote for my own purposes, with extra features.
 *
 */


// The array that will hold all of the ahah objects
// This allows us to create multiple XMLHttpRequest's
// at the same time.
var ahahObjects = new Array();

// ahahRequest(url, target, method, action, parameters);
// This is the main function and the only one to be called from other scripts
// It will fetch the url given, and display the output in the HTML item with
// id="target".
// eg. ahahRequest('content.php', 'content'); will display content.php in
// <div id="content">CONTENT WILL APPEAR HERE</div>
// --
// url = url to GET/POST.
// target = target div's id to APPEND/INSERT results.
// method = (optional) GET or POST. (default: GET)
// action = (optional) INSERT or APPEND. (default: INSERT)
// parameters = (optional) any parameters to GET or POST (default: null);
function ahahRequest(url, target, method, action, params) {
    if (method != 'POST') method = 'GET';
    if (!action) action = 'INSERT';
    if (!params) params = false;
    
    ahahObjects[target] = getNewXmlHttpRequest();
    if (method == 'GET') {
        ahahObjects[target].open(method,url,true);
    }
    else {
        ahahObjects[target].open(method,url,true);
        ahahObjects[target].setRequestHeader("Content-type","application/x-www-form-urlencoded");
    }
    ahahObjects[target].onreadystatechange = function() {parseQuery(target, action)};
    ahahObjects[target].send(params);
}

// parseQuery(target, action);
// This is the function that gets called when the server replies from the
// original XMLHttpRequest, this then displays the output to the user
// depending on what options have been set.
function parseQuery(target, action) {
    if (ahahObjects[target].readyState == 4) {
        if (ahahObjects[target].status == 200) {
            x=document.getElementById(target);
            if (action == 'INSERT') {
                x.innerHTML=ahahObjects[target].responseText;
            } else if (action == 'APPEND') {
                x.innerHTML=x.innerHTML+ahahObjects[target].responseText;
            } else if (action == 'PREPEND') {
                x.innerHTML=ahahObjects[target].responseText+x.innerHTML;
            }
            evalJavascript(ahahObjects[target].responseText);
        } else {
            alert('ahah error:\n'+ahahObjects[target].statusText);
        }
    }
}

// getNewXmlHttpRequest();
// This function builds the XMLHttpRequest, based on what browser the user
// is using.
function getNewXmlHttpRequest() {
    var obj = false;
    try {
        obj = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try  {
            obj = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            obj = new XMLHttpRequest();
        }
    }
    return obj;
}

// evalJavascript(responseText)
// Any javascript that is returned from a XmlHttpRequest will not be processed
// this function attempts to take care of that, it should be called straight
// after the parseQuery() function has displayed the new HTML.
function evalJavascript(responseText) {
    // RegExp from prototype.sonio.net
    var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
           
    var match    = new RegExp(ScriptFragment, 'img');
    var scripts  = responseText.match(match);

    if(scripts) {
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
            var match = new RegExp(ScriptFragment, 'im');
            js += scripts[s].match(match)[1];
        }
        eval(js);
    }
}

