// scripts realizados a partir del código de Sergi Meseguer - www.zigotica.com


function elMes(mes)
{
	var meses = new Array("Ene","Feb","Mar","Abr","May","Jun",
	                       "Jul","Ago","Sep","Oct","Nov","Dic");
	return meses[mes] 	  	 
}

function fecha()
{
	var hoy = new Date();

	var cadena = '<span>' + hoy.getDate() + '</span>';
	cadena += elMes(hoy.getMonth()) + "<br />" + hoy.getFullYear();
	return cadena
}



var EVENTS = {
	
	init : function() {
      document.getElementById('calen').innerHTML = fecha();
  }
	
}


var BUSQUEDA = {
	
	init : function() {
		if (document.getElementById('buscar')) {
			document.getElementById('buscar').onsubmit = function() {
				return BUSQUEDA.valida(this);
			}
		}
	},
	
	valida : function(cual) {
		for (i=0;i<cual.elements.length;i++) {
			if ((cual.elements[i].type != null) && (cual.elements[i].type != 'submit') && (cual.elements[i].value == '')) {
				alert('Debe introducir un texto para poder comenzar la búsqueda');
				return false;
			}
		}
		return true;
	}
	
}


var MENU_PRINCIPAL = {
	
	init : function() {
		var listaPrincipal = document.getElementById('menu-principal').getElementsByTagName('ul')[0];
		var listas = listaPrincipal.getElementsByTagName('ul');
		for (i=0;i<listas.length;i++) {
			if(listas[i].className == 'tercerNivel'){
				listas[i].className = 'oculta';
			}
		}
		var enlaces = listaPrincipal.getElementsByTagName('a');
		for (i=0;i<enlaces.length;i++) {
			enlaces[i].onclick = function() {
				return MENU_PRINCIPAL.actualiza(this,listaPrincipal);
			}
		}
	},
	
	actualiza : function(cual,donde) {
		if (cual.href.indexOf('#') != -1) {
			if (donde.className == cual.parentNode.id)
				donde.className = '';
			else
				donde.className = cual.parentNode.id;
			return false;
		}
		return true;
	}
	
}


