function crearTargetBlankLink(){
	var links = document.getElementsByTagName("a");
	if(links != null && links.length > 0){
		for(var i = 0; i < links.length; i++){
			if(links[i].getAttribute("rel") !=null){
				if(links[i].getAttribute("rel").indexOf("external") != -1){
					links[i].setAttribute("target", "_blank");
				}
			}
		}
	}
}

function validarEmail(valor){
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(valor);
}

function addOnloadEvent(fnc){
	if ( typeof window.addEventListener != "undefined" ){
		window.addEventListener( "load", fnc, false );
	}else if ( typeof window.attachEvent != "undefined" ) {
		window.attachEvent( "onload", fnc );
	}else {
		if ( window.onload != null ) {
			var oldOnload = window.onload;
			window.onload = function ( e ) {
				oldOnload( e );
				window[fnc]();
			};
		}else{
			window.onload = fnc;
		}
	}
}

var objetoAjax;
function getObjetoAjax(){
	var xhr = false;
	if(window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	 //Para controlar el ActiveXObject dependiendo de la version del IE
	 	try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");			
			}catch(e){
				xhr = false;
			}
		}
	}
	return xhr;
}

var ejecutando = 1;
function gestionarCambioIdioma(nomCookie){
	var link = document.getElementById("cambioIdioma");
	if(link != null){
		var nuevoIdioma = link.getAttribute("name");
		link.onclick = function() {
			introducirCookieIdioma(nuevoIdioma, nomCookie);
		}
	}
}

function introducirCookieIdioma(idioma, nomCookie){
	objetoAjax = getObjetoAjax();
	objetoAjax.onreadystatechange=okCookieIdioma;
	objetoAjax.open("POST","/servlet/ContentServer?pagename=JNETPortalComun/Logica/JNETAnadirCookieIdioma&nombreCookie="+nomCookie,false);
	objetoAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	objetoAjax.send("nuevoIdioma=" + idioma);
	while(ejecutando == 1){
		okCookieIdioma();
	}
}

function okCookieIdioma(){
	if(objetoAjax.readyState==4 && objetoAjax.status==200){
		ejecutando = 0;				
	}
}

function gestionarBuscadorGeneral(){	
	$("#cbuscar").attr("autocomplete", "off"); 
	$("#lnkDesplBusc").click(function(){
		$("#bloqueFilroBusc").slideToggle("fast");
		var textoActual = $("#lnkDesplBusc").attr("title");
		$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
		$("#txtBotonFiltrado").attr("value", textoActual);
		if(window.event != null){
			document.onkeydown = detectar_tecla_ie;
		}else{
			document.onkeydown = detectar_tecla;
		}
		
		$(document).click(function() { 
 			if($("#bloqueFilroBusc").is(':visible')){
				$("#bloqueFilroBusc").slideUp("fast");
				var textoActual = $("#lnkDesplBusc").attr("title");
				$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
				$("#txtBotonFiltrado").attr("value", textoActual);
			}
 		}); 

 		$('#buscar').click(function(e){ 
     		e.stopPropagation(); 
 		});
	});
	
	$("#bloqueFilroBusc ul li a").click(function(){
		$("#bloqueFilroBusc ul li").attr("class", "");
		$(this).parent().attr("class", "filtro");

		if($("#bloqueFilroBusc").is(':visible')){
			$("#bloqueFilroBusc").slideUp("fast");
		}else{
			$("#bloqueFilroBusc").slideDown("fast");
		}
		var textoActual = $("#lnkDesplBusc").attr("title");
		$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
		$("#txtBotonFiltrado").attr("value", textoActual);
		$("#tipo").attr("value", $(this).parent().find("a:first").attr("id"));
		$("#cbuscar").focus();
	});
	
	$("#bloqueFilroBusc ul li").click(function(){
		$("#bloqueFilroBusc ul li").attr("class", "");
		$(this).attr("class", "filtro");

		if($("#bloqueFilroBusc").is(':visible')){
			$("#bloqueFilroBusc").slideUp("fast");
		}else{
			$("#bloqueFilroBusc").slideDown("fast");
		}
		var textoActual = $("#lnkDesplBusc").attr("title");
		$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
		$("#txtBotonFiltrado").attr("value", textoActual);
		$("#tipo").attr("value", $(this).find("a:first").attr("id"));
		$("#cbuscar").focus();
	});
			
	$("#bbuscar").click(function(){
		if($("#cbuscar").attr("value") == ""){
			return false;
		}
	});
	
	$("#frmBuscadorGeneral").submit(function(){
		if($("#cbuscar").attr("value") == ""){
			return false;
		}
	});
}

