/*
 * Creado por Luis Remicio
 */
Ext.BLANK_IMAGE_URL = '../javascript_class/ext-3.2.1/resources/images/default/s.gif';
Ext.ns('login');

login={
    captchaURL : "captcha/CaptchaSecurityImages.php?width=160&height=80&characters=4&t=",
    vurl:'cambio_password.php',
    init:function(){
         Ext.QuickTips.init();
         Ext.get(document.body).setStyle('background-color','#005429');
         var boxCaptcha = 	new Ext.BoxComponent({
            columnWidth:.50,
            left:50,
            autoEl: {
                tag:'img'
                ,id: 'activateCodeImg'
                ,title : 'Click to refresh code'
                ,src:this.captchaURL+new Date().getTime()
            }
        });

        var win = new Ext.Window({
            id:'vnt_password_x',
            title:'Actualizacion de Clave de Acceso',
            width:600,
            height:350,
            padding:5,
            autoDestroy:true,
            minimizable:false,
            maximizable:false,
            closable:false,
            layout:'fit',
            frame:true,
            bodyStyle:'background-color:#fff;padding: 10px',
            items:[{
                    xtype:'panel',
                    layout:'table',
                    layoutConfig:{
                        columns:2
                    },
                    items:[
                        {
                            xtype:'panel',
                            rowspan:2,
                            width:355,
                            heigth:400,
                            border:false,
                            items:[
                                {
                                    xtype:'form',
                                    border:false,
                                    labelWidth:130,
                                    id:'form_login',
                                    name:'form_login',
                                    url:this.vurl,
                                    items:[
                                        {
                                            xtype:'textfield',
                                            fieldLabel:'Usuario',
                                            width:200,
                                            name:'usuario',
                                            id:'usuario',
                                            allowBlank:false
                                        },
                                        {
                                            xtype:'textfield',
                                            fieldLabel:'Contrase&ntilde;a Actual',
                                            width:200,
                                            name:'clave_a',
                                            id:'clave_a',
                                            inputType:'password',
                                            allowBlank:false
                                        },
                                        {
                                            xtype:'textfield',
                                            fieldLabel:'Nueva Contrase&ntilde;a',
                                            width:200,
                                            name:'password',
                                            id:'password',
                                            inputType:'password',
                                            allowBlank:false
                                        },
                                        {
                                            xtype:'panel',
                                            border:false,
                                            height:25,
                                            html:"<div id='passwdRating' style='float: left; padding-left: 5px;'><div id='pass_bar' class='pass_bar'><div class='pass_bar_base pass_bar_0 pass_bar_color_1'></div></div><div style='display: inline;' id='ps-rating'></div></div>"
                                        },
                                        {
                                            xtype:'textfield',
                                            fieldLabel:'Repetir Contrase&ntilde;a',
                                            width:200,
                                            name:'password_re',
                                            id:'password_re',
                                            inputType:'password',
                                            allowBlank:false
                                        },
                                        {
                                            xtype:'panel',
                                            border:false,
                                            layout : 'table',
                                            layoutConfig: {
                                                columns: 2
                                            },
                                            height:100,
                                            items:[
                                                {
                                                    xtype:'panel',
                                                    width:130,
                                                    border:false
                                                },boxCaptcha,{
                                                    xtype:'hidden',
                                                    name:'p1',
                                                    id:'p1',
                                                    value:'cambiar_contrasenia'
                                                },{
                                                    xtype:'hidden',
                                                    name:'id_sistema',
                                                    id:'id_sistema',
                                                    value:'4'
                                                }
                                            ]
                                        },
                                        {
                                            xtype:'textfield',
                                            fieldLabel:'Codigo de Seguridad',
                                            width:50,
                                            name:'codigo',
                                            id:'codigo',
                                            allowBlank:false,
                                            maxLength: 4,
                                            autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete: 'off', maxlength: '4'}
                                        }
                                    ],
                                    buttons:[
                                        {
                                            text:'Aceptar',
                                            name:'btn_aceptar',
                                            id:'btn_aceptar'
                                        },
                                        {
                                            text:'Limpiar',
                                            name:'btn_limpiar',
                                            id:'btn_limpiar'
                                        },
                                        {
                                            text:'Regresar',
                                            name:'btn_regresar',
                                            id:'btn_regresar'
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            xtype:'panel',
                            title:'Requerimientos minimos',
                            width:243,
                            height:231,
                            html:'<li>* Mínimo 8 caracteres de longitud</li><li>* Debe de contener 3 de los 4 de los siguientes elementos:<br>- Letras en mayúsculas<br>- Letras en minúsculas<br>- Números<br>- Símbolos Especiales @$#&%*<br>Solo se aceptaran Contraseñas consideradas Fuertes<br> Ejemplo:<br>ParaIso23<br>P@raiso45<br>RobertO34<br>PerUanos567<br>LosOlivos#@12</li>'
                        },
                        {
                            xtype:'panel',
                            height:80,
                            frame:true,
                            border:false
                        }
                    ]
            }],
            draggable:false,
            resizable:false
        });
        win.show();
        setTimeout("login.evt_password();",500);
        
    },
    evt_password:function(){
        Ext.fly('usuario').focus();
        Ext.fly('usuario').on('keypress',function(event){
            this.evt_key(event, 1);
        },this);
        Ext.fly('clave_a').on('keypress',function(event){
            this.evt_key(event, 2);
        },this);
        
        Ext.fly('password').on('keypress',function(event){
            this.evt_key(event, 3);
        },this);
        Ext.fly('password').on('blur',function(event){
            var clave00 = Ext.fly('password').getValue().trim();
            var clave01 = Ext.fly('ps-rating').dom.textContent;
            if(clave00!=''){
                var nivel = parseInt(clave01.split("(")[1].split('/')[0]);
                if(nivel<70){
                    Ext.Msg.alert('Simi-Web','Clave poco robusta');
                    Ext.fly('password').dom.value = '';
                }
            }
        },this);
        Ext.fly('password').on('keyup',function(event){
            this.passkey_handler();
        },this);
        Ext.fly('password_re').on('keypress',function(event){
            this.evt_key(event, 4);
        },this);
        Ext.fly('password_re').on('blur',function(event){
            var clave00 = Ext.fly('password').getValue().trim();
            var clave01 = Ext.fly('password_re').getValue().trim();
            if(clave00!='' && clave01!=''){
                if(Ext.fly('password').getValue().trim()!=Ext.fly('password_re').getValue().trim()){
                    Ext.Msg.alert('Simi-Web','Contrase&ntilde;as no coinciden');
                    Ext.fly('password_re').dom.value = '';
                }
            }
        },this);
        Ext.fly('codigo').on('keypress',function(event){
            this.evt_key(event, 5);
        },this);
        Ext.getCmp('btn_aceptar').on('click',function(obj,e){
            this.actualizarPassword();
        },this);
        Ext.getCmp('btn_limpiar').on('click',function(obj,e){
            this.limpia();
        },this);
        Ext.getCmp('btn_regresar').on('click',function(obj,e){
            var redirect = '../';
            window.location = redirect;
        },this);
    },
    evt_key:function(e,op){
        var tecla = e.keyCode;
        if(tecla == 13){
            switch(parseInt(op))
            {
                case 1:Ext.fly('clave_a').focus();break;
                case 2:Ext.fly('password').focus();break;
                case 3:Ext.fly('password_re').focus();break;
                case 4:Ext.fly('codigo').focus();break;
                case 5:Ext.getCmp('btn_aceptar').focus();break;
            }
        }
    },
    passkey_handler:function(){
        var passwordEl = document.getElementById('password');
        updatePasswordStrength_new(passwordEl,'passwdRating',{'text':2});
    },
    limpia:function(){
        var txt = ['usuario','clave_a','password','password_re','codigo'];
        for(i=0;i<txt.length;++i){
            Ext.fly(txt[i]).dom.value='';
        }
        this.passkey_handler();
        Ext.fly('usuario').focus();
    },
    actualizarPassword:function(){
        Ext.getCmp('form_login').getForm().submit({
            clientValidation:true,
            waitTitle:'Simi-Web',
            waitMsg:'Enviando datos...',
            scope:this,
            success:function(form,action){
                Ext.Msg.alert('Simi-Web',action.result.msg,function(btn,text){
                    if(btn=='ok' || btn=='cancel'){
                        var redirect = '../';
                        window.location = redirect;
                    }
                },this);
            },
            failure: function(form,action){
                switch (action.failureType) {
                    case Ext.form.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Simi-Web', 'Debe de completar todos los campos.');
                        break;
                    case Ext.form.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('Simi-Web', 'Error en la comunicacion con el servidor');
                        break;
                    case Ext.form.Action.SERVER_INVALID:
                        Ext.Msg.alert('Simi-Web', action.result.msg);
                        break;
                    default:
                        Ext.Msg.alert('Simi-Web',action.result.msg);
                }
            }
        });
    }
}
var pass_strength_phrases = {
    0: 'MUY DEBIL',
    10: 'MUY DEBIL',
    20: 'DEBIL',
    30: 'DEBIL',
    40: 'DEBIL',
    50: 'OK',
    60: 'OK',
    70: 'FUERTE',
    80: 'FUERTE',
    90: 'MUY FUERTE',
    100: 'MUY FUERTE'
};
Ext.onReady(login.init,login);

