﻿// JScript File
//=====================================================================================
//    page call example for all browsers
//    edel_communication - Javascript web client API

//    edel_communication is web client API written Javascript.
//    This has a object oriented interface which makes it easy to use.
//    Communication is implemented by using AJAX technology.
//        Asynchronous calls.
//        GET or POST request - ensuring that if it is a GET request, the URL is unique.
//        POST data sent as part of the POST request.
//        Multiple concurrent requests.

    /*
    Coding style:
        Methods and arguments internal to this object start with underscore.
    */
//=====================================================================================

function edel_communication()
{
    _DataRequest = HttpRequest.create();
    _callback_handle_response = null;
    _getReadyStateHandler = edel_communication._getReadyStateHandler;
    
    // API
    this.send_request = edel_communication.send_request;
    
};

// 
edel_communication.send_request = function(callback_handle_response, url, method, getData, postData)
{
    try
    {
        _callback_handle_response = callback_handle_response;
        
        var query_str = "";
        
        if(method == "GET")
        {
            // Prevent browser caching by sending always sending a unique url.
            var randomnumber = "randomnumber="+ new Date().getTime();
            
            if(getData) 
            {
                query_str = "?"+getData+"&"+randomnumber;
            }
            else
            {
                query_str = "?"+randomnumber;
            }
        }
        
        //
        _DataRequest.open(method, url+query_str, true);

        _DataRequest.onreadystatechange = _getReadyStateHandler(_DataRequest,callback_handle_response);
        
        if(method == "POST")
        {    
            _DataRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
            _DataRequest.setRequestHeader("Content-length", postData.length);
            _DataRequest.setRequestHeader("Connection", "Keep-Alive");  
        }
        
        _DataRequest.send(postData);
     }
     catch(err)
     {
        txt="send_request.\n\n";
        txt+="Error description: " + err.description + "\n\n";
        txt+="Click OK to continue.\n\n";
        //alert(txt);
     }

};


//
// The readyState property holds the status of the server's response. 
// Each time the readyState changes, the onreadystatechange function will be executed.
// Here are the possible values for the readyState property:
    // State Description 
    //  0 The request is not initialized 
    //  1 The request has been set up 
    //  2 The request has been sent 
    //  3 The request is in process 
    //  4 The request is complete 

/** 
* Create and return a call back function that checks for server 
* success/failure. * 
* Param req - The XMLHttpRequest whose state is changing 
* Param responseHandler - Function to pass the response to 
* Return: a function that waits for the specified XMLHttpRequest 
*     to complete, then passes its XML response to the given handler function. 
*/
edel_communication._getReadyStateHandler = function(req, responseHandler) 
{  
    // Return an anonymous function that listens to the XMLHttpRequest instance  
    return function () 
    {    
        try
        {
            var status = false;
            
            // If the request's status is "complete"    
            if(req.readyState==4)
            {
                // check if HTTP responce is '200 OK' and responseXML is not null
                
                if(req.status && req.status==200 && req.responseXML && req.responseXML.childNodes.length > 0)
                {
                    status = true;
                }
                else
                {
                    status = false;
                }
            
                responseHandler(status, req.responseXML, req.responseText);
            }
       }
       catch(err)
       {
         txt="_getReadyStateHandler.\n\n";
         txt+="Error description: " + err.description + "\n\n";
         txt+="Click OK to continue.\n\n";
         //alert(txt);
         HandleException();
       }
    };
};



