function nuevoAjax() {
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp = false;
	try {
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {
		try {
			// Creacion del objet AJAX para IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E) {
			if (!xmlhttp && typeof XMLHttpRequest != 'undefined') xmlhttp = new XMLHttpRequest();
		}
	}
	return xmlhttp;
}

function sessionOrdenarMostrar() {
	ordenar = document.getElementById('ordenar').value;
	mostrar = document.getElementById('mostrar').value;

	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=om&ordenar=" + ordenar + "&mostrar=" + mostrar);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			//alert("Datos guardados correctamente");
			//window.close();
			//msgDatosGuardados();
			location.href = location.href;
		}
	}
	ajax.send(null);
}

function cargarLocalidades(id_localidad) {
	tipo = document.getElementById('inmueble').value;

	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=cl&tipo=" + tipo);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			localidad = document.getElementById('localidad');
			localidad.length = 0;

			localidad.length++;
			localidad.options[0].value = '';
			localidad.options[0].text = 'Cualquier localidad';
			if (ajax.responseText != '') {
				arrayLocalidades = ajax.responseText.split(";");

				cont = 1;
				for (i=0;i<arrayLocalidades.length;i=i+2) {
					localidad.length++;
					localidad.options[cont].value = arrayLocalidades[i];
					localidad.options[cont].text = arrayLocalidades[i+1];
					//alert(id_localidad + ' - ' + arrayLocalidades[i]);
					if (id_localidad != '' && id_localidad == arrayLocalidades[i]) localidad.options[cont].selected = true;
					cont++;
				}
			}
		}
	}
	ajax.send(null);
}

function seleccionarModalidad(id_modalidad) {
	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=sm&id_modalidad=" + id_modalidad);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			location.href = 'inmuebles.php';
		}
	}
	ajax.send(null);
}

function agregarFavoritos(id_inmueble) {
	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=af&id_inmueble=" + id_inmueble);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				document.getElementById('tus_favoritos').innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);
}

function eliminarFavoritos(id_inmueble) {
	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=ef&id_inmueble=" + id_inmueble);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				document.getElementById('tus_favoritos').innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);
}

function ampliarFoto(id_foto) {
	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=mf&id_foto=" + id_foto);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				document.getElementById('visor_imagenes').innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);
}

function ampliarVideo(id_video,video) {
	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=av&id_video=" + id_video);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			if (ajax.responseText != '') {
				document.getElementById('visor_imagenes').innerHTML = ajax.responseText;
				var s1 = new SWFObject("mediaplayer.swf","mediaplayer",'400','300','8');
				s1.addParam("allowfullscreen","true");
				s1.addVariable("enablejs","true");
				s1.addVariable("width","400");
				s1.addVariable("height","300");
				s1.addVariable("wmode","opaque");
				s1.addVariable("file","flv/inmuebles/" + video);
				s1.addVariable("image","img/fondovideo.jpg");
				s1.write("player");
			}
		}
	}
	ajax.send(null);
}

function activarDesactivarMapa(opcion) {
	var ajax = nuevoAjax();
	ajax.open("GET","inc/web/funciones.ajax.php?func=adm&opcion=" + opcion);
	ajax.onreadystatechange=function() {
		if (ajax.readyState == 4) {
			//alert(ajax.responseText);
			location.reload();
		}
	}
	ajax.send(null);
}

function filtroBusquedaSEO() {
	inmueble = document.getElementById('inmueble').value;
	localidad = document.getElementById('localidad').value;

	if (inmueble != '' || localidad != '') {
		var ajax = nuevoAjax();
		ajax.open("GET","inc/web/funciones.ajax.php?func=fbs&inmueble=" + inmueble + "&localidad=" + localidad);
		ajax.onreadystatechange=function() {
			if (ajax.readyState == 4) {
				//alert(ajax.responseText);
				//document.getElementById('frm_filtros').action = ajax.responseText;
				//document.getElementById('frm_filtros').submit();
				location.href = ajax.responseText;
				//location.reload();
			}
		}
		ajax.send(null);
	}
	else {
		location.href = 'buscar-cualquier-inmueble-en-cualquier-localidad';
	}
}

function filtroBusquedaCriterioSEO() {
	busqueda = document.getElementById('buscar').value;
	inmueble = document.getElementById('inmueble').value;
	localidad = document.getElementById('localidad').value;

	if (busqueda != '') {
		var ajax = nuevoAjax();
		ajax.open("GET","inc/web/funciones.ajax.php?func=fbcs&busqueda=" + busqueda + "&inmueble=" + inmueble + "&localidad=" + localidad);
		ajax.onreadystatechange=function() {
			if (ajax.readyState == 4) {
				//alert(ajax.responseText);
				//document.getElementById('frm_filtros').action = ajax.responseText;
				//document.getElementById('frm_filtros').submit();
				document.getElementById('frm_buscar').action = ajax.responseText;
				document.getElementById('frm_buscar').submit();
				//location.reload();
			}
		}
		ajax.send(null);
	}
}