/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
 
 Ext.apply(Ext.form.VTypes, {
    daterange : function(val, field) {
        var date = field.parseDate(val);

        if(!date){
            return;
        }
        if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
            var start = Ext.getCmp(field.startDateField);
            start.setMaxValue(date);
            start.validate();
            this.dateRangeMax = date;
        } 
        else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
            var end = Ext.getCmp(field.endDateField);
            end.setMinValue(date);
            end.validate();
            this.dateRangeMin = date;
        }
        /*
         * Always return true since we're only using this vtype to set the
         * min/max allowed values (these are tested for after the vtype test)
         */
        return true;
    },

    password : function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },

    passwordText : 'la clave no es igual'
});

Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var bd = Ext.getBody();
	Ext.namespace('solitud');  
    //bd.createChild({tag: 'h2', html: 'Form 3 - A little more complex'});
    solitud.movimientoRecord = new Ext.data.Record.create([  
        {name: 'mov_id', type: 'int'},  
        {name: 'cNom', type: 'string'},  
		{name: 'cApe', type: 'string'},  
        {name: 'mov_fecha', type: 'date', dateFormat: 'Y-m-d'},  
        {name: 'mov_deber', type: 'float'},  
        {name: 'mov_haber', type: 'float'}  
    ]);  	
	
    solitud.movimientosFormReader = new Ext.data.JsonReader({  
        root : 'data',  
        successProperty : 'success',  
        totalProperty: 'total',  
        id: 'mov_id'  
       },solitud.movimientoRecord  
     );  	
 
    var top = new Ext.FormPanel({
        labelAlign: 'top',
		layout: 'fit',
		//buttonAlign:'center',
        frame:true,
        reader: solitud.movimientosFormReader,  
        title: 'Registro de Usuarios',
        bodyStyle:'padding:5px 5px 0',
        width: 600,
        items: [{
            layout:'column',
            items:[{
                columnWidth:.5,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    fieldLabel: 'Nombre',
                    name: 'cNom',
                    anchor:'95%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'Apellido',
                    name: 'cApe',
                    anchor:'95%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'No. Cedula',
                    name: 'cCed',
                    anchor:'95%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'Ocupación',
                    name: 'cOcup',
                    anchor:'95%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'Ciudad',
                    name: 'cCiud',
                    anchor:'95%'
                }]
            },{
                columnWidth:.5,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    fieldLabel: 'Telefono',
                    name: 'cTel',
                    anchor:'95%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'Celular',
                    name: 'cTel',
                    anchor:'95%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'Correo Electronico',
                    name: 'email',
                    vtype:'email',
                    anchor:'95%'
                },{ xtype:'textfield',
                    fieldLabel: 'Clave',
                    name: 'pass',
					inputType: 'password',
                    id: 'pass'
                },{ xtype:'textfield',
                    fieldLabel: 'Confirmar Clave',
                    name: 'pass-cfrm',
                    vtype: 'password',
					inputType: 'password',
                    initialPassField: 'pass' // id of the initial password field
                  }				
				]
            }]
        }]

    });

   top.addButton({  
         text : 'Borrar formulario',  
         disabled : false,  
         handler : function() {  
             top.getForm().reset();  
         }  
     });  

   top.addButton({  
         text : 'Guardar',  
         disabled : false,  
         handler : function() {  
             top.getForm().submit({  
                 url : 'salvaClaves.php',  
                 waitMsg : 'Salvando datos...',  
                 failure: function (form, action) {  
                     Ext.MessageBox.show({  
                         title: 'Error al salvar los datos',  
                         msg: 'Error al salvar los datos.',  
                         buttons: Ext.MessageBox.OK,  
                         icon: Ext.MessageBox.ERROR  
                     });  
                 },  
                 success: function (form, request) {  
                     Ext.MessageBox.show({  
                         title: 'Datos salvados correctamente',  
                         msg: 'Datos salvados correctamente',  
                         buttons: Ext.MessageBox.OK,  
                         icon: Ext.MessageBox.INFO  
                     });  
                     responseData = Ext.util.JSON.decode(request.response.responseText);  
                     top.getForm().reset();  
                     //top.movimientosDataStore.load({params: {start:0,limit:20}});  
                 }  
             });  
         }  
     });  
   

   top.render(document.body);

});
