﻿// Kris Garrein's Ajax object 
// (c)2006 - kris.garrein@telenet.be

// Ajax Object
var p_oAjax = null;
function CAjax(name)
{
    this.name = name;
    this.loadingHtml = "<img src=\"img/global/ajaxprogress.gif\" alt=\"\" />"; //"Loading...";
    this.req = false;
    this.ajaxLoading = false;
    this.ajaxStatus = 4;
    this.renderTarget = "";
    this.result = "";
    this.useRenderTarget = false;        
}

// Is it still loading ?
CAjax.prototype.isLoading = function()
{
    return this.ajaxLoading;
}

// XMLHTTP initialization
CAjax.prototype.initXMLHTTP = function()
{
    this.req = false;
    
    if(window.XMLHttpRequest) 
    {
        try 
        {
            this.req = new XMLHttpRequest();
        } 
        catch(e) 
        {
            this.req = false;
        }       
    } 
    else if(window.ActiveXObject) 
    {
        try 
        {
            this.req = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch(e) 
        {
            try 
            {
                this.req = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch(e) 
            {
                this.req = false;
            }
        }
    }   
    
    return this.req;
}

//Set target text
CAjax.prototype.setRenderTargetContent = function( sText )
{
    try
    {
        document.getElementById(this.renderTarget).innerHTML = sText;
    }
    catch(e)
    { 
        alert("Ajax render target not found: " + this.renderTarget); 
    }  
}

//retry if loader is busy
function LoadTryAgain( url, target, bPost )
{
    var oAj = new CAjax();
    oAj.loadAjaxText( url, target, bPost );
}

//loading function
CAjax.prototype.loadAjaxText = function( url, target, bPost )
{
    //wait for previous request
    if( this.ajaxLoading == true || this.ajaxStatus != 4 || p_oAjax != null )
    {   
        if( target && target != "" && target != null )
        {
            document.getElementById(target).innerHTML = this.loadingHtml;
        }
        setTimeout( "LoadTryAgain('" + url + "','" + target + "'," + bPost + ")", 500);
        return;
    }
    
    //set status
    this.ajaxLoading = true;
    
    //Reference to the rendering target
    this.renderTarget = target;

    //will we render to a DOM element ?
    if( target && target != "" && target != null)
    {
        this.useRenderTarget = true;
    }
    
    //Display loading screen
    if(this.useRenderTarget)
    {
        this.setRenderTargetContent( this.loadingHtml );
    }
        
    //initialize
    this.req = this.initXMLHTTP();

    //set callback
    if(this.req) 
    {        
        //make ready for GET or POST        
        var sPostOrGet = bPost ? "POST" : "GET";        
        var sPostParams, requestUrl;
        if( url.indexOf('?') != -1 && bPost )
        {
            requestUrl = url.split('?')[0];
            sPostParams = url.split('?')[1];
        }
        else
        {
            requestUrl = url;
            sPostParams = "";            
        }     
        
        //set global reference to CAjax object (needed by the callback)
        p_oAjax = this;
        
        //create async request                 
        this.req.onreadystatechange = this.processingCallback;	         
        this.req.open(sPostOrGet, requestUrl, true);
        if(bPost) this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        this.req.send(sPostParams);
    }
}

// Callback method
CAjax.prototype.processingCallback = function()
{       
    if( p_oAjax != null && p_oAjax )
    {
        //set the reference to the ajax object for this callback object
        if( !this.p_ref_oAjax )
        {       
            this.p_ref_oAjax = p_oAjax;
        }
        
        //perform callback
        if( this.p_ref_oAjax.req )
        {
            this.p_ref_oAjax.ajaxStatus = this.p_ref_oAjax.req.readyState;
            
            if (this.p_ref_oAjax.ajaxStatus == 4) 
            {
                if (this.p_ref_oAjax.req.status == 200) 
                {
                    this.p_ref_oAjax.result = this.p_ref_oAjax.req.responseText; 
                     
                    if( this.p_ref_oAjax.useRenderTarget )
                    {
                        this.p_ref_oAjax.setRenderTargetContent(this.p_ref_oAjax.result);
                    } 
                } 
                else 
                {          
                    if( this.p_ref_oAjax.useRenderTarget )
                    {
                        this.p_ref_oAjax.setRenderTargetContent("<p>Error processing request.</p>");
                    }
                    else
                    {      
                        this.p_ref_oAjax.result = "<p>Error processing request.</p>";
                    }
                }
                
                this.p_ref_oAjax.ajaxLoading = false;
                this.p_ref_oAjax = null;
                p_oAjax = null;
            }
        }
        else
        {
            if( this.p_ref_oAjax.useRenderTarget )
            {
                this.p_ref_oAjax.setRenderTargetContent("<p>XMLHTTP not instanciated.</p>");
            }
            else
            { 
                this.p_ref_oAjax.result = "<p>XMLHTTP not instanciated.</p>";
            }
            
            this.p_ref_oAjax.ajaxLoading = false;
            this.p_ref_oAjax = null;
            p_oAjax = null;
        }
    }
}