/**
 * Función para ordenar una tabla de listado por un campo determinado
 */
function ordenarTabla(campo,orden,destino)
{
  	document.adminForm.campo.value=campo;
  	document.adminForm.orden.value=orden;
  	document.adminForm.action=destino;
  	document.adminForm.submit();
}


/**
 * Función para ordenar una tabla de listado por un campo determinado
 */
function moverEnTabla(campo,orden,destino,updown,elemento)
{

		function recargar_listado(response,responseXML)
	    {
	    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
				if (codigo < 0)
	    	{
					var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
					alert(mensaje);	    		
	    	}	
	    	else
	    	{
  				document.adminForm.campo.value=campo;
  				document.adminForm.orden.value=orden;
  				document.adminForm.action=destino;
  				document.adminForm.submit();
	    	}    	
	    }
	
		var destinoajax='op_evento.php?op=even_ordenar&cod_evento='+elemento+'&updown='+updown;
		new Ajax(destinoajax, {	method: 'get',onComplete:recargar_listado }).request();

}

/**
 * Función para enviar a la pagina de edición de un objeto
 * si se ha seleccionado alguno
 */
function editarTabla(destino)
{
	if (document.adminForm.id.value != "")
	{
		document.adminForm.action=destino;
		document.adminForm.submit();	
	}
	else
	{
		alert ('Debe seleccionar un elemento antes de pulsar este botón');
	}
}



/**
 * Función para enviar a la pagina de edición de un objeto
 * si se ha seleccionado alguno
 */
function editarContenido(destino)
{
	if (document.listForm.id.value != "")
	{
		document.listForm.action=destino;
		document.listForm.submit();	
	}
	else
	{
		alert ('Debe seleccionar un elemento antes de pulsar este botón');
	}
}



/**
 * Función para guardar la lista de elementos seleccionados en el campo oculto del formulario
 */
function chequearTabla(elemento)
{
	var contenido='';
	for(i=0;i<$('num_filas').getValue();i++)
	{
		if ($('cb'+i).checked)
			contenido=contenido+$('cb'+i).getValue()+',';
	}
	if (contenido!='')
		contenido = contenido.substring(0,contenido.length-1);
	document.adminForm.id.value = contenido;
}


/**
 * Función para guardar la lista de elementos seleccionados en el campo oculto del formulario
 */
function chequearContenido(elemento)
{
	var contenido='';
	for(i=0;i<$('num_filas').getValue();i++)
	{
		if ($('cb'+i).checked)
			contenido=contenido+$('cb'+i).getValue()+',';
	}
	if (contenido!='')
		contenido = contenido.substring(0,contenido.length-1);
	document.listForm.id.value = contenido;
}


function chequearContenidoDocu(elemento)
{
	var contenido='';
	for(i=0;i<$('num_filas').getValue();i++)
	{
		if ($('cb'+i).checked)
			contenido=$('cb'+i).getValue();
	}
	document.listForm.id.value = contenido;
}


/**
 * Función para borrar un elemento de la tabla de listado
 */
function borrarTabla(destino,vuelta)
{
	if (document.adminForm.id.value != "")
	{
	    function respuestaB(response,responseXML)
    	{
    		var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
    		var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			if (codigo > 0)
    			location.href="gestor.php?op="+vuelta+"_listar&e="+codigo;
    		else
    			var tipo='error';
    		$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="'+tipo+' message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
    	}

		new Ajax(destino, {postBody: $('adminForm'), onComplete:respuestaB}).request();
               
	}
	else
	{
		alert ('Debe seleccionar un elemento antes de pulsar este botón');
	}
}


/**
 * Función para borrar un elemento de la tabla de listado
 */
function borrarContenido(destino,vuelta)
{
	if (document.listForm.id.value != "")
	{
	    function respuestaB(response,responseXML)
    	{
    		var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
    		var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			if (codigo > 0)
			{
				document.listForm.action="gestor.php?op="+vuelta+"_listar&e="+codigo;
    			document.listForm.submit();
			}
    		else
    			var tipo='error';
    		$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="'+tipo+' message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
    	}

		new Ajax(destino, {postBody: $('listForm'), onComplete:respuestaB}).request();
               
	}
	else
	{
		alert ('Debe seleccionar un elemento antes de pulsar este botón');
	}
}



