﻿/***************************************************************************************
$Workfile: SimpleAJAX.js $
$Author: Syed Ghulam Akbar $
$Revision: 1 $
$Created Date: 09/08/08 01:01 PM $
$Description: Compact AJAX Utilities
***************************************************************************************/

// A simple and compact AJAX request object
function SimpleAjax()
{
    var xmlHttpReq = null;      // HML HTTP Request object
    var Callback = null;       // Callback function handler
    var XmlResponse = false;    // Return true when the client wants the XML response
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) 
        xmlHttpReq = new XMLHttpRequest();
    // IE
    else if (window.ActiveXObject) 
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    // Simulate a simple get request
    this.Get = function (url, callbackMethod)
    {
        Callback = callbackMethod; // Set the call back method

        // Make the XHTML get call
        xmlHttpReq.open('get', url);
        xmlHttpReq.onreadystatechange = this.OnReadyStatusChange
        xmlHttpReq.send(null);
    }
    
    this.OnReadyStatusChange = function()
    {
        if(xmlHttpReq.readyState==4)  // AJAX request is complete
        {            
            // AJAX request completed
            if(xmlHttpReq.status==200)
                (XmlResponse && xmlHttpReq.responseXML) ? Callback(xmlHttpReq.responseXML) : Callback(xmlHttpReq.responseText)
//            else
//                alert("Request Failed. Error Code: " + xmlHttpReq.status);
        }
    }
}
