/*
	UNIVERSIDAD DE ALCALA

	SCRIPTS

	Borja Delgado	- http://www.orangeside.org
	12 - 2006
*/

if(!window.EXTRAS) EXTRAS = {};

UAH = {

	preventExplorerFlicker : function() { 
			try {
				document.execCommand("BackgroundImageCache", false, true);
			} catch(err) {}
	},

	/*
		ADAPTED FROM:	Sergi Messeguer
									http://www.zigotica.com/
	*/

	popThisUp : function(obj) {
			obj.onclick = function() {
				window.open(obj.href, '');
				return false;
			}
	},

	autoPopup : function() {
			var pops = EXTRAS.getElementsByClass('popup',null , 0);
			for (var i = 0; i < pops.length; i++) {
					UAH.popThisUp(pops[i]);
			}
	},

	addStyles : function() {
		
		//Parche para solucionar la existencia de enlaces a un ancla ir_contenido que no existe
		//var cuerpo = document.getElementsByTagName('body')[0];
		//cuerpo.innerHTML=cuerpo.innerHTML.replace(/href=\"#ir_contenido\"/g,"href=\"#\"");
		
		//Arranco aquí la función que indica la posición del tabulador aprovechando esta función
		//para no tener que modificar la plantilla y tener que actualizar todas las páginas
		//Tapar esta línea para desactivar la ayuda del tabulador
		iniciarAyudaTabulador();
		
		//alert("En la función");
		
		var css_actual = UAH.getActiveStyleSheet();
		//alert("CSS actual = "+css_actual);

        if (document.getElementById('cambio_estilos'))
		{
			//Mostramos opción de alto contraste o bajo contraste según el tipo de contraste actual
			if (css_actual != "alto_contraste")
			{
				//document.getElementById('cambio_estilos').innerHTML = '<li><a href="javascript:void(null);" id="texto_menor" tabindex="5" title="Tama&ntilde;o de letra por defecto" class="cambio_estilos"><span class="letra_peq" id="letra_peq_span">A</span></a></li><li><a href="javascript:void(null);" id="texto_medio" tabindex="6" title="Tama&ntilde;o de letra medio" class="cambio_estilos"><span class="letra_med" id="letra_med_span">A</span></a></li><li><a href="javascript:void(null);" id="texto_mayor"  tabindex="7" title="Tama&ntilde;o de letra mayor" class="cambio_estilos"><span class="letra_gran" id="letra_gran_span">A</span></a></li><li><a href="javascript:void(null);" id="alto_contraste"  tabindex="8" title="Contraste alto" class="cambio_estilos"><span class="letra_peq">Contraste alto</span></a></li>';
				document.getElementById('cambio_estilos').innerHTML = '<a href="javascript:void(null);" id="alto_contraste"  tabindex="8" title="Contraste alto" class="cambio_estilos"><span class="letra_peq">Contraste alto</span></a>';
				
				var d = document.getElementById('alto_contraste');
			}
			else
			{
				//document.getElementById('cambio_estilos').innerHTML = '<li><a href="javascript:void(null);" id="texto_menor" tabindex="29" title="Tama&ntilde;o de letra por defecto" class="cambio_estilos"><span class="letra_peq" id="letra_peq_span"></span></a></li><li><a href="javascript:void(null);" id="texto_medio" tabindex="30" title="Tama&ntilde;o de letra medio" class="cambio_estilos"><span class="letra_med" id="letra_med_span"></span></a></li><li><a href="javascript:void(null);" id="texto_mayor"  tabindex="31" title="Tama&ntilde;o de letra mayor" class="cambio_estilos"><span class="letra_gran" id="letra_gran_span"></span></a></li><li><a href="javascript:void(null);" id="normal_contraste"  tabindex="33" title="Contraste normal" class="cambio_estilos"><span class="letra_peq">Contraste normal</span></a></li>';
				document.getElementById('cambio_estilos').innerHTML = '<a href="javascript:void(null);" id="normal_contraste"  tabindex="33" title="Contraste normal" class="cambio_estilos"><span class="letra_peq">Contraste normal</span></a>';
				
				var d = document.getElementById('normal_contraste');
			}
		}
			
        var a = document.getElementById('texto_menor');
        var b = document.getElementById('texto_medio');
        var c = document.getElementById('texto_mayor');		

        a.onclick = a.onkeypress = b.onclick = b.onkeypress = c.onclick = c.onkeypress = d.onclick = d.onkeypress = function (e) {
			//alert("entrando en la función" + this.id);
			
            if (!e) e = window.event;
            if(e.type == "keypress" && e.keyCode != 13) return;
            if(e.type == "click") this.blur();

			//Si queremos contraste normal cargamos la hoja de texto menor
			this.id=="normal_contraste" ? UAH.setActiveStyleSheet("texto_menor") : UAH.setActiveStyleSheet(this.id);
			
			//Modificamos el contenido del enlace si hemos pinchado sobre alguna opción de contraste
			if (this.id == "alto_contraste")
			{
				this.innerHTML="Contraste normal";
				this.id="normal_contraste";
				this.title="Contraste normal";
				document.getElementById("letra_peq_span").innerHTML = document.getElementById("letra_med_span").innerHTML = document.getElementById("letra_gran_span").innerHTML = "";
			}
			else if (this.id == "normal_contraste")
			{
				this.innerHTML="Contraste alto";
				this.id="alto_contraste";
				this.title="Contraste alto";
				document.getElementById("letra_peq_span").innerHTML = document.getElementById("letra_med_span").innerHTML = document.getElementById("letra_gran_span").innerHTML = "A";
			}
			
            return false;
        }
	},

	addPrinter : function() {
        if(document.getElementById('imprimir')) document.getElementById('imprimir').innerHTML = '<a href="#" title="Imprimir la p&aacute;gina" onclick=" window.print(); return false;" accesskey="4" title="Imprimir pagina ( alt + 4 )" id="enlace_imprimir" tabindex="9000">Imprimir</a> |';
    },
	
	addStartPage : function() {
        if(document.getElementById('haz_pagina_inicio')) document.getElementById('haz_pagina_inicio').innerHTML = '<a href="http://www.uah.es" onclick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.uah.es\');return false;" style="cursor: hand">Haz de la UAH tu p&aacute;gina de inicio</a>';
    },

	/*
		ADAPTED FROM:	A List Apart - Working With Alternate Style Sheets
									http://www.alistapart.com/stories/alternate/
	*/

	initiateStyles : function(e) {
		var cookie = UAH.readCookie("style");
		var title = cookie ? cookie : UAH.getPreferredStyleSheet();
		if ((title!="texto_menor") && (title!="texto_medio") && (title!="texto_mayor") && (title!="alto_contraste")) 
		 { 
		    title = "texto_menor";
		 }
		UAH.setActiveStyleSheet(title);
	},

	setSheet : function(e) {
		var title = UAH.getActiveStyleSheet();
		UAH.createCookie("style", title, 365);
	},

	setActiveStyleSheet : function(title) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel") != null && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				a.disabled = true;
				if(a.getAttribute("title") == title) a.disabled = false;
			}
		//Modificamos el logotipo. Esto no es diseño bonito, pero tal y como está montada la página es la solución
		//menos costosa
		if (title == "alto_contraste")
			document.getElementsByTagName("img")[0].src="/grafica/comunes/universidad_alcala_logotipo_accesible.gif";
		else
			document.getElementsByTagName("img")[0].src="/grafica/comunes/universidad_alcala_logotipo.gif";
		}
	},

	getActiveStyleSheet : function() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel") != null && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
		}
		return null;
	},

	getPreferredStyleSheet : function() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1
				 && a.getAttribute("rel").indexOf("alt") == -1
				 && a.getAttribute("title")
				 ) return a.getAttribute("title");
		}
		return null;
	},

	createCookie : function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},

	readCookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
}

