﻿


//********************* PROPERTIES ***********************************
function Ajax() {
    this.req = null;
    this.url = null;
    this.method = 'GET';  //Method is case sensitive
    this.async = true;  //TRUE = execute normally while waiting for a response to the request FALSE = stop until the response comes back.
    this.status = null;
    this.statusText = '';
    this.postData = null;
    this.readyState = null; //0 = UnInitialized, 1 = Loading, 2 = Loaded(send called no response yet), 3 = Interactive(responseText holds partial data), 4 = Completed (Laoded and ready) 
    this.responseText = null; // response from the server string format
    this.responseXML = null; //XML doc object, if response is not XML will be empty
    this.responseObj = null;
    this.handleResp = null;
    this.responseFormat = 'text'; //'text, xml, or object
    this.mimeType = null;


    //********************* INITIALIZE ***********************************
    //Initialization function used to create the XMLHttpRequest
    //Uses the ActiveXObject for older version of IE
    this.init = function() {
        if (!this.req) {
            try {
                //Try to create object for Firefox, Safari, IE7 and others that I dont know.
                this.req = new XMLHttpRequest();
            }
            catch (e) {
                try {
                    //Try this to create object for later versions of IE.
                    this.req = new ActiveXObject('MSXML2.XMLHTTP');
                }
                catch (e) {
                    try {
                        //Try to create object for early versions of IE.
                        this.req = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    catch (e) {
                        //Could not an XMLHttpRequest Object
                        return false;
                    }
                }
            }
        }
        //Return the initialized object.
        return this.req;
    };
    //********************************************************************


    //********************* DO REQUEST **********************************
    this.doReq = function() {
        if (!this.init()) {
            alert('Could not create XMLHttpRequest object.');
            return;
        }
        //remember method is case sensitive use caps
        this.req.open(this.method, this.url, this.async);

        //Ability to override the Content-Type headers  
        if (this.mimeType) {
            try {
                req.overrideMimeType(this.mimeType);
            }
            catch (e) {
                //Couldn't ovveride mime type old browser IE6 Safari?
            }
        }

        var self = this; //**NEED THIS** Handles loss of scope when it comes back with readyState == 4, when is comes back it doesnt know what 'this' is
        this.req.onreadystatechange = function() {
            if (self.req.readyState == 4) {
                //Handle response
                switch (self.responseFormat) {
                    case 'text':
                        resp = self.req.responseText;
                        break;
                    case 'xml':
                        resp = self.req.responseXML;
                        break;
                    case 'object':
                        resp = req;
                        break;
                }

                //http status code 404 missing, 500 server-side script error, 200 succcess
                if (self.req.status >= 200 && self.req.status <= 299) {
                    self.handleResp(resp);                   
                }
                else {
                    self.handleErr(resp);
                }
            }
        }

        //Start http request
        this.req.send(this.postData);

    };
    //********************************************************************

    //****************** SET MIME TYPE ***********************************
    this.setMimeType = function() {
        this.mimeType = mimeType;
    }
    //********************************************************************

    //****************** ERROR HANDLER ***********************************
    this.handleErr = function() {
        var errorWin;

        //Can use this below for testing but dont use in production

        //    try {
        //        errorWin = window.open('', 'errorWin');
        //        errorWin.document.body.innerHTML = this.responseText;
        //    }
        //    catch (e) {
        //        alert('Error occured and pop-up blocker blocked the error window'
        //        + ' the error was \n'
        //        + 'Status: ' + this.req.status + '\n' + ' Status Desc: ' + this.req.statusText);
        //    }
    };
    //********************************************************************

    //****************** CUSTOM ERROR HANDLER ****************************
    //Custom error handler you can use
    this.setHandlerErr = function(funcRef) {
        this.handleErr = funcRef;
    };
    //********************************************************************


    //****************** RESPONSE HANDLER ********************************
    //Handler for both errors and successful responses
    this.setHandlerBoth = function(funcRef) {
        this.handleResp = funcRef;
        this.handleErr = funcRef;
    };
    //********************************************************************

    //****************** ABORT *******************************************
    //Abort Request
    this.abort = function() {
        if (this.req) {
            this.req.onreadystatechange = function() { };
            this.req.abort();
            this.req = null;
        }
    };
    //********************************************************************

    //****************** DO GET ******************************************
    this.doGet = function(url, hand, format) {
        this.url = url;
        this.handleResp = hand;
        this.responseFormat = format || 'text'; //will default to text if not set
        this.doReq();
    };
    //********************************************************************

    this.DoSomething = function() {
        alert('cAjax did something');
    };

}
//********************************************************************

