/*
 *   Class myAjax v 0.1.0
 *   2007-01-23 petr@enincz.info
 */ 

function myAjax() {
	//private atributes
    var xmlHttp = null;
    var _this = null;

    //private methods
    function prepareRequest() {
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    function getResponse() {
        switch (xmlHttp.readyState) {
            case 0:
                //window.setTimeout("void(0)", 100);
                onUninitialize();
                break;

            case 1:
                //window.setTimeout("void(0)", 100);
                onLoading();
                break;

            case 2:
                //window.setTimeout("void(0)", 100);
                onLoaded();
                break;

            case 3:
                //window.setTimeout("void(0)", 100);
                onInteractive();
                break;

            case 4 :
                if (xmlHttp.status == 200) {
                    onSuccess();
                }
                else {
                    onFailure();
                }
                break;
            default :
                break;
        }
    }

    function onSuccess() {
        _this.onSuccess();
    }

    function onFailure() {
        alert("Error:\n" + xmlHttp.statusText);
        //_this.onFailure();
    }

    function onUninitialize() {
        _this.onUninitialize();
    }

    function onLoading() {
        _this.onLoading();
    }

    function onLoaded() {
        _this.onLoaded();
    }

    function onInteractive() {
        _this.onInteractive();
    }

    //public methods
    this.makeRequest = function(strMethod, strRequest, driverPath) {

        xmlHttp.onreadystatechange = getResponse;
        _this = this;

        switch (strMethod) {
            case "GET" :
                strRequest += "&gettime="+new Date().getTime();
                xmlHttp.open("GET", driverPath+"?"+strRequest, true);
                xmlHttp.send(null);
                break;

            case "POST" :
                xmlHttp.open("POST", driverPath, true);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttp.send(strRequest);
                break;

            default :
                break;
        }
    }

    this.getXmlResponse = function() {
        return xmlHttp.responseXML;		
    }

    this.getTextResponse = function() {
        return xmlHttp.responseText;
    }

    //overloaded methods
    this.onSuccess = function() {};
    this.onFailure = function() {};
    this.onUninitialize = function() {};
    this.onLoading = function() {};
    this.onLoaded = function() {};
    this.onInteractive = function() {};

    prepareRequest();
}
