function ListarDocumentos(pag){
	if(pag==null) pag=0;
	
	var datos="pagajax=paginas/documentos/tabla_documentos.php";
	datos+="&pag="+pag;
	datos+="&"+$('#frmFiltro').serialize();
	
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){
			$('#visor').html(html);	
		}
	});	
}


/*=============== CATEGORIAS DOCUMENTO ===============*/

function AccionesCategoriaDocumento(accion, id, data){
	if(id==null) id="";
	if(data==null) data="";
	
	var datos="pagajax=paginas/documentos/ajax_categorias.php";
	datos+="&accion="+accion;
	datos+="&id="+id;
	datos+=data;
	
	if(accion=="almacenar"){
		datos+="&"+$('#frmCategoria').serialize();	
	}
	
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;	
	
	return(html);
}

function ListarCategoriasDocumento(){
	var html=AccionesCategoriaDocumento('listar');
	$('#lista').html(html);
}

function FichaCategoriaDocumento(id){
	if(id==null) id="";
	var html=AccionesCategoriaDocumento('ficha', id);
	$('#ficha').html(html);
}


function EliminarCategoriaDocumento(id){
	if(id!=null && id>0 && confirm("\xBFDesea eliminar esta categor\xECa?\nLos documentos asociados a esta categor\xECa se colocar\xE0n dentro de \"Sin Categor\xECa\"")){
		var html=AccionesCategoriaDocumento('eliminar', id);
		if(html!=""){
			MostrarAviso(html, "Eliminar Categor&iacute;a Documento");	
		}else{
			ListarCategoriasDocumento();
			FichaCategoriaDocumento();	
		}		
	}
}

function CambiarEstadoCategoriaDocumento(id){
	if(id!=null){
		var html=AccionesCategoriaDocumento('cambiarestado', id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Estado Categor&iacute;a Documento");	
		}else{
			ListarCategoriasDocumento();	
		}		
	}
}

function AlmacenarCategoriaDocumento(id){
	if($('#nombre').val()!=""){
		if(id==null) id="";
		var html=AccionesCategoriaDocumento('almacenar', id);		
		if(html!=""){
			MostrarAviso(html, "Almacenar Catego&iacute;a Documento");	
		}else{
			ListarCategoriasDocumento();
			FichaCategoriaDocumento();	
		}
	}else{
		MostrarAviso("Debe asignarle un nombre al documento", "Almacenar Documentos");	
	}
}

function SelectCategoriasDocumento(id_obj, nombre){
	if(nombre==null) nombre="";
	var data="&nombre="+nombre;
	var html=AccionesCategoriaDocumento('select', '', data);
	$('#'+id_obj).html(html);
}

function SelectVisorCategoriasDocumento(nombre){
	var chk;
	if(nombre==null) nombre="";
	var data="&nombre="+nombre;
	var html=AccionesCategoriaDocumento('select', '', data);
	if(nombre=="-1") chk=" selected='selected'"; else chk="";
	html="<option value=\"-1\""+chk+">Todos</option>\r\n"+html;
	$('#filtro_categoria').html(html);
}

/*=============== DOCUMENTOS ===============*/


function FichaDocumento(id){
	var data="&id="+id;
	CargarSeccion("documentos","ficha_documento", data);		
}

function AccionesDocumento(accion, id, data){
	if(id==null) id="";
	if(data==null) data="";
	
	var datos="pagajax=paginas/documentos/ajax_documentos.php";
	datos+="&accion="+accion;
	datos+="&id="+id;
	datos+=data;
	
	if(accion=="almacenar"){
		datos+="&"+$('#frmDocumento').serialize();	
	}
	
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;	
	
	return(html);
}

function AlmacenarDocumento(id){
		
	var  html=AccionesDocumento('almacenar', id);
	if(html!=""){
		MostrarAviso(html,"Almacenar Documento");	
	}else{
		CargarSeccion("documentos", "visor_documentos");
	}
}

function EliminarDocumento(id){
	var  html=AccionesDocumento('eliminar', id);
	if(html!=""){
		MostrarAviso(html,"Eliminar Documento");	
	}else{
		ListarDocumentos();
	}
}

function CambiarEstadoDocumento(id){
	var  html=AccionesDocumento('cambiarestado', id);
	if(html!=""){
		MostrarAviso(html,"Cambiar Estado Documento");	
	}else{
		ListarDocumentos();
	}
}

/*=============== DOCUMENTO PADRE ===============*/

function SelectDocumentoPadre(id_obj, nombre, id){
	
	if(nombre==null) nombre=0;
	if(id==null) id=0;
	var data="&nombre="+nombre;
	var html=AccionesDocumentoPadre('select', id, data);
	$('#'+id_obj).html(html);
}

function AccionesDocumentoPadre(accion, id, data){
	if(id==null) id="";
	if(data==null) data="";
	
	var datos="pagajax=paginas/documentos/ajax_documentopadre.php";
	datos+="&accion="+accion;
	datos+="&id="+id;
	datos+=data;
	
	
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;	
	
	return(html);
}

function DocumentosCategoria(id_obj,id){
	
	var datos="pagajax=paginas/documentos/ajax_documentopadre.php";
	datos+="&id="+id;
	datos+="&accion=carga";
	
	
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
		type: "POST",
		async: false,
		success: function(html){
			
			$('#'+id_obj).html(html);
		}
	}).responseText;
	
}