/**
 * Funcion para añadir filas a tablas
 */
function anhadirTabla(elemento,campos,capa)
{
		document.getElementById('num_'+elemento).value= (parseInt($('num_'+elemento).getValue())+1);

		var row = document.createElement("tr");
		var atributo = document.createAttribute("class");
		if (parseInt($('num_'+elemento).getValue()) % 2 == 0)
			atributo.nodeValue = "odd";
		else
			atributo.nodeValue = "even";
		row.setAttributeNode(atributo);
		var atributo = document.createAttribute("id");
		atributo.nodeValue = 'row_'+elemento+'_'+parseInt($('num_'+elemento).getValue());
		row.setAttributeNode(atributo);		

		
		var cell = document.createElement("td");
		var cellText = document.createTextNode($('num_'+elemento).getValue());
		cell.appendChild(cellText);
		row.appendChild(cell);

		for(i=0;i<campos.length;i++)
		{
			var cell = document.createElement("td");
			var cellText = document.createTextNode($(campos[i]).getValue());
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = campos[i]+'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("id");
			atributo.nodeValue = campos[i]+'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(campos[i]).getValue();
			$(campos[i]).value='';
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
			cell.appendChild(cellText);
			row.appendChild(cell);
		}

		var cell = document.createElement("td");
		var boton = document.createElement("input");
		var atributo = document.createAttribute("type");
		atributo.nodeValue = "button";
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("name");
		atributo.nodeValue = "btn_eliminar_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("id");
		atributo.nodeValue = "btn_eliminar_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("value");
		atributo.nodeValue = "<< Borrar";
		boton.setAttributeNode(atributo);
		var valor = parseInt($('num_'+elemento).getValue());
		boton.onclick=function(){borrarFila($('tbody_'+elemento),'row_'+elemento+'_'+valor,valor);acordeon.display(-1);acordeon.display(capa);};
		cell.appendChild(boton);
		row.appendChild(cell);


		$('tbody_'+elemento).appendChild(row);

//alert($('adminForm').toQueryString());
}


