
// Logs a message to a text area with an id of "log"
// for debugging purposes.

function log(message) {    
    try {
        var logTextArea = document.getElementById("log");
    
        if( log != null ) { 
            logTextArea.value += message + "\n";
        }
    } catch(objException) {
    }
}

// easy XML helper function

function getText(element) {
    return element.firstChild.nodeValue;
}
                
// Sends an asynchronous HTTP request to a given URL
// whose response will be sent to a given handler.
function sarissaSend(url, handler) {
    // XMLHttpRequest is used to send asynchronous HTTP requests.
    // Firefox seems to require creating a new XMLHttpRequest object
    // for each request.
    xhr = new XMLHttpRequest(); // from Sarissa
    
    xhr.onreadystatechange = handler;
    
    log("send: opening " + url);
    async = true;
    xhr.open("GET", url, async);
    
    log("send: sending to " + url);
    body = null;
    xhr.send(body);
    
    return xhr;
}    

function sarissaSendForm(url, handler, f) {
    // XMLHttpRequest is used to send asynchronous HTTP requests.
    // Firefox seems to require creating a new XMLHttpRequest object
    // for each request.
    xhr = new XMLHttpRequest(); // from Sarissa
    
    xhr.onreadystatechange = handler;
    
    log("send: opening " + url);
    async = true;
    xhr.open("POST", url, async);
    
    log("send: sending to " + url);

    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    body = null;
   
    var params = "";
    for(i=0; i<f.elements.length; i++){
          var field = f.elements[i];

          if(i > 0) {
            params += "&";
          }
          params += field.name + "=" + field.value;
          //alert(field.name + "=" + field.value);
    }

    log("params=" + params);
    body = params;

    xhr.send(body);
    
    return xhr;
}    

// Sends a _synchronous_ HTTP request to a given URL
// whose response will be sent to a given handler.
function sarissaSendSync(url, handler) {
    // XMLHttpRequest is used to send asynchronous HTTP requests.
    // Firefox seems to require creating a new XMLHttpRequest object
    // for each request.
    xhr = new XMLHttpRequest(); // from Sarissa
    
    xhr.onreadystatechange = handler;
    
    log("send: opening " + url);
    async = false;
    xhr.open("GET", url, async);
    
    log("send: sending to " + url);
    body = null;
    xhr.send(body);
    
    return xhr;
}    

// this handler does nothing... useful for Ajax "actions" which
// feed info to the server

function noOpHandler() {
    if (xhr.readyState == 4) {
        log("noOpHandler -- received response!");
    }
}
        

