function carga_campo_zona(buscador){
  valor = document.getElementById('restaurante_filtro'+buscador+'_provincia_id').value;
  document.getElementById('campo_zona'+buscador).style.display = '';
  document.getElementById('mapa_zona'+buscador).innerHTML = '&nbsp;<a href="#" onclick="crea_mapa_zona('+valor+', \''+buscador+'\');">Ver mapa</a>';
}

function crea_mapa_zona(mapa,buscador){
  generateCoverDiv('recubrediv', '#000000', 75);
  $j('<div id=\'zona_mapa\'></div>').appendTo('#main');
  $j('#zona_mapa').html('<span style=\'color: white; font-weight: bold;\'>Un momento por favor...</span>');
  $j.get('/zonas/mapa', { provincia_id: mapa, buscador: buscador }, function(data) { $j('#zona_mapa').html(data); }, 'html');
}

function cambia_zona(provincia, zona, buscador){
  $j.get('/zonas/filtrado', { provincia_id: provincia, seleccionado: zona }, function(data) { $j('#restaurante_filtro'+buscador+'_zona_id').html(data); }, 'html');
  cierra_mapa_zona();
}

function cierra_mapa_zona(){
  $j('#zona_mapa').html('');
  borracapa('recubrediv');
}

function pais_modificado(){
  valor = document.getElementById('restaurante_filtro_pais_id').value; if (valor == '1') document.getElementById('campo_provincia').style.display = ''; else document.getElementById('campo_provincia').style.display = 'none'; document.getElementById('campo_zona').style.display = 'none'; document.getElementById('restaurante_filtro_provincia_id').value = '';
}

// Enload indica si es la primera carga de la web y no hay que modificar el _localidad porque sólo es una comprobación para saber si mostrar el mapa
function provincia_modificada(enload, buscador){
  if (!buscador) buscador = '';
  valor = document.getElementById('restaurante_filtro'+buscador+'_provincia_id').value;
  if (enload) document.getElementById('restaurante_filtro'+buscador+'_localidad').value = '';
  if ((valor == '46') || (valor == '28') || (valor == '8')) carga_campo_zona(buscador); else document.getElementById('campo_zona'+buscador).style.display = 'none';
  $j.get('/zonas/filtrado', { provincia_id: valor }, function(data) { $j('#restaurante_filtro'+buscador+'_zona_id').html(data); }, 'html');
}

function localidad_modificada(){
  valor = document.getElementById('restaurante_filtro_localidad').value.toLowerCase(); 
  if ((valor == 'valencia') || (valor == 'barcelona') || (valor == 'madrid')){
    document.getElementById('campo_zona').style.display = '';
    $j('#restaurante_filtro_localidad'); $j.get('/zonas/filtrado', { localidad: valor }, function(data) { $j('#restaurante_filtro_zona_id').html(data); }, 'html');
  } else {
    if (valor == '') {
      provincia_modificada(false, ''); 
    } else {
      document.getElementById('restaurante_filtro_zona_id').value = '';
      document.getElementById('campo_zona').style.display = 'none';
    }
  }
}

function generateCoverDiv(id, color, opacity){
  var navegador=1;
  if(navigator.userAgent.indexOf("MSIE")>=0) navegador=0;
  var layer=document.createElement('div');
  layer.id=id;
  //layer.style.width=document.body.offsetWidth+'px';
  //layer.style.height=document.body.offsetHeight+'px';
  layer.style.width='100%';
  layer.style.height='200%';
  layer.style.backgroundColor=color;
  layer.style.position='absolute';
  layer.style.top=0;
  layer.style.left=0;
  layer.style.zIndex=100;
  if(navegador==0) layer.style.filter='alpha(opacity='+opacity+')';
  else layer.style.opacity=opacity/100;
  document.getElementById('main').appendChild(layer);
  document.getElementById(id).onclick=cierra_mapa_zona;
}

function borracapa(divID) {
  var Node1 = document.getElementById('main'); 
  var len = Node1.childNodes.length;

  for(var i = 0; i < len; i++){           
    if(Node1.childNodes[i].id == divID){
      Node1.removeChild(Node1.childNodes[i]);
    }
  }
}

function activa_direccion(buscador){
  if (!buscador) buscador = '';
  valor = document.getElementById('restaurante_filtro'+buscador+'_provincia_id').value;
  if (valor != 0) {
    direccion = document.getElementById('restaurante_filtro'+buscador+'_direccion');
    direccion.disabled = false;
    direccion.value = '';
  } else {
    direccion.disabled = true;
    direccion.value = 'Selecciona una provincia';
  }
}

var $j = jQuery.noConflict();
