// A templatized generic response handler. The handler is preprogrammed to check
// error codes from the AJAX call. If there is a valid response then the
// handler offloads the processing of the response to the function provided by 
// the client of this class. Clients of this object can override the definition
// of the 'getResponseProcessor' method to define the actual response processing
// routine. If none is provided by the client, the dafualt do-nothing is used.
function ResponseHandler() {
    // This method templatizes the response processing. The actual processing is
    // done by the function represented by 'processResponse'.
    this.handleResponse = function (processResponse) {
        return function(xmlHttp) {
            // Dispose the wait panel, if any.
            GCROUtils.hideWaitingMessage();
            
            // If the response is valid then process the response.
            if (xmlHttp != null && xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                processResponse(xmlHttp);
            }
        };
    };
    
    // The default do-nothing response processor.
    this.getResponseProcessor = function() {
        return function(xmlHttp) {
        };
    };
};
  
// Definition of a very common response handler that updates the map. Examples
// of usage can be found in keyword search, zooming to a feature from a keyword
// search etc.
var MapUpdateResponseHandler = new ResponseHandler();
MapUpdateResponseHandler.getResponseProcessor =  function () {
    return function (xmlHttp) {
        // The following call updates the map based on the response.
        EsriControls.processPostBack(xmlHttp);
        
        // As part of the map refresh, the tools associated with it are
        // also refreshed. The keyword search tool is the current tool item
        // that is active. As a result of the toolbar refresh, the current
        // tool gets activated again. This in turn causes the dialog panel
        // to show up at the end of the zoom. This can happen for any 
        // command or tool that requires a map refresh. To prevent this from
        // happening, the current tool marker is cleared so as to trick the
        // ESRI JavaScript into not activating the search tool item. 
        GCROUtils.getMap().clearCurrentToolItem();
    };
};
  
var GCROUtils = new function () {
    this.getMap = function() {
        return EsriControls.maps[currentMapId];
    };
    
    this.performCommand = function(url, doGet, commandName, contextName, requestParameters, responseHandler, waitMessage) {
        // The map is necessary to get some information required by the server-side.
        var map = GCROUtils.getMap();
        var mapId = map.id;
        var formId = map.formId;
        
        // Assemble the parameters provided by the caller of this method. The
        // 'ignore' parameter is not used at all by any server side action. It
        // is used so as to avoid logic to figure out whether a & character is 
        // required or not at the very end of the parameter list.
        var requestParamsString = "&ignore=ignore";
        if (requestParameters) {
            for (var i = 0; i < requestParameters.length; i++) {
                requestParamsString += "&" + requestParameters[i];
            }
        }
        
        // Assemble the parameters for the request.
        var params = "command=" + commandName;
        params += requestParamsString;
        params += "&mapId=" + mapId;
        params += "&formId=" + formId;
        params += "&contextName=" + contextName;
        params += "&" + EsriUtils.buildRequestParams(map.formId);
        
        // If no custom response handler is passed in then use the default. The 
        // default handler assumes changes to the map and hence applies them.
        if (responseHandler == null) {
            responseHandler = new ResponseHandler();
        }

        // Send out the request and receive the response.
        var xmlHttp = EsriUtils.sendAjaxRequest(url, params, doGet, 
            responseHandler.handleResponse(responseHandler.getResponseProcessor()));
            
        // Create a new modal wait panel and display it, if there is a message.
        if (waitMessage != null && waitMessage != "") {
            this.showWaitMessage(waitMessage);
        }
    };
    
    // This is a utility function that shows a customizable wait panel. Currently
    // only the message can be customized.
    this.showWaitMessage = function (message) {
        // Create a new modal wait panel and display it.
        waitPanel = new YAHOO.widget.Panel("wait",   
            { 
                width:"300px",  
                fixedcenter:true,  
                close:false,  
                draggable:false,  
                modal:true, 
                visible:false 
             }); 
    
        waitPanel.setHeader(message); 
        waitPanel.setBody("<img src=\"img/rel_interstitial_loading.gif\" />");
        waitPanel.render(document.body);
        waitPanel.show();
    };
    
    this.hideWaitingMessage = function () {
        // Dispose the wait panel, if any.
        if (waitPanel != undefined) {
            waitPanel.hide();
        }
    };    
};
