var xmlHttp;

function show(id){	
	document.getElementById(id).style.display="inline";
	document.getElementById(id).style.visibility="visible";
}

function hide(id){
	document.getElementById(id).style.display="none";
	document.getElementById(id).style.visibility="hidden";
}

function GetXmlHttpObject(){
  var xmlHttp=null;
  try{xmlHttp=new XMLHttpRequest();
  }catch (e){
    try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
   }
  return xmlHttp;
}

function updateField(fiesta, datos, field){
	var url="changefield.php?fiesta="+fiesta+"&field="+field+"&data="+datos;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;}
	xmlHttp.onreadystatechange=updateFieldHandler;
	xmlHttp.open("GET",encodeURI(url),true);
	xmlHttp.send(null);		
}

function updateFieldHandler(){
	if (xmlHttp.readyState==4){ 
		if(xmlHttp.responseText == "AUTH_ERROR"){
			document.getElementById("info_loading").innerHTML="Error de autenticación";
			setTimeout(document.getElementById("info_loading").innerHTML="", 1000);
		}else if(xmlHttp.responseText == "OK"){		
			document.getElementById("info_loading").innerHTML="Campo actualizado";
			setTimeout(document.getElementById("info_loading").innerHTML="", 1000);
		}else{
			document.getElementById("info_loading").innerHTML="Error desconocido";
			setTimeout(document.getElementById("info_loading").innerHTML="", 1000);
		}
	}else{
		document.getElementById("info_loading").innerHTML="<img src='img/load.gif'/><br/>Actualizando...";
	}	
}

function performSearch(idioma){
	var provincia = document.getElementById("select_provincia").value;
	var precio = document.getElementById("form_precio").value;
	var municipio = document.getElementById("select_municipio").value;
	var tipo_local = document.getElementById("local").value;
	var espectaculo = document.getElementById("espectaculo").value;
	var cena = document.getElementById("cena").value; 
	var url = "searchFiesta.php?idioma="+idioma+"&cena="+cena+"&provincia="+provincia+"&precio="+precio+"&municipio="+municipio+"&tipo_local="+tipo_local+"&espectaculo="+espectaculo;
	if (document.getElementById("select_barrio") != null){
		var barrio = document.getElementById("select_barrio").value;
		url = url + "&barrio="+barrio;
	}		
//alert(url);
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;}
	xmlHttp.onreadystatechange=performSearchHandler;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function performSearchHandler(){
	if (xmlHttp.readyState==4){ 
		//$("#nueva_busqueda").slideDown(1000);
		$("#resultados_busqueda").slideDown(1000).html(xmlHttp.responseText)
	}else{
		document.getElementById("resultados_busqueda").innerHTML="<img src='img/load.gif'/><br/>Cargando resultados...";
	}
}

function searchAgain(){
	//$("#nueva_busqueda").slideUp(1000);
	$("#resultados_busqueda").slideUp(1000);

}

function loadMunicipio(id_provincia, indiferente){
	if(id_provincia==-1) return;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;} 
	var url="listMunicipios.php?provincia="+id_provincia;
	if (indiferente==1) url = url+"&indiferente";
	xmlHttp.onreadystatechange=loadMunicipioHandler;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function loadMunicipioHandler(){ 
	if (xmlHttp.readyState==4){ 
		document.getElementById("bloque_municipio").style.display="inline";
		document.getElementById("bloque_municipio").innerHTML=xmlHttp.responseText;
	}else{
		document.getElementById("bloque_municipio").innerHTML="<option><img src='img/load.gif'/>Cargando...</option>";
	}
}

function loadBarrios(id_municipio){
	if(id_municipio==-1) return;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;} 
	var url="listBarrios.php?municipio="+id_municipio;
	xmlHttp.onreadystatechange=loadBarriosHandler;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function loadBarriosHandler(){ 
	if (xmlHttp.readyState==4){ 
		if(xmlHttp.responseText == "ERROR"){
			document.getElementById("bloque_barrio").innerHTML='<span class="soft">No hay zonas disponibles en el municipio seleccionado</span>';
		}else{
			document.getElementById("bloque_barrio").style.display="inline";
			document.getElementById("bloque_barrio").innerHTML=xmlHttp.responseText;
		}
	}else{
		document.getElementById("bloque_barrio").innerHTML='<img src="img/load.gif"/><br/>Cargando...';
	}
}

function validar(f) {
	var ok = true;
	if (f.nombre_local.value.length < 4) {
		alert("Escriba por lo menos 4 caracteres en el campo \"Nombre local\".");
		f.nombre_local.focus();
		ok=false;
	}
	if (f.provincia.value == -1 || f.municipio.value < 0){
		alert("Debe seleccionar la provincia y el municipio a los que pertenecen su fiesta");
		f.provincia.focus();
		ok=false;
	}
	
	if (isNaN(parseInt(f.precio_anticipada.value)) || isNaN(parseInt(f.precio_puerta.value))) {
		alert("El precio debe ser un número. Si es de entrada libre ingrese un 0.");
		f.precio_anticipada.focus();
		ok=false;
	}
/*	if (isNaN(parseInt(f.superficie.value)) || parseInt(f.superficie.value)<0) {
		alert("La superficie debe ser un número. Si la desconoce ingrese un 0.");
		f.superficie.focus();
		ok=false;
	}
	if (isNaN(parseInt(f.aforo.value)) || parseInt(f.aforo.value)<0) {
		alert("El aforo debe ser un número. Si lo desconoce ingrese un 0.");
		f.aforo.focus();
		ok=false;
	}   */
	if (ok==true) f.submit();
}

