
MantenedorLV = function(config){ 

	this.dh 	= Ext.DomHelper;
	// captura la configuracion para los metodos 
	this.config = config;

	// crea el elemento para el dialogo
	el = this.dh.append(document.body, {tag: 'div' }, true);
	YAHOO.util.Dom.generateId(el);
	
	// crea los elementos para el dialogo
	this.hd = this.dh.append(el.id , {tag: 'div', cls : 'x-dlg-hd' }, true);
	YAHOO.util.Dom.generateId(this.hd);
	this.hd.update(this.config.titulo);

	this.bd  	= this.dh.append(el.id 	   		,{tag: 'div', cls : 'x-dlg-bd'  }, true);
	this.td  	= this.dh.append(this.bd.id		,{tag: 'div', cls : 'x-dlg-tab'  , title : this.config.titulo }, true);
	this.intd  	= this.dh.append(this.td.id		,{tag: 'div', cls : 'inner-tab'}, true);
	//this.png  	= this.dh.append(this.intd.id	,{tag: 'div'}, true);								// crea el panel para el grid
	this.gd  	= this.dh.append(this.intd.id	,{tag: 'div', style: 'width:370px'}, true);				// crea el elemento para el grid
	
	// invoca al constructor BasicDialog
	MantenedorLV.superclass.constructor.call(this, el.id, this.config.dialog);

	// agrega botones aceptar cancelar 
	// this.btnAceptar   = this.addButton('Aceptar' , this.guardarCambios , this); // this.aceptar
	this.btnCancela   = this.addButton('Cerrar', this.hide, this);
	
	this.on('hide', this.destruir, this, true);
	this.addKeyListener(27, this.hide, this);
};


Ext.extend(MantenedorLV, Ext.BasicDialog, {
	
	init		: function(){

		function formatBoolean(value){
        	return value ? '<img src="images/delete.gif">' : ''; 
    	};
	
		this.cm = new Ext.grid.ColumnModel([{
			   header: " ",
			   dataIndex: 'Elm',
			   width: 30,
			   renderer: formatBoolean,
			   editor: new Ext.grid.GridEditor(new Ext.form.Checkbox())
       		},{
			   header: "Id",
			   dataIndex: 'Id',
			   width: 35,
			   editor: null
//			   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
//				   allowBlank: false
//			   }))
			},{
			   header: "Descripción",
			   dataIndex: 'Descripcion',
			   width: 300,
			   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
				   allowBlank: false
			   }))
			}]);
	
		this.cm.defaultSortable = true;
	
		this.Items = Ext.data.Record.create([{name: 'Elm', type: 'string'},{name: 'Id', type: 'string'}, {name: 'Descripcion', type: 'string'} ]);
	
		// crea el Data Store
		this.ds = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({url: 'modulos/listasValores/listar.asp'}),
			baseParams  : {TIPO_ID: this.config.tipoId, PADRE_ID: this.config.padreId},
			reader: new Ext.data.XmlReader({
				   record: 'Item'
			   }, this.Items)
		});
	
		// crea el editor grid
		this.grid = new Ext.grid.EditorGrid(this.gd.id, {
			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.png.id);*/

		this.grid.render();
	
		
        var gridHead = this.grid.getView().getHeaderPanel(true);
		var tb = new Ext.Toolbar(gridHead, [{
					text: 'Guardar',
					handler : this.guardarCambios.createDelegate(this)
				},{
					text: 'Nuevo',
					handler : this.nuevoRegistro.createDelegate(this)
				}]);
	
		this.ds.load();	
		
		if (this.config.listaSelect){
			this.show(this.config.listaSelect.id);
		}else{
			this.show();
		}
		
	},
	
	nuevoRegistro  : function(){
		w = this
		var p = new this.Items({Id : '??',	Descripcion : ''});
		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('Error.', cadErrores ,{modal:true, width:380, height:80, 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);
						}
			   };
			
			if (eds[i].data.Elm){
				url = 'modulos/listasValores/eliminar.asp?LV_ID=' + eds[i].data.Id
			}else{
				url = 'modulos/listasValores/insertar.asp?LV_ID=' + eds[i].data.Id + '&LV_DESC=' + eds[i].data.Descripcion 
													+ '&LV_TIPO_ID=' + this.config.tipoId + '&LV_PADRE=' + this.config.padreId
			}
			
			YAHOO.util.Connect.asyncRequest('GET', url, callback);
		}
		
//		this.ds.commitChanges();
//		
//		this.ds.load();
		
	},

	destruir : function(){
		var listaSelect = this.config.listaSelect;
		
		if (this.config.listaSelect){
			callback = { 
					success: function(o){
							loadList(listaSelect, o.responseText,';','=');
							},
					failure: function(o){
							new mensaje('Error.',o.responseText,{modal:true, width:380, height:400, shadow:true, closable:true}).show(this.id);
							}
				   };
			url = 'modulos/listasValores/cargaLV.asp?LV_TIPO_ID=' + this.config.tipoId + '&LV_PADRE=' + this.config.padreId;
			YAHOO.util.Connect.asyncRequest('GET', url, callback);
		}
		
		this.destroy(true);
	}
	
} );


