// JavaScript Document

function ListarClientes(pag){
	if(pag==null) pag="";
	var datos="pagajax=paginas/clientes/tabla_clientes.php";
	datos+="&"+$("#frmFiltro").serialize();
	datos+="&pag="+pag;
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){$('#visor').html(html);}
	});	
}
function FichaCliente(id){
	var data="&id="+id;
	CargarSeccion("clientes","ficha_cliente", data);		
}

/*=========================== FICHA CLIENTE ==============================*/

function AccionesClientes(accion, cliente){
	if(cliente==null) cliente="";
	
	var datos="pagajax=paginas/clientes/ajax_clientes.php";
	datos+="&accion="+accion+"&id="+cliente;
	
	if(accion=="almacenar" || accion=="modificar"){
		datos+="&"+$("#frmCliente").serialize();
	}

	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;
	return(html);
}

function MostrarFichaCliente(cliente){
	if(cliente==null) cliente="";
	var html=AccionesClientes("ficha",cliente);
	$('#ficha').html(html);
}

function MostrarCliente(cliente){
	if(cliente==null) cliente="";
	var html=AccionesClientes("mostrarcliente",cliente);
	$('#ficha_cliente').html(html);
	if(cliente!="") MostrarClienteWeb(cliente);
}

function AlmacenarCliente(cliente){
	if(cliente==null) cliente="";
	var html=AccionesClientes("almacenar", cliente);
	
	MostrarAviso(html,"Almacenar Cliente");	
}

function SelectCliente(obj, cliente){
	if(cliente==null) cliente="";
	var html=AccionesClientes("select", cliente);
	$(obj).html(html);
}

function EliminarImagenCliente(cliente){
	if(cliente!=null){
		var html=AccionesClientes("eliminarimg", cliente);
		$('#img_cliente').attr('src', html);
		$('#borrar_imagen').attr("disabled","disabled");
	}
}

/*=============== CLIENTES WEB =================*/

function AccionesClientesWeb(accion, cliente, web){
	if(cliente==null) cliente="";
	if(web==null) web="";
	//if(web==null) web=cliente;
	
	var datos="pagajax=paginas/clientes/ajax_clientesweb.php";
	datos+="&accion="+accion+"&id="+cliente+"&idw="+web;
	
	if(accion=="almacenar" || accion=="modificar"){
		datos+="&"+$("#frmClienteWeb").serialize();
	}

	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;
	return(html);
}

function ListarClientesWeb(cliente){
	if(cliente!=null){
		var html=AccionesClientesWeb("listar",cliente);
		$('#listado_clientes_web').html(html);	
	}
}

function MostrarClienteWeb(cliente, id){
	if(id==null) id="";
	
	if(cliente!=null){
		var html=AccionesClientesWeb("mostrar",cliente, id);
		$('#ficha').html(html);
	}
}

function CambiarEstadoClienteWeb(cliente, id){
	
	if(id!=null){
		var html=AccionesClientesWeb("cambiarestado",cliente, id);
		if(html!=""){
		//	MostrarAviso(html, "Cambiar Estado Cliente Web");	
		//}else{
		//	ListarClientesWeb(cliente);
		}
	ListarClientesWeb(cliente);
	}
}


function EliminarClienteWeb(cliente, id){
	
	
	if(confirm("\xF1Desea Eliminar este cliente web?")){
		if(id!=null){
			var html=AccionesClientesWeb("eliminar",cliente, id);
			/*
			 if(html!=""){
				MostrarAviso(html, "Eliminar Cliente Web");
			}else{	
				ListarClientesWeb(cliente);
			}
			*/
			ListarClientesWeb(cliente);
		}
	}
}

function AlmacenarClienteWeb(cliente, web){
	var html=AccionesClientesWeb("almacenar", cliente, web);
	//MostrarAviso(html,"SEGUIMIENTO");
	//MostrarClienteWeb(cliente, web);
	ListarClientesWeb(cliente);
	MostrarClienteWeb(cliente);
}

function SelectClienteWeb(obj, cliente, web){
	if(cliente==null) cliente="";
	if(web==null) web="";
	if(cliente=="" || cliente==null || cliente=="NULL"){
		$(obj).attr("disabled","disabled");
	}else{
		$(obj).attr("disabled","");
	}
	var html=AccionesClientesWeb("select", cliente, web);
	$(obj).html(html);
}

function Listar_LogClientes(pag){
	if(pag==null) pag=0;
	var datos="pagajax=paginas/clientes/tabla_logclientes.php";
	datos+="&"+$("#frmFiltro").serialize();
	datos+="&pag="+pag;
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){$('#visor').html(html);}
	});	
}

/*=============== DELEGACIONES =================*/

function AccionesDelegacion(accion, cliente, del){
	if(cliente==null) cliente="";
	if(del==null) del="";
	
	var datos="pagajax=paginas/clientes/ajax_delegaciones.php";
	datos+="&accion="+accion+"&id="+cliente+"&iddel="+del;
	
	if(accion=="almacenar" || accion=="modificar"){
		datos+="&"+$("#frmDelegacion").serialize();
	}
	
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;
	return(html);
}

function ListarDelegaciones(cliente){
	if(cliente!=null){
		var html=AccionesDelegacion("listar",cliente);
		$('#listado_clientes_web').html(html);	
	}
}

