/**
 * THis is a thread-safe ajax implementation. It is safe to call it multiple times
 * within the same page. To use it, define a callback function which processes the
 * ajax response; then, pass it as one of the parameters to the makeRequest function.
 *
 * Example:
 *
 * <script type="text/javascript" src="/site/javascript/ajax.js"></script>
 * <script type="text/javascript" src="/site/javascript/ajax.js">
 * function myCallback( response ){
 *     alert( "Response Text: " + response.getText();
 * }
 * makeRequest( 'http://my.site.com/myajax.jsp', myCallback );
 * </script>
 */

// Generate a random parameter to bi-pass cache'ing.
function randomParameter(){
    var uid = new Date().getTime();
    return uid;
}

var http_request = false;

// The url is the URL on the server which will be called for content.
// The blockId is the span or div which will display the results of the call.
function makeRequest( url, callback ) {

    function ajaxBindCallback(){
        if (ajaxRequest.readyState == 4) {
            if (ajaxRequest.status == 200) {
                if (ajaxCallback){
                    ajaxCallback( ajaxRequest.responseText );
                } else {
                    // alert('no callback defined');
                }
            } else {
                // alert("There was a problem retrieving the data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
            }
        }
    }

    // use a local variable to hold our request and callback until the inner function is called...
    var ajaxRequest = null;
    var ajaxCallback = callback;


    // bind our callback then hit the server...
    if (window.XMLHttpRequest) {
        // moz et al
        ajaxRequest = new XMLHttpRequest();
        ajaxRequest.onreadystatechange = ajaxBindCallback;
        ajaxRequest.open("GET", url, true);
        ajaxRequest.send(null);
    } else if (window.ActiveXObject) {
        // ie
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        if (ajaxRequest) {
            ajaxRequest.onreadystatechange = ajaxBindCallback;
            ajaxRequest.open("GET", url, true);
            ajaxRequest.send();
        }
    }

}