//Busqueda de personal
function cargarVisual(valor1,valor2)
  {
	  var opciones="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=508,height=440,top=170,left=220";

      var titulo="";
      window.open("visualizar.asp?id1="+document.getElementById("idpersona1").value+"&id2="+document.getElementById("idpersona2").value,titulo,opciones);
  }
  
function mayus()
  {
	form13.Nomb.value=form13.Nomb.value.toUpperCase();
	form13.Nomb.value=acentos(form13.Nomb.value);
	form13.Ape1.value=form13.Ape1.value.toUpperCase();
	form13.Ape1.value=acentos(form13.Ape1.value);
	form13.Ape2.value=form13.Ape2.value.toUpperCase();
	form13.Ape2.value=acentos(form13.Ape2.value);
	form13.submit();
  }
  
function cambiarValor(i)
  {
	  switch (i)
	   {
	    case 1: document.getElementById("idpersona1").value="";break;
		case 2: document.getElementById("idpersona2").value="";break;
		case 3: document.getElementById("comboPuesto").value="";
				document.getElementById("Nomb").value="";
				document.getElementById("Ape1").value="";
				document.getElementById("Ape2").value=""; break;
		case 4: document.getElementById("comboCargo").value="";
				document.getElementById("Nomb").value="";
				document.getElementById("Ape1").value="";
				document.getElementById("Ape2").value=""; break;
		case 5: document.getElementById("comboPuesto").value="";
				document.getElementById("comboCargo").value="";break;
	   }
  }
