usuarios = function(parametros){
	
	Ext.apply(this, parametros);
	
	var el = Ext.DomHelper.append(document.body, {tag: "div", id: "usuarios"}, true);
	
	var store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: "modulos/usuario/listar.php"
		}),
		reader: new Ext.data.JsonReader({  
			root: "resultado", 
			successProperty: "exito",
			totalProperty: "total", 
			id: "USU_ID" 
		}, [
		   {name: "USU_ID", type: "int"},
		   {name: "USU_RUT", type: "int"},
		   {name: "USU_NOMBRES", type: "string"},
		   {name: "USU_APELLIDOS", type: "string"},
		   {name: "USU_EDAD", type: "int"},
		   {name: "PER_NOMBRE", type: "string"},
		   {name: "IGL_NOMBRE", type: "string"},
		   {name: "USU_CIUDAD", type: "string"},
		   {name: "USU_EMAIL", type: "string"},
		   {name: "USU_PAIS", type: "string"},
		   {name: "USU_CUMPLE", type: "string"},
		   {name: "USU_FONOFIJO", type: "string"},
		   {name: "USU_CELULAR", type: "string"},
		   {name: "ESC_NOMBRE", type: "string"},
		   {name: "USU_ACTIVIDAD", type: "string"},
		   {name: "USU_QUEACTIVIDAD", type: "string"},
		   {name: "USU_TALENTO", type: "string"},
		   {name: "USU_INFO", type: "string"},
		   {name: "USU_MASINFO", type: "string"}
		])
	});
	
	var cm = new Ext.grid.ColumnModel([
		{header: "Rut", width: 80, dataIndex: "USU_RUT"},
		{header: "Nombres", width: 120, dataIndex: "USU_NOMBRES"},
		{header: "Apellidos", width: 120, dataIndex: "USU_APELLIDOS"},
		{header: "Edad", width: 50, dataIndex: "USU_EDAD"},
		{header: "País", width: 100, dataIndex: "USU_PAIS"},
		{header: "Perfil", width: 120, dataIndex: "PER_NOMBRE"},
		{header: "Iglesia", width: 100, dataIndex: "IGL_NOMBRE"},
		{header: "Ciudad", width: 120, dataIndex: "USU_CIUDAD"},
		{header: "E-mail", width: 120, dataIndex: "USU_EMAIL"},
		{header: "Cumpleaños", width: 100, dataIndex: "USU_CUMPLE"},
		{header: "Teléfono fijo", width: 100, dataIndex: "USU_FONOFIJO"},
		{header: "Celular", width: 100, dataIndex: "USU_CELULAR"},
		{header: "Actividad", width: 120, dataIndex: "USU_ACTIVIDAD"},
		{header: "Detalle actividad", width: 120, dataIndex: "USU_QUEACTIVIDAD"},
		{header: "Talento", width: 120, dataIndex: "USU_TALENTO"},
		{header: "Colegio", width: 100, dataIndex: "ESC_NOMBRE"}
	]);
	
	var selModel = new Ext.grid.RowSelectionModel({
		singleSelect: true
	});
	
	var grid = new Ext.grid.Grid(el, {
		ds: store,
		cm: cm,
		selModel: selModel
	});
	
	grid.render();
	
	var elMenu = grid.getView().getHeaderPanel(true);
	
	var menu = new botonera({
		el: elMenu,
		ambito: this,
		parametros: {
			APL_PADRE_ID: this.APL_ID
		}
	});
	
	menu.on("crear", this.refrescar, this);
	
	var elPaginador = grid.getView().getFooterPanel(true);
	
	var paginador = new Ext.PagingToolbar(elPaginador, store, {
        pageSize: this.limite,
        displayInfo: true,
        displayMsg: "Displaying topics {0} - {1} of {2}",
        emptyMsg: "No topics to display"
    });
	
	usuarios.superclass.constructor.call(this, grid, {
		title: "Usuarios",
		closable: true
	});
	
	var created = false;
	
	this.on("activate",
		function(){
			if(created == false){
				this.store.load({
					params: {
						start: 0, 
						limit: this.limite
					}
				});
			}
		},
	this);
	
	this.on("deactivate",
		function(){
			created = true;
		},
	this);
	
	this.on("close",
		function(){
			created = true;
		},
	this);
	
	var loadMask = new Ext.LoadMask(document.body, {
		store: store,
		msg: "Cargando..."
	});
	
	this.grid = grid;
	this.store = store;
	this.selModel = selModel;
	this.cnn = new Ext.data.Connection();
	
};

