window.onload=function() {
	var cod=gup("cod");
	obreNode(cod);
	iguala();
}

function obreNode(cod) {
	var d=$('b'+cod);
	
	if (d) {
		if (d.childNodes.length>0) {
			d.style.display='block';
		}
		
		while (d.parentNode.className="bMenu") {
			var cod=d.parentNode.id.substring(1);
			
			if ((cod=="0") || (cod=="")) { return; }
			
			var d=$('b'+cod);
			
			if (d) {
				d.style.display='block';
			}
		}
	}
}

function gup( name ){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}

function iguala() {
	var hMenu=$('tree').getHeight();
	var hTot=hMenu-196;  //aquest 196 es l'altaria de la imatge del peu, si la imatge es canvia llavors cal canviar aquest valor
	var hCont=$('contenido').getHeight();
	
	if (hCont<hTot) {
		$('contenido').style.height=hTot+"px";
	}
	
}

function enviar() {
	
	if (($('nombre').value=="") || ($('apellidos').value=="") || ($('mensaje').value=="")) {
		alert("Debe rellenar todos los campos marcados con asterisco");
		return;
	}
	
	var url="mail.php";
	var params="nombre="+$('nombre').value+"&apellidos="+$('apellidos').value+"&tel="+$('telefono').value+"&mail="+$('mail').value+"&mensaje="+$('mensaje').value;
	var ajax=new Ajax.Request ( url, {
		method: 'post',
		postBody: params,
		onLoading: function() {
					buida($('msg'));
					var t=document.createTextNode('Enviando mensaje...');
					$('msg').style.visibility='visible';
					$('msg').appendChild(t);
				},
		onComplete:function(reply) {
					if (reply.responseText=="Err") {
						var txt="Ha ocurrido un error al enviar el mensaje.";
					} else {
						var txt="El mensaje se ha enviado correctamente.";
					}
					buida($('msg'));
					var t=document.createTextNode(txt);
					$('msg').appendChild(t);
					
					if (reply.responseText=="Ok") {
						$('nombre').value="";
						$('apellidos').value="";
						$('telefono').value="";
						$('mail').value="";
						$('mensaje').value="";
					}
				}
		});	
}

function buida(el){// elimina tots els "child nodes" de l'element que li passam per parámetre
    if(el!=null){
        if(el.childNodes){
            for(var i=el.childNodes.length-1;i>=0;i--){
                var childNode=el.childNodes[i];
                el.removeChild(childNode);
            }
        }
    }
}