function getIdCuerpo() {
	var elementos=document.getElementsByTagName('div');
	return elementos[0].id
	
	/*
	if(document.getElementById("z_contenido_index"))
		return "z_contenido_index";	
	if(document.getElementById("z_contenido_comunidad"))
		return "z_contenido_comunidad";
	if(document.getElementById("z_contenido_listado"))
		return "z_contenido_listado";			
	if(document.getElementById("z_contenido_insertar_anuncio"))
		return "z_contenido_insertar_anuncio";
	if(document.getElementById("z_contenido_crear_alerta"))
		return "z_contenido_crear_alerta";		
	if(document.getElementById("z_contenido_crear_tienda"))
		return "z_contenido_crear_tienda";
	if(document.getElementById("z_contenido_editar_anuncio"))
		return "z_contenido_editar_anuncio";				
	if(document.getElementById("z_contenido_editar_tienda"))
		return "z_contenido_editar_tienda";				
	if(document.getElementById("z_contenido_ver_anuncio"))
		return "z_contenido_ver_anuncio";
	if(document.getElementById("z_contenido_mas_buscados"))
		return "z_contenido_mas_buscados";		
	if(document.getElementById("z_contenido_administrar_tienda"))
		return "z_contenido_administrar_tienda";		
	if(document.getElementById("z_contenido_administrar_tienda"))
		return "z_contenido_administrar_tienda";
	*/
}

function adjustLayout()  
{  
id_cuerpo=getIdCuerpo();
var lHeight2 = xHeight(id_cuerpo);
var rHeight2 = xHeight("z_sombra_lat_dcho");  
var maxHeight2 = Math.max(lHeight2, rHeight2);  
xHeight("z_sombra_lat_dcho", maxHeight2);  
/*	id_cuerpo=getIdCuerpo();
	// Get natural heights  
	var lHeight = xHeight("contenido_col_izq");  
	var rHeight = xHeight("contenido_ultimos_anuncios");  

	var lHeight2 = xHeight(id_cuerpo);  
	var rHeight2 = xHeight("z_sombra_lat_dcho");  
 
	// Find the maximum height  
	var maxHeight = Math.max(lHeight, rHeight);  
	var maxHeight2 = Math.max(lHeight2, rHeight2);  
	
	// Assign maximum height to all columns   
	xHeight("contenido_col_izq", maxHeight);  
	xHeight("contenido_ultimos_anuncios", maxHeight);  

	xHeight(id_cuerpo, maxHeight2);  
	xHeight("z_sombra_lat_dcho", maxHeight2);  
*/
}

function ajustarSombra() {
	var altoActual = xHeight("z_contenido_sin_sombra_ficha_tienda");
	xHeight("z_sombra_lat_dcho", altoActual);  
	//alert("hecho "+altoActual);
}

function ajustarSombraInsertarAnuncio() {
	var altoActual = xHeight("z_contenido_sin_sombra_insertar_anuncio");
	xHeight("z_sombra_lat_dcho", altoActual);  	
}

window.onload = function()  
{  
	/*xAddEventListener(window, "resize", adjustLayout, false);*/
	adjustLayout();	
}

function mostrar_provincia( id ){
	var provincia_actual = document.getElementById("provincia_mapa");
	var provincia_texto_actual = document.getElementById("prov_texto_" + id);
		document.getElementById("provincia_mapa").style.backgroundImage = "url('imagenes_css/mapa/prov_"+ id +".gif')";
		/*alert ('url(imagenes_css/mapa/prov_'+ id + '.gif)');*/
		document.getElementById("provincia_mapa").style.visibility = 'hidden';
		setTimeout('return',500);
		document.getElementById("provincia_mapa").style.visibility = 'visible';
		if (provincia_texto_actual != null){
		provincia_texto_actual.style.color = '#0064C8';
		}
	return true;
}