Ext.extend(usuarios, Ext.GridPanel, {

	limite: 25,
	ventanas: [],
	
	listar: function(){
	
		this.store.load({
			params: {
				start: 0, 
				limit: this.limite
			}
		});
		
	},
	
	refrescar: function(){
		
		this.grid.autoSize();
		
	},
	
	agregar: function(boton){
	
		if(!this.ventanas["agregar"]){
			
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 280,
				height: 310,
				shadow: true,
				collapsible: false,
				autoTabs: true,
				resizable: false,
				title: "Agregar",
				center: {
					autoCreate: true
				}
			});
			
			var layout = ventana.getLayout();
			
			var panelCentro = layout.add("center", new Ext.ContentPanel({
				autoCreate: true										  
			}));
			
			var formulario = new Ext.form.Form({
				style: "padding:10px",
				labelWidth: 75,
				waitTitle: "Espere por favor",
				method: "GET",
				errorReader: new Ext.data.JsonReader({
					root: "resultado", 
					successProperty: "exito"
				}, ["id", "msg"])
			});
			
			formulario.fieldset({legend:"Datos del Usuario"}, 
				new Ext.form.TextField({
					fieldLabel: "Rut",
					name: "USU_RUT",
					selectOnFocus: true,
					width: 125
				}),
				new Ext.form.TextField({
					fieldLabel: "Nombres",
					name: "USU_NOMBRES",
					selectOnFocus: true,
					width: 125
				}),
				new Ext.form.TextField({
					fieldLabel: "Apellidos",
					name: "USU_APELLIDOS",
					selectOnFocus: true,
					width: 125
				}),	
				new Ext.form.TextField({
					fieldLabel: "Contraseña",
					name: "USU_CONTRASENA",
					selectOnFocus: true,
					inputType: "password",
					width: 125
				}),	
				new Ext.form.ComboBox({
					fieldLabel: "Perfil",
					hiddenName: "USU_PER_ID",
					editable : false,
					store: new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({
							url: "modulos/perfil/listar.php"
						}),
						reader: new Ext.data.JsonReader({  
							root: "resultado", 
							successProperty: "exito",
							id: "PER_ID" 
						}, ["PER_ID", "PER_NOMBRE"])
					}),
					valueField: "PER_ID",
					displayField: "PER_NOMBRE",
					mode: "remote",
					triggerAction: "all",
					selectOnFocus: true,
					width: 125
				}),
				new Ext.form.ComboBox({
					fieldLabel: "Iglesia",
					hiddenName: "USU_IGL_ID",
					editable : false,
					store: new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({
							url: "modulos/iglesia/listar.php"
						}),
						reader: new Ext.data.JsonReader({  
							root: "resultado", 
							successProperty: "exito",
							id: "IGL_ID" 
						}, ["IGL_ID", "IGL_NOMBRE"])
					}),
					valueField: "IGL_ID",
					displayField: "IGL_NOMBRE",
					mode: "remote",
					triggerAction: "all",
					selectOnFocus: true,
					width: 125
				}),
				new Ext.form.ComboBox({
					fieldLabel: "Escuela",
					hiddenName: "USU_ESC_ID",
					editable : false,
					//allowBlank: false,
					//value: "SIN ESCUELA",
					emptyText: "Seleccionar...",
					store: new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({
							url: "modulos/escuela/listar.php"
						}),
						reader: new Ext.data.JsonReader({  
							root: "resultado", 
							successProperty: "exito",
							id: "ESC_ID" 
						}, ["ESC_ID", "ESC_NOMBRE"])
					}),
					valueField: "ESC_ID",
					displayField: "ESC_NOMBRE",
					mode: "remote",
					triggerAction: "all",
					selectOnFocus: true,
					width: 125
				})
			);
			
		    formulario.render(panelCentro.getEl());
			
			formulario.on("actioncomplete", function(form, action){
	
				if(action.type == "submit"){
					
					ventana.hide();
					this.listar();
					
				};
					
			}, this);
			
			ventana.addButton("Cerrar", ventana.hide, ventana);
			
			ventana.addButton("Aceptar", function(){
			
				formulario.submit({
					url: "modulos/usuario/insertar.php",
					waitMsg: "Grabando..."
				});
													 
			});
			
			ventana.on("show", formulario.reset, formulario);
			
			this.ventanas["agregar"] = ventana;
			
		};
		
		this.ventanas["agregar"].show(boton.id);
		
	},
	
	editar: function(boton){
		
		var registro = this.selModel.getSelected();
		
		if(registro){
	
			if(!this.ventanas["editar"]){
				
				var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
				
				var ventana = new Ext.LayoutDialog(el, {
					modal: true,
					width: 656,
					height: 504,
					shadow: true,
					collapsible: false,
					autoTabs: true,
					resizable: false,
					title: "Editar",
					center: { autoCreate: true, autoScroll:true }
				});
				
				var layout = ventana.getLayout();
				
				var panelCentro = layout.add("center", new Ext.ContentPanel({
					autoCreate: true										  
				}));
				
				var formulario = new Ext.form.Form({
					style: "padding:10px",
					labelWidth: "auto",
					labelAlign: "top",
					waitTitle: "Espere por favor",
					method: "GET",
					reader: new Ext.data.JsonReader({
						root: "resultado", 
						successProperty: "exito"
					}, [
						{name: "USU_RUT", type: "int"},
						{name: "USU_NOMBRES", type: "string"},
						{name: "USU_APELLIDOS", type: "string"},
						{name: "USU_CONTRASENA", type: "string"},
					    {name: "USU_EDAD", type: "int"},
					    {name: "PER_NOMBRE", type: "string"},
					    {name: "IGL_NOMBRE", type: "string"},
					    {name: "USU_CIUDAD", type: "string"},
					    {name: "USU_EMAIL", type: "string"},
					    {name: "USU_PAIS", type: "string"},
					    {name: "USU_CUMPLE", type: "string"},
					    {name: "USU_FONOFIJO", type: "string"},
					    {name: "USU_CELULAR", type: "string"},
					    {name: "ESC_NOMBRE", type: "string"},
					    {name: "USU_ACTIVIDAD", type: "string"},
					    {name: "USU_QUEACTIVIDAD", type: "string"},
					    {name: "USU_TALENTO", type: "string"},
					    {name: "USU_INFO", type: "string"},
					    {name: "USU_MASINFO", type: "string"},
						{name: "USU_PER_ID", type: "int"},
						{name: "USU_IGL_ID", type: "int"},
						{name: "USU_ESC_ID", type: "int"}
					]),
					errorReader: new Ext.data.JsonReader({
						root: "resultado", 
						successProperty: "exito"
					}, ["id", "msg"])
				});
				
			formulario.fieldset({legend:"Datos del Usuario"});
				formulario.column({width: "auto"},
					new Ext.form.NumberField({
						fieldLabel: "Rut/RG (ej: 113334442 ; k=0)",
						name: "USU_RUT",
						allowBlank: false,
						allowDecimals: false,
						allowNegative: false,
						selectOnFocus: true,
						minLength: 8,
						maxLength: 9,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Nombres",
						name: "USU_NOMBRES",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Apellidos",
						name: "USU_APELLIDOS",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Contraseña",
						name: "USU_CONTRASENA",
						selectOnFocus: true,
						inputType: "password",
						minLength: 4,
						maxLength: 10,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "E-mail",
						name: "USU_EMAIL",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "País",
						name: "USU_PAIS",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Ciudad",
						name: "USU_CIUDAD",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Teléfono",
						name: "USU_FONOFIJO",
						selectOnFocus: true,
						width: 180
					})
				);
				formulario.column({style:"margin-left: 10px;", width: "auto"},
					new Ext.form.TextField({
						fieldLabel: "Celular",
						name: "USU_CELULAR",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.ComboBox({
						fieldLabel: "Iglesia",
						hiddenName: "USU_IGL_ID",
						editable : false,
						store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy({
								url: "modulos/iglesia/listar.php"
							}),
							reader: new Ext.data.JsonReader({  
								root: "resultado", 
								successProperty: "exito",
								id: "IGL_ID" 
							}, ["IGL_ID", "IGL_NOMBRE"])
						}),
						valueField: "IGL_ID",
						displayField: "IGL_NOMBRE",
						mode: "remote",
						triggerAction: "all",
						selectOnFocus: true,
						//value: "SIN IGLESIA",
						allowBlank: false,
						width: 180
					}),
					new Ext.form.ComboBox({
						fieldLabel: "Colegio",
						hiddenName: "USU_ESC_ID",
						editable : false,
						store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy({
								url: "modulos/escuela/listar.php"
							}),
							reader: new Ext.data.JsonReader({  
								root: "resultado", 
								successProperty: "exito",
								id: "ESC_ID" 
							}, ["ESC_ID", "ESC_NOMBRE"])
						}),
						valueField: "ESC_ID",
						displayField: "ESC_NOMBRE",
						mode: "remote",
						triggerAction: "all",
						selectOnFocus: true,
						//value: "SIN ESCUELA",
						allowBlank: false,
						width: 180
					}),
					new Ext.form.NumberField({
						fieldLabel: "Edad",
						name: "USU_EDAD",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.DateField({
						fieldLabel: "Cumpleaños",
						name: "USU_CUMPLE",
						selectOnFocus: true,
						format: "d-m-Y",
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Actividad",
						name: "USU_ACTIVIDAD",
						emptyText: "Estudias, trabajas...",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Detalle de actividad",
						name: "USU_QUEACTIVIDAD",
						emptyText: "Carrera, profesión...",
						selectOnFocus: true,
						width: 180
					}),
					new Ext.form.TextField({
						fieldLabel: "Talento especial",
						name: "USU_TALENTO",
						emptyText: "Cantas, bailas, te ríes...",
						selectOnFocus: true,
						width: 180
					})
				);
				formulario.column({style:"margin-left: 10px;", width: "auto"},
					new Ext.form.ComboBox({
						fieldLabel: "Perfil",
						hiddenName: "USU_PER_ID",
						editable : false,
						store: new Ext.data.Store({
							proxy: new Ext.data.HttpProxy({
								url: "modulos/perfil/listar.php"
							}),
							reader: new Ext.data.JsonReader({  
								root: "resultado", 
								successProperty: "exito",
								id: "PER_ID" 
							}, ["PER_ID", "PER_NOMBRE"])
						}),
						valueField: "PER_ID",
						displayField: "PER_NOMBRE",
						mode: "remote",
						triggerAction: "all",
						selectOnFocus: true,
						width: 180
					})
				);
			formulario.end();
				
				formulario.render(panelCentro.getEl());
				
				formulario.on("actioncomplete", function(form, action){
		
					if(action.type == "submit"){
						
						ventana.hide();
						this.listar();
						
					};
						
				}, this);
				
				ventana.addButton("Cerrar", ventana.hide, ventana);
				
				ventana.addButton("Aceptar", function(){
				
					formulario.submit({
						url: "modulos/usuario/editar.php",
						waitMsg: "Grabando..."
					});
														 
				});
				
				ventana.on("show", function(){
				
					formulario.load({
						url: "modulos/usuario/listar.php",
						waitMsg: "Cargando..."
					});
															   
				});
				
				ventana.formulario = formulario;
				this.ventanas["editar"] = ventana;
				
			};
			
			this.ventanas["editar"].formulario.baseParams = {
				USU_ID: registro.get("USU_ID")
			};
		
			this.ventanas["editar"].show(boton.id);
			
		};
		
	},
	
	eliminar: function(boton){
	
		var registro = this.selModel.getSelected();
		
		if(registro){
			
			Ext.MessageBox.wait("Eliminando...", "Espere por favor");
			
			this.cnn.request({
				url: "modulos/usuario/eliminar.php",
				params: {
					USU_ID: registro.id	
				},
				success: function(response, options){
					
					Ext.MessageBox.updateProgress(1);
					Ext.MessageBox.hide();
					this.store.reload();
					
				},
				failure: function(response, options){
					
					Ext.MessageBox.updateProgress(1);
					Ext.MessageBox.hide();
					Ext.Msg.alert("Error", response.responseText);
					
				},
				scope: this
			});
			
		};
		
	},
	
	iglesias: function(boton){
		
		if(!this.ventanas["iglesias"]){
			
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 500,
				height: 400,
				shadow: true,
				resizable: false,
				collapsible: false,
				title: "Iglesias",
				center: {
					autoCreate: true
				}
			});
			
			var layout = ventana.getLayout();
			
			var panel = layout.add("center", new iglesias({
				APL_ID: boton.APP_ID
			}));
			
			ventana.addButton("Cerrar", ventana.hide, ventana);
			
			ventana.on("show", panel.listar, panel);
			
			this.ventanas["iglesias"] = ventana;
			
		};
		
		this.ventanas["iglesias"].show(boton.id);
		
	},
	
	escuelas: function(boton){
		
		if(!this.ventanas["escuelas"]){
			
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 500,
				height: 400,
				shadow: true,
				resizable: false,
				collapsible: false,
				title: "Escuelas",
				center: {
					autoCreate: true
				}
			});
			
			var layout = ventana.getLayout();
			
			var panel = layout.add("center", new escuelas({
				APL_ID: boton.APP_ID
			}));
			
			ventana.addButton("Cerrar", ventana.hide, ventana);
			
			ventana.on("show", panel.listar, panel);
			
			this.ventanas["escuelas"] = ventana;
			
		};
		
		this.ventanas["escuelas"].show(boton.id);
		
	},
	
	perfiles: function(boton){
		
		if(!this.ventanas["perfiles"]){
			
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 500,
				height: 400,
				shadow: true,
				resizable: false,
				collapsible: false,
				title: "Perfiles",
				center: {
					autoCreate: true
				}
			});
			
			var layout = ventana.getLayout();
			
			var panel = layout.add("center", new perfiles({
				APL_ID: boton.APP_ID
			}));
			
			ventana.addButton("Cerrar", ventana.hide, ventana);
			
			ventana.on("show", panel.listar, panel);
			
			this.ventanas["perfiles"] = ventana;
			
		};
		
		this.ventanas["perfiles"].show(boton.id);
		
	}

});