var SERVICIOS = {
	
	init : function() {
		
		//var inputVisible = document.getElementById('txtEmails');
		//inputVisible.onfocus = function(){
		//	return SERVICIOS.verMail(this);
		//}
		//var textareaVisible = document.getElementById('txtAny');
		//textareaVisible.onfocus = function(){
		//	return SERVICIOS.verTextarea(this);
		//}
		var wizard = document.getElementById('wizardsend');
		wizard.onclick = function(){
			return SERVICIOS.busqueda();
		}

		var check = document.getElementById('todos');
		check.onclick = function(){
			return SERVICIOS.tInput2(true);
		}
		
		var uncheck = document.getElementById('ninguno');
		uncheck.onclick = function(){
			return SERVICIOS.tInput(false);
		}
		
		
	},
	
	verDd : function(cual) {
		if((cual.nextSibling.nodeType == 3)||(cual.nextSibling.nodeType == 8)){
			if(cual.nextSibling.nextSibling.style.display == 'block'){
				cual.nextSibling.nextSibling.style.display='none';		
			}else{
				cual.nextSibling.nextSibling.style.display='block';
			}	
		}else{
			if(cual.nextSibling.style.display == 'block'){
				cual.nextSibling.style.display='none';		
			}else{
				cual.nextSibling.style.display='block';
			}	
		}
		return false;
	},
	verMail : function(cual) {
		cual.value = '';
		return false;
	},
	verTextarea : function(cual) {
		cual.value = '';
		return false;
	},
	verVelo : function(opcion) {
		if (document.getElementById('objeto') != null) {
			if (opcion == 'block')
				document.getElementById('objeto').style.visibility = 'hidden';
			else
				document.getElementById('objeto').style.visibility = 'visible';
		}
		var capaOpaca = document.getElementById('capaOpaca');
		capaOpaca.style.height = document.documentElement.scrollHeight + 'px';
		capaOpaca.style.display = opcion;
		var capaContenedora = document.getElementById('capaContenedora');
		capaContenedora.style.height = document.documentElement.scrollHeight + 'px';
		capaContenedora.style.display = opcion;
		//centrarCapa();
		return false;
	},
	busqueda : function() {
		var encontrado = false;
		var masters = document.getElementById("menuProgramas").getElementsByTagName("dt");
		for(var i=0;i<masters.length;i++){
			masters[i].style.display = 'none';
		}

		var masters2 = document.getElementById("menuProgramas").getElementsByTagName("dd");
		for(var j=0;j<masters2.length;j++){
			masters2[j].style.display = 'none';
		}

		var inputsI = document.getElementById("idioma").getElementsByTagName("input");
		var texto;
		for(var i=0;i<inputsI.length;i++){
			if(inputsI[i].checked){
			texto += '.' + inputsI[i].id + ','; 
			}
		}

		var inputsF = document.getElementById("formato").getElementsByTagName("input");
		var texto2;
		for(var i=0;i<inputsF.length;i++){
			if(inputsF[i].checked){
			texto2 += '.' + inputsF[i].id + ','; 
			}
		}

		var inputsE = document.getElementById("especializacion").getElementsByTagName("input");
		var texto3;
		for(var i=0;i<inputsE.length;i++){
			if(inputsE[i].checked){
			texto3 += '.' + inputsE[i].id + ','; 
			}
		}
		

		if(texto){
			var posicion = texto.lastIndexOf(',');
			texto = texto.substring(9,posicion);
			encontrado = true;
		}
		if(texto2){
			var posicion2 = texto2.lastIndexOf(',');
			texto2 = texto2.substring(9,posicion2);
			encontrado = true;
		}
		if(texto3){
			var posicion3 = texto3.lastIndexOf(',');
			texto3 = texto3.substring(9,posicion3);
			encontrado = true;
		}
/*		$("dt").filter(texto).filter(texto2).filter(texto3).css("display","inline");
		$("dd").filter(texto).filter(texto2).filter(texto3).css("display","inline");
*/
		if((texto)&&(texto2)&&(texto3)){
		$("dt").filter(texto).filter(texto2).filter(texto3).css("display","inline");
		$("dd").filter(texto).filter(texto2).filter(texto3).css("display","inline");
		}else if((texto)&&(texto2)&&(!texto3)){
		$("dt").filter(texto).filter(texto2).css("display","inline");
		$("dd").filter(texto).filter(texto2).css("display","inline");
		}else if((texto)&&(!texto2)&&(!texto3)){
		$("dt").filter(texto).css("display","inline");
		$("dd").filter(texto).css("display","inline");
		}else if((!texto)&&(texto2)&&(!texto3)){
		$("dt").filter(texto2).css("display","inline");
		$("dd").filter(texto2).css("display","inline");
		}else if((!texto)&&(texto2)&&(texto3)){
		$("dt").filter(texto2).filter(texto3).css("display","inline");
		$("dd").filter(texto2).filter(texto3).css("display","inline");
		}else if((!texto)&&(!texto2)&&(texto3)){
		$("dt").filter(texto3).css("display","inline");
		$("dd").filter(texto3).css("display","inline");
		}else if((texto)&&(!texto2)&&(texto3)){
		$("dt").filter(texto).filter(texto3).css("display","inline");
		$("dd").filter(texto).filter(texto3).css("display","inline");
		}
		
		if(encontrado == true){
			document.getElementById("encontrado").style.display = 'none';		
		}else{
			document.getElementById("encontrado").style.display = 'block';		
		}
		comprobacion();
		return false;
	},
	tInput : function(tipo){

		var inputs = document.getElementById('wizardsegundo').getElementsByTagName('input');		
		for(var j=0;j<inputs.length;j++){
			inputs[j].checked = tipo;
		}
		$("dt").css("display","none");
		$("dd").css("display","none");		
		document.getElementById("encontrado").style.display = 'block';
		comprobacion();
/*		return SERVICIOS.busqueda();*/
	},
	tInput2 : function(tipo){

		var inputs = document.getElementById('wizardsegundo').getElementsByTagName('input');		
		for(var j=0;j<inputs.length;j++){
			inputs[j].checked = tipo;
		}
		$("dt").css("display","inline");		
		$("dd").css("display","inline");		
		document.getElementById("encontrado").style.display = 'none';
		comprobacion();
/*		return SERVICIOS.busqueda();*/
	}
	
}

