﻿var AjaxProcessor = {
    //ErrorHandler: handler which will be called if any error occure while ajx request
    //parameters
    //error: object of XMLHttpRequest.error
    ErrorHandler: function(error, request)
    {        
        
        var form = document.forms[0];
        
        if (error.Type == 'ConnectFailure')
        {
            var formId = AjaxProcessor.contexts_[request.args.id].formEl.id;
            Ext.get(formId).unmask();
            Ext.Msg.show({
                   title:'Server is not responding.',
                   msg: 'Try again later.',
                   buttons: Ext.Msg.OK,
                   width: 200                   
                });
        }
        else
        {
            if (Ext.isIE) // IE
            {
                var input = document.createElement('<input name="' + request.args.action + '"/>');
            }
            else
            {
                var input = document.createElement('input');
                input.setAttribute('name', request.args.action);
            }
            
            input.setAttribute('type', 'hidden');
            form.appendChild(input);
            form.submit();
        }
                
        
    },    
    TimeoutHandler: function()
    {
        var formId = AjaxProcessor.contexts_[this.args.id].formEl.id;
        Ext.get(formId).unmask();
        Ext.Msg.show({
               title:'Timeout error',
               msg: 'Try again later.',
               buttons: Ext.Msg.OK,
               width: 200                   
            });
    },
    TimeoutPeriod: 60000,
    DefaultServerResponseHandler: null,

    //RegisterServerCallback: registers handler for parsed ajax response
    //parameters
    //commandName: string name of command
    //handler: function(cmd) - function which as parameter receive object of cmd
    //cmd.Action: String name of action
    //cmd.Params: object with params
    RegisterServerCallback: function(commandName, handler)
    {
        AjaxProcessor.handlers_[commandName] = { func: handler};
    },

    //Request: function which performs Ajax request to specified page with specified paramters
    //parameters
    //page: page object name
    //action: string action name, which will be executed on server side
    //action name format: action_[moduleName]_[actionName]_[paramName0]_[paramValue0]_[...]_[paramNameN]_[paramValueN]
    //parameters: additional parameters for action
    Request: function (page, action, parameters, context)
    {
        var customViewState = document.getElementById("__CUSTOMVIEWSTATE");
        var value;
        if (customViewState)
        {
            value = customViewState.getAttribute("value");
        }
        if (AjaxProcessor.TimeoutPeriod)
            AjaxPro.timeoutPeriod = AjaxProcessor.TimeoutPeriod;
        if (AjaxProcessor.TimeoutHandler)
        {
            if (AjaxPro.onTimeout)
                AjaxPro.onTimeout = AjaxProcessor.TimeoutHandler;
        }
    
        var id = AjaxProcessor.lastRequestId_;
        AjaxProcessor.lastRequestId_++;
        if (context)
        {
            AjaxProcessor.contexts_[id] = context;
        }
        page.ProcessAjaxRequest(id, action, parameters, value, AjaxProcessor.HandleAjaxResponse)
    }, 
    
    //Return context object by id (need to timeout handler, etc...)
    GetContext: function(id)
    {
        var ret = null;
        if (AjaxProcessor.contexts_)
        {
            try
            {
                ret = AjaxProcessor.contexts_[id];
            }
            catch(e)
            {
                ret = null;
            }
        }
        return ret;
    },       
    
    HandleAjaxResponse: function(response)
    {
        if(response.error)    
        {
            if (AjaxProcessor.ErrorHandler)
            {
                AjaxProcessor.ErrorHandler(response.error, response.request);
            }
        }
        else
        {
            AjaxProcessor.ProcessAjaxResponse(response.value);
        }
    },
    
    ProcessAjaxResponse: function(response)
    {
        var r = AjaxProcessor.ParseAjaxResponse(response);
        var id = r.id;
        var cmds = r.actions;
        var context = AjaxProcessor.contexts_[id];
        var newViewState = r.viewstate;
        var customViewState = document.getElementById("__CUSTOMVIEWSTATE");        
        if (customViewState)
        {
            customViewState.setAttribute("value", newViewState);
        }
        AjaxProcessor.contexts_[id] = null;
        if (cmds)
        {
            for (var i = 0; i < cmds.Actions.length; i++)
            {
                var cmd = cmds.Actions[i];
                
                var handler = AjaxProcessor.handlers_[cmd.Action];
                var func = null;
                if (handler)
                {
                    func = handler.func;
                }
                else
                {
                    if (AjaxProcessor.DefaultServerResponseHandler)
                    {
                        func = AjaxProcessor.DefaultServerResponseHandler;
                    }
                }
                if (func)
                {
                    func(cmd, context);
                }
            }
        }
    },
   
    ParseAjaxResponse: function(response)
    {
        eval("var commands = " + response);
        return commands
    },   
    
    handlers_: new Array(),
    contexts_: new Array(),
    lastRequestId_: 0
}