/******************************************************************************
 *                Copyright (c) 2005, Bazzisoft
 ******************************************************************************
 * AJAX.js
 *
 * JavaScript class with AJAX functions.
 *
 *
 * $Id$
 *
 */

//*************************************************************************
//* AJAX
//*
//* Prepares a new AJAX object for use.
//*
//* callback    - Function to call once request is complete.
//*               Callback function takes 1 or 2 parameters -
//*               1st param is returned content.
//*               2nd param is return HTTP headers.
//* 

function AJAX(callback)
{
    this.m_xmlhttp = AJAX.CreateXMLHttpRequest();
    this.m_vars = new Array();
    this.m_callback = callback;
}

//*************************************************************************
//* CreateXMLHttpRequest()
//*
//* Creates an XMLHttpRequest object (cross-browser)
//*

AJAX.CreateXMLHttpRequest = function()
{
    var xmlHttp;
    
    try
    {
        // Firefox, Opera 8.0+, Safari, IE7
        
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer

        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                return null;
            }
        }
    }
    
    return xmlHttp;
}

//*************************************************************************
//* AddPOSTVar()
//*
//* Adds a new post variable to the request.
//*

AJAX.prototype.AddPOSTVar = function(key, val)
{
    this.m_vars[key] = val;
}

//*************************************************************************
//* Send()
//*
//* Sends the request
//*

AJAX.prototype.Send = function(url)
{
    // Define state change callback
    
    var inst = this;    
    this.m_xmlhttp.onreadystatechange = function() { inst.onReadyStateChange(); };
    
    // Setup any POST variables

    var vars = "";
    
    for ( var i in this.m_vars )
    {
        if ( vars )
            vars += "&";
            
        vars += i + "=" + encodeURI(this.m_vars[i]);
    }
    
    // Send request
    
    if ( !vars )
    {
        // GET request
        
        this.m_xmlhttp.open("GET", url, true);
        this.m_xmlhttp.send(null);
    }
    else
    {
        // POST request
        
        this.m_xmlhttp.open("POST", url, true);
        this.m_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        this.m_xmlhttp.send(vars);
    }
}

//*************************************************************************
//* onReadyStateChange()
//*
//* AJAX state change handler.
//*

AJAX.prototype.onReadyStateChange = function()
{
    if ( this.m_xmlhttp.readyState == 4 && this.m_callback )
    {
        this.m_callback(this.m_xmlhttp.responseText, this.m_xmlhttp.getAllResponseHeaders());
    }
}