function comprobacion(){
	var valorMBA = false;
	var valorLaw = false;
	var valorSpe = false;
	var valorDoc = false;
	var dtMBA = document.getElementById('losMBA');
	var dtLaw = document.getElementById('losLaw');
	var dtSpe = document.getElementById('losSpe');
	var dtDoc = document.getElementById('losDoc');
	listaMBA = dtMBA.getElementsByTagName('dt');	
	listaLaw = dtLaw.getElementsByTagName('dt');
	listaSpe = dtSpe.getElementsByTagName('dt');
	listaDoc = dtDoc.getElementsByTagName('dt');	
/* MBA */
	for(var i = 0;i < listaMBA.length; i++){
		if(listaMBA[i].style.display != 'none'){
			valorMBA = true;
		}
	}
	
	if(valorMBA == false){
		if((dtMBA.previousSibling.nodeType == 3)||(dtMBA.previousSibling.nodeType == 8)){
			dtMBA.previousSibling.previousSibling.style.display = 'none';
			dtMBA.style.display = 'none';
		}else{
			dtMBA.previousSibling.style.display = 'none';
			dtMBA.style.display = 'none';
		}
	}else{
		if((dtMBA.previousSibling.nodeType == 3)||(dtMBA.previousSibling.nodeType == 8)){
			dtMBA.previousSibling.previousSibling.style.display = 'block';
			dtMBA.style.display = 'block';			
		}else{
			dtMBA.previousSibling.style.display = 'block';
			dtMBA.style.display = 'block';						
		}

	}
/* Law */
	for(var i = 0;i < listaLaw.length; i++){
		if(listaLaw[i].style.display != 'none'){
			valorLaw = true;
		}
	}
	
	if(valorLaw == false){
		if((dtLaw.previousSibling.nodeType == 3)||(dtLaw.previousSibling.nodeType == 8)){
			dtLaw.previousSibling.previousSibling.style.display = 'none';
			dtLaw.style.display = 'none';
		}else{
			dtLaw.previousSibling.style.display = 'none';
			dtLaw.style.display = 'none';
		}
	}else{
		if((dtLaw.previousSibling.nodeType == 3)||(dtLaw.previousSibling.nodeType == 8)){
			dtLaw.previousSibling.previousSibling.style.display = 'block';
			dtLaw.style.display = 'block';
		}else{
			dtLaw.previousSibling.style.display = 'block';
			dtLaw.style.display = 'block';
		}

	}
/* Spe */
	for(var i = 0;i < listaSpe.length; i++){
		if(listaSpe[i].style.display != 'none'){
			valorSpe = true;
		}
	}
	
	if(valorSpe == false){
		if((dtSpe.previousSibling.nodeType == 3)||(dtSpe.previousSibling.nodeType == 8)){
			dtSpe.previousSibling.previousSibling.style.display = 'none';
			dtSpe.style.display = 'none';
		}else{
			dtSpe.previousSibling.style.display = 'none';
			dtSpe.style.display = 'none';
		}
	}else{
		if((dtSpe.previousSibling.nodeType == 3)||(dtSpe.previousSibling.nodeType == 8)){
			dtSpe.previousSibling.previousSibling.style.display = 'block';
			dtSpe.style.display = 'block';
		}else{
			dtSpe.previousSibling.style.display = 'block';
			dtSpe.style.display = 'block';
		}

	}
/* Doc */
	for(var i = 0;i < listaDoc.length; i++){
		if(listaDoc[i].style.display != 'none'){
			valorDoc = true;
		}
	}
	
	if(valorDoc == false){
		if((dtDoc.previousSibling.nodeType == 3)||(dtDoc.previousSibling.nodeType == 8)){
			dtDoc.previousSibling.previousSibling.style.display = 'none';
			dtDoc.style.display = 'none';
		}else{
			dtDoc.previousSibling.style.display = 'none';
			dtDoc.style.display = 'none';
		}
	}else{
		if((dtDoc.previousSibling.nodeType == 3)||(dtDoc.previousSibling.nodeType == 8)){
			dtDoc.previousSibling.previousSibling.style.display = 'block';
			dtDoc.style.display = 'block';
		}else{
			dtDoc.previousSibling.style.display = 'block';
			dtDoc.style.display = 'block';
		}

	}
	
}