function anhadirTablaRepresentantes(elemento,capa)
{
		// Aumenta en una Unidad el 
		document.getElementById('num_'+elemento).value= (parseInt($('num_'+elemento).getValue())+1);

		// DETERMINACION DE LA CLASE DE LAS FILAS
		var row1 = document.createElement("tr");
		var atributo = document.createAttribute("class");
		if (parseInt($('num_'+elemento).getValue()) % 2 == 0)
			atributo.nodeValue = "odd";
		else
			atributo.nodeValue = "even";
		row1.setAttributeNode(atributo);
		var row2 = document.createElement("tr");
		var atributo = document.createAttribute("class");
		if (parseInt($('num_'+elemento).getValue()) % 2 == 0)
			atributo.nodeValue = "odd";
		else
			atributo.nodeValue = "even";
		row2.setAttributeNode(atributo);
		var row3 = document.createElement("tr");
		var atributo = document.createAttribute("class");
		if (parseInt($('num_'+elemento).getValue()) % 2 == 0)
			atributo.nodeValue = "odd";
		else
			atributo.nodeValue = "even";
		row3.setAttributeNode(atributo);
		var row4 = document.createElement("tr");
		var atributo = document.createAttribute("class");
		if (parseInt($('num_'+elemento).getValue()) % 2 == 0)
			atributo.nodeValue = "odd";
		else
			atributo.nodeValue = "even";
		row4.setAttributeNode(atributo);

		//DETERMINACION DE LOS IDENTIFICADORES DE LAS FILAS
		var atributo = document.createAttribute("id");
		atributo.nodeValue = 'row1_'+elemento+'_'+parseInt($('num_'+elemento).getValue());
		row1.setAttributeNode(atributo);		
		var atributo = document.createAttribute("id");
		atributo.nodeValue = 'row2_'+elemento+'_'+parseInt($('num_'+elemento).getValue());
		row2.setAttributeNode(atributo);		
		var atributo = document.createAttribute("id");
		atributo.nodeValue = 'row3_'+elemento+'_'+parseInt($('num_'+elemento).getValue());
		row3.setAttributeNode(atributo);	
		var atributo = document.createAttribute("id");
		atributo.nodeValue = 'row4_'+elemento+'_'+parseInt($('num_'+elemento).getValue());
		row4.setAttributeNode(atributo);	


		// Para la fila superior
		var cell = document.createElement("td");
		var cellText = document.createTextNode($('num_'+elemento).getValue());
		cell.appendChild(cellText);
		row1.appendChild(cell);
		
		// NIF
		var cell = document.createElement("td");
		var cellText = document.createTextNode('N.I.F.: '+ $('rep_nif').getValue());
		cell.appendChild(cellText);
		row1.appendChild(cell);

		// NOMBRE, APELLIDO1 Y APELLIDO2
		var cell = document.createElement("td");
		var atributo = document.createAttribute("colspan");
		atributo.nodeValue = "2";
		cell.setAttributeNode(atributo);
		var cellText = document.createTextNode('Nombre: '+ $('rep_nombre').getValue()+' '+ $('rep_apellido1').getValue()+' '+ $('rep_apellido2').getValue());

		var lista = ['rep_nombre','rep_apellido1','rep_apellido2','rep_nif'];
		for (i=0;i<lista.length;i++)
		{
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = lista[i] +'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(lista[i]).getValue();
			$(lista[i]).value='';
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
		}	

		cell.appendChild(cellText);
		row1.appendChild(cell);
		
		var cell = document.createElement("td");
		var cellText = document.createTextNode(" ");
		cell.appendChild(cellText);
		row1.appendChild(cell);
		$('tbody_'+elemento).appendChild(row1);		
		
		// Para la fila 2
		var cell = document.createElement("td");
		var cellText = document.createTextNode(" ");
		cell.appendChild(cellText);
		row2.appendChild(cell);
		
		// DIRECCIÓN
		var cell = document.createElement("td");
		var atributo = document.createAttribute("colspan");
		atributo.nodeValue = "3";
		cell.setAttributeNode(atributo);
		var cellText = document.createTextNode('Dirección: '+ $('rep_direccion').getValue()+', '+ $('rep_portal').getValue()+' '+ $('rep_piso').getValue()+'-'+ $('rep_puerta').getValue()+' -- '+ $('rep_cp').getValue()+' -- '+ $('rep_localidad').getValue()+' , '+ $('rep_provincia').getValue()+' , '+ $('rep_pais').getValue());
		
		var lista = ['rep_direccion','rep_portal','rep_piso','rep_puerta','rep_cp','rep_localidad','rep_provincia','rep_pais'];
		for (i=0;i<lista.length;i++)
		{
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = lista[i] +'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(lista[i]).getValue();
			$(lista[i]).value='';
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
		}	
		cell.appendChild(cellText);
		row2.appendChild(cell);
		
		var cell = document.createElement("td");
		var cellText = document.createTextNode(" ");
		cell.appendChild(cellText);
		row2.appendChild(cell);
		$('tbody_'+elemento).appendChild(row2);		

		// Para la fila 3
		var cell = document.createElement("td");
		var cellText = document.createTextNode(" ");
		cell.appendChild(cellText);
		row3.appendChild(cell);
		
		// TELÉFONO FIJO Y MOVIL
		var cell = document.createElement("td");
		var cellText = document.createTextNode('Teléfonos: '+ $('rep_tfno').getValue()+' / '+$('rep_movil').getValue());
		
		var lista = ['rep_tfno','rep_movil'];
		for (i=0;i<lista.length;i++)
		{
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = lista[i] +'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(lista[i]).getValue();
			$(lista[i]).value='';
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
		}	
		cell.appendChild(cellText);
		row3.appendChild(cell);

		// TIPO DE REPRESENTACION
		var cell = document.createElement("td");
		var atributo = document.createAttribute("colspan");
		atributo.nodeValue = "2";
		cell.setAttributeNode(atributo);
		
		var cadenarepresentacion = 'Tipo de Representación: ';		
		var lista = ['rep_tipo_socio','rep_tipo_adm_socio','rep_tipo_adm_nosocio','rep_tipo_rep','rep_tipo_apoderado'];
		for (i=0;i<lista.length;i++)
		{
			if ($(lista[i]).getValue()!=false)
				cadenarepresentacion = cadenarepresentacion + $(lista[i]).getValue()+', ';
			
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = lista[i] +'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			if ($(lista[i]).getValue()==false)
				atributo.nodeValue = '';
			else
				atributo.nodeValue = $(lista[i]).getValue();
			$(lista[i]).checked=false;
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
		}	
		var cellText = document.createTextNode(cadenarepresentacion);
		cell.appendChild(cellText);
		row3.appendChild(cell);

		var cell = document.createElement("td");
		var cellText = document.createTextNode(" ");
		cell.appendChild(cellText);
		row3.appendChild(cell);
		$('tbody_'+elemento).appendChild(row3);	
		
		
		// Para la fila 4
		var cell = document.createElement("td");
		var cellText = document.createTextNode(" ");
		cell.appendChild(cellText);
		row4.appendChild(cell);
		
		
		// Nº DE PARTICIPACIONE Y REPRESENTACION
		var cell = document.createElement("td");
		var cellText = document.createTextNode('Participaciones: '+ $('rep_num_part').getValue()+' / '+$('rep_valor_part').getValue()+' %');
		
		var lista = ['rep_num_part','rep_valor_part'];
		for (i=0;i<lista.length;i++)
		{
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = lista[i] +'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(lista[i]).getValue();
			$(lista[i]).value='';
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
		}	
		cell.appendChild(cellText);
		row4.appendChild(cell);		
		
		// PODERES
		var cell = document.createElement("td");
		var atributo = document.createAttribute("colspan");
		atributo.nodeValue = "2";
		cell.setAttributeNode(atributo);
		
		var cadenapoderes = 'Poderes: ';		
		var lista = ['rep_tipopoder','rep_fecpoder','rep_notario'];
		for (j=0;j<parseInt($('num_poderes').getValue());j++)
		{
			for (i=0;i<lista.length;i++)
			{
				var campooculto = document.createElement("input");
				var atributo = document.createAttribute("type");
				atributo.nodeValue = "hidden";
				campooculto.setAttributeNode(atributo);
				var atributo = document.createAttribute("name");
				atributo.nodeValue = lista[i] +'_'+(j+1)+'_'+parseInt($('num_'+elemento).getValue());
				campooculto.setAttributeNode(atributo);
				var atributo = document.createAttribute("value");
				var elemtemp = lista[i]+'_'+(j+1);
				atributo.nodeValue = $(elemtemp).getValue();
				cadenapoderes = cadenapoderes +  $(elemtemp).getValue()+ ' - ';
				campooculto.setAttributeNode(atributo);
				cell.appendChild(campooculto);
			}
			cadenapoderes = cadenapoderes +  ' / ';
		}	
		
		// Num poderes
		var campooculto = document.createElement("input");
		var atributo = document.createAttribute("type");
		atributo.nodeValue = "hidden";
		campooculto.setAttributeNode(atributo);
		var atributo = document.createAttribute("name");
		atributo.nodeValue = 'rep_num_poderes_'+parseInt($('num_'+elemento).getValue());
		campooculto.setAttributeNode(atributo);
		var atributo = document.createAttribute("value");
		atributo.nodeValue = $('num_poderes').getValue();
		campooculto.setAttributeNode(atributo);
		cell.appendChild(campooculto);
				
		document.getElementById('num_poderes').value = 0;
		$('tbody_poderes').setHTML('');
		var cellText = document.createTextNode(cadenapoderes);
		cell.appendChild(cellText);
		row4.appendChild(cell);

		

		var cell = document.createElement("td");
		var boton = document.createElement("input");
		var atributo = document.createAttribute("type");
		atributo.nodeValue = "button";
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("name");
		atributo.nodeValue = "btn_eliminar_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("id");
		atributo.nodeValue = "btn_eliminar_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("value");
		atributo.nodeValue = "<< Borrar";
		boton.setAttributeNode(atributo);
		var valor = parseInt($('num_'+elemento).getValue());
		boton.onclick=function(){borrarFilaMultiple($('tbody_'+elemento),elemento+'_'+valor,valor);acordeon.display(-1);acordeon.display(capa);};
		cell.appendChild(boton);
		row4.appendChild(cell);

		$('tbody_'+elemento).appendChild(row4);

//alert($('adminForm').toQueryString());
}


