function call_cgi(callback, obj)
{
    var req;

    function processReqChange()
    {
        // only if req shows "complete"
        if (req.readyState == 4) {
            // only if "OK"
            if (req.status == 200) {
                try {
                    callback(eval('('+req.responseText+')'));
                } catch (e) {
                    alert("There was a problem communicating with the server. "
                          + "Our engineers are working on fixing the problem. "
                          + "Please try back later.");
                    alert(e);
                }
            } else if (obj['function'] == 'error') {
                alert("There was a problem retrieving",
                      "data from the server:", req.statusText,
                      req.responseText, obj);
            } else {
                alert("There was a problem communicating with the server.  "
                      + "Our engineers are working on fixing the problem.  "
                      + "Please try back later.");
            }
        }
    }


    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST", '/mod-bin/ajax.py', true);
        req.setRequestHeader('Content-Type',
                             'application/json');
        req.send(obj.toJSONString());
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        req.onreadystatechange = processReqChange;
        req.open("POST", '/mod-bin/ajax.py', true);
        req.setRequestHeader('Content-Type',
                             'application/json');
        req.send(obj.toJSONString());
    }
}

function _(id)
{
    return document.getElementById(id);
}

function parse_done(obj)
{
    var output = _('output');
    var html = _('outputhtml');
    var button = _('button');
    var input = _('history');
    var debug = _('converterdebug');
    var obutton = _('outputbutton');

    output.value = obj['text'];
    output.select();
/*
    if (window.clipboardData) {
        clipboardData.setData('Text', obj['text']);
    }
*/
    html.innerHTML = obj['html'];
    button.disabled = false;
    input.disabled = false;
    button.innerHTML = 'Go!';
    if (obj['debug'] != undefined) {
        debug.innerHTML = obj['debug'];
        obutton.innerHTML = '';
    } else {
        debug.innerHTML = '';
        msg = 'Add a subject above and some text here describing any reads you had on your opponents, and what part of the hand you found difficult.  What do you want advice on, exactly?\n\n' + obj['sleuth'];
        obutton.innerHTML = '<a href="http://pokersleuth.com/cgi-bin/forum/topic_add.pl?bid=6&preview=Preview&body=' + escape(msg) + '>Post this hand to the forum!</a>';
    }
}

var last_parsed = '';

function parse()
{
    var input = _('history');
    var format = _('format').value;
    var button = _('button');

    last_parsed = input.value;
    button.innerHTML = 'Wait...';
    button.disabled = true;
    input.disabled = true;

    call_cgi(parse_done, 
             {'function': 'parse', 'input': input.value, 'format': format});

    return 0;
}

function output_clear()
{
    window.setTimeout(output_clear_real, 0);
    return 0;
}

function output_clear_real()
{
    var input = _('history').value;
    
    if (input == last_parsed)
        return;

    var output = _('output');
    output.innerHTML = '';
}
