/**
 * AJAX handler
 * @author      Matthew Eernisse
 * @copyright   SitePoint - Build Your Own AJAX Web Applications
 * $Id: ajax.js,v 1.1.1.1 2007/04/28 05:59:46 Ignatius Teo Exp $
 */

function Ajax() {
    this.req = null;
    this.url = null;
    this.method = 'GET';
    this.async = true;
    this.status = null;
    this.postData = null;
    this.readyState = null;
    this.responseText = null;
    this.responseXML = null;
    this.handleResp = null;
    this.responseFormat = 'text'; // text | xml | object
    this.mimeType = null;

    // initialize
    this.init = function() {
        if (!this.req) {
            try {
                /* Try to create object for FireFox, Safari, IE7 etc. */
                this.req = new XMLHttpRequest();
            }
            catch (e) {
                try {
                    /* Try later versions of IE */
                    this.req = new ActiveXObject('MSXML2.XMLHTTP');
                }
                catch (e) {
                    try {
                        /* Try earlier versions of IE */
                        this.req = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    catch (e) {
                        /* could not creare an XMLHttpRequest object */
                        return false;
                    }
                }
            }
        }
        return this.req;
    };

    // do request
    this.doReq = function() {
        if (!this.init()) {
            alert('Could not create an XMLHttpRequest object. Your browser is not capable of supporting AJAX.');
            return;
        }
        this.req.open(this.method, this.url, this.async);

        if (this.method == 'POST') {
            this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }

        // override mimetype
        if (this.mimeType) {
            try {
                req.overrideMimeType(this.mimeType);
            }
            catch (e) {
                // couldn't override mimetype
            }
        }
        var self = this;    // fix loss of scope in inner function
        this.req.onreadystatechange = function() {
            var resp = null;
            if (self.req.readyState == 4) {
                // do stuff to handle response
                switch (self.req.responseFormat) {
                    case undefined:
                    case 'text':
                        resp = self.req.responseText;
                        break;
                    case 'xml':
                        resp = self.req.responseXML;
                        break;
                    case 'object':
                        resp = req;
                        break;
                }
                //alert('Status: ' + self.req.status + '\nFormat: ' + self.req.responseFormat + '\nResponse: ' + resp);
                // HTTP response codes 200-299 basically OK
                if (self.req.status >= 200 && self.req.status <= 299) {
                    self.handleResp(resp);
                }
                else {
                    self.handleErr(resp);
                }
            }
        };
        this.req.send(this.postData);
    };

    // error handler
    this.handleErr = function() {
        var errorWin;
        try {
            errorWin = window.open('', 'errorWin');
            errorWin.document.body.innerHTML = this.responseText;
        }
        catch (e) {
            alert('An error occurred, but the error message cannot be '
            + 'displayed. This is probably because of your browser\'s '
            + 'pop-up blocker.\n'
            + 'Please allow pop-ups from this web site if you want to '
            + 'see the full error message.\n'
            + '\n'
            + 'Status Code: ' + this.req.status + '\n'
            + 'Status Description: ' + this.req.statusText);
        }

    }

    // set the response and error handler
    this.setHandleBoth = function(funcRef) {
        this.handleResp = funcRef;
        this.handleErr = funcRef;
    }

    // set content mimetype
    this.setMimeType = function(MimeType) {
        this.mimeType = MimeType;
    }

    // abort function
    this.abort = function() {
        if (this.req) {
            this.req.onreadystatechange = function() {};
            this.req.abort();
            this.req = null;
        }
    }

    // GET
    this.doGet = function(url, hand, format) {
        this.url = url;
        this.handleResp = hand;
        this.responseFormat = format || 'text';
        this.doReq();
    }

    // POST
    this.doPost = function(url, postData, hand, format) {
        this.url = url;
        this.handleResp = hand;
        this.responseFormat = format || 'text';
        this.method = 'POST';
        this.postData = postData;
        this.doReq();
    }
}