// returns an xmlhttp object
function _XMLHTTP() {

    var rpc = null;

    try {
        rpc = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
        try {
            rpc = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (e) {
            rpc = null;
        }
    }
    if (!rpc && typeof XMLHttpRequest != 'undefinded') {
        rpc = new XMLHttpRequest();
    }
    return rpc;
}
 
// used for remote procedural calls
function _rpc(url, div, form, callback, debug) {

    // create rpc object;
    var rpc = _XMLHTTP();
 
    if (!rpc) {
        document.location.href=url;
        return false;
    }
 
    // notification when rpc is complete
    rpc.onreadystatechange = function() {
        if (rpc.readyState == 4) {
            if (debug) {
                alert('RPC DEBUG:\n' + rpc.responseText);
            }

            // parse xml and evaluate each response
            response = rpc.responseText;
            if (div) {
                // put html into div
                document.getElementById(div).innerHTML = response;
            }
            else if (callback) {
                // user-defined callback function
                eval(callback + '(response)');
            }
            else {
                // run the script
                try {
                    eval(response);
                }
                catch (e) {
                    newWin = window.open('');
                    newWin.document.write('<html><body>');
                    newWin.document.write('<b>RPC FAILED:</b><br /><br /><pre>');
                    newWin.document.write(response);
                    newWin.document.write('</pre></body></html>');
                }
            }
        }
    }
 
    // randomize url (so that no caching is done; headers don't seem to work properly)
    url += ((url.indexOf('?') == -1) ? '?' : '&') + 'random=' + Math.random();

    // send post or get
    if (form) {
        rpc.open('POST', url, true);
 
        now = new Date();
        rpc.setRequestHeader('lastCached', now.toUTCString());
        rpc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 
        // post all elements in form
        var post = '';
        for (var i = 0; i < form.elements.length; i++) {
            if (form.elements[i].name != '') {
                if (i != 0) {
                    post += '&';
                }
                post += form.elements[i].name + '=' + escape(form.elements[i].value);
            }
        }
        rpc.send(post);
    }
    else {
        // get url
        rpc.open('GET', url, true);
        rpc.send(null);
    }
 
    return false;
}
