/**
 * THIS FILE FORMS PART OF THE WEBRIVET WEB-API FRAMEWORK.
 * Webrivet Framework is Copyright©2010 Halogen Graphics and is distributed
 * under the GNU General Public License version 3.0.
 */
/*******************************************************************************
 * HalogenLABS Webrivet
 * includes/js/ajax.js
 *
 * $Revision: 11 $
 * $Author: Warrick $
 * $Date: 2010-01-18 01:43:44 +0200 (Mon, 18 Jan 2010) $
 *******************************************************************************
 *
 * A simple AJAX class library.
 *
 * LICENSE
 * -------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright© 2010 Halogen Graphic and Digital Design CC.
 *
 *******************************************************************************
 */

/**
 * The AJAX class provides an easy to use AJAX framework.
 *
 */
function AJAX()
{
    /************************ PROPERTY VARS ****************************/
    // A reference to this instance.
    var thisObject = this;
    // The local XmlHttpRequest object
    var XMLHttpObject = null;

    /************************ METHODS ****************************/
    /**
     * Refresh the XMLHttpObject.
     */
    this.RefreshXMLHttpObject = function() {
        returnValue = null; // The method will always return NULL if something goes wrong.
        if (window.XMLHttpRequest) {
            // Any modern borwser should use this.
            returnValue = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            // IE6 and 5 use this
            returnValue = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (XMLHttpObject != returnValue)
            XMLHttpObject = returnValue;
    }

    /**
     * Asynchronous GET
     */
    this.AsynchronousGet = function(url) {
        if (XMLHttpObject)
        {
            XMLHttpObject.open("GET", url, true);
            XMLHttpObject.send(null);
        }
    }

    /**
     * Asynchronous POST
     */
    this.AsynchronousPost = function(url, postValue) {
        if (XMLHttpObject) {
            XMLHttpObject.open("POST", url, true);
            XMLHttpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            XMLHttpObject.setRequestHeader("Content-length", postValue.length);
            XMLHttpObject.send(postValue);
        } else {
            return;
        }
    }

    /************************ EVENT HANDLERS ****************************/
    /**
     * Event: Request has not initialzied
     * readyState = 0
     */
    this.OnRequestNotInitialized = function() {}

    /**
     * Event: Request has been set up
     * readyState = 1
     */
    this.OnRequestSetUp = function() {}

    /**
     * Event: Request has been sent
     * readyState = 2
     */
    this.OnRequestSent = function() {}

    /**
     * Event: Request has begun process
     * readyState = 3
     */
    this.OnRequestProcess = function() {}

    /**
     * Event: Request is complete
     * readyState = 4
     */
    this.OnRequestComplete = function(response) {}

    /************************ CONSTRUCTOR ****************************/
    // Generate a new XmlHttpRequest object
    this.RefreshXMLHttpObject();

    // Define the state changed event function
    XMLHttpObject.onreadystatechange = function()
    {
        if (XMLHttpObject.readyState == 0) {
            thisObject.OnRequestNotInitialized();
        } else if (XMLHttpObject.readyState == 1) {
            thisObject.OnRequestSetUp();
        } else if (XMLHttpObject.readyState == 2) {
            thisObject.OnRequestSent();
        } else if (XMLHttpObject.readyState == 3) {
            thisObject.OnRequestProcess();
        } else if (XMLHttpObject.readyState == 4) {
            thisObject.OnRequestComplete(XMLHttpObject.responseText);
        }
    }
}