function acentos(x) 
  {

	x = x.replace(/À/g,"A");    x = x.replace(/È/g,"E");	x = x.replace(/Ì/g,"I");
	x = x.replace(/Á/g,"A");    x = x.replace(/É/g,"E");	x = x.replace(/Í/g,"I");
	x = x.replace(/Â/g,"A");    x = x.replace(/Ê/g,"E");	x = x.replace(/Î/g,"I");
	x = x.replace(/Ä/g,"A");    x = x.replace(/Ë/g,"E");	x = x.replace(/Ï/g,"I");
	
	x = x.replace(/Ò/g,"O");	x = x.replace(/Ù/g,"U");
	x = x.replace(/Ó/g,"O");	x = x.replace(/Ú/g,"U");
	x = x.replace(/Ô/g,"O");	x = x.replace(/Û/g,"U");
	x = x.replace(/Ö/g,"O");	x = x.replace(/Ü/g,"U");
		
	return x
}

function esMail(Cadena) {

	Punto = Cadena.substring(Cadena.lastIndexOf('.') + 1, Cadena.length)			// Cadena del .com
	Dominio = Cadena.substring(Cadena.lastIndexOf('@') + 1, Cadena.lastIndexOf('.')) 	// Dominio @lala.com
	Usuario = Cadena.substring(0, Cadena.lastIndexOf('@'))					// Cadena lalala@
	Reserv = "@/º\"\'+*{}\\<>?¿[]áéíóú#·¡!^*;,:"						// Letras Reservadas
	
	// Añadida por El Codigo para poder emitir un alert en funcion de si email valido o no
	valido = true
	
	// verifica qie el Usuario no tenga un caracter especial
	for (var Cont=0; Cont<Usuario.length; Cont++) {
		X = Usuario.substring(Cont,Cont+1)
		if (Reserv.indexOf(X)!=-1)
                	valido = false
	}

	// verifica qie el Punto no tenga un caracter especial
	for (var Cont=0; Cont<Punto.length; Cont++) {
		X=Punto.substring(Cont,Cont+1)
		if (Reserv.indexOf(X)!=-1)
			valido = false
	}
                        
	// verifica qie el Dominio no tenga un caracter especial
	for (var Cont=0; Cont<Dominio.length; Cont++) {
		X=Dominio.substring(Cont,Cont+1)
		if (Reserv.indexOf(X)!=-1)
			valido = false
		}

	// Verifica la sintaxis básica.....
	if (Punto.length<2 || Dominio <1 || Cadena.lastIndexOf('.')<0 || Cadena.lastIndexOf('@')<0 || Usuario<1) {
		valido = false
	}

	return valido
}

/***
FUNCIONES DE AYUDA PARA VER VISUALMENTE LA POSICION DEL TABULADOR
***/
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function colocarIndicadorTab()
{
		posX=findPosX(document.getElementById(document.activeElement.id));
		posY=findPosY(document.getElementById(document.activeElement.id));	
		
		document.getElementById("tabPosicion").style.top=(posY-10)+"px";
		document.getElementById("tabPosicion").style.left=(posX-32)+"px";	
}

function iniciarAyudaTabulador()
{
	var cuerpo = document.getElementsByTagName('body')[0];
	
	cuerpo.onkeyup=function avisoTab(e)
	{
		var keyCode;
		var posX=0;
		var posY=0;
		
		if (!e) e = window.event;

		if ("which" in e)
		{// NN4 & FF & Opera
			keyCode=e.which;
		} else if ("keyCode" in e)
		{// Safari & IE4+
			keyCode=e.keyCode;
		} else if ("keyCode" in window.event)
		{// IE4+
			keyCode=window.e.keyCode;
		} else if ("which" in window.event)
		{
			keyCode=e.which;
		} else  {    alert("the browser don't support");  }
		
		//alert(keyCode);

		if (keyCode == 9)
		{
	//		alert(document.activeElement.id);
			
			if (document.activeElement.id=="")// || document.activeElement.id.indexOf("mp_")==0)
				document.getElementById("tabPosicion").style.top="-1000px";
			else
			{
				colocarIndicadorTab();
				/*
				posX=findPosX(document.getElementById(document.activeElement.id));
				posY=findPosY(document.getElementById(document.activeElement.id));	
			
				//alert(posX);
			
				document.getElementById("tabPosicion").style.top=(posY-10)+"px";
				document.getElementById("tabPosicion").style.left=(posX-32)+"px";
				*/
			}
		}
	}
}

/***
FIN FUNCIONES DE AYUDA PARA VER VISUALMENTE LA POSICION DEL TABULADOR
***/
