function abrirFormularioMapas(){
	document.getElementById("formulario_mapas").style.display = "block";
	document.getElementById("texto").style.display = "none";
}
function abrirFormularioRegistro(){
	document.getElementById("formulario_registro").style.display = "block";
	document.getElementById("texto").style.display = "none";
}
function validarFormulario(){
	var errores="";
	if(document.getElementById("nombre").value=="")
		errores +="<p>Debes rellenar tu nombre y apellidos.</p>";
	if(document.getElementById("email").value=="")
		errores +="<p>Debes escribir un email.</p>";
	if(document.getElementById("telefono").value=="")
		errores +="<p>Debes rellenar tu tel&eacute;fono.</p>";
	if(document.getElementById("condiciones").checked==false)
		errores +="<p>Debes leer  y aceptar las condiciones.</p>";
	
	if(errores=="")
		return true;
	else{
		escribirMensaje(errores);
		return false;
		}
}

function cargarMapas(){
	var promocion = document.getElementById("promocion").value;
	var pin = document.getElementById("pin").value;
	
	if(promocion!="" && pin!="" && /^([0-9])+([a-h]|[A-H])+?$/.test(pin) ){//con promocion y pin valido
		ajax=nuevoAJAX();
		ajax.open("GET", "php/mapas.php?promocion="+promocion, true);
		ajax.onreadystatechange=function(){
			if(ajax.readyState==4){
				var contenido = ajax.responseText;
				if(contenido==0){
					escribirMensaje("C&oacute;digo de Promoci&oacute;n y/o PIN incorrectos.");
				}else{
					actualizar(contenido);
					document.getElementById("atesa").style.display = "none";
					document.getElementById("atesa_reg").style.display = "block";
				}
			}
		}
		ajax.send(null);
	}else{
		escribirMensaje("Debe introducir un C&oacute;digo de Promoci&oacute;n y un PIN v&aacute;lido.");
	}
	return false;
}
function actualizar(id){
	var flashvars = {promo:id};
	var params = {};
	var attributes = {};
	params.menu = "false";
	params.wmode = "transparent";
	swfobject.embedSWF("swf/mapas.swf", "mapas", "720", "324", "8.0.0", false, flashvars, params, attributes);
	document.getElementById("formulario_mapas").style.display = "none";
	document.getElementById("mapas").style.display = "block";
	document.getElementById("id_promo").value = id;
}

function escribirMensaje(msg){
	document.getElementById("mensajes").innerHTML="<p>"+msg+"</p><p id='seg'></p>";
	borrarMensajes(5);
}
function borrarMensajes(seg){
	if(seg>0){
		document.getElementById('seg').innerHTML="<em>(borrando en "+seg+" segundos)</em>";
		setTimeout("borrarMensajes("+(--seg)+")", 1000);
	}else
		document.getElementById("mensajes").innerHTML="";
}
function buscarHotel(id){
	var hoteles = document.getElementById("listado_hoteles");
	var promocion = document.getElementById("id_promo");
	ajax=nuevoAJAX();
	ajax.open("POST", "php/hoteles.php", true);
	ajax.onreadystatechange=function(){
		if(ajax.readyState==4 && (ajax.status == 200 || window.location.href.indexOf ("http") == - 1)){
			var contenido = ajax.responseText;
			if(contenido==0){
				escribirMensaje("Ha ocurrido alg&uacute;n error inesperado.");
			}else{
				hoteles.innerHTML = contenido;
				$("#hoteles").tablesorter({headers:{3: {sorter: false}}}); //inhabilitamos la ordenacion por la columna de ampliar info
			}
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("provincia="+id+"&promocion="+promocion.value);
	hoteles.innerHTML = "Cargando informaci&oacute;n...";
}
function ampliarInfo(id){
	var popup = document.getElementById('popup');
	
	popup.innerHTML = document.getElementById(id).innerHTML;
	document.getElementById("fondo_popup").style.display = "block";
	popup.style.display = "block";
}
function cerrarInfoAmpliada(){
	document.getElementById("popup").style.display = "none";
	document.getElementById("fondo_popup").style.display = "none";
}
var idNormal=-1;
function ampliarImagen(idMiniatura, hotel){
	var src_miniatura = document.getElementById("imagen"+idMiniatura+"de"+hotel).src;
	var alt_miniatura = document.getElementById("imagen"+idMiniatura+"de"+hotel).alt;
	var src_normal = document.getElementById("normal").src;
	var alt_normal = document.getElementById("normal").alt;
	document.getElementById("imagen"+idMiniatura+"de"+hotel).src = src_normal;
	document.getElementById("normal").src = src_miniatura;
	document.getElementById("imagen"+idMiniatura+"de"+hotel).alt = alt_normal;
	document.getElementById("normal").alt = alt_miniatura;
}

function verCondiciones(){
	document.getElementById("info_privacidad").style.display = "block";
}
function cerrarCondiciones(){
	document.getElementById("info_privacidad").style.display = "none";
}


//slider fotos
function moverFotos(direccion){
	var pos = parseInt($("#visor_fotos").css("left").replace("px","")) + direccion;
	
	var izquierda = parseInt($('.img_visor:first').offset().left);
	var derecha = parseInt($('.img_visor:last').offset().left);

	if( (direccion < 0) && (izquierda  < -300) ){
		var anchoFaltante = parseInt($('.img_visor:first').width());
		pos = pos + anchoFaltante;
		$("#visor_fotos").append( $('.img_visor:first') );
	}

	if( (direccion > 0) && (derecha > 1100) ){
		var anchoSobrante = parseInt($('.img_visor:last').width());
		pos = pos - anchoSobrante;
		$("#visor_fotos").prepend( $('.img_visor:last') );
	}

	$("#visor_fotos").css("left", pos);
}
function moverIzquierda(){
	clearInterval(intervalVisor);
	intervalVisor = setInterval("moverFotos(-1)", 15);
}
function moverDerecha(){
	clearInterval(intervalVisor);
	intervalVisor = setInterval("moverFotos(1)", 15);
}


//banner
function abrirFormularioBanner(id){
	var popup = document.getElementById('popup');
	
	ajax=nuevoAJAX();
	ajax.open("POST", "php/funciones.php", true);
	ajax.onreadystatechange=function(){
		if(ajax.readyState==4 && (ajax.status == 200 || window.location.href.indexOf ("http") == - 1)){
			var contenido = ajax.responseText;
			if(contenido==0){
				escribirMensaje("Ha ocurrido alg&uacute;n error inesperado.");
			}else{
				popup.innerHTML = contenido;
				document.getElementById("formulario_banner").style.display = "block";
				document.getElementById("fondo_popup").style.display = "block";
				popup.style.display = "block";
			}
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("form_banner="+id);
	
}

function validarFormBanners(arrayCamposObligatorios){


	var errores="";
	var inputs = document.getElementById("div_form_banner").getElementsByTagName("input");


	for(var i=0; i < arrayCamposObligatorios.length; i++){
		var obligatorio = arrayCamposObligatorios[i];
		if(obligatorio != -1){
			if(obligatorio == 1){
				if(inputs[i].value==""){
					errores +="<p>"+inputs[i].title+" es un campo obligatorio.</p>";
				}
			}
		}
	}
	
	if(errores=="")
		return true;
	else{
		document.getElementById("mensajesBanners").innerHTML=errores;
		return false;
	}
}

