//var xml_http;

function create_xmlhttprequest() {
//function send_ajax_request(url) {
    try {
        // IE 6+
        xml_http = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
        // IE 5, IE 5.5(?)
        try {
            xml_http = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (e2) {
            xml_http = false;
        }
    }

    // All other browsers
    if (!xml_http && typeof(XMLHttpRequest) != 'undefined') {
        xml_http = new XMLHttpRequest();
    }

    if (!xml_http) {
        alert('Error initialising XMLHttpRequest!');
    }

    return xml_http;
}

/**
 * send_request
 * KWL - 2006-03-30
 * Input:   Accepts String url, String DOM element id whose contents'll
 *          to be replaced, Function name of a javascript function to 
 *          initialise new content
 */
function send_request(url, element, init) {
    //alert('Element: ' + element);
    //this.element = element;
    
    document.getElementById(element).innerHTML = "<img src=images/loading.gif>";
    
    this.create_ajaxrequest = function() {
        var xml_http;
        try {
            // IE 6+
            xml_http = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e) {
            // IE 5, IE 5.5(?)
            try {
                xml_http = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e2) {
                xml_http = false;
            }
        }

        // All other browsers
        if (!xml_http && typeof(XMLHttpRequest) != 'undefined') {
            xml_http = new XMLHttpRequest();
        }

        if (!xml_http) {
            alert('Error initialising XMLHttpRequest!');
        }

        return xml_http;
    }
    this.update_page = function() {
        // Valid states are:
        // 0. Request is uninitialised (before open() is called)
        // 1. Request is initialised but not sent()
        // 2. Request is sent & being processed
        // 3. Request is being processed & some partial data is sent back.
        // 4. Request is complete
        if (ajax_obj.readyState == 4) {
            // Request is valid
            if (ajax_obj.status == 200) {
                var response = ajax_obj.responseText;
                // Update page element
                if (document.getElementById(element) != null) {
                    document.getElementById(element).innerHTML = response;
                    //document.getElementById(this.element).innerHTML = response;
                }
                // Run initialise function
                if (typeof init == 'function') {
                    init();
                }
                return true;
            }
            else if (ajax_obj.status == 404) {
                alert('Request URL does not exist.');
            }
            else {
                alert('Error: Status code is ' + ajax_obj.status);
                //alert('There was a problem retrieving the data: ' + "\n" + xml_http.statusText);
            }
            return false;
        }
    }
    this.post_update = function(url, stuff) {
        // To encode safe characters for your URL, use encodeURIComponent() or 
        // encodeURI() or escape() to encode, and decodeURIComponent() or
        // decodeURI() or unescape() to decode.
        // http://xkr.us/articles/javascript/encode-compare/
        xml_http.open('POST', url, true);
        xml_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        var url_safe_stuff = '';
        url_safe_stuff =    'id=' + encodeURIComponent(name)
                            + '&password=' + encodeURIComponent(password)
                            + '&login=1';
        xml_send(url_safe_stuff);
        var xml = xml_send.responseXML; // or responseText, etc
    }
    var ajax_obj = this.create_ajaxrequest();
    ajax_obj.open('GET', url, true);
    ajax_obj.onreadystatechange = this.update_page;
    ajax_obj.send(null);
    return false;
}



