cargado_14=false;
cargado_4=false;
cargado_11=false;
cargado_18=false;
cargado_21=false;
cargado_23=false;
cargado_29=false;
cargado_41=false;

function centrar(latitud,longitud,zoom)
{

	var iZoom = parseInt(zoom, 10);
	map.setZoom(iZoom);
	map.panTo(new GLatLng(latitud,longitud));
	//map.setCenter(new GLatLng(latitud,longitud),iZoom);

}

function centrarAjax(pk_cat)
{
	loadDatosCentrado('./include/centrado.jsp?pk_cat='+pk_cat,tratarDatosCentrado);

}
function tratarDatosCentrado()
{
	if(reqCentrado.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(reqCentrado.status==200)
    	{
    		arrayDatos = eval(reqCentrado.responseText);
    		centrar(arrayDatos[0].latitud,arrayDatos[0].longitud,arrayDatos[0].zoom);
		}
		else
    	{
    		mostrarError("Problema al generar los options:\n"+reqCentrado.statusText);
    	}
	}

}

function mostrarPorMunicipios(idioma,tipo,categoria)
{
	ponerCargandoPuntos();
	limpia_mapa();
	parametros=categoria;
	primero=true;
	tipoEmpresa="";
	if (getRadioButtonSelectedValue(document.formulario.tipoEmpresa)){
		tipoEmpresa = getRadioButtonSelectedValue(document.formulario.tipoEmpresa);
	}
	//alert('./include/puntos.jsp?tipo='+tipo+'&tipoEmpresa='+tipoEmpresa+'&idioma='+idioma+'&parametros='+escape(parametros));
	loadDatosPuntos('./include/puntos.jsp?tipo='+tipo+'&tipoEmpresa='+tipoEmpresa+'&idioma='+idioma+'&parametros='+escape(parametros),
	function ()
		{
			pintarPuntos(tipo, tipoEmpresa)
		});
}

function mostrarPorMunicipios2(idioma,tipo,categoria,iteracion)
{
	ponerCargandoPuntos();
	if (iteracion==0)
	{
		limpia_mapa();
	}
	parametros=categoria;
	primero=true;
	tipoEmpresa="";
	if (getRadioButtonSelectedValue(document.formulario.tipoEmpresa)){
		tipoEmpresa = getRadioButtonSelectedValue(document.formulario.tipoEmpresa);
	}
	loadDatosPuntos('./include/puntos.jsp?tipo='+tipo+'&tipoEmpresa='+tipoEmpresa+'&idioma='+idioma+'&parametros='+escape(parametros),
	function ()
		{
			pintarPuntos(tipo)
		});
}

function muestraCategorias(id,imgFolder,idioma,tipo)
{
	var obj=document.getElementById(id);
	var obj2=document.getElementById('capa_'+id);
	var btn=document.getElementById('btn_'+id);

	//para que solo cargue los options una vez
	if (eval("!cargado_"+btn.value))
	{
		loadOptionsMunicipios('./include/optionsMunicipios.jsp?prov='+btn.value+'&idioma='+idioma+'&tipo='+tipo,
					function (){
						generarOptions(id)
					});

		eval("cargado_"+btn.value+"=true");
	}


	recogeAbiertas(imgFolder);
	if(obj!=null)
	{
		obj.style.display='block';
		obj2.style.display='block';
	}
	//btn.src = imgFolder+'menos_cat.jpg';
	/*btn.onclick = function() {
			       	  	recogeCategoria(id,imgFolder);
			       	  };*/
}

function recogeCategoria(id,imgFolder)
{
	var obj=document.getElementById(id);
	var obj2=document.getElementById('capa_'+id);
	var btn=document.getElementById('btn_'+id);
	if(obj!=null)
	{
		obj.style.display='none';
		obj2.style.display='none';
	}
	//btn.src = imgFolder+'mas_cat.jpg';
	/*btn.onclick = function() {
			       	  	muestraCategorias(id,imgFolder);
			       	  };*/
}
function recogeAbiertas(imgFolder)
{
	for (var i=0;i < document.formulario.elements.length;i++)
	{
		var elemento = document.formulario.elements[i];

		if (elemento.type == "select-one")
		{
			if (elemento.style.display!='none')
			{
				elemento.style.display='none';
				var btn2=document.getElementById('capa_'+elemento.id);
				btn2.style.display='none';
				var btn=document.getElementById('btn_'+elemento.id);
				//img.src = imgFolder+'mas_cat.jpg';
				var id = elemento.id;
				/*btn.onclick = function() {
						       	  	muestraCategorias(id,imgFolder);
			   	 			   	  };				*/
			}

		}
	}
}