/**
 * Funcion para añadir filas a tablas de directorios de documentos
 */
function anhadirTablaDocumentos(elemento,campos,capa)
{
		document.getElementById('num_'+elemento).value= (parseInt($('num_'+elemento).getValue())+1);

		var row = document.createElement("tr");
		var atributo = document.createAttribute("class");
		if (parseInt($('num_'+elemento).getValue()) % 2 == 0)
			atributo.nodeValue = "odd";
		else
			atributo.nodeValue = "even";
		row.setAttributeNode(atributo);
		var atributo = document.createAttribute("id");
		atributo.nodeValue = 'row_'+elemento+'_'+parseInt($('num_'+elemento).getValue());
		row.setAttributeNode(atributo);		

		
		var cell = document.createElement("td");
		var cellText = document.createTextNode($('num_'+elemento).getValue());
		cell.appendChild(cellText);
		row.appendChild(cell);

		for(i=0;i<campos.length;i++)
		{
			var cell = document.createElement("td");
			var cellText = document.createTextNode('ID: '+$(campos[i]).getValue().substring(0,$(campos[i]).getValue().indexOf('##')));
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = campos[i]+'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("id");
			atributo.nodeValue = campos[i]+'_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(campos[i]).getValue().substring(0,$(campos[i]).getValue().indexOf('##'));
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
			cell.appendChild(cellText);
			row.appendChild(cell);

			var cell = document.createElement("td");
			var cellText = document.createTextNode($(campos[i]).getValue().substr($(campos[i]).getValue().indexOf('##')+2));
			var campooculto = document.createElement("input");
			var atributo = document.createAttribute("type");
			atributo.nodeValue = "hidden";
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("name");
			atributo.nodeValue = campos[i]+'_ruta_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("id");
			atributo.nodeValue = campos[i]+'_ruta_'+parseInt($('num_'+elemento).getValue());
			campooculto.setAttributeNode(atributo);
			var atributo = document.createAttribute("value");
			atributo.nodeValue = $(campos[i]).getValue().substr($(campos[i]).getValue().indexOf('##')+2);
			$(campos[i]).value='';
			campooculto.setAttributeNode(atributo);
			cell.appendChild(campooculto);
			cell.appendChild(cellText);
			row.appendChild(cell);
		}

		var cell = document.createElement("td");
		var boton = document.createElement("input");
		var atributo = document.createAttribute("type");
		atributo.nodeValue = "button";
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("name");
		atributo.nodeValue = "btn_view_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("id");
		atributo.nodeValue = "btn_view_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("value");
		atributo.nodeValue = "Ver ficheros";
		boton.setAttributeNode(atributo);
		var valor = parseInt($('num_'+elemento).getValue());
		boton.onclick=function(){abrirDirectorio($('dir_id_'+valor).getValue() );};
		cell.appendChild(boton);
		
		var boton = document.createElement("input");
		var atributo = document.createAttribute("type");
		atributo.nodeValue = "button";
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("name");
		atributo.nodeValue = "btn_eliminar_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("id");
		atributo.nodeValue = "btn_eliminar_"+elemento+"_"+parseInt($('num_'+elemento).getValue());
		boton.setAttributeNode(atributo);
		var atributo = document.createAttribute("value");
		atributo.nodeValue = "<< Borrar";
		boton.setAttributeNode(atributo);
		var valor = parseInt($('num_'+elemento).getValue());
		boton.onclick=function(){borrarFila($('tbody_'+elemento),'row_'+elemento+'_'+valor,valor);acordeon.display(-1);acordeon.display(capa);};
		cell.appendChild(boton);
		row.appendChild(cell);


		$('tbody_'+elemento).appendChild(row);

//alert($('adminForm').toQueryString());
}


