
var peticion;

var peticionBanners;

var codProv = 0;

var indexProv = 0;

var indexPoblacion = 0;

var idpais = 1;

var provSelec = false;

function recargaProvincias() {
	codProv = 0;
	indexProv = 0;
	recargaProvincias1();
}

function recargaProvincias1() {
	vacialstPoblaciones();
	var cmboPaises = document.getElementById('lstPaises');
	var codPais = cmboPaises.options[cmboPaises.selectedIndex].value;
	
	idpais = codPais;
	//document.getElementById('lstProvincias').onchange = recargaPoblaciones1;
	
	if (window.XMLHttpRequest) {
		peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	peticion.open("GET", "/ATREA/public/datosCombo.jsp?tipoDatos=provincias&colectivo=" + marca_blanca + "&codPais=" + codPais +"&grupoInm=-1", true);
	peticion.onreadystatechange = recargaProvincias2;
	peticion.send(null);
}
function recargaProvincias2() {
	var combo = document.getElementById('lstProvincias');
	if (peticion.readyState == 4) {
		if (peticion.status == 200) {
			//  Borrar el contenido del combo
			var len = Number(combo.length) - 1;
			for (k=len; k>0; k--) {
				combo.options[k] = null;
			}
			combo.options[0].value = "-1";
			if (ilang == 3) {
				combo.options[0].text = "-distrito-";
			} else  if (ilang == 1) {
				combo.options[0].text = "-province-";
			} else {
				combo.options[0].text = "-provincia-";
			}
			combo.selectedIndex=0;

			opciones = peticion.responseXML.getElementsByTagName("opcion");
			if (opciones != null) {
				n = opciones.length;
				i = 0;
				while (i < n) {
					valor = opciones[i].getElementsByTagName("valor")[0].childNodes[0].nodeValue;
					texto = opciones[i].getElementsByTagName("texto")[0].childNodes[0].nodeValue;
					combo.length=(i+2);
					combo.options[i+1].value = valor;
					combo.options[i+1].text = texto;
					if (valor == codProv) combo.selectedIndex = i+1;
					i++;
				}
			}
			if (codProv != 0) {
				validarCampos();
			} else if (indexProv != 0) {
				combo.selectedIndex = indexProv;
				recargaPoblaciones1();
			} else {
				if (idpais == 2) {
					document.getElementById('lstPoblaciones').options[0].text = "-concelho-";
				} else if (ilang == 3) {
					document.getElementById('lstPoblaciones').options[0].text = "-freguesia-";
				} else {
					document.getElementById('lstPoblaciones').options[0].text = "-población-";
				}
			}
		}
	}
}

function recargaPoblaciones1() {
	var cmboProv = document.getElementById('lstProvincias');
	if (cmboProv.selectedIndex < 0) cmboProv.selectedIndex = 0;
	var codProv = cmboProv.options[cmboProv.selectedIndex].value;
	if (window.XMLHttpRequest) {
		peticion = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		peticion = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (idpais == 2) {
		peticion.open("GET", "/ATREA/public/datosCombo.jsp?tipoDatos=zonas&cargaTodo=NO&colectivo=" + marca_blanca + "&codProv=" + codProv + "&grupoInm=-1", true);
	} else {
		peticion.open("GET", "/ATREA/public/datosCombo.jsp?tipoDatos=poblaciones&colectivo=" + marca_blanca + "&codProv=" + codProv + "&grupoInm=-1", true);
	}
	peticion.onreadystatechange = recargaPoblaciones2;
	
	peticion.send(null);
}
function vacialstPoblaciones () {
	combo = document.getElementById('lstPoblaciones');
	//  Borrar el contenido del combo
	var len = Number(combo.length) - 1;
	for (k=len; k>0; k--) {
		combo.options[k] = null;
	}
	combo.options[0].value = "-1";
	if (idpais == 1 || idpais == 3) {
		if (ilang == 3) {
			combo.options[0].text = "-freguesia-";
		} else {
			combo.options[0].text = "-población-";
		}
	} else {
		if (ilang == 3) {
			combo.options[0].text = "-concelho-";
		} else {
			combo.options[0].text = "-concelho-";
		}
	}
	combo.selectedIndex = 0;
}
function recargaPoblaciones2() {
	combo = document.getElementById('lstPoblaciones');
	if (peticion.readyState == 4) {
		if (peticion.status == 200) {
			vacialstPoblaciones();

			opciones = peticion.responseXML.getElementsByTagName("opcion");
			if (opciones != null) {
				n = opciones.length;
				i = 0;
				while (i < n) {
					valor = opciones[i].getElementsByTagName("valor")[0].childNodes[0].nodeValue;
					texto = opciones[i].getElementsByTagName("texto")[0].childNodes[0].nodeValue;
					combo.length=(i+2);
					combo.options[i+1].value = valor;
					combo.options[i+1].text = texto;
					if (document.f.cod_muni.value == valor) combo.selectedIndex = i+1;
					i++;
				}
			}
		}
	}
}

// 99 -> cod_provinci ficticio para redirigir a mapa de portugal

function validarSeleccion(id) {
	if (id != 99) {
		provSelec = true;	
		loadMunis(id);
	} else {
		irApagina(2);
	}
}

var grupo;
var uso;
var tipooferta;

function irApagina(apartado) {
	document.location.replace("http://www.atrea.com/ATREA/public/proceso.jsp?metodo=get&pgFrom=1017&linkFrom=" + apartado + "&url=/ATREA/public/A3/central.jsp?marca_blanca=" + marca_blanca + "&pgFrom=1017&apartado=" + apartado);
}
var entrada = true;

function loadMunis(codProvTmp) {
	var pais = 0;
	if (codProvTmp >= 60) pais=1;
	var obj = document.f.lstPaises;
	if (obj.selectedIndex != pais) {
		// Hay que recargar el combo de provincias
		obj.selectedIndex = pais;
		codProv = codProvTmp;
		indexProv = 0;
		recargaProvincias1();
		document.f.lstPoblaciones.selectedIndex=0;
	} else {
		var objProvincias = document.f.lstProvincias;
		var nProvs = objProvincias.options.length;
		for (iProv = 0; iProv < nProvs; iProv++) {
			if (objProvincias.options[iProv].value == codProvTmp) {
				objProvincias.selectedIndex=iProv;
				break;
			}
		}
		document.f.lstPoblaciones.selectedIndex=0;
		validarCampos();
	}
}

function validarCampos(){

	document.f.grupo.value=damegrupo();

	if (document.f.selTipoOperacion.selectedIndex == 0) { // Comprar 
		document.f.cod_tipo_oferta.value="1";
	} else if (document.f.selTipoOperacion.selectedIndex == 1) { // Alquilar 
		document.f.cod_tipo_oferta.value="2";
	} else if (document.f.selTipoOperacion.selectedIndex == 2) { // Obra Nueva 
		document.f.cod_tipo_oferta.value="3";
		document.f.tipoBusqueda.value="obranueva";
	}
	var grupo = document.f.selGrupoTipoInmueble.options[document.f.selGrupoTipoInmueble.selectedIndex].value;
	if (grupo == "10") { // Obra Nueva
		document.f.grupo.value=0;
		document.f.cod_tipo_oferta.value="3";
		document.f.tipoBusqueda.value="obranueva";
	} else if (grupo == "11") { // Segunda Mano
		document.f.grupo.value=0;
		document.f.tipoBusqueda.value="segundamano";
	} else if (grupo == "12") { // Todo
		document.f.grupo.value=0;
		document.f.tipoBusqueda.value="todo";
	} else if (grupo == "13") { // Plaza de Garaje
		document.f.grupo.value=6;
		document.f.tipo.value="50&Plaza_de_garaje";
		document.f.tipoBusqueda.value="segundamano";
	}
	

	document.f.cod_pais.value = idpais;
	document.f.cod_prov.value=document.f.lstProvincias.options[document.f.lstProvincias.selectedIndex].value;
	if (document.f.lstPoblaciones.selectedIndex < 0) document.f.lstPoblaciones.selectedIndex = 0;
	if (idpais == 2) {
		document.f.cod_muni.value="-1";
		document.f.zona.value=document.f.lstPoblaciones.options[document.f.lstPoblaciones.selectedIndex].value;
	} else {
		document.f.cod_muni.value=document.f.lstPoblaciones.options[document.f.lstPoblaciones.selectedIndex].value;
		document.f.zona.value="-1";
	}
	document.f.cod_barrio.value=document.f.lstBarrios.options[document.f.lstBarrios.selectedIndex].value;
	if(document.f.lstProvincias.options[document.f.lstProvincias.selectedIndex].value==-1){
		if (provSelec) {
			alert(txtNoHayOferta);
			provSelec = false;
		} else {
			alert(txtSelProvincia);
		}
		return;
	}

	mostrarProcesando("A3");
	document.f.submit();
}

function damegrupo(){
	var grupo;
	grupo = document.f.selGrupoTipoInmueble.options[document.f.selGrupoTipoInmueble.selectedIndex].value;
	if (grupo >= 10) grupo = 0;
	return(grupo); 
}

function warnInvalid (f, msg) {
	f.focus(); 
	f.select();
	alert(msg);
	return false;
}

function isEmpty(s) {return ((s	== null) || (s.length == 0));}

function isWhitespace(s) {	
	var whitespace = " \\t\\n\\r";
	if (isEmpty(s))	return true;
	for (var i=0; i	< s.length; i++){
			var c =	s.charAt(i);
			if (whitespace.indexOf(c) == -1) return false;
		}
		return true;
}


function Popup(ayuda) { 
	var msg = window.open('/ATREA/public/aviso.jsp?ayuda='+ayuda+'&alto=380&ancho=400','gl', 'status=no,scrollbars=no,resizable=no,width=400,height=400,top=150,left=150');
	msg.focus();
}

var AreasConOferta = [];

function recargaBanners() {
	if (window.XMLHttpRequest) {
		peticionBanners = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		peticionBanners = new ActiveXObject("Microsoft.XMLHTTP");
	}
	peticionBanners.open("GET", "/ATREA/public/datosBanners.jsp?iPagina=" + iPagina + "&iCodProv=0&marca_blanca=" + marca_blanca +
								"&pgFrom=" + iPgFrom + "&idioma=" + idioma, true);
	peticionBanners.onreadystatechange = setBanners;
	peticionBanners.send(null);
}

function setBanners() {

	if (peticionBanners.readyState == 4) {
		if (peticionBanners.status == 200) {
			banners = peticionBanners.responseXML.getElementsByTagName("banner");

			if (banners != null) {
				n = banners.length;
				i = 0;
				while (i < n) {
					id = banners[i].getElementsByTagName("idbanner")[0].childNodes[0].nodeValue;
					img = banners[i].getElementsByTagName("img")[0].childNodes[0].nodeValue;
					idenlace = banners[i].getElementsByTagName("idenlace")[0].childNodes[0].nodeValue;
					nueva_ventana = banners[i].getElementsByTagName("nueva_ventana")[0].childNodes[0].nodeValue;

					if (banners[i].getElementsByTagName("enlace")[0].childNodes[0]) {
						enlace1 = '<a href=\"/ATREA/public/proceso.jsp?metodo=get&pgFrom=' + iPagina + '&linkFrom=' + idenlace + '&url=' + banners[i].getElementsByTagName("enlace")[0].childNodes[0].nodeValue + '&marca_blanca=A3\"';
						if (nueva_ventana == 1) {
							enlace1 += ' target=_blank>';
						} else {
							enlace1 += '>';
						}
						enlace2 = '<\/a>';
					} else {
						enlace1 = "";
						enlace2 = "";
					}

					if (banners[i].getElementsByTagName("piefoto")[0].childNodes[0])
						piefoto = banners[i].getElementsByTagName("piefoto")[0].childNodes[0].nodeValue;
					else
						piefoto = "";

					ancho = banners[i].getElementsByTagName("ancho")[0].childNodes[0].nodeValue;
					alto = banners[i].getElementsByTagName("alto")[0].childNodes[0].nodeValue;
					divbannerImg = document.getElementById('banner' + id + 'Img');

					if (divbannerImg) {
						divbannerImg.innerHTML = enlace1 + '<img src=\"/ATREA/banners/' + img + '"\" width=\"' + ancho + '\" height=\"' + alto + '\" border=\"0\" alt=\"\">' + enlace2;
					}

					divbannerTxt = document.getElementById('banner' + id + 'Txt');
					if (divbannerTxt) {
						divbannerTxt.innerHTML = '<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign="top" align="center" style=\"FONT-SIZE: 12px; FONT-FAMILY: trebuchet ms,Verdana, Arial, Helvetica, sans-serif; COLOR: #303030; line-height: 110%;\">' + piefoto + '<\/td><\/tr><\/table>';

					}

					i++;
				}
			}
		}
	}
}

marqueestep=-3;
marqueepos = 0;
timerDown="";

function scrollMarquee(){
el=document.getElementById("inmarquee");
clearTimeout(timerDown);
marqueepos += marqueestep;
el.style.left = marqueepos + "px";
timerDown=setTimeout("scrollMarquee()",200);
if (marqueepos < -2700) marqueepos = 0;
}

function inicializa() {
	setFecha();
	indexProv = document.f.lstProvincias.selectedIndex;
	document.f.lstProvincias.selectedIndex = -1;
	codProv = 0;
	recargaProvincias1();
	recargaBanners();
	scrollMarquee();
}
