SubirAdjunto = function(titulo, url, config,fnc){ 
	
	this.dh 	= Ext.DomHelper;
	this.config = config;

	// crea el elemento para el dialogo
	this.el = this.dh.append(document.body, {tag: 'div' }, true);
	YAHOO.util.Dom.generateId(this.el);

	// crea el elemento para el titulo del dialogo
	this.hd = this.dh.append(this.el.dom , {tag: 'div', cls : 'x-dlg-hd' }, true);
	Ext.get(this.hd.id).update(titulo);
	
	this.bd  	= this.dh.append(Ext.get(this.el.id).dom ,  {tag: 'div', cls : 'x-dlg-bd' }, true);
	this.td  	= this.dh.append(Ext.get(this.bd.id).dom ,  {tag: 'div', cls : 'x-dlg-tab', title : 'Seleccionar Archivo' }, true);
	this.intd  	= this.dh.append(Ext.get(this.td.id).dom ,  {tag: 'div', cls : 'inner-tab' }, true);
	
	SubirAdjunto.superclass.constructor.call(this, this.el.id, this.config);
	
	// actualiza el asistente con la primera pagina
	this.um = Ext.get(this.intd).getUpdateManager();
	this.um.disableCaching = true;
	this.um.showLoadIndicator = false;
	this.um.setRenderer({
				 	 render : function(el, o){
						 var contenido = o.responseText.htmlDecode();
						 el.update(contenido,true);
				     }
				    });
	this.um.update(url);
	
	this.btnAceptar   = this.addButton('Aceptar',  fnc , this);
	this.btnCancela   = this.addButton('Cancelar', this.hide, this);
		
	this.on('hide', this.destruir, this, true);
};



Ext.extend(SubirAdjunto, Ext.BasicDialog, {

	destruir : function(){
		this.destroy(true);
	}
} );


Adjunto = function(config)
{
	this.Botonera  = new Ext.Toolbar(config.menu.id); 
  	this.Botonera.addButton({id:'Nuevo-Adjunto-btn', text: 'Adjuntar', className: 'Nuevo-Adjunto', 
  							handler: this.mostrarDialogoSubirArchivo.createDelegate(this)});
  	this.Botonera.addSeparator();							
  	this.Botonera.addButton({id:'Quitar-Adjunto-btn', text: 'Eliminar', className: 'Quitar-Adjunto ', 
  							handler: this.quitarArchivoAdjunto.createDelegate(this)});
	this.config = config;
};

Adjunto.prototype = {

	init : function(AST_ID){
		
		this.astId = AST_ID;

		var fm = Ext.form, Ed = Ext.grid.GridEditor;

		this.cm = new Ext.grid.ColumnModel([{
			   header: "Id",
			   dataIndex: 'Id',
			   width: 20,
			   editor: null
			},{
			   header: "Nombre",
			   dataIndex: 'Nombre',
			   width: 150,
			   editor: null
			},{
			   header: "Descripcion",
			   dataIndex: 'Descripcion',
			   width: 300,
			   editor: null
			},{
			   header: "Tipo",
			   dataIndex: 'Tipo',
			   width: 95,
			   editor: null
			}]);
		
		this.cm.defaultSortable = true;
	
		var Plant = Ext.data.Record.create([
			   {name: 'Id', type: 'string'},
			   {name: 'Nombre', type: 'string'},
			   {name: 'Descripcion', type: 'string'},
			   {name: 'Tipo', type: 'string'}
		  ]);
	
		this.ds = new Ext.data.Store({
			proxy		: new Ext.data.HttpProxy({url: 'modulos/adjuntos/listar.asp'}),
			baseParams  : {AST_ID: this.astId},
			reader: new Ext.data.XmlReader({
				   record: 'Adjunto'
			   }, Plant)
		});
		
		this.grid = new Ext.grid.EditorGrid(this.config.grid.id, {
			ds: this.ds,
			cm: this.cm,
			enableColLock:false
		});
		
		this.grid.render();

		this.ds.load();

	},
	setNumItems: function(ob){
		this.numItems = ob.xml.getElementsByTagName(ob.schema.tagName).length;
		if(!this.dm.getValueAt(0, 0)){
			this.numItems--;
		}
	},
	mostrarDialogoSubirArchivo: function(){

		this.plantillaAdjunto = new SubirAdjunto('Subir Archivo Adjunto',
									  this.config.url.formulario + this.astId,
										{ 
										  autoTabs	: true,
										  width		: 470,
										  height	: 205,
										  shadow	: true,
										  closable	: true,
										  modal		: true,
										  minWidth	: 150,
										  minHeight	: 80
										},
										this.subirArchivoAdjunto.createDelegate(this)
						 );
		this.plantillaAdjunto.toFront();
		this.plantillaAdjunto.show('Nuevo-Adjunto-btn');
	},
	
	subirArchivoAdjunto: function(){
		var callback = { upload: this.uploadOk.createDelegate(this) , 
						 failure: function(o){
							 new mensaje('Error.',
								o.responseText,
								{ modal:true,
								  width:450,
								  height:250,
								  shadow:true,
								  closable:true }).show('Adjunto-btn');}
					   };
		YAHOO.util.Connect.setForm(this.config.frmName, true);
		YAHOO.util.Connect.asyncRequest('POST', this.config.url.guardar, callback );
		
		this.plantillaAdjunto.btnCancela.disable();
		this.plantillaAdjunto.btnAceptar.disable();
	},
	
	uploadOk		: function(o){
		this.ds.load();
		this.plantillaAdjunto.hide();
	},
	
	quitarArchivoAdjunto: function(){
		if (this.grid.selModel.hasSelection()){
			var row = this.grid.selModel.selection.record;
			var callback = { success: this.quitarOk.createDelegate(this) , 
						 	 failure: function(o){
								 new mensaje('Error.',
									o.responseText,
									{ 
										modal:true,
								  		width:450,
								  		height:300,
								  		shadow:true,
								  		closable:true 
									}).show('Nuevo-Participante-btn');
							 	}
							};
			
			url = this.config.url.eliminar + row.data.Id
			YAHOO.util.Connect.asyncRequest('GET', url, callback);					
		}
	},
	
	quitarOk		: function(o){
		this.ds.load();
	}
	
};