function borrarFila(tabla,fila,numfila)
{
	var navegador = navigator.appName;
	if (navegador == "Microsoft Internet Explorer")
	{
		var filas = tabla.getElementsByTagName("tr");
		
		for(i=0;i<filas.length;i++)
		{
			if (filas[i].id == fila)
		        tabla.deleteRow(i);
		}
	}
    else 
		tabla.removeChild(document.getElementById(fila));
}


function borrarFilaMultiple(tabla,fila,numfila)
{
	var navegador = navigator.appName;
	if (navegador == "Microsoft Internet Explorer")
	{
		var filas = tabla.getElementsByTagName("tr");
		
		for(i=filas.length-1;i>=0;i--)
			if ((filas[i].id == 'row1_'+fila) || (filas[i].id == 'row2_'+fila) ||
			    (filas[i].id == 'row3_'+fila) || (filas[i].id == 'row4_'+fila))
		        tabla.deleteRow(i);
	}
    else 
	{
		tabla.removeChild(document.getElementById('row1_'+fila));
		tabla.removeChild(document.getElementById('row2_'+fila));
		tabla.removeChild(document.getElementById('row3_'+fila));
		tabla.removeChild(document.getElementById('row4_'+fila));
	}
}





/**
 * Función para hacer peticiones AJAX a paginas web
 */
