// JavaScript Document
var Globals = {
	initialize: function() {
		Custom.init();
	}
}
window.onload = Globals.initialize;

var selectWidth = "265";

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;

		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				//inputs[a].onchange = Custom.choose;
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}


function mostrarOcultarLugar(origen){

	if (origen=='pestana') {
		//alert("HE ENTRADO EN mostrarOcultarLugar con: "+origen);
		createCookie("tipoBuscador",'lugares',1);
	}

	document.getElementById('lugar').style.display = "block";
	document.getElementById('empresa').style.display = "none";
}

function mostrarOcultarEmpresa(origen){

	if (origen=='pestana') {
		//alert("HE ENTRADO EN mostrarOcultarEmpresa con: "+origen);
		createCookie("tipoBuscador",'empresas',1);
	}

	document.getElementById('lugar').style.display = "none";
	document.getElementById('empresa').style.display = "block";
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function cambiaProvincia(id,tipo) {

	object=document.getElementById(id);
	provincia=object.value;
	nombreProvincia=object.options[object.selectedIndex].text;

	document.getElementById("select"+id).innerHTML=nombreProvincia;

	createCookie("tipoBuscador",tipo,1);

	location.href='/index.htm?bdProvincia='+provincia;
}

function cambiaMoll(id,tipo) {
	object=document.getElementById(id);
	moll=object.value;
	if (moll==0 || moll=="") return;
	nombreProvincia=object.options[object.selectedIndex].text;

	if (tipo!='prehome') document.getElementById("select"+id).innerHTML=nombreProvincia;

	createCookie("tipoBuscador",tipo,1);

	location.href='/templates/buscador_redirige_moll.php?moll='+moll+'&nombreProvincia='+nombreProvincia;
}

function vePlanta(moll) {

	var nowtime = new Date();
	var tiempo = nowtime.getTime();

	planta=document.getElementById('bdBuscadorEmpresaPlanta').value;

	// Llamada al Ajax
	jQuery.ajax( {
		type:"GET", url:"/templates/script_ajax/estadisticasBuscadorEmpresa.php",
		data:"planta="+planta+"&t="+tiempo,
		success: function (msg) {
			location.href='/index.htm?moll='+moll+'&planta='+planta;
		}
	} )


}

function cambiaEspacio(moll,provincia) {

	var nowtime = new Date();
	var tiempo = nowtime.getTime();

	buscadorEmpresas=document.getElementById('bdBuscadorLugaresComoBuscadorEmpresas').value;
	tipoespacio=document.getElementById('bdBuscadorLugarTipoEspacio').value;
	rango=document.getElementById('bdBuscadorLugarRango').value;

	if (buscadorEmpresas==1) {

		// Llamada al Ajax
		jQuery.ajax( {
			type:"GET", url:"/templates/script_ajax/estadisticasBuscadorEmpresa.php",
			data:"planta="+tipoespacio+"&t="+tiempo,
			success: function (msg) {
				//alert(msg);
				//return false;
				location.href='/index.htm?moll='+moll+'&planta='+tipoespacio;
			}
		} )
	}
	// Buscador de Lugares anterior (/templates/cabecera_buscador.php)
	else	{
		location.href='/index.htm?moll='+moll+'&tipoespacio='+tipoespacio+'&buscadorLugares='+provincia+'&precio='+rango;
	}
}

//Buscador columna derecha y album
function tecla(evento,webroot) {

	if (evento.keyCode)
		iAscii = evento.keyCode;
	else if (evento.which)
		iAscii = evento.which;
	else
		return false;
	if (iAscii == 13) buscar('',webroot);
}

// Buscador genérico columna derecha
function buscar(textoAviso,webroot) {

	var nowtime = new Date();
	var tiempo = nowtime.getTime();
        
	busq = escape(document.getElementById('busqueda').value);
        //Nos encargamos de los caracteres raros
        busq = reemplazaCaracteresEspeciales(unescape(busq));

	if (busq=="") {
		alert (textoAviso);
		return;
	}

	// Llamada al Ajax
	jQuery.ajax( {
		type:"GET", url:"/templates/script_ajax/estadisticasBuscadorGenerico.php",
		data:"search="+busq+"&t="+tiempo,
		success: function (msg) {
			location.href='/?search='+busq;
		}
	} )

}

function reemplazaCaracteresEspeciales(texto){
    
    texto = texto.replace(/ñ/i, "n");
    texto = texto.replace(/á/i, "a");
    texto = texto.replace(/é/i, "e");
    texto = texto.replace(/í/i, "i");
    texto = texto.replace(/ó/i, "o");
    texto = texto.replace(/ú/i, "u");
    return texto; 
}

