/*

# ---------------------------------------
# PublishExec/interface/http.js
# (c) publishexec.com all rights reserved
# Author: Cezary Tomczak
# created 2005-07-15 modified 2005-07-20
# ---------------------------------------

    Note (httpLoad): seems like IE always caches retrieved data until you close the browser,
    so when you call a few times the same url, it always returns the same, even
    if the content in the given url changed. There are 2 solutions: add to url
    some random query string or in the url script send NOCACHE headers.

    IMPORTANT:
    - using async connection is quite faster than normal, on both, IE and mozilla
    
    TODO:
    - cache activexobject do not create it each time (not possible?conflicts?)
    
    ISSUES:
    - resolved IE cache problem, load requests changed to POST
    
    PROBLEMS:
    - do we need to create a seperate object for each request ?
      async requests could make a mess if they all use the same object

*/

function http()
{
    var req;
    var w3c = Boolean(window.XMLHttpRequest);
    var ie = Boolean(window.ActiveXObject);

    if (w3c) req = new XMLHttpRequest();
    else if (ie) req = new ActiveXObject("Microsoft.XMLHTTP");
    else alert("Your browser does not support XMLHttpRequest object");

    this.open = function(method, url, async) {
        if (ie && method == "GET") method = "POST";
        req.open(method, url, async);
        if (method == "POST") this.setRequestHeader("Connection", "close");
    }
    this.setRequestHeader = function(name, value) {
        req.setRequestHeader(name, value);
    }
    this.send = function(data) {
        if (typeof data == "undefined") {
            if (w3c) req.send(null);
            else if (ie) req.send();
        } else {
            req.send(data);
        }
    }
    this.getStatus = function() {
        return req.status;
    }
    this.getStatusText = function() {
        return req.statusText;
    }
    this.getResponseText = function() {
        return req.responseText;
    }
    this.setOnReadyStateChange = function(func) {
        req.onreadystatechange = func;
    }
    this.getReadyState = function() {
        return req.readyState;
    }
    this.abort = function() {
        return req.abort();
    }
}

// @returns false|string
function httpLoad(url)
{
    var req = new http();

    req.open("GET", url, false);
    req.setRequestHeader('Accept-Charset', CHARSET);
    req.send();

    if (req.getStatus() != 200) {
        alert("httpLoad() failed, there was a problem while retrieving the data:\n" + req.getStatusText());
        req.abort();
        return false;
    }
    
    return req.getResponseText();
}

// @returns false|void
function httpLoadAsync(url, func, errorCallback)
{
    var req = new http();

    req.open("GET", url, true);
    req.setRequestHeader('Accept-Charset', CHARSET);
    req.setOnReadyStateChange(function() {
        if (req.getReadyState() == 4) {
            if (req.getStatus() == 200) {
                func(req.getResponseText());
            } else {
                if (errorCallback) errorCallback();
                else alert("httpLoadAsync() failed, there was a problem while retrieving the data:\n" + req.getStatusText());
            }
        }
    });
    req.send();
}

// @returns true|false|string
function httpSave(url, data)
{
    var req = new http();

    var content = "";
    
    if (data) {
        for (var i in data) {
            if (content) { content += "&"; }
            content += (i + "=" + escape(data[i]));
        }
    }
    
    req.open("POST", url, false);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset="+CHARSET);
    req.send(content);
    
    if (req.getStatus() != 200) {
        alert("httpSave() failed, there was a problem while sending the data:\n" + req.getStatusText());
        req.abort();
        return false;
    }
    
    if (req.getResponseText().length) {
        return req.getResponseText();
    }
    
    return true;
}

// @returns true|false|string
function httpSaveAsync(url, data, func, errorCallback)
{
    var req = new http();

    var content = "";
    
    if (data) {
        for (var i in data) {
            if (content) { content += "&"; }
            content += (i + "=" + escape(data[i]));
        }
    }

    req.open("POST", url, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset="+CHARSET);
    req.setOnReadyStateChange(function() {
        if (req.getReadyState() == 4) {
            if (req.getStatus() == 200) {
                func(req.getResponseText());
            } else {
                if (errorCallback) errorCallback();
                else alert("httpSaveAsync() failed, there was a problem while sending the data:\n" + req.getStatusText());
            }
        }
    });
    req.send(content);
}