function enviarDatos(direccion)
{
    $('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="notice message fade"><ul><li>Procesando información ...</li></ul></dd></dl>');		
	new Ajax(direccion, {postBody: $('adminForm'), onComplete:respuesta}).request();
               
    function respuesta(response,responseXML)
    {
    	var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
		if (codigo > 0)
    		var tipo='mensaje';
    	else
    		var tipo='error';
    	$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="'+tipo+' message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
    }
}



/**
 * Función para hacer peticiones AJAX a paginas web
 */
function enviarDatosPublica(direccion)
{
  $('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="notice message fade"><ul><li>Procesando información ...</li></ul></dd></dl>');		
	new Ajax(direccion, {postBody: $('adminForm'), onComplete:respuesta}).request();
               
    function respuesta(response,responseXML)
    {
    	var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
		if (codigo > 0)
    		var tipo='mensaje';
    	else
    		var tipo='error';
    	$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="'+tipo+' message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
    }
}


/**
 * Función para hacer peticiones AJAX a la BD de códigos de actividades CNAE
 */
function obtenerCodigosIAECNAE(tipo)
{
	if ($('act_'+tipo).value!='' || $('act_descripcion_'+tipo).value!='')
	{

		function rellenar_temporal_actividades(response,responseXML)
	    {
	    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			
			if (codigo < 0)
	    	{
				var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
	    		$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="error message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
	    	}
	    	else
	    	{    	
				var actividades = responseXML.getElementsByTagName("actividad");
				var cadena='';
				if (actividades.length < 25)
					for(i=0;i<actividades.length;i++)
						cadena += '<a href="JavaScript:rellenarActividad(\''+tipo+'\',\''+ actividades[i].childNodes[0].firstChild.data + '\',\''+ actividades[i].childNodes[1].firstChild.data + '\')">'+ actividades[i].childNodes[0].firstChild.data + ' - '+ actividades[i].childNodes[1].firstChild.data+ '</a><br>';
	    		$('temporal_actividades').setHTML(cadena);	
	    		acordeon.display(-1);acordeon.display(1);
	    	}
	    }

		var destino='op_cnae_iae.php?op='+tipo.toUpperCase()+'_listar&valor='+$('act_'+tipo).value+'&des='+$('act_descripcion_'+tipo).value;
		new Ajax(destino, {method: 'get',onComplete:rellenar_temporal_actividades }).request();
	    
	}
}


function rellenarActividad(tipo,codigo,descripcion)
{
	$('act_'+tipo).value=codigo;
	$('act_descripcion_'+tipo).value=descripcion;
}


function obtenerClientes()
{
		function rellenar_temporal_clientes(response,responseXML)
	    {
	    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			
			if (codigo < 0)
	    	{
				var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
	    		$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="error message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
	    	}
	    	else
	    	{    	
				var clientes = responseXML.getElementsByTagName("cliente");
				var cadena = '<table class="adminlist" cellspacing="1"><thead><tr><th width="5">#</th><th width="5">&nbsp;</th><th width="5%" class="title">Id</th><th width="15%">N.I.F./C.I.F.</th><th width="40%">Nombre Comercial</th><th width="40%">Nombre Contacto</th></tr></thead><tbody>';
				for(i=0;i<clientes.length;i++)
				{
				    (i % 2 == 0 )?clase='row0':clase='row1';
				    cadena += '<tr class="'+clase+'"><td>'+(i+1)+'</td><td align="center"><input type="checkbox" id="cb'+i+'" name="cid[]" value="'+clientes[i].childNodes[0].firstChild.data+'" onClick="JavaScript:chequearTabla(this);" /></td><td>'+clientes[i].childNodes[0].firstChild.data+'</a></td><td><a href="JavaScript:document.adminForm.id.value = '+clientes[i].childNodes[0].firstChild.data+';editarTabla(\'gestor.php?op=clie_editar\');">'+clientes[i].childNodes[1].firstChild.data+'</td><td><a href="JavaScript:document.adminForm.id.value = '+clientes[i].childNodes[0].firstChild.data+';editarTabla(\'gestor.php?op=clie_editar\');">'+clientes[i].childNodes[2].firstChild.data+'</a></td><td>'+clientes[i].childNodes[3].firstChild.data+'</td></tr>';
				}
				cadena += '</tbody></table><input type="hidden" name="id" value="">';
	    		$('temporal_clientes').setHTML(cadena);	
	    	}
	    }
							

		var destino='op_cliente.php?op=clie_buscar';
		new Ajax(destino, {postBody: $('inForm'),onComplete:rellenar_temporal_clientes }).request();
	    
}