function MostrarDelegacion(cliente, id){
	if(id==null) id="";
	if(cliente!=null){
		var html=AccionesDelegacion("mostrar",cliente, id);
		$('#ficha').html(html);
	}
}

function CambiarEstadoDelegacion(cliente, id){
	if(id!=null){
		var html=AccionesDelegacion("cambiarestado",cliente, id);
		if(html!=""){
			MostrarAviso(html, "Cambiar Estado Delegaci&oacute;n");	
		}else{
			ListarDelegaciones(cliente);
		}
	}
}


function EliminarDelegacion(cliente, id){
	if(confirm("\xF1Desea Eliminar esta delegaci\xF3n?")){
		if(id!=null){
			var html=AccionesDelegacion("eliminar",cliente, id);
			if(html!=""){
				MostrarAviso(html, "Eliminar Cliente Web");
			}else{	
				ListarDelegaciones(cliente);
			}
		}
	}
}

function AlmacenarDelegacion(cliente, del){
	var html=AccionesDelegacion("almacenar", cliente, del);
	//MostrarAviso(html,"SEGUIMIENTO");
	//MostrarClienteWeb(cliente, web);
	ListarDelegaciones(cliente);
	MostrarDelegacion(cliente);
}

function SelectDelegacion(obj, cliente, del){
	if(cliente==null) cliente="";
	if(del==null) del="";
	if(cliente=="" || cliente==null || cliente=="NULL"){
		$(obj).attr("disabled","disabled");
	}else{
		$(obj).attr("disabled","");
	}
	var html=AccionesDelegacion("select", cliente, del);
	$(obj).html(html);
}


/*=============== CATEGORIAS CLIENTES WEB =================*/

function SelectCategoriasCliente(id, valor){
	if(valor==null) valor="";
	
	var datos="pagajax=paginas/clientes/ajax_categorias.php";
	datos+="&accion=select&valor="+valor;
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: true,
		success: function(html){$(id).html(html);}
	});
}

function MostrarCategoriasCliente(id){
	if(id==null)id="";
	var datos="pagajax=paginas/clientes/ajax_categorias.php";
	datos+="&accion=mostrar&id="+id;
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: true,
		success: function(html){
			$('#div_ctg').html(html);
			MostrarDialogo('div_ctg', 'Editar Categor&iacute;as de Cliente Web');
			VisorCategoriasClientes();
			FormularioCategoriasClientes('');
		}			
	});
}

function VisorCategoriasClientes(){
	var datos="pagajax=paginas/clientes/ajax_categorias.php";
	datos+="&accion=visor";
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){
			$('#visor_categorias').html(html);
		}			
	});
}

function FormularioCategoriasClientes(id){
	if(id==null)id="";
	var datos="pagajax=paginas/clientes/ajax_categorias.php";
	datos+="&accion=formulario&id="+id;
	
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){
			$('#formulario_categorias').html(html);
		}			
	});
}

function AlmacenarCategoriaCliente(id){
	if(id==null)id="";
	var datos="pagajax=paginas/clientes/ajax_categorias.php";
	datos+="&accion=almacenar&id="+id;
	datos+="&"+$("#frmCategoria").serialize();	
	var valor=document.getElementById('categoria_web').value;
	$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(html){
			VisorCategoriasClientes();
			FormularioCategoriasClientes('');
			SelectCategoriasCliente("#categoria_web",valor);
		}			
	});	
}

function EliminarCategoriaCliente(id){
	if(confirm("\xBFDesea eliminar esta Categor\xECa de Cliente?")){
		if(id==null)id="";
		var datos="pagajax=paginas/clientes/ajax_categorias.php";
		datos+="&accion=eliminar&id="+id;
		var valor=document.getElementById('categoria_web').value;
		$.ajax({
			url: "paginas/ajax.php",
			data: datos,      
			global: false,
			type: "POST",
			async: false,
			success: function(html){
				VisorCategoriasClientes();
				FormularioCategoriasClientes('');
				SelectCategoriasCliente("#categoria_web",valor);
			}			
		});	
	}
}

/*=============== ACTIVIDADES =================*/

function MostrarActividadesCliente(cliente, id){
	if(id==null) id="";
	
	if(cliente!=null){
		var html=AccionesActividadesCliente("mostrar",cliente);
		$('#ficha').html(html);
	}
}

function AccionesActividadesCliente(accion, cliente){
	var seleccionadas = new Array();
	
	if(cliente==null) cliente="";
		
	
	
	var datos="pagajax=paginas/clientes/ajax_actividadescliente.php";
	datos+="&accion="+accion+"&id="+cliente;
	
	if(accion=="almacenar" || accion=="modificar"){
			
	$("input[@name='actividades[]']:checked + label").each(function(){
		seleccionadas.push($(this).html());
	});
	
		if (seleccionadas.length>0){
			datos+="&actividades="+seleccionadas;
		}	
	}
		
	var html=$.ajax({
		url: "paginas/ajax.php",
		data: datos,      
		global: false,
      	type: "POST",
		async: false,
		success: function(){}
	}).responseText;
	return(html);
}

function AlmacenarActividadesCliente(cliente){
	
			
	AccionesActividadesCliente("almacenar", cliente);
	
	//ListarClientesWeb(cliente);
	//MostrarActividadesCliente(cliente);
}