var estado = 'inicial';

var BRIEF_ME = {
	
	init : function() {
		document.getElementById('imprimirBriefMe').onclick = function(){
			BRIEF_ME.imprimir();
		}
	},
	
	imprimir : function() {
		var enlaces = document.getElementsByTagName("link");
		var temp;
		for(i=0;i<enlaces.length;i++) {
			if (enlaces[i].getAttribute("href").indexOf("css/es_general.css")!= -1){
				enlaces[i].setAttribute("href", "css/es_brief_me_print.css");
			}
			if (enlaces[i].getAttribute("href").indexOf("css/es_general_print.css") != -1) {
				enlaces[i].setAttribute("href", "css/es_brief_me_print.css");
			}
		}
		window.print();
		for(i=0;i<enlaces.length;i++) {
			if ((enlaces[i].getAttribute("href").indexOf("css/es_brief_me_print.css")!= -1)&&(enlaces[i].getAttribute("media").indexOf("screen")!= -1)){
				enlaces[i].setAttribute("href", "css/es_general.css");
			}
			if ((enlaces[i].getAttribute("href").indexOf("css/es_brief_me.css") != -1)&&(enlaces[i].getAttribute("media").indexOf("print")!= -1)) {
				enlaces[i].setAttribute("href", "css/es_general_print.css");
			}
		}
	}
}


var PRUGUNTAS = {
    
    init : function() {
    var formo = document.getElementById('pruguntas');
       
       /* formo.onsubmit = function(){
          document.getElementById('avisoL').className = 'avisoLegal';
          document.getElementById('avisoL').style.display = 'block';      
      return false;
        }
    
        document.getElementById('avisoLButton').onclick = function(){
      formo.submit();
        }
        
        document.getElementById('avisoLCerrar').onclick = function(){
          document.getElementById('avisoL').style.display = 'none';  
        }*/
    },
    
    condiciones : function() {
        return false;
    }
    
}


var IMPRIMIR = {
	
	init : function() {
		var imprimir = document.getElementById('imprimir');
		imprimir.onclick = function(){
			print();
			return false;
		}
	}
}

var ENVIARAMIGO = {
	
	init : function() {
		/*var amigo = document.getElementById('enviarAmigo');
		amigo.onclick = function(){
			document.getElementById('enviar').style.top = parseInt(document.documentElement.scrollTop) + 152 + 138 + 'px';
			document.getElementById('enviar').className = 'amigo';
			document.getElementById('enviar').getElementsByTagName('form')[0].reset();
			return false;
		}
		document.getElementById('enviar').getElementsByTagName('a')[0].onclick = function() {
			document.getElementById('enviar').className = '';
			return false;
		}*/
		document.getElementById('enviarAmigo').onclick = function() {
			document.getElementById('capaEnviarAmigo').style.top = (parseInt(document.documentElement.clientHeight) - 220)/2 + 100 + parseInt(document.documentElement.scrollTop) + 'px';
			document.getElementById('capaEnviarAmigo').style.display = 'block';
			return false;
		}
		document.getElementById('capaEnviarAmigo').getElementsByTagName('a')[0].onclick = function() {
			document.getElementById('capaEnviarAmigo').style.display = 'none';
			return false;
		}
		var formulario = document.getElementById('capaEnviarAmigo').getElementsByTagName('form')[0];
		var valores = new Array();
		for (i=0;i<formulario.elements.length;i++) {
			if (formulario.elements[i].id != '') {
				formulario.elements[i].onfocus = function() {
					valores[i] = this.value;
					this.value = '';
				}
				formulario.elements[i].onblur = function() {
					if (this.value == '') {
						this.value =  valores[i];
					}
				}
			}
		}
	}
}