function  loadOptionsMunicipios(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	reqOptMun=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	reqOptMun= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(reqOptMun)
    {
    	reqOptMun.onreadystatechange=procesoReqCambio;
    	reqOptMun.open("POST",url,true);
    	reqOptMun.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	reqOptMun.send("id=3");
    }
}

function generarOptions(id)
{
	if(reqOptMun.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(reqOptMun.status==200)
    	{

    		var select = document.getElementById(id);
    		arrayDatos = eval(reqOptMun.responseText);
    		for (i=0;i<arrayDatos.length;i++)
    		{
    			select.options[i+1]=new Option(arrayDatos[i].node,arrayDatos[i].value);
			}
		}
		else
    	{
    		mostrarError("Problema al generar los options:\n"+reqOptMun.statusText);
    	}
	}

}

function  loadDatosCentrado(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	reqCentrado=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	reqCentrado= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(reqCentrado)
    {
    	reqCentrado.onreadystatechange=procesoReqCambio;
    	reqCentrado.open("POST",url,true);
    	reqCentrado.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	reqCentrado.send("id=3");
    }
}

function buscarCodPostal()
{
	codPostal = document.formulario.codPostal.value;
	if (isNaN(codPostal))
	{
		mostrarError('El código postal debe ser un número');
	}
	else if (codPostal.indexOf('.')!=-1)
	{
		mostrarError('El código postal no puede tener decimales');
	}
	else if (codPostal.length!=5)
	{
		mostrarError('El código postal debe tener 5 dígitos')
	}
	else
	{
		loadDatosCodPostal('./include/buscadorCodPostal.jsp?codPostal='+codPostal,tratarCodPostal)
	}
}

function loadDatosCodPostal(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	req_cp=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	req_cp= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req_cp)
    {
    	req_cp.onreadystatechange=procesoReqCambio;
    	req_cp.open("POST",url,true);
    	req_cp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req_cp.send("id=3");
    }
}

function tratarCodPostal()
{
	if(req_cp.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_cp.status==200)
    	{
    		arrayDatos = eval(req_cp.responseText);
    		if (arrayDatos.length!=0)
    		{
    			for (i=0;i<arrayDatos.length;i++)
    			{
    				mostrarPorMunicipios2('spa','comercio',arrayDatos[i].cod_munic,i);
    			}
    			latitud = arrayDatos[0].latitud;
    			longitud = arrayDatos[0].longitud;
    			centrar(latitud,longitud,14);
    		}
    		else
    		{
    			mostrarError("No se encuentra el código postal indicado");
    		}
		}
		else
    	{
    		mostrarError("No se encuentra el código postal indicado");
    	}
	}
}

function mostrarError(mensaje)
{
	document.getElementById("capaError").style.display='block';
	document.getElementById("msgError").innerHTML=mensaje;
	//alert(mensaje);
}
function mostrarMensajeIntro(mensajeIntro, mensajeOptimizacion)
{
	document.getElementById("capaMensajeIntro").style.display='block';
	document.getElementById("msgIntro").innerHTML=mensajeIntro;
	document.getElementById("msgOptimizacion").innerHTML=mensajeOptimizacion;
}