function ocultar_provincia( id ){
	var provincia_actual = document.getElementById("provincia_mapa");
	var provincia_texto_actual = document.getElementById("prov_texto_" + id);
		provincia_actual.style.backgroundImage = 'url()';
		if (provincia_texto_actual != null){
		provincia_texto_actual.style.color = '#003B75';
		}
	return true;
}

function modificar_informacion(mensaje) {
	//document.getElementById('informacion_contenido').innerHTML = mensaje;
}

//Generalizar esta función con la de ocultar_bajada_precio pasandole el nombre del div
function ocultar_campo_precio(check) {
	if (check.checked) {
		document.getElementById('campo_precio').style.display="none";
	} else {
		document.getElementById('campo_precio').style.display="inline";
	}
}

function ocultar_area_precio() {
	
}

//Copia el nombre de la imagen antigua a un campo oculto para que se borre y muestra un file para subir una nueva imagen.
function mostrar_file_imagen() {
	nombre_vieja_imagen=document.nuevo_anuncio.img_principal.value;
	zona_imagen=document.getElementById('imagen_nuevo_anuncio');
	zona_imagen.innerHTML="<input type=hidden name=old_imagen value='"+nombre_vieja_imagen+"'/>";
	zona_imagen.innerHTML=zona_imagen.innerHTML+"<label>Imagen principal : </label><input type=file name=imagen_principal style='float:left;' onFocus=\"javascript:modificar_informacion('Si quieres subir una imagen que apoye el texto del anuncio, pulsa el botón examinar y selecciona la imagen que quieras desde tu disco duro.&lt;br/&gt;&lt;br/&gt; &lt;small&gt;* Campo opcional&lt;/small&gt;');\"/><br/>";
}

function crear_input_imagen() {
	var num_imagenes=document.getElementsByName('campo_imagen').length;
	if (num_imagenes==9) //Si ya ha añadido 10, se quita el botón de añadir más imágenes.
		document.getElementById('boton_nueva_imagen').innerHTML="";
	num_imagenes+=1;
	var nuevo_div=document.createElement("div");
	nuevo_div.id="campo_imagen_"+num_imagenes;
	nuevo_div.setAttribute("name","campo_imagen");
	nuevo_div.innerHTML="<label></label><input type='file' name='nueva_imagen_anuncio[]' style='float:left;'/>";
	nuevo_div.innerHTML+="<input type='button' name='cancelar' value='Cancelar' style='float:left;' onclick='javascript:eliminar_div_imagen("+num_imagenes+");'/><br/>";
	var zona_imagenes=document.getElementById('zona_imagenes_anuncio');
	zona_imagenes.appendChild(nuevo_div);
	ajustarSombraInsertarAnuncio();
}

function eliminar_div_imagen(num) {
	var div_eliminar=document.getElementById("campo_imagen_"+num);
	var div_padre=div_eliminar.parentNode;
	div_padre.removeChild(div_eliminar);
	
	var div_boton=document.getElementById('boton_nueva_imagen');
	//Si ha borrado una y no estaba el botón, lo ponemos.
	if (div_boton.innerHTML == "") {
		div_boton.innerHTML="<label></label><input type='button' name='add_imagen' value='Añadir más imágenes' style='float:left' onclick='javascript:crear_input_imagen();'/><br/>";
	}
	
	var num_imagenes=document.getElementsByName('campo_imagen').length;
	//Si ha borrado la última imagen, le ponemos el campo para añadir
	if (num_imagenes==0){
		var nuevo_div=document.createElement("div");
		nuevo_div.id="campo_imagen_1";
		nuevo_div.setAttribute("name","campo_imagen");
		nuevo_div.innerHTML="<label>Imagen : </label><input type='file' name='nueva_imagen_anuncio[]' style='float:left;'/><br/>";
		var zona_imagenes=document.getElementById('zona_imagenes_anuncio');
		zona_imagenes.appendChild(nuevo_div);		
	}
	ajustarSombraInsertarAnuncio();		
}

