MantenedorUsuarios = function(config){ 
	this.config = config;
};


Ext.extend(MantenedorUsuarios, Ext.BasicDialog, {
	
	init		: function(){
		
		function formatBoolean(value){
        	return value ? '<img src="images/delete.gif">' : ''; 
    	};

    	this.cm = new Ext.grid.ColumnModel([
				{
				   header: " ",
				   dataIndex: 'Elm',
				   width: 20,
				   renderer: formatBoolean,
			   	   editor: new Ext.grid.GridEditor(new Ext.form.Checkbox())
				},{
				   header: "Login",
				   dataIndex: 'USU_LOGIN_RED',
				   width: 70,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 1
				   }))
				},{
				   header: "Perfil",
				   dataIndex: 'UP_PFL_ID',
				   width: 100,
				   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'perfiles', 
						   lazyRender:true
						}))
				},{
				   header: "Rut",
				   dataIndex: 'USU_RUT',
				   width: 65,
				   editor: new Ext.grid.GridEditor(new Ext.form.NumberField({
					   allowBlank: false,
					   allowNegative: false
				   }))
				},{
				   header: "Dv",
				   dataIndex: 'USU_DV',
				   width: 20,
				   align: 'right',
				   editor: null
				},{
				   header: "Area",
				   dataIndex: 'USU_AREA',
				   width: 80,
				   editor:  new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'areas',
						   lazyRender:true
						}))
				},{
				   header: "Nombres",
				   dataIndex: 'USU_NOMBRES',
				   width: 100,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 50
				   }))
				},{
				   header: "Apellido Paterno",
				   dataIndex: 'USU_APELLIDO_PATERNO',
				   width: 100,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 50
				   }))
				},{
				   header: "Apellido Materno",
				   dataIndex: 'USU_APELLIDO_MATERNO',
				   width: 100,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 50
				   }))
				},{
				   header: "Empresa",
				   dataIndex: 'USU_EMPRESA',
				   width: 150,
				   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'empresas', 
						   lazyRender:true
						}))
				},{
				   header: "Email",
				   dataIndex: 'USU_EMAIL',
				   width: 100,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 50
				   }))
				},{
				   header: "Telefono",
				   dataIndex: 'USU_TELEFONO',
				   width: 80,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 50
				   }))
				},{
				   header: "Password",
				   dataIndex: 'USU_PASSWORD',
				   width: 80,
					   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 50
				   }))
				},{
				   header: "Celular",
				   dataIndex: 'USU_CELULAR',
				   width: 60,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 20
				   }))
				},{
				   header: "Cargo",
				   dataIndex: 'USU_CARGO',
				   width: 130,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxValue: 20
				   }))
				},{
				   header: "Genero",
				   dataIndex: 'USU_GENERO',
				   width: 70,
				   align: 'right',
				   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'generos', 
						   lazyRender:true})
							)
				}
		]);
