MantenedorEmpresa = function(config){ 
	this.config = config;
};


Ext.extend(MantenedorEmpresa, Ext.BasicDialog, {
	
	init		: function(){
		
		function formatBoolean(value){
        	return value ? '<img src="images/delete.gif">' : ''; 
    	};
		
    	this.cm = new Ext.grid.ColumnModel([
				{
				   header: " ",
				   dataIndex: 'Elm',
				   width: 25,
				   renderer: formatBoolean,
			   	   editor: new Ext.grid.GridEditor(new Ext.form.Checkbox())
				},{
				   header: "Id",
				   dataIndex: 'ES_ID',
				   width: 5,
				   editor: null
				},{
				   header: "Rut",
				   dataIndex: 'ES_RUT',
				   width: 65,
				   editor: new Ext.grid.GridEditor(new Ext.form.NumberField({
					   allowBlank: false,
					   allowNegative: false
				   }))
				},{
				   header: "Dv",
				   dataIndex: 'ES_RUT_DV',
				   width: 15,
				   editor: null
				},{
				   header: "Nombre",
				   dataIndex: 'ES_NOMBRE',
				   width: 80,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxLength: 50
				   }))
				},{
				   header: "Dirección",
				   dataIndex: 'ES_DIRECCION',
				   width: 80,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxLength: 50
				   }))
				},{
				   header: "Region",
				   dataIndex: 'ES_REGION',
				   width: 40,
				   // renderer: renderRegion,
				   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'regiones',
						   lazyRender:true
						}))
				},{
				   header: "Ciudad",
				   dataIndex: 'ES_CIUDAD',
				   width: 80,
				   // renderer: renderCiudad,
				   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'ciudades', 
						   lazyRender:true
						}))
				},{
				   header: "Comuna",
				   dataIndex: 'ES_COMUNA',
				   width: 80,
				   // renderer: renderComuna,
				   editor: new Ext.grid.GridEditor(new Ext.form.ComboBox({
						   typeAhead: true,
						   triggerAction: 'all',
						   transform:'comunas', 
						   lazyRender:true
						}))
				},{
				   header: "Nombre Fantasia",
				   dataIndex: 'ES_NOMBRE_FANTASIA',
				   width: 100,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: true,
					   maxLength: 50
				   }))
				},{
				   header: "Razón Social",
				   dataIndex: 'ES__RAZON_SOCIAL',
				   width: 100,
				   renderer: '',
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxLength: 50
				   }))
				},{
				   header: "Giro",
				   dataIndex: 'ES__GIRO',
				   width: 150,
					   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: false,
					   maxLength: 50
				   }))
				},{
				   header: "Fono",
				   dataIndex: 'ES_FONO',
				   width: 60,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: true,
					   maxLength: 20
				   }))
				},{
				   header: "Email",
				   dataIndex: 'ES__EMAIL',
				   width: 130,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: true,
					   maxLength: 50
				   }))
				},{
				   header: "Web",
				   dataIndex: 'ES_WEB',
				   width: 100,
				   align: 'right',
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: true,
					   maxLength: 50
				   }))
				},{
				   header: "Casilla",
				   dataIndex: 'ES__CASILLA',
				   width: 60,
				   editor: new Ext.grid.GridEditor(new Ext.form.TextField({
					   allowBlank: true,
					   maxLength: 50
				   }))
				}
		]);
	
		this.cm.defaultSortable = true;
	
		this.Items = Ext.data.Record.create([
							{name: 'Elm', 				type: 'string'},
							{name: 'ES_ID', 			type: 'string'},
							{name: 'ES_RUT', 			type: 'string'},
							{name: 'ES_RUT_DV', 		type: 'string'},							
							{name: 'ES_NOMBRE', 		type: 'string'},
							{name: 'ES_DIRECCION', 		type: 'string'},
							{name: 'ES_REGION', 		type: 'string'},
							{name: 'ES_CIUDAD', 		type: 'string'},
							{name: 'ES_COMUNA', 		type: 'string'},
							{name: 'ES_NOMBRE_FANTASIA',type: 'string'},
							{name: 'ES__RAZON_SOCIAL', 	type: 'string'},
							{name: 'ES__GIRO',			type: 'string'},
							{name: 'ES_FONO', 			type: 'string'},
							{name: 'ES__EMAIL', 		type: 'string'},
							{name: 'ES_WEB', 			type: 'string'},
							{name: 'ES__CASILLA', 		type: 'string'}
						]);
	
		// crea el Data Store
		this.ds = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({url: 'modulos/empresa/listar.asp'}),
			/*baseParams  : {TIPO_ID: this.config.tipoId},*/
			reader: new Ext.data.XmlReader({
				   record: 'Empresa'
			   }, 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 : '',
							ES_ID : '',
							ES_RUT : '',
							ES_RUT_DV : '',
							ES_NOMBRE : '',
							ES_DIRECCION : '',
							ES_REGION : '',
							ES_CIUDAD : '',
							ES_COMUNA : '',
							ES_NOMBRE_FANTASIA : '',
							ES__RAZON_SOCIAL : '',
							ES__GIRO : '',
							ES_FONO : '',
							ES__EMAIL : '',
							ES_WEB : '',
							ES__CASILLA : ''});
		this.grid.stopEditing();
		this.ds.insert(0, p);
		this.grid.startEditing(0, 0);
	},
	
	guardarCambios : function(){
		var url, callback, lds;
		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/empresa/insertar.asp?ES_ID=' + eds[i].data.ES_ID +  
										'&ES_RUT=' + eds[i].data.ES_RUT +  
										'&ES_NOMBRE=' + eds[i].data.ES_NOMBRE +  
										'&ES_DIRECCION=' + eds[i].data.ES_DIRECCION +  
										'&ES_REGION=' + eds[i].data.ES_REGION +  
										'&ES_CIUDAD=' + eds[i].data.ES_CIUDAD +  
										'&ES_COMUNA=' + eds[i].data.ES_COMUNA +  
										'&ES_NOMBRE_FANTASIA=' + eds[i].data.ES_NOMBRE_FANTASIA +  
										'&ES__RAZON_SOCIAL=' + eds[i].data.ES__RAZON_SOCIAL +  
										'&ES__GIRO=' + eds[i].data.ES__GIRO +  
										'&ES_FONO=' + eds[i].data.ES_FONO +  
										'&ES__EMAIL=' + eds[i].data.ES__EMAIL + 
										'&ES_WEB=' + eds[i].data.ES_WEB +  
										'&ES__CASILLA=' + eds[i].data.ES__CASILLA
			YAHOO.util.Connect.asyncRequest('GET', url, callback);
		}

		// this.ds.load();
		
	},
	
	eliminarSeleccionados: function(){
		
		var url, callback, lds;
		lds = this.ds;
		// obtiene filas modificadas
		var eds = this.ds.getModifiedRecords(); 
		
		eliminaSeleccionadosSi = function(){
				for (i = 0; i < eds.length; i++){
					callback = {
						success: function(o){
									if (o.responseText == '') {
										lds.commitChanges();
										lds.load();
									}else{
										new mensaje('Error.',o.responseText,{modal:true, width:380, height:100, shadow:true, closable:true}).show(this.id);
									}
								},
						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/empresa/eliminar.asp?ES_ID=' + eds[i].data.ES_ID;
						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);
	}
	
} );