function obtenerFicheros()
{
		function rellenar_temporal_ficheros(response,responseXML)
	    {
	    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			if (codigo < 0)
	    	{
				var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
	    		$('mensajes_editables').setHTML('<dl id="system-message"><dt class="message">Mensaje</dt><dd class="error message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>');		
	    	}
	    	else
	    	{    	
				var ficheros = responseXML.getElementsByTagName("fichero");
				var cadena = '<table class="adminlist" cellspacing="1"><thead><tr><th width="5">#</th><th width="5">&nbsp;</th><th width="50%" class="title">Nombre</th><th width="22%">Creado por</th><th width="22%">Fecha de creación</th></tr></thead><tbody>';
				for(i=0;i<ficheros.length;i++)
				{
				    (i % 2 == 0 )?clase='row0':clase='row1';
					cadena += '<tr class="'+clase+'"><td>'+(i+1)+'</td><td align="center">&nbsp;</td><td style="padding:0px;"><table width=100%><tr style="padding:0px;"><td rowspan=2 style="padding:0px;" width=30><img src="images/ico/'+ficheros[i].childNodes[1].firstChild.data.substring(ficheros[i].childNodes[1].firstChild.data.lastIndexOf('.')+1)+'.png" border=0 width=25 height=25></td><td style="padding:0px;" align=left><a target="_blank" href='+ficheros[i].childNodes[5].firstChild.data+ficheros[i].childNodes[1].firstChild.data+'><B>'+ficheros[i].childNodes[1].firstChild.data+'</B></a></td></tr><tr><td style="padding:0px;" align=left>'+ficheros[i].childNodes[2].firstChild.data+'</td></tr></table></td><td>'+ficheros[i].childNodes[3].firstChild.data+'</a></td><td>'+ficheros[i].childNodes[4].firstChild.data+'</td></tr>';	

				}
				cadena += '</tbody></table>';
	    		$('temporal_ficheros').setHTML(cadena);	
	    	}
	    }

		var destino='op_fichero.php?op=fich_buscar';
		new Ajax(destino, {postBody: $('inForm'),onComplete:rellenar_temporal_ficheros }).request();
	    
}


/**
 * Función para abrir una ventana con los ficheros de un directorio dado
 * Se emplea en el fichero pa_cliente.php pasandole el directorio que se desea abrir
 * Recupera los ficheros de ese directorio y los tipos de ficheros existentes de BD
 * y los muestra para que se pueda modificar tanto el tipo como la fecha de caducidad
 * de dichos ficheros
 */
