Ext.namespace("Ext.ux");
Ext.ux.AjaxRequestFactory = function(){    
    /*var _do = function(strUrl, objParams, objFunctionSuccess, objFunctionFailure, strMethod, strFormName){
        Ext.Ajax.request({
            method:strMethod,
            url: strUrl,
            success: objFunctionSuccess,
            failure: objFunctionFailure,
            params: objParams,
            form: strFormName
        });
    }*/
    var _do = function(strUrl, objParams, objFunctionSuccess, objFunctionFailure, strMethod, strFormName, objFunctionGetTex){
        Ext.Ajax.request({
            method:strMethod,
            url: strUrl,
            success: function(response,options){
                var objJSon = _fnConverTextToJSon(response.responseText);
                if(objJSon === null){
                    if(objFunctionGetTex != null){
                        objFunctionGetTex(response.responseText);
                    }else{
                        alert('Ocurrio un error al enviar los datos. Por favor presione F5 y reintente');
                    }
                }else{
                    if(objJSon.resultado == true){
                        objFunctionSuccess(objJSon);
                    }else{
                        objFunctionFailure(objJSon);
                    }
                }
            },
            failure: function(response,options){
                if(response.status != '401' && response.statusText){
                    alert(response.statusText);
                }
                else if(response.status == '401'){
                    Ext.Msg.show({
                    title:'Atención',
                    closable: false,
                    msg: '<BR>LA SESION HA FINALIZADO. Deberá identificarse nuevamente.<br><b>Presione la tecla F5 para continuar.</b><br>',
                    icon: Ext.MessageBox.WARNING
                    });
                }else{
                    alert('Ocurrio un error de comunicación. Reintente...');
                }
            },
            params: objParams,
            form: strFormName
        });
    }
    var _doUpload = function(strUrl, strFormName, objParams){
        Ext.Ajax.request({
            method:'POST',
            url: strUrl,
            params: objParams,
            headers: {'Content-type':'multipart/form-data'},
            success: fnSuccess,
            failure: fnFailure,
            form: Ext.getCmp(strFormName).getForm().getEl().dom
        });
    }
    var fnFailure = function(response,options){
        if(response.status != '401' && response.statusText){
            MG54.Ext.ux.MessagesHelper.Alert('Error', response.statusText);
        }
        else if(response.status == '401'){
            Ext.Msg.show({
            title:'Atención',
            closable: false,
            msg: '<BR>LA SESION HA FINALIZADO. Deberá identificarse nuevamente.<br><b>Presione la tecla F5 para continuar.</b><br>',
            icon: Ext.MessageBox.WARNING
            });
        }else{
            MG54.Ext.ux.MessagesHelper.Alert('Error', 'Ocurrio un error de comunicación. Reintente...');
        }
    }
    var fnSuccess = function(response,options){
            var objJSon = _fnConverTextToJSon(response.responseText);
            if(objJSon === null){
                MG54.Ext.ux.MessagesHelper.Alert('Error', 'Ocurrio un error al enviar los datos. Por favor presione F5 y reintente');
            }else{
                if(objJSon.resultado == true){
                    MG54.Ext.ux.MessagesHelper.Info('Informacion', objJSon.mensaje);
                }else{
                    MG54.Ext.ux.MessagesHelper.Alert('Error', objJSon.mensaje);
                }
            }
    }
    var _fnConverTextToJSon = function(strText){
            var objJSon = null;
            try {
                objJSon = Ext.decode(strText);
            }
            catch(e){
                objJSon = null
            }
            return objJSon;
    }
    return {
        doPOST: function(strUrl, objParams, objFunctionSuccess, objFunctionFailure, objFunctionGetTex){
            _do(strUrl, objParams, objFunctionSuccess, objFunctionFailure, 'POST', null, objFunctionGetTex);
        },
        doGET: function(strUrl, objParams, objFunctionSuccess, objFunctionFailure){
            _do(strUrl, objParams, objFunctionSuccess, objFunctionFailure, 'GET', null);
        },
        doFormPOST: function(strUrl, strFormName, objParams, objFunctionSuccess, objFunctionFailure, objFunctionGetTex){
            _do(strUrl, null, objFunctionSuccess, objFunctionFailure, 'POST', strFormName, objFunctionGetTex);
        },
        doFormGET: function(strUrl, strFormName, objParams, objFunctionSuccess, objFunctionFailure){
            _do(strUrl, objParams, objFunctionSuccess, objFunctionFailure, 'GET', null);
        },
        doSimplePOST: function(strUrl, objParams){
            _do(strUrl, objParams, fnSuccess, fnFailure, 'POST', null);
        },
        doSimpleGET: function(strUrl, objParams){
            _do(strUrl, objParams, fnSuccess, fnFailure, 'GET', null);
        },
        doSimpleFormPOST: function(strUrl, strForm){
            _do(strUrl, null, fnSuccess, fnFailure, 'POST', strForm);
        },
        doSimpleFormGET: function(strUrl, strForm){
            _do(strUrl, null, fnSuccess, fnFailure, 'GET', strForm);
        },doFormUpload: function(strUrl, strForm, objParams){
            _doUpload(strUrl, strForm, objParams);
        },
        converTextToJSon: function(strText){
            return _fnConverTextToJSon(strText);
        }
    }
}();