<!--
/***************************
AJAX Object
***************************/
function CAjax() {
/* --CAjax object-- */
     /* --CAjax object properties-- */
     this.url = null;
     this.params = null;
     this.method = null;
     
     /* --CAjax object methods-- */
     this.createXMLHttp = CAjax_createXMLHttp;
     this.sendRequest = CAjax_sendRequest;
       
     /* --CAjax object callback methods-- */
     this.callback = null;
     this.errorcallback = null;
     
     /* --CAjax can allow multiple constructors 
          and therefore checks the recieved arguments-- */
     switch (CAjax.arguments.length)
     {
        case 0:
            // Do Nothing;
            break;
        case 1: 
            if ('string' == typeof(CAjax.arguments[0])) // Only one string parameter
            {
                // Only url recieved
                this.url = CAjax.arguments[0];
            } else if ('function' == typeof(CAjax.arguments[0])) // Only one function parameter
            {
                // Only callback recieved
                this.callback = CAjax.arguments[0];
            }
            break;
        case 2:
            if ('string' == typeof(CAjax.arguments[0]) && 'string' == typeof(CAjax.arguments[1])) // Two string parameters
            {
                // url and params recieved
                this.url = CAjax.arguments[0];
                this.params = CAjax.arguments[1];   
            } else if (('string' == typeof(CAjax.arguments[0]) && 'function' == typeof(CAjax.arguments[1]))) // One string & one function parameter
            {
                // url and callback recieved
                this.url = CAjax.arguments[0];
                this.callback = CAjax.arguments[1];
            }
            break;
        case 3:
            if ('string' == typeof(CAjax.arguments[0]) && 'string' == typeof(CAjax.arguments[1]) && 'string' == typeof(CAjax.arguments[2])) // Three string parameters
            {
                // url, params and method recieved
                this.url = CAjax.arguments[0];
                this.params = CAjax.arguments[1];
                this.method = CAjax.arguments[2];
            } else if ('string' == typeof(CAjax.arguments[0]) && 'string' == typeof(CAjax.arguments[1]) && 'function' == typeof(CAjax.arguments[2])) // Two strings & one function parameter
            {
                // url, params and callback recieved
                this.url = CAjax.arguments[0];
                this.params = CAjax.arguments[1];
                this.callback = CAjax.arguments[2];
            } else if ('string' == typeof(CAjax.arguments[0]) && 'function' == typeof(CAjax.arguments[1]) && 'function' == typeof(CAjax.arguments[2])) // One string & two functions parameter
            {
                // url, callback and errorcallback recieved
                this.url = CAjax.arguments[0];
                this.callback = CAjax.arguments[1];
                this.errorcallback = CAjax.arguments[2];
            }
            break;
        case 4:
            if ('string' == typeof(CAjax.arguments[0]) && 'string' == typeof(CAjax.arguments[1]) && 'string' == typeof(CAjax.arguments[2]) && 'function' == typeof(CAjax.arguments[3])) // Three strings & one function parameters
            {
                // url, params, method and callback recieved
                this.url = CAjax.arguments[0];
                this.params = CAjax.arguments[1];
                this.method = CAjax.arguments[2];
                this.callback = CAjax.arguments[3];
            } else if ('string' == typeof(CAjax.arguments[0]) && 'string' == typeof(CAjax.arguments[1]) && 'function' == typeof(CAjax.arguments[2]) && 'function' == typeof(CAjax.arguments[3])) // Two strings & two functions parameters
            {
                // url, params, callback and errorcallback recieved
                this.url = CAjax.arguments[0];
                this.params = CAjax.arguments[1];
                this.callback = CAjax.arguments[2];
                this.errorcallback = CAjax.arguments[3];
            }
            break;
        case 5:
            if ('string' == typeof(CAjax.arguments[0]) && 'string' == typeof(CAjax.arguments[1]) && 'string' == typeof(CAjax.arguments[2]) && 'function' == typeof(CAjax.arguments[3]) && 'function' == typeof(CAjax.arguments[4])) // Three strings & two functions parameters
            {
                // url, params, method, callback and errorcallback recieved
                this.url = CAjax.arguments[0];
                this.params = CAjax.arguments[1];
                this.method = CAjax.arguments[2];
                this.callback = CAjax.arguments[3];
                this.errorcallback = CAjax.arguments[4];
            }
            break;
        default:
            alert("Can't find a matching constructor for the suplied parameters!!!");
            break;
     }
}