function cerrarMensajeIntro()
{
	document.getElementById("capaMensajeIntro").style.display='none';
	document.getElementById("msgIntro").innerHTML='';
	document.getElementById("msgOptimizacion").innerHTML='';
}
function mostrarErrorComercios(mensaje,id_comercio)
{
	/*loadDatosErrorComercios('./include/datosErrorComercio.jsp?id_comercio='+id_comercio,
					function (){
						mostrarErrorComerciosCompleto(mensaje);
					});*/

	loadDatosErrorComercios('./include/bocadillo.jsp?idioma=spa&pk_rec='+id_comercio,
					function (){
						mostrarErrorComerciosCompleto(mensaje);
					});




}
function mostrarErrorComerciosCompleto(mensaje)
{

	mensaje = "<br/><font color=\"red\"><b>"+mensaje+"</b></font>";
	if(req_ec.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_ec.status==200)
    	{
    		arrayDatos = eval(req_ec.responseText);
    		if (arrayDatos.length!=0)
    		{
    			codigoHtml = arrayDatos[0].codigoHtml;
    			codigoHtml = replace(codigoHtml,'</div></center>',mensaje+'</div></center>');
    			codigoHtml = replace(codigoHtml,'javascript:map.panTo(new GLatLng(null,null))','javascript:void(0)');
    			codigoHtml = replace(codigoHtml,'javascript:zoomMaximo(null,null)','javascript:void(0)');
    			efectoBocata(null,codigoHtml,false);
    		}
    		else
    		{
    			mostrarError(mensaje);
    		}
		}
		else
    	{
    		mostrarError(mensaje);
    	}
	}
}
function cerrarCapaError()
{
	document.getElementById("capaError").style.display='none';
	document.getElementById("msgError").innerHTML='';
}
function limpiaBuscador()
{
	for (var i=0;i < document.formulario.elements.length;i++)
	{
		var elemento = document.formulario.elements[i];

		if (elemento.type == "radio" && elemento.checked)
		{
			elemento.checked=false;
			capa = replace(elemento.id,'btn','capa');
			if(document.getElementById(capa))
			{
				document.getElementById(capa).style.display='none';
			}

		}
	}
	estableceParametrosBuscador('');
	quitaBuscador();
	limpia_mapa();
}
function quitaBuscador()
{
	capa_resultados = document.getElementById('resultados_busqueda');
	capa_resultados.innerHTML = '';
}

function loadDatosErrorComercios(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	req_ec=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	req_ec= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req_ec)
    {
    	req_ec.onreadystatechange=procesoReqCambio;
    	req_ec.open("POST",url,true);
    	req_ec.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req_ec.send("id=3");
    }
}

function muestraInfo(tipoEmpresa)
{
	//loadInfo('./include/info.jsp?tipoEmpresa='+tipoEmpresa,mostrarInfoAjax);
	loadInfo('./html/'+tipoEmpresa+'.html',mostrarInfoAjax);
}

function cerrarCapaInfo()
{
	document.getElementById("capaInfo").style.display='none';
	document.getElementById("msgInfo").innerHTML='';
}

function loadInfo(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	req_inf=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	req_inf= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req_inf)
    {
    	req_inf.onreadystatechange=procesoReqCambio;
    	req_inf.open("POST",url,true);
    	req_inf.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req_inf.send("id=3");
    }
}

function mostrarInfoAjax(){
	if(req_inf.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_inf.status==200)
    	{
    		document.getElementById("capaInfo").style.display='block';
    		document.getElementById('msgInfo').innerHTML=req_inf.responseText;
    	}

	}

}

function navegarPaginacion()
{
	//alert("accion=" + arguments[0] + "\nformulario=" + arguments[1] + "\ndestino=" + arguments[2]);
	//var accionFormulario = arguments[0];
	//var nombreFormulario = arguments[1];
	//var paginaDestino = arguments[2];
	var parametros = '';

	for(i=0;i<arguments.length;i++){
		if(i>0){
			parametros += '&';
		}
		parametros += arguments[i];
	}
	loadDatosListadioAjax('busca.form?'+ parametros, tratarDatosListadoAjax);

}

function tratarDatosListadoAjax()
{
	if(reqListado.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(reqListado.status==200)
    	{
			codigoHtml=reqListado.responseText;
    		capa_resultados = document.getElementById('resultados_busqueda');
			capa_resultados.innerHTML = codigoHtml.replace("form","pepe");
			quitarCargandoBuscador();
			quitarCargandoBuscadorInterior();
			window.location='#etiqueta_buscador';
		}
		else
    	{
    		mostrarError("Al calcular el listado");
    	}
	}

}

