function ahah_stash() {}                // the stash object
var stash = new ahah_stash();           // the (global) stash instance
var ahah_DONT_CACHE = '';               // just a self-documenting constant


function ahah_retrieve(id,url,key) {    // get url, display as id, store as key
    if (key && stash[key]) {
        document.getElementById(id).innerHTML  =  stash[key];
    }
    else {
        ahah_request(id,url,key);
    }
}


function ahah_swap(id,url) {            // swap contents of id with its stash
    if (stash[id]) {
        var swap   =  stash[id];
        stash[id]  =  document.getElementById(id).innerHTML;
        document.getElementById(id).innerHTML  =  swap;
    }
    else {
        stash[id]  =  document.getElementById(id).innerHTML;
        ahah_request(id,url,ahah_DONT_CACHE);
    }
}


function ahah_clear(key) {
    stash[key] = false;
}


function ahah_store(id,key) {
    stash[key]  =  document.getElementById(id).innerHTML;
}


function ahah_store_id(id) {
    stash[id]  =  document.getElementById(id).innerHTML;
}


function ahah_request(id,url,key) {     // retrieve from net
    document.getElementById(id).innerHTML  =  '...retrieving...';
    var xHR = (window.XMLHttpRequest) ? new XMLHttpRequest()
                                      : new ActiveXObject("Microsoft.XMLHTTP");
    if (xHR) {
        xHR.onreadystatechange  =  function() {ahah_request_done(xHR,id,key);};
        xHR.open("GET",url,true);
        xHR.send(null);
    }
}


function ahah_request_done(xHR,id,key) {  // callback: response available
    if (xHR.readyState == 4) {
        if (xHR.status == 200) {
            document.getElementById(id).innerHTML  =  xHR.responseText;
            if (key) { stash[key] = xHR.responseText; }
        } else {
            document.getElementById(id).innerHTML  =  stash[id] +
                '<!-- XMLHttpRequest Error: ' + xHR.statusText + ' -->';
        }
    }
}

