//<![CDATA[

var comercio_id = 0;

function initPage() {
   document.getElementById('nombre_com').focus();
}

function cargando(flag) {
   if (flag == true)
      document.getElementById("loading").style.display = "block";
   else
      document.getElementById("loading").style.display = "none";
}

function buscarComercios(first, no_map, lang) {
    cargando(true);
	selectTab("tab_comercios");
	comercio_id = 0;
	var sel = document.getElementById('categoria');
	var cat = sel.options[sel.selectedIndex].value;
	sel = document.getElementById('direccion');
	var dir = sel.options[sel.selectedIndex].value;
	var nom = document.getElementById('nombre_com').value;
	var url = "buscar.php?first="+first+"&cat="+cat+"&via="+dir+"&nombre="+nom;
	if (lang)
	   url += "&lang="+lang;
	GDownloadUrl(url, function(data, responseCode) {
      map.clearOverlays();
	  document.getElementById("result").innerHTML = data;
	  cargando(false);
	});
	if (no_map != true)
       contentMode("map");
	return false;
}

function comerciosDeCategoria(cat, first, lang) {
    document.forms[0].reset();
    cargando(true);
	selectTab("tab_comercios");
	comercio_id = 0;
	GDownloadUrl("buscar.php?first="+first+"&cat="+cat+"&lang="+lang, function(data, responseCode) {
      map.clearOverlays();
	  document.getElementById("result").innerHTML = data;
	  cargando(false);
	});
	return false;
}

function comerciosDeVia(dir, first, lang) {
    document.forms[0].reset();
    cargando(true);
	selectTab("tab_comercios");
	comercio_id = 0;
	GDownloadUrl("buscar.php?first="+first+"&via="+dir+"&lang="+lang, function(data, responseCode) {
      map.clearOverlays();
	  document.getElementById("result").innerHTML = data;
	  cargando(false);
	});
	return false;
}

function listarCategorias(lang) {
    cargando(true);
	GDownloadUrl("categorias.php?lang="+lang, function(data, responseCode) {
      map.clearOverlays();
	  document.getElementById("result").innerHTML = data;
	  cargando(false);
	});
	selectTab("tab_categorias");
	return false;
}

function listarVias(lang) {
    cargando(true);
	GDownloadUrl("vias.php?lang="+lang, function(data, responseCode) {
      map.clearOverlays();
	  document.getElementById("result").innerHTML = data;
	  cargando(false);
	});
	selectTab("tab_vias");
	return false;
}

function updateLatLng(lat,lng)
{
	var id = comercio_id;
	GDownloadUrl("posicion.php?id="+comercio_id+"&lat="+lat+"&lng="+lng, function(data, responseCode) {
	  // document.getElementById("result").innerHTML = data;
	  document.getElementById("icon"+comercio_id).onclick = function() { return selectAndCenter(id,lat,lng); };
	  document.getElementById("title"+comercio_id).onclick = function() { return seleccionarComercio(id,lat,lng); };
	});
}

function selectTab(id)
{
	document.getElementById("tab_comercios").className = "";
	document.getElementById("tab_categorias").className = "";
	document.getElementById("tab_vias").className = "";
    sel = document.getElementById(id);
	sel.className = "selected";
	sel.blur();
}

function showDetails(id,lang)
{
   fillDetails(id,lang);
   contentMode("detail");
   return false;
}

function crearComercio()
{
   seleccionarComercio(0,0,0);
   contentMode("detail");
   return false;
}

function editarCategoria(id)
{
    if (!id) id = 0;
    sinSeleccion();
    contentMode("detail");
	GDownloadUrl("categoria.php?id="+id, function(data, responseCode) {
	  document.getElementById("detail").innerHTML = data;
	});
   return false;
}

function fillDetails(id,lang)
{
	GDownloadUrl("comercio.php?id="+id+"&lang="+lang, function(data, responseCode) {
	  document.getElementById("detail").innerHTML = data;
	});
}

function goAndCenter(lat,lng)
{
   contentMode("map");
   map.setCenter(getLatLng(lat,lng));
   return false;
}

function selectAndCenter(id,lat,lng,lang)
{
   fillDetails(id,lang);
   goAndCenter(lat,lng);
   return false;
}

function sinSeleccion() {
   if (comercio_id > 0) {
      var icon = document.getElementById("icon"+comercio_id);
      if (icon)
         icon.style.display = "none";
   }
   map.clearOverlays();
   comercio_id = 0;
}

function seleccionarComercio(id,lat,lng)
{
   if ((comercio_id > 0) && (id == comercio_id))
       contentMode("detail");
   else {
	   sinSeleccion();
	   if (id > 0) {
		   document.getElementById("icon"+id).style.display = "inline";
		   showMarker(id,lat,lng);
           map.setCenter(getLatLng(lat,lng));
	   }
	   fillDetails(id);
	   comercio_id = id;
   }
   return false;
}

function contentMode(mode_id)
{
   if (mode_id == "detail") {
      document.getElementById("tab_map").className = "";
      var sel = document.getElementById("tab_detail");
      sel.className = "selected";
      sel.blur();
      document.getElementById("map").style.display = "none";
      document.getElementById("zoom").style.display = "none";
      document.getElementById("detail").style.display = "block";
   }
   else {
      document.getElementById("tab_detail").className = "";
      var sel = document.getElementById("tab_map");
      sel.className = "selected";
      sel.blur();
      document.getElementById("detail").style.display = "none";
      document.getElementById("zoom").style.display = "block";
      document.getElementById("map").style.display = "block";
   }
}

function ajustarVia(lista) {
   var val = lista.options[lista.selectedIndex].value;
   var via = document.getElementById('via');
   via.value = val;
   if (val == "") {
      via.style.display = "inline";
      via.focus();
   }
   else
      via.style.display = "none";
}

function confirmar(msg,url) {
   if (confirm(msg))
      frames['iframeUpdate'].location.href = url;
}

function logoff() {
   location.href = "logoff.php";
}

//]]>