//		alert(this.cm.config[2].editor.actionMode);
		this.cm.config[2].editor.addListener('beforeselect', function() {alert('.')} );
		
		this.cm.defaultSortable = true;
	
		this.Items = Ext.data.Record.create([
							{name: 'Elm', 				type: 'string'},
							{name: 'USU_LOGIN_RED', type: 'string'},
							{name: 'UP_PFL_ID', type: 'string'},
							{name: 'USU_AREA', type: 'string'},
							{name: 'USU_RUT', type: 'string'},
							{name: 'USU_DV', type: 'string'},
							{name: 'USU_NOMBRES', type: 'string'},
							{name: 'USU_APELLIDO_PATERNO', type: 'string'},
							{name: 'USU_APELLIDO_MATERNO', type: 'string'},
							{name: 'USU_EMPRESA', type: 'string'},
							{name: 'USU_EMAIL', type: 'string'},
							{name: 'USU_TELEFONO', type: 'string'},
							{name: 'USU_PASSWORD', type: 'string'},
							{name: 'USU_CARGO', type: 'string'},
							{name: 'USU_CELULAR', type: 'string'},
							{name: 'USU_GENERO', type: 'string'}
						]);
	
		// crea el Data Store
		this.ds = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({url: 'modulos/usuarios/listar.asp'}),
			baseParams  : {fun: ''},
			reader: new Ext.data.XmlReader({
				   record: 'Usuario'
			   }, this.Items)
		});
	
		// crea el editor grid
		this.grid = new Ext.grid.EditorGrid(this.config.gridEl, {
			ds: this.ds,
			cm: this.cm,
			enableColLock:false
		});
	
		var layout = Ext.BorderLayout.create({
			center: {
				margins:{left:3,top:3,right:3,bottom:3},
				panels: [new Ext.GridPanel(this.grid)]
			}
		}, this.config.conEl);
	
		this.grid.render();
	
		var gridHead = this.grid.getView().getHeaderPanel(true);
		this.tb = new Ext.Toolbar(gridHead, [{
					text: 'Guardar',
					handler : this.guardarCambios.createDelegate(this)
				},{
					text: 'Eliminar',
					handler : this.eliminarSeleccionados.createDelegate(this)
				},{
					text: 'Nuevo',
					handler : this.nuevoRegistro.createDelegate(this)
				}]);
	
		this.ds.load();	
	},
	
	nuevoRegistro  : function(){
		var p = new this.Items({Elm : '',
							USU_LOGIN_RED: '',
							UP_PFL_ID: '',
							USU_AREA: '',
							USU_RUT: '',
							USU_DV: '',
							USU_NOMBRES: '',
							USU_APELLIDO_PATERNO: '',
							USU_APELLIDO_MATERNO: '',
							USU_EMPRESA: '',
							USU_EMAIL: '',
							USU_TELEFONO: '',
							USU_PASSWORD: '',
							USU_CARGO: '',
							USU_CELULAR: '',
							USU_GENERO: ''});
		this.grid.stopEditing();
		this.ds.insert(0, p);
		this.grid.startEditing(0, 0);
	},
	
	guardarCambios : function(){
		var lds = this.ds;
		// obtiene filas modificadas
		var eds = this.ds.getModifiedRecords();   
			
		for (i = 0; i < eds.length; i++){
			callback = { 
				success: function(o){
						 var cadErrores = o.responseText.htmlDecode();
						 if (cadErrores != ""){
							 new mensaje('Guardar.', cadErrores ,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
						 }else{
							lds.commitChanges();
							lds.load();
						 }
						},
				failure: function(o){
						new mensaje('Error.',o.responseText,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
						}
			   };
			
			url = 'modulos/usuarios/insertar.asp?USU_LOGIN_RED=' + eds[i].data.USU_LOGIN_RED + 
												'&UP_PFL_ID=' + eds[i].data.UP_PFL_ID + 
												'&USU_AREA=' + eds[i].data.USU_AREA + 
												'&USU_RUT=' + eds[i].data.USU_RUT + 
												'&USU_DV=' + eds[i].data.USU_DV + 
												'&USU_NOMBRES=' + eds[i].data.USU_NOMBRES + 
												'&USU_APELLIDO_PATERNO=' + eds[i].data.USU_APELLIDO_PATERNO + 
												'&USU_APELLIDO_MATERNO=' + eds[i].data.USU_APELLIDO_MATERNO + 
												'&USU_EMPRESA=' + eds[i].data.USU_EMPRESA + 
												'&USU_EMAIL=' + eds[i].data.USU_EMAIL + 
												'&USU_TELEFONO=' + eds[i].data.USU_TELEFONO + 
												'&USU_PASSWORD=' + eds[i].data.USU_PASSWORD + 
												'&USU_CARGO=' + eds[i].data.USU_CARGO +
												'&USU_CELULAR=' + eds[i].data.USU_CELULAR + 
												'&USU_GENERO=' + eds[i].data.USU_GENERO
			YAHOO.util.Connect.asyncRequest('GET', url, callback);
		}
		
		
		
		//this.ds.load();
		
	},
	
	eliminarSeleccionados: function(){
		var lds = this.ds;
		// obtiene filas modificadas
		var eds = this.ds.getModifiedRecords(); 
		
		eliminaSeleccionadosSi = function(){
				for (i = 0; i < eds.length; i++){
					callback = {
						success: function(o){
								lds.load();
								},
						failure: function(o){
								new mensaje('Error.',o.responseText,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
								}
					   };
					if (eds[i].data.Elm){
						url = 'modulos/usuarios/eliminar.asp?USU_LOGIN_RED=' + eds[i].data.USU_LOGIN_RED;
						YAHOO.util.Connect.asyncRequest('GET', url, callback);
					}
				}
				
				this.ds.commitChanges();
				//this.ds.load();
				
				dialogValida.hide();
		};
		
		eliminaSeleccionadosNo = function(){
			dialogValida.hide();
		};

		
		if (eds.length > 0){
			//confima si quiere eliminar
		 	var dialogValida = new mensaje('Eliminación.',
									'¿ Está seguro que desea eliminar los elementos seleccionados ?',
									{ modal:true,
									  width:300,
									  height:120,
									  shadow:true,
									  closable:true });
			
			dialogValida.addButton('Sí', eliminaSeleccionadosSi.createDelegate(this), this);		
			dialogValida.addButton('No', eliminaSeleccionadosNo.createDelegate(this), this);						  
			dialogValida.show(this.tb.items.map.item2.id);		
			
		}

	},

	destruir : function(){
		this.destroy(true);
	}
	
} );


