
/* HOJA DE FUNCIONES JAVASCRIPT

   Autor: 		Interesa Consultores
   
   --------------------------------------------------------*/




/**************************************************************************************************
Función para marcar el menu	
	var cadena_separacion = "_"; //cadena de separación nivel
**************************************************************************************************/

function setPage()
{
	var cadena_separacion = "_"; //cadena de separación nivel
	
	var pagina = obtener_nombre_pagina();
		
	seleccionar_enlace(pintar_primer_nivel(cadena_separacion,pagina));
	seleccionar_enlace(pintar_segundo_nivel(cadena_separacion,pagina));
	seleccionar_enlace(pintar_tercer_nivel(pagina));
}

function obtener_nombre_pagina(){
	var vble_url;
	if(document.location.href){ 
	    vble_url = document.location.href;
	}else{
		vble_url = document.location;
	}
	
    return vble_url.slice (vble_url.lastIndexOf('/')+1,vble_url.length);		
}


function pintar_primer_nivel(cadena_separacion,pagina){	
	var primer_nivel = pagina;	
	primer_nivel = primer_nivel.slice(0,primer_nivel.indexOf(cadena_separacion));
	return primer_nivel;
}

function pintar_segundo_nivel(cadena_separacion,pagina){	
	var segundo_nivel = pagina;
	if(pagina.indexOf(cadena_separacion)==pagina.lastIndexOf(cadena_separacion)){	
		//Cuando no hay tercer nivel
		segundo_nivel = segundo_nivel.slice(0,segundo_nivel.indexOf('.'));
	}else{
		//cuando hay tercer nivel
		segundo_nivel = segundo_nivel.slice(0,segundo_nivel.lastIndexOf(cadena_separacion));
	}
		return segundo_nivel;
}


function pintar_tercer_nivel(pagina){	
	var tercer_nivel = pagina;
		tercer_nivel = tercer_nivel.slice(0,tercer_nivel.indexOf('.'));
		return tercer_nivel;
}




function seleccionar_enlace(enlace){
	var array_enlaces = document.getElementsByTagName('a');
	var nombre_class="seleccionado";
	var tag_padre = "LI";
	for(var i=0; i < array_enlaces.length; i++){
		var enlace_menu = array_enlaces[i].href;
		enlace_menu=enlace_menu.slice(enlace_menu.lastIndexOf('/')+1,enlace_menu.lastIndexOf('.'));
		if(enlace_menu.toUpperCase()==enlace.toUpperCase()){
			if(array_enlaces[i].parentNode.tagName==tag_padre){
				var nombre;
				nombre=array_enlaces[i].parentNode.getAttribute("class");
				if(nombre==null){
					nombre=nombre_class;
				}else{
					if(nombre != nombre_class){				
						nombre=nombre + " " + nombre_class;
					}
				}
				array_enlaces[i].parentNode.className = nombre;
			}
		}
	}
	
}


/**************************************************************************************************
**************************************************************************************************/


/**************************************************************************************************
Funcion para validar formularios
	Campo Obligatorio: class="obligatorio"
	Campo mail: class="obligatorio mail"
	Nota: Primero te comprueba los campos de tipo input y despues los campos de tipo textarea
**************************************************************************************************/