function gestionarBuscadorGeneralIntra(){	
	$("#cbuscar").attr("autocomplete", "off"); 
	$("#lnkDesplBusc").click(function(){
		$("#bloqueFilroBusc").slideToggle("fast");
		var textoActual = $("#lnkDesplBusc").attr("title");
		$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
		$("#txtBotonFiltrado").attr("value", textoActual);
		if(window.event != null){
			document.onkeydown = detectar_tecla_ie;
		}else{
			document.onkeydown = detectar_tecla;
		}
		
		$(document).click(function() { 
 			if($("#bloqueFilroBusc").is(':visible')){
				$("#bloqueFilroBusc").slideUp("fast");
				var textoActual = $("#lnkDesplBusc").attr("title");
				$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
				$("#txtBotonFiltrado").attr("value", textoActual);
			}
 		}); 

 		$('#buscar2').click(function(e){ 
     		e.stopPropagation(); 
 		});
	});
	
	$("#bloqueFilroBusc ul li a").click(function(){
		$("#bloqueFilroBusc ul li").attr("class", "");
		$(this).parent().attr("class", "filtro");

		if($("#bloqueFilroBusc").is(':visible')){
			$("#bloqueFilroBusc").slideUp("fast");
		}else{
			$("#bloqueFilroBusc").slideDown("fast");
		}
		var textoActual = $("#lnkDesplBusc").attr("title");
		$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
		$("#txtBotonFiltrado").attr("value", textoActual);
		$("#tipo").attr("value", $(this).parent().find("a:first").attr("id"));
		$("#cbuscar").focus();
	});
	
	$("#bloqueFilroBusc ul li").click(function(){
		$("#bloqueFilroBusc ul li").attr("class", "");
		$(this).attr("class", "filtro");

		if($("#bloqueFilroBusc").is(':visible')){
			$("#bloqueFilroBusc").slideUp("fast");
		}else{
			$("#bloqueFilroBusc").slideDown("fast");
		}
		var textoActual = $("#lnkDesplBusc").attr("title");
		$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
		$("#txtBotonFiltrado").attr("value", textoActual);
		$("#tipo").attr("value", $(this).find("a:first").attr("id"));
		$("#cbuscar").focus();
	});
			
	$("#bbuscar").click(function(){
		if($("#cbuscar").attr("value") == ""){
			return false;
		}
	});
	
	$("#frmBuscadorGeneral").submit(function(){
		if($("#cbuscar").attr("value") == ""){
			return false;
		}
	});
}

function detectar_tecla_ie(){
	with (event){
		if (keyCode==27) {
			if($("#bloqueFilroBusc").is(':visible')){
				$("#bloqueFilroBusc").slideUp("fast");
				var textoActual = $("#lnkDesplBusc").attr("title");
				$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
				$("#txtBotonFiltrado").attr("value", textoActual);
			}
			return false;
		}
	}
} 
 
function detectar_tecla(event){
	with (event){
		if (keyCode==27) {
			if($("#bloqueFilroBusc").is(':visible')){
				$("#bloqueFilroBusc").slideUp("fast");
				var textoActual = $("#lnkDesplBusc").attr("title");
				$("#lnkDesplBusc").attr("title", $("#txtBotonFiltrado").attr("value"));
				$("#txtBotonFiltrado").attr("value", textoActual);
			}
			return false;
		}
	}
} 

function poner_boton_cargando(){
	$('.recargando').submit(function(){
    	// On submit disable its submit button
    	var idHidden = $('input[type=submit]', this).attr('id');
    	var nameHidden = $('input[type=submit]', this).attr('name');
    	var valueHidden = $('input[type=submit]', this).attr('value');
    	if(idHidden != null && nameHidden != null && valueHidden != null ){
    		$("<input>", {
    	  		"type": "hidden",
		  		"class": "oculto",
		  		"id": idHidden,
		  		"name": nameHidden,
		  		"value": valueHidden
			}).appendTo(this);
    		$('input[type=submit]', this).attr('disabled', 'disabled');
    		$('input[type=submit]', this).removeClass('bpositivo');    		
    		$('input[type=submit]', this).addClass('botcarga');
    	}
	});
}

function abrirVentanaNueva (url,nombre,ancho,alto,resizable,scrollbars){
	var intResizable = 0;
	if (resizable){
		intResizable = 1;
	}
	var intScrollbars = 0;
	if (scrollbars){
		intScrollbars = 1;
	}
	var parametros = "toolbar=1,status=1,menubar=1";
	if (ancho != ""){
		parametros = parametros + ",width=" + ancho; 
	}
	if (alto != ""){
		parametros = parametros + ",height=" + alto; 
	}
	if (resizable != ""){
		parametros = parametros + ",resizable=" + intResizable; 
	}
	if (scrollbars != ""){
		parametros = parametros + ",scrollbars=" + intScrollbars; 
	} 
	window.open(url , nombre , parametros);
}

function ocultarMenuIzqVacio(){
	$("ul.listaizq  > li").each(function(){
		if ($(this).children("ul").html() == null
				|| $(this).children("ul").html() == "" ){
			$(this).addClass("oculto");
		}
	});

}