function abrirDirectorio(directorio)
{
	
		function abrir_listado_ficheros(response,responseXML)
	    {
	    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			if (codigo < 0)
	    	{
				var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
	    		var contenido = '<dl id="system-message"><dt class="message">Mensaje</dt><dd class="error message fade"><ul><li>'+mensaje+'</li></ul></dd></dl>';		
	    	}
	    	else
	    	{    	
				var ficheros = responseXML.getElementsByTagName("fichero");
				var tipoficheros = responseXML.getElementsByTagName("tipofichero");
				var cadena = '<table class="adminlist" cellspacing="1"><thead>' +
						'<tr><th width="5">#</th>' +
						'<th width="5">&nbsp;</th>' +
						'<th width="50%" class="title">Nombre</th>' +
						'<th width="22%">Tipo de Documento</th>' +
						'<th width="22%">Fecha de Caducidad</th>' +
						'<th width="15">&nbsp;</th></tr></thead><tbody>';
				for(i=0;i<ficheros.length;i++)
				{
				    (i % 2 == 0 )?clase='row0':clase='row1';
					var opciones='<option value=""></option>';
					for(j=0;j<tipoficheros.length;j++)
					{
						if (tipoficheros[j].childNodes[0].firstChild.data == ficheros[i].childNodes[6].firstChild.data)
							opciones += '<option value="'+tipoficheros[j].childNodes[0].firstChild.data+'" selected>'+tipoficheros[j].childNodes[1].firstChild.data+'</option>';
						else
							opciones += '<option value="'+tipoficheros[j].childNodes[0].firstChild.data+'">'+tipoficheros[j].childNodes[1].firstChild.data+'</option>';
					}
					cadena += '<tr class="'+clase+'"><td>'+(i+1)+'</td>' +
							'<td align="center">&nbsp;</td>' +
							'<td style="padding:0px;">' +
							'<table width=100%>' +
							'<tr style="padding:0px;"><td rowspan=2 style="padding:0px;" width=30><img src="images/ico/'+ficheros[i].childNodes[1].firstChild.data.substring(ficheros[i].childNodes[1].firstChild.data.lastIndexOf('.')+1)+'.png" border=0 width=25 height=25></td>' +
							'<td style="padding:0px;" align=left><input type="hidden" name="fichero_'+i+'" id="fichero_'+i+'" value="'+ficheros[i].childNodes[0].firstChild.data+'"><B>'+ficheros[i].childNodes[1].firstChild.data+'</B></td></tr>' +
							'<tr><td style="padding:0px;" align=left>'+ficheros[i].childNodes[2].firstChild.data+'</td></tr></table></td>' +
							'<td><select name="tipo_'+i+'" id="tipo_'+i+'" class="text_area" >'+opciones+'</select></td>' +
							'<td><input name="fecha_'+i+'" id="fecha_'+i+'" class="text_area" type="text" maxlength="10" size="12" value="'+ficheros[i].childNodes[7].firstChild.data+'"></td>' +
							'<td><input name="btnMod_'+i+'" id="btnMod_'+i+'" class="text_area" type="button" value="<< Modificar" onClick="guardarInfoFile('+i+');"> </td></tr>';	

				}
				cadena += '</tbody></table>';
	    		var contenido = cadena;	
	    	}
	    	document.mochaNewWindowForm.mochaNewWindowFormContent.value=contenido;
			document.mochaNewWindowForm.mochaNewWindowFormHeaderTitle.value="Ficheros del directorio";
			document.mochaNewWindowForm.mochaNewWindowFormWidth.value=700;
			document.mochaNewWindowForm.mochaNewWindowFormHeight.value=40*(i+2);
			document.mochaNewWindowForm.mochaNewWindowFormSubmit.click();
	    }

		var destino='op_directorio.php?op=dire_listar&directorio='+directorio;
		new Ajax(destino, {	method: 'get',onComplete:abrir_listado_ficheros }).request();
	

}


/**
 * Función para guardar en base de datos la información adicional de un fichero relativo a su tipo
 * Se emplea en el fichero funciones_sca.js para pasar a op_fichero.php los datos de tipo de fichero
 * y fecha de caducidad para que sea guardada en BD
 */
function guardarInfoFile(elemento)
{
	
		function mensaje_respuesta_InfoFile(response,responseXML)
	    {
	    	var codigo = responseXML.getElementsByTagName("cod_error")[0].firstChild.data;
			if (codigo < 0)
	    	{
				var mensaje = responseXML.getElementsByTagName("msg_error")[0].firstChild.data;
				alert(mensaje);	    		
	    	}	
	    	else
	    	{
	    		alert('Modificación realizada correctamente.');
	    	}    	
	    }
		var id_file = document.getElementById('fichero_'+elemento).value;
		var id_tipo = document.getElementById('tipo_'+elemento).value;
	 	var fecha = document.getElementById('fecha_'+elemento).value;
	
		var destino='op_fichero.php?op=fich_infoAdicional&file='+id_file+'&tipo='+id_tipo+'&fecha='+fecha;
		new Ajax(destino, {	method: 'get',onComplete:mensaje_respuesta_InfoFile }).request();

}