function validar_formulario(){
	var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*){2,3}\.[a-zA-Z]{2,3}$/;
	var class_obligatorio = "obligatorio";
	var class_mail = "mail";
	
	var obj = document.getElementsByTagName("fieldset");
	for (i=0; i<obj.length; i++){//Bucle para los fieldset
		for (var b=0; b<obj[i].childNodes.length; b++) {//Bucle para los hijos de cada fieldset
			var campo = obj[i].childNodes[b];

			if (existe_cadena(campo.className,class_obligatorio)){//Comprobar si es obligatorio
				//Si es un input
				if (campo.tagName == "INPUT") {
					if (campo.type == "text") {
						if(campo.value=='' || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
						
						//Si el campo es un mail
						if (existe_cadena(campo.className,class_mail)){
							if (!pattern.test(campo.value) ){//Si el formato es incorrecto
								alert(campo.getAttribute("title"));
								campo.focus();
								return false;
							}
						}
						
					}
					if (campo.type == "checkbox") {
					   if (!campo.checked) {
						  alert(campo.getAttribute("title"));
						  return false;
						  
					   }
					}
		
				 }//Fin Si es input
				 
				 //Si es un textarea
				if (campo.tagName == "TEXTAREA") {
					var Str = campo.value;
						if (Str.length == 0 || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
				}
				//Si es un select
				if (campo.tagName == "SELECT") {
					if(campo.options[campo.selectedIndex].text=='' || campo.options[campo.selectedIndex].text==campo.getAttribute("title")){
						alert(campo.getAttribute("title"));
						campo.focus();
						return false;
					}
				}
				

				 
			}//Fin si es obligatorio
			
		}
	}
	document.getElementsByTagName("form").submit();//Poner cuando no lleva boton submit	
}


function existe_cadena(cadena1,cadena2){

	if(cadena1==null){
			return false;
	}
	if(cadena1.indexOf(cadena2)!=-1){
		return true;
	}else{
		return false;
	}
}

/********************  /Funcion para validar formularios ******************************************/

/**************************************************************************************************
Funcion para indicar que se abre en ventana nueva los enlaces target=_blank rel=external
**************************************************************************************************/
function externalLinks() {
	var txt_ventana = ". Se abre en ventana nueva";
	var txt_ventana_no_title = " Se abre en ventana nueva.";
		if(obtenerIdioma()=="es"){
		     txt_ventana = ". Se abre en ventana nueva";
		     txt_ventana_no_title = " Se abre en ventana nueva";
			}
			if(obtenerIdioma()=="en"){
		     txt_ventana = ". New window";
		     txt_ventana_no_title = " New window";
			}
			if(obtenerIdioma()=="eu"){
		     txt_ventana = ". Leiho berrian irekitzen da";
		     txt_ventana_no_title = " Leiho berrian irekitzen da";
			}
			if(obtenerIdioma()=="fr"){
		     txt_ventana = ". Affichage dans une nouvelle fen\u00EAtre";
		     txt_ventana_no_title = " Affichage dans une nouvelle fen\u00EAtre";
			}		
	
 if (!document.getElementsByTagName) return;

 var anchors = document.getElementsByTagName("a");
 var tit;
 for (var i=0; i<anchors.length; i++) {

   var anchor = anchors[i];
	
	//sino tiene title lo pone el javascript
	if(anchor.getAttribute("title")){
	}else{
		//anchor.title = anchor.innerHTML ;
		asignar_no_title(anchor,anchor.innerHTML);
	}
	
	//Comprobar si el enlace tiene target _blank
	if (anchor.getAttribute("target") == "_blank"){
		if(anchor.getAttribute("title")){
			tit = anchor.getAttribute("title");
			asignar_title(anchor,txt_ventana,txt_ventana_no_title,tit)
		}else{
			tit = anchor.innerHTML;
			asignar_title(anchor,txt_ventana,txt_ventana_no_title,tit)
		}
	}
	
		if(anchor.getAttribute("rel") == "external"){
			anchor.target = "_blank";
			if(anchor.getAttribute("title")){
				tit = anchor.getAttribute("title");
				asignar_title(anchor,txt_ventana,txt_ventana_no_title,tit)
			}else{
				tit = anchor.innerHTML;
				asignar_title(anchor,txt_ventana,txt_ventana_no_title,tit)
			}
		}
		

 }
}

function asignar_title(anchor,txt_ventana,txt_ventana_no_title,tit){
	
	
	tit=replace_title(tit);
	
	
	if(tit.slice(tit.length-1,tit.length)=='.'){
		anchor.title = tit + txt_ventana_no_title;
	}else{
		anchor.title = tit + txt_ventana;
	}
}


function asignar_no_title(anchor,tit){
	tit=replace_title(tit);
	anchor.title = tit ;
	//Si es imágen
	if(tit.indexOf('<img')==0){
		var vble_img = anchor.getElementsByTagName('img');
		anchor.title = vble_img[0].getAttribute("alt");
	}else{
		if(tit.indexOf('<img')==0){
			anchor.title = "";
		}
		
	}
	
	
}

function replace_title(tit){
	var txt_eliminar = new Array(
				"<span>",
				"</span>",				
				"&gt;&gt;",
				"&gt;"
			);
	for(var i=0;i<=txt_eliminar.length;i++){
		tit=tit.replace(txt_eliminar[i], "");
	}
	return tit;
}
function obtenerIdioma(){
	var idioma;
	var tmpURL=window.location.href;
	
	if (tmpURL.indexOf("/es/") != -1){
		idioma="es";
	}
	if (tmpURL.indexOf("/eu/") != -1){
		idioma="eu";
	}
	if (tmpURL.indexOf("/fr/") != -1){
		idioma="fr";
	}
	if (tmpURL.indexOf("/en/") != -1){
		idioma="en";
	}
	return idioma;
}
/**************** /Funcion para indicar que se abre en ventana nueva **/

window.onload=function()
{
  setPage();
  externalLinks();
}