var ENVIARAMIGOBRIEFME = {
	
	init : function() {
		document.getElementById('enviarBriefMe').onclick = function() {
			document.getElementById('capaEnviarAmigo').style.top = (parseInt(document.documentElement.clientHeight) - 220)/2 + 100 + parseInt(document.documentElement.scrollTop) + 'px';
			document.getElementById('capaEnviarAmigo').style.display = 'block';
			return false;
		}
		document.getElementById('capaEnviarAmigo').getElementsByTagName('a')[0].onclick = function() {
			document.getElementById('capaEnviarAmigo').style.display = 'none';
			return false;
		}
		var formulario = document.getElementById('capaEnviarAmigo').getElementsByTagName('form')[0];
		var valores = new Array();
		for (i=0;i<formulario.elements.length;i++) {
			if (formulario.elements[i].id != '') {
				formulario.elements[i].onfocus = function() {
					valores[i] = this.value;
					this.value = '';
				}
				formulario.elements[i].onblur = function() {
					if (this.value == '') {
						this.value =  valores[i];
					}
				}
			}
		}
	}
}


var DELICIOUS = {
	ini : function() {
    var title = document.title;
    var url = document.location.href;
    document.getElementById('delicious').href +='post?url=' + url + '&title=' + title;
	}
}

function centrarCapa(){
	if ((navigator.appName.indexOf('Microsoft') != -1) && document.body.currentStyle)
		var alturaCapa = parseInt(document.getElementById('capaContenidoBlanca').currentStyle.height);
	else
		var alturaCapa = parseInt(window.getComputedStyle(document.getElementById('capaContenidoBlanca'), null).getPropertyValue("height"));
	var resultaInicial = (parseInt(document.documentElement.clientHeight) - alturaCapa)/2;
	var resultaTotal = resultaInicial + parseInt(document.documentElement.scrollTop);
	document.getElementById('capaContenidoBlanca').style.marginTop = resultaTotal + 'px';
}


function borrarSubCapas(cual){
	var anterior = cual.parentNode.parentNode.parentNode;
	var contenidop = anterior.getElementsByTagName('dd');
	for (i=0;i<contenidop.length;i++) {
		contenidop[i].style.display = 'none';
	}
	var contenidodt = anterior.getElementsByTagName('dt');
	for (i=0;i<contenidodt.length;i++) {
		contenidodt[i].className = '';
	}
}

function ocultardos(cual){
		var anterior = cual.parentNode.parentNode;
		var contenidodt = anterior.getElementsByTagName('dt');
		for (i=0;i<contenidodt.length;i++) {
			contenidodt[i].className = '';
		}
		var contenidodd = anterior.getElementsByTagName('dd');
		for (i=0;i<contenidodd.length;i++) {
			contenidodd[i].style.display = 'none';
		}
		return false;
	}


// añadido de eventos realizado como en http://simon.incutio.com/archive/2004/05/26/addLoadEvent

function addLoadEvent(fn) {
	var old = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = fn;
	} else {
		window.onload = function() {
			old();
			fn();
		}
	}
}

addLoadEvent(function() {
	BUSQUEDA.init();
});

addLoadEvent(function() {
	MENU_PRINCIPAL.init();
});

addLoadEvent(function() {
	SERVICIOS.init();
});

/*addLoadEvent(function() {
	BRIEF_ME.init();
});*/

/*addLoadEvent(function() {
	EVENTS.init();
});
*/
addLoadEvent(function() {
	PRUGUNTAS.init();
});

addLoadEvent(function() {
	IMPRIMIR.init();
});

addLoadEvent(function() {
	ENVIARAMIGO.init();
});

/*addLoadEvent(function() {
	ENVIARAMIGOBRIEFME.init();
});*/

addLoadEvent(function() {
	DELICIOUS.ini();
});