
Ext.onReady(function(){

    this.screen = Ext.get('login');
    //this.screen = Ext.getBody();
    //this.screen.setWidth(screen.width);
    //this.screen.setHeight(screen.height);
   
    Ext.apply(this,{
        contenedor     : Ext. getBody()
        ,validar : function(){

            if (!this.u.isValid()){
                this.u.focus();
                return false;
            }
            else if (!this.c.isValid()){
                this.c.focus();
                return false;
            }
            else
                return true;

        }
        ,enter : function(e){

            if(e.getKey()==13) this.aut(e);

        }
        ,aut : function(e){

            if (this.process == 1) return false;
            if (!this.validar()==true) return false;
            if (e.stopEvent) e.stopEvent();
            this.process = 1;

             var myMask = new Ext.LoadMask(this.screen, {msgCls: '',msg:'Autenticando, espere un momento por favor..'});
             myMask.show();
            
            var options = {
                url        : 'autacceso.php'
                ,method     : 'post'
                ,scope      : this
                ,params     : {}
                ,timeout    : 300000
                ,callback   : function(options, success, response){

                    Ext.Ajax.timeout=30000;

                    this.process=0;
                    

                    var i, o;
                    if(true === success) {
                        
                        try {
                            o = Ext.decode(response.responseText);
                        }
                        catch(ex) {
                            try{
                                eval(response.responseText);
                            }
                            catch(ex){
                                this.screen.unmask();
                                alert('Lo sentimos pero el sistema intranet no se encuentra disponible. Error :' + ex + ' ' +response.responseText);
                                
                            }
                            //Ext.Msg.alert("Error", response.responseText);
                            return;
                        }
                        if(true === o.success) {
                            if (o.url) {
                                top.location=o.url;
                                return;
                            }
                        }
                        else {
                            if (o.msgError)
                                alert( o.msgError);
                            else
                                alert( 'Lo sentimos pero el sistema intranet no se encuentra disponible<br>Respuesta del servidor: ' + (response.responseText || 'Sin respuesta'));
                            
                        }
                    }
                    else {
                        alert('Lo sentimos pero el sistema intranet no se encuentra disponible<br>Respuesta del servidor: ' + (response.responseText || 'Sin respuesta'));
                    }
                    this.screen.unmask();
                }
            };

            Ext.apply(options.params,{
                idenus    : this.u.getValue()
                ,idenps  : this.c.getValue()
            });

            Ext.Ajax.request(options);
       }
    });

    this.u = new Ext.form.TextField({
        applyTo         : 'idenus'
        ,allowBlank     : false
        ,selectOnFocus  : true
        ,width          : 103
        ,emptyText      : 'Usuario'
        ,listeners      : {
            specialkey : function(t,e){
                this.enter(e);
            }
            ,scope     : this
        }
    });

    this.c = new Ext.form.TextField({
        applyTo         : 'idenps'
        ,allowBlank     : false
        ,selectOnFocus  : true
        ,width          : 103
        ,emptyText      : 'clave'
        ,listeners      : {
            specialkey : function(t,e){
                this.enter(e);
            }
            ,scope          : this
        }
    });

    this.boton = Ext.get('boton');

    this.boton.on({
        click : function(bt, e){
            this.aut(e)
        }
        ,scope : this
    });
 

}, Ext.ux);