function  loadDatosListadioAjax(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	reqListado=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	reqListado= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(reqListado)
    {
    	reqListado.onreadystatechange=procesoReqCambio;
    	reqListado.open("POST",url,true);
    	reqListado.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	reqListado.send("id=3");
    }
}

function exportacion()
{
	var parametros = '';

	for(i=0;i<arguments.length;i++){
		if(i>0){
			parametros += '&';
		}
		parametros += arguments[i];
	}

	abreVentanaCentrada('busca.form?'+ parametros);
}

function centarListado(coordenada_x,coordenada_y,nombre,icono,iconWidth,iconHeight,id, fk_cat){
	map.panTo(new GLatLng(coordenada_x,coordenada_y));
    mostrarPunto(nombre,'(\''+coordenada_x+'\',\''+coordenada_y+'\')',icono,iconWidth,iconHeight,id,fk_cat);
    abrir_bocadillo(id);
}

function zoomListado(coordenada_x,coordenada_y,nombre,icono,iconWidth,iconHeight,id, fk_cat){
	zoomMaximo(coordenada_x,coordenada_y);
	mostrarPunto(nombre,'(\''+coordenada_x+'\',\''+coordenada_y+'\')',icono,iconWidth,iconHeight,id,fk_cat);
	abrir_bocadillo(id);
}

//Esta funcion posiciona la ventana popup en el centro de la pantalla
function abreVentanaCentrada()
{
	var url=arguments[0];
	var nombre=arguments[1];
	var anchoVentana=arguments[2];
	var altoVentana=arguments[3];
	var configuracion = arguments[4];
	var anchoPantalla = screen.width;
	var altoPantalla = screen.height;
	var estilo ="";
	var altoVentanaPadre = top.screenTop;
	var anchoVentanaPadre = top.screenLeft;

	if(arguments.length < 3) {
		anchoVentana=screen.width;
		altoVentana=screen.height;
	}

	if(arguments.length <5)
		estilo = "titlebar=no,menubar=no,location=no,status=no,directories=no,toolbar=no,border=no,resizable=yes,width="+anchoVentana+",height="+altoVentana;
	else
		estilo = configuracion + ",width="+anchoVentana+",height="+altoVentana;

	if(anchoPantalla==800 && altoPantalla==600) {
		if (navigator.appName.toLowerCase() == "navigator")
			estilo = estilo + ",screenY=0,screenX=0";
		else
			estilo = estilo + ",top=0,left=0";
	} else {
		if(altoVentanaPadre==0 && anchoVentanaPadre==0) {
			var distanciaArriba = (altoPantalla/2)-(altoVentana/2);
			var distanciaLateral = (anchoPantalla/2)-(anchoVentana/2);
			if (navigator.appName.toLowerCase() == "navigator")
				estilo = estilo + ",screenY="+distanciaArriba+",screenX="+distanciaLateral;
			else
				estilo = estilo + ",top="+distanciaArriba+",left="+distanciaLateral;
		} else {
			if (navigator.appName.toLowerCase() == "navigator") {
				var distanciaArriba = (altoPantalla/2)-(altoVentana/2);
				var distanciaLateral = (anchoPantalla/2)-(anchoVentana/2);
				estilo = estilo + ",screenY="+distanciaArriba+",screenX="+distanciaLateral;
			} else {
/*				var altoVentanaPadre = top.screenTop;
				var anchoVentanaPadre = top.screenLeft;
				var anchuraPadre=top.document.body.offsetWidth;
				var alturaPadre=top.document.body.offsetHeight;
				var distanciaArriba = (altoVentanaPadre+(alturaPadre/2))-(altoVentana/2);
				var distanciaLateral = (anchoVentanaPadre+(anchuraPadre/2))-(anchoVentana/2);*/

				distanciaArriba = (screen.height - altoVentana)/2;
				distanciaLateral = (screen.width - anchoVentana)/2;

				estilo = estilo + ",top="+distanciaArriba+",left="+distanciaLateral;
			}
		}
	}

	return window.open(url,nombre,estilo);
}