function marcar_imagen_para_eliminar(imagen) {
	var div_eliminar_imagenes=document.getElementById('zona_imagenes_descartadas');
	div_eliminar_imagenes.innerHTML+="<input type='hidden' name='borrar_imagen[]' value='"+imagen+"'/>";
}

function ocultar_bajada_precio(check) {
	if (check.checked) {
		document.getElementById('campo_precio_bajada').style.display="none";
	} else {
		document.getElementById('campo_precio_bajada').style.display="inline";
	}
}

function mostrar_provincias() {
	display=document.getElementById('check_provincias').style.display;
	if (display == "inline")
		document.getElementById('check_provincias').style.display="none";
	else
		document.getElementById('check_provincias').style.display="inline";
}

function ocultar_provincias() {
	document.getElementById('check_provincias').style.display="none";
}


function alto(idAnuncio) {
	var obj=document.getElementById("titulo_"+idAnuncio);
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop
		}
	}
	return curtop;
}

function mostrar_div_formulario(id_anuncio) {
	var altura=alto(id_anuncio);
	altura+=10;
	document.getElementById('form_anuncio_tienda').style.top=altura+'px';
	document.getElementById('form_anuncio_tienda').style.display="inline";
	xajax_recuperar_datos_anuncio(id_anuncio);
}

function cambiar_a_imagenes(id_anuncio) {
	xajax_recuperar_imagenes_anuncio(id_anuncio, 0);
}

function cambiar_a_datos(id_anuncio) {
	xajax_recuperar_datos_anuncio(id_anuncio);
}

function ocultar_div_formulario() {
	document.getElementById('form_anuncio_tienda').style.display="none";
}

function ocultar_y_preguntar_div_formulario() {
	if (confirm('¿Seguro que desea cerrar el formulario sin guardar los cambios?')) {
		document.getElementById('form_anuncio_tienda').style.display="none";
	}
}

function confirmar_borrado_imagen(id_imagen, ficha) {
	if (confirm('¿Seguro que desea borrar esta imagen?')) {
		xajax_eliminar_imagen_anuncio(id_imagen, ficha);
	}
}

function mostrar_mensajes(pos_interesado, num_interesados, id_interesado) {
	for (var i=1; i<=num_interesados; i++) {
		div=document.getElementById("mensajes_"+i);
		div.innerHTML="";
	
		divs_accion=document.getElementById("ver_ocultar_"+i);
		divs_accion.innerHTML=divs_accion.innerHTML.replace("ocultar_mensajes", "mostrar_mensajes");
		divs_accion.innerHTML=divs_accion.innerHTML.replace("Ocultar conversación", "Ver conversación");
	}		
	xajax_mostrar_conversacion(pos_interesado, num_interesados, id_interesado);
}

function ocultar_mensajes(pos_interesado, num_interesados, id_interesado) {
	accion=document.getElementById("ver_ocultar_"+pos_interesado);
	accion.innerHTML="<a href='javascript:mostrar_mensajes("+pos_interesado+", "+num_interesados+", "+id_interesado+")'>Ver conversación</a>";
	conversacion=document.getElementById("mensajes_"+pos_interesado);
	conversacion.innerHTML="";
	ajustarSombra();
}