/* --Return a XMLHTTPRequest in a browser independent fashion.-- */
function CAjax_createXMLHttp()
{
    // Start with false
    var oxmlhttp = false;
            
    try 
    {
        // Try to create with MSXML
        oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {            
            // Try to create the microsoft way
            oxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            // Failed to create the XMLHTTP object
            oxmlhttp = false;
        }
    }

    // Mozilla then?
    if (!oxmlhttp && typeof(XMLHttpRequest) != 'undefined') 
        oxmlhttp = new XMLHttpRequest();
       
    return oxmlhttp;
}

/* --Perform an AJAX style request and return all arguments sent to the callback function.-- */
function CAjax_sendRequest()
{
    var xmlhttp;
    var reply;
    var error;
    var httpmethod;
    var parsedurl;
    var parsedparams;
    var args = new Array('');
    
    // NOTE: After request submittion, the object is disposed
    //       and all public properties and methods are unavailable.
    //       I will have to save all this data internally so I can
    //       use it when I recieve the reply.
    
    // Check if we have a callback and save it locally for later use.
    if (null != this.callback)
        reply = this.callback;
    
    // Check if we have arguments and save them locally for later use.    
    if (0 < CAjax_sendRequest.arguments.length)
        args = CAjax_sendRequest.arguments;
    
    // Create the XMLHTTP request object.
    xmlhttp = CAjax_createXMLHttp();
    
    // Check if success.
    if (xmlhttp)
    {
        // Some versions of some Mozilla browsers won't work properly 
        // if the response from the server doesn't have an XML mime-type header. 
        // To satisfy this, I'll use an extra method call to override the header 
        // sent by the server, just in case it's not text/xml.
        if (xmlhttp.overrideMimeType)
            xmlhttp.overrideMimeType("text/xml");
        
        // Set default request method to GET.
        httpmethod = "GET";
        if (null != this.method && "POST" == this.method.toUpperCase())
        {
            // POST request
            
            // Set the POST header if needed.
            httpmethod = this.method;
            
            // Set the URL
            parsedurl = this.url;
            
            // Set the parameters
            if (null != this.params)
                parsedparams = this.params;
        } else {
            // GET request
            
            // Set the URL
            parsedurl = this.url;
            
            // Set the parameters
            if (null != this.params)
                parsedurl = parsedurl + "?" + this.params;
         
            parsedparams = null;
        }
        
        // Open connection.
        xmlhttp.open(httpmethod, parsedurl, true);
        
        xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/GetXmlString");
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

        // Set the call back proceedure.
        xmlhttp.onreadystatechange = function()
        {
            // Wait for end. 
            if (xmlhttp.readyState == 4)
            {  
                // The apply method on a given function allows us to call a function and specify 
                // what the keyword "this" will refer to within the context of that function. 
                // The first argument should be an object to which the keyword "this" will refer 
                // to within the context of that function. The second argument to the apply method 
                // is an array. The elements of this array will be passed as the arguments to the 
                // function being called. The array parameter can be either an array literal or 
                // the deprecated arguments property of a function.
                // So what I have to do is to only call the apply on the callback function
                // with the xmlhttp object and the parameters array that had been passed to the 
                // current method. This way, the keyword "this" inside the callback function will 
                // refer to the xmlhttp object and the result can be retrieved by using "this.responseText" 
                // and the parameters array will be passed as the arguments to the function being called.
                
                // Check if we have an error callback.
                if (error)
                {
                    // So I'll check if I have an error
                    if (xmlhttp.status != 200)
                    {
                        try
                        {
                            // Apply parameters and call the error callback function
                            error.apply(xmlhttp, args);
                            
                            // Exit the function
                            return;
                        } catch (e)
                        {
                            return;
                        }
                    }   
                }
                
                // I got here since I did not have an error calback function or recieve any error in the request
                // Check if we have to send the reply to a callback function.
                if (reply)
                {
                    try
                    {
                        // Apply parameters and call the callback function
                        reply.apply(xmlhttp, args);
                        
                        // Exit the function
                        return;
                    } catch (e)
                    {
                        return;
                    }
                }
            }
        }
        
        // Send the request.
        xmlhttp.send(parsedparams);
    }
}
// -->