function mostrar_ocultar_subcategorias(num_categorias, categoria) {

	for (var i=1; i<=num_categorias; i++) {
		if (i == categoria) {
			if (document.getElementById("subcategorias_"+i).style.display == "none") {
				document.getElementById("texto_ver_todas_categorias_"+i).innerHTML="Ocultar";
				document.getElementById("mostrar_subcategorias_"+i).innerHTML="<a href='javascript:mostrar_ocultar_subcategorias(\""+num_categorias+"\", \""+i+"\");'><img src='imagenes_css/signo_contraer.png' class='img_signo_desplegar' /></a>";
				document.getElementById("subcategorias_"+i).style.display="block";
			} else {
				document.getElementById("texto_ver_todas_categorias_"+i).innerHTML="Ver";				
				document.getElementById("mostrar_subcategorias_"+i).innerHTML="<a href='javascript:mostrar_ocultar_subcategorias(\""+num_categorias+"\", \""+i+"\");'><img src='imagenes_css/signo_desplegar.png' class='img_signo_desplegar' /></a>";
				document.getElementById("subcategorias_"+i).style.display="none";			
			}
		} else {
			document.getElementById("texto_ver_todas_categorias_"+i).innerHTML="Ver";			
			document.getElementById("mostrar_subcategorias_"+i).innerHTML="<a href='javascript:mostrar_ocultar_subcategorias(\""+num_categorias+"\", \""+i+"\");'><img src='imagenes_css/signo_desplegar.png' class='img_signo_desplegar' /></a>";
			document.getElementById("subcategorias_"+i).style.display="none";			
		}
	}
	
	var altoActual = xHeight("z_contenido_sin_sombra_comunidad");	
	xHeight("z_sombra_lat_dcho", altoActual);

}

function set_foco_localidad() {
	campos=document.getElementsByName('localidad');
	campos[0].focus();
}

function set_foco_municipio() {
	campos=document.getElementsByName('municipio');
	campos[0].focus();
}

function set_foco_subcategoria() {
	campos=document.getElementsByName('subcategoria');
	campos[0].focus();	
}

function set_foco_titulo() {
	document.getElementById('titulo_foco').focus();
}

function guardar_sin_previsualizar() {
	document.getElementById('guardar_previsualizar').innerHTML="<input type='hidden' name='guardar' value='2'/>";
}

function agregar_favoritos(id_anuncio, titulo_anuncio){
	var url="http://www.compraryvender.com/index.php?accion=ver_anuncio&id_anuncio="+id_anuncio;
	var titulo="compraryvender.com / "+titulo_anuncio;
	if (document.all)
		window.external.AddFavorite(url, titulo);
	else if (window.sidebar)
		window.sidebar.addPanel(titulo, url, "")
}

function ocultar_mostrar_tipo_anuncio(categoria) {
	if (categoria == 8) {//Inmobiliaria Alquiler
		document.getElementById('ocultar_tipo_anuncio').style.display='none';
	} else {
		document.getElementById('ocultar_tipo_anuncio').style.display='block';
	}
}

function cambiar_imagen_actual(nombre, tipo) {
	document.getElementById('imagen_actual').innerHTML="<div id='mensaje_carga_imagen' style='margin-top:120px;font-weight:bold;color:#2864A2;>Cargando Imagen....<br/><br/>Espere por favor</div>";
	xajax_ver_imagen(nombre, tipo);
}

/*
function add_campo_referencia_inmueble(es_registrado) {
	if (es_registrado) {
		document.getElementById('campo_referencia_inmueble').innerHTML="<label><em>Referencia : </em></label><input type='text' class='campo_text_insertar' name='referencia_inmueble' maxlength='50'/>";
	} else {
		document.getElementById('campo_referencia_inmueble').innerHTML="";
	}
}

function add_campo_referencia_inmueble_ficha(categoria) {
	if ((categoria == 7) || (categoria==8)) {
		document.getElementById('campo_referencia_inmueble').innerHTML="<strong>Referencia : </strong><input type='text' class='campo_text_insertar' name='referencia_inmueble' maxlength='50'/><br/>";
	} else {
		document.getElementById('campo_referencia_inmueble').innerHTML="";
	}
}
*/
/*
function ocultar_subcategorias(num_categorias, categoria) {
	document.getElementById("mostrar_subcategorias_"+categoria).innerHTML="<a href='javascript:mostrar_subcategorias(\""+num_categorias+"\", \""+categoria+"\");'><img src='imagenes_css/signo_desplegar.png' class='img_signo_desplegar' /></a>";
	document.getElementById("subcategorias_"+categoria).style.display="none";

	var altoActual = xHeight("z_contenido_sin_sombra_comunidad");
   xHeight("z_sombra_lat_dcho", altoActual);

}
*/

//addEvent(window, "load", adjustLayout);
