/**********************************************************
Tipo:			Biblioteca de funciones Javascript
Versión: 		1.0
Descripción: 	Funciones genéricas Javascript
**********************************************************/

/**
@funcion	parsearURL(texto)
@desc	Parsea una URL cambiando los caracteres raros por %NN, donde NN es su código ASCII
@param	texto	texto a modificar
@return	texto modificado
*/
function parsearURL(texto){
	texto = texto.replace(/%/g,"%25");
	texto = texto.replace(/@/g,"%40");
	texto = texto.replace(/‘/g,"%91");
	texto = texto.replace(/’/g,"%92");
	texto = texto.replace(/“/g,"%93");
	texto = texto.replace(/”/g,"%94");
	texto = texto.replace(/•/g,"%95");
	texto = texto.replace(/–/g,"%96");
	texto = texto.replace(/—/g,"%97");
	texto = texto.replace(/˜/g,"%98");
	texto = texto.replace(/™/g,"%99");
	texto = texto.replace(/š/g,"%9A");
	texto = texto.replace(/›/g,"%9B");
	texto = texto.replace(/œ/g,"%9C");
	texto = texto.replace(/ž/g,"%9E");
	texto = texto.replace(/Ÿ/g,"%9F");
	texto = texto.replace(/¡/g,"%A1");
	texto = texto.replace(/¢/g,"%A2");
	texto = texto.replace(/£/g,"%A3");
	texto = texto.replace(/¥/g,"%A5");
	texto = texto.replace(/¦/g,"%A6");
	texto = texto.replace(/§/g,"%A7");
	texto = texto.replace(/¨/g,"%A8");
	texto = texto.replace(/©/g,"%A9");
	texto = texto.replace(/ª/g,"%AA");
	texto = texto.replace(/«/g,"%AB");
	texto = texto.replace(/¬/g,"%AC");
	texto = texto.replace(/­/g,"%AD");
	texto = texto.replace(/®/g,"%AE");
	texto = texto.replace(/¯/g,"%AF");
	texto = texto.replace(/°/g,"%B0");
	texto = texto.replace(/±/g,"%B1");
	texto = texto.replace(/²/g,"%B2");
	texto = texto.replace(/³/g,"%B3");
	texto = texto.replace(/´/g,"%B4");
	texto = texto.replace(/µ/g,"%B5");
	texto = texto.replace(/¶/g,"%B6");
	texto = texto.replace(/·/g,"%B7");
	texto = texto.replace(/¸/g,"%B8");
	texto = texto.replace(/¹/g,"%B9");
	texto = texto.replace(/º/g,"%BA");
	texto = texto.replace(/»/g,"%BB");
	texto = texto.replace(/¼/g,"%BC");
	texto = texto.replace(/½/g,"%BD");
	texto = texto.replace(/¾/g,"%BE");
	texto = texto.replace(/¿/g,"%BF");
	texto = texto.replace(/À/g,"%C0");
	texto = texto.replace(/Á/g,"%C1");
	texto = texto.replace(/Â/g,"%C2");
	texto = texto.replace(/Ã/g,"%C3");
	texto = texto.replace(/Ä/g,"%C4");
	texto = texto.replace(/Å/g,"%C5");
	texto = texto.replace(/Æ/g,"%C6");
	texto = texto.replace(/Ç/g,"%C7");
	texto = texto.replace(/È/g,"%C8");
	texto = texto.replace(/É/g,"%C9");
	texto = texto.replace(/Ê/g,"%CA");
	texto = texto.replace(/Ë/g,"%CB");
	texto = texto.replace(/Ì/g,"%CC");
	texto = texto.replace(/Í/g,"%CD");
	texto = texto.replace(/Î/g,"%CE");
	texto = texto.replace(/Ï/g,"%CF");
	texto = texto.replace(/Ð/g,"%D0");
	texto = texto.replace(/Ñ/g,"%D1");
	texto = texto.replace(/Ò/g,"%D2");
	texto = texto.replace(/Ó/g,"%D3");
	texto = texto.replace(/Ô/g,"%D4");
	texto = texto.replace(/Õ/g,"%D5");
	texto = texto.replace(/Ö/g,"%D6");
	texto = texto.replace(/×/g,"%D7");
	texto = texto.replace(/Ø/g,"%D8");
	texto = texto.replace(/Ù/g,"%D9");
	texto = texto.replace(/Ú/g,"%DA");
	texto = texto.replace(/Û/g,"%DB");
	texto = texto.replace(/Ü/g,"%DC");
	texto = texto.replace(/Ý/g,"%DD");
	texto = texto.replace(/Þ/g,"%DE");
	texto = texto.replace(/ß/g,"%DF");
	texto = texto.replace(/à/g,"%E0");
	texto = texto.replace(/á/g,"%E1");
	texto = texto.replace(/â/g,"%E2");
	texto = texto.replace(/ã/g,"%E3");
	texto = texto.replace(/ä/g,"%E4");
	texto = texto.replace(/å/g,"%E5");
	texto = texto.replace(/æ/g,"%E6");
	texto = texto.replace(/ç/g,"%E7");
	texto = texto.replace(/è/g,"%E8");
	texto = texto.replace(/é/g,"%E9");
	texto = texto.replace(/ê/g,"%EA");
	texto = texto.replace(/ë/g,"%EB");
	texto = texto.replace(/ì/g,"%EC");
	texto = texto.replace(/í/g,"%ED");
	texto = texto.replace(/î/g,"%EE");
	texto = texto.replace(/ï/g,"%EF");
	texto = texto.replace(/ð/g,"%F0");
	texto = texto.replace(/ñ/g,"%F1");
	texto = texto.replace(/ò/g,"%F2");
	texto = texto.replace(/ó/g,"%F3");
	texto = texto.replace(/ô/g,"%F4");
	texto = texto.replace(/õ/g,"%F5");
	texto = texto.replace(/ö/g,"%F6");
	texto = texto.replace(/÷/g,"%F7");
	texto = texto.replace(/ø/g,"%F8");
	texto = texto.replace(/ù/g,"%F9");
	texto = texto.replace(/ú/g,"%FA");
	texto = texto.replace(/û/g,"%FB");
	texto = texto.replace(/ü/g,"%FC");
	texto = texto.replace(/ý/g,"%FD");
	texto = texto.replace(/þ/g,"%FE");
	texto = texto.replace(/ÿ/g,"%FF");
	return texto;
}	

/**
@funcion	esMozilla()
@desc	Determina si el navegador usado es mozilla
@return	true/false
*/
function esMozilla(){
	return (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
}

/**
@funcion	esIE()
@desc	Determina si el navegador usado es Internet Explorer
@return	true/false
*/
function esIE(){
	return (typeof window.ActiveXObject != 'undefined');	
}

/**
@funcion	obtenerScrollX()
@desc	Obtiene el scroll horizontal de la pagina actual
@return	Posición en pixels
*/
function obtenerScrollX(){
	if(navigator.appName == "Microsoft Internet Explorer")
		return document.documentElement.scrollLeft;
	else
		return window.pageXOffset;	
}

/**
@funcion	obtenerScrollY()
@desc	Obtiene el scroll vertical de la pagina actual
@return	Posición en pixels
*/
function obtenerScrollY(){
	if(navigator.appName == "Microsoft Internet Explorer")
		return document.documentElement.scrollTop;
	else
		return window.pageYOffset;	
}

/**
@funcion	anchoPagina()
@desc	Obtiene el ancho total de la pagina en pixels
@return	Ancho en pixels
*/
function anchoPagina(){
 	var de = document.documentElement;
 	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
 	return w;
}

/**
@funcion	altoPagina()
@desc	Obtiene el alto total de la pagina en pixels
@return	Alto en pixels
*/
function altoPagina(){
	var de = document.documentElement;
 	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
 	return h;
}

/**
@funcion	abrirVentana(objVentana)
@desc	Muestra la ventana pasada (nodo DOM) en forma de popup emergente
@param	objVentana	Nodo DOM a mostrar
*/
function abrirVentana(objVentana){
	//creacion del fondo para oscurecer el resto de la pagina
	var fondo = document.createElement("div");
	fondo.className = "divFondoVentana"; //lo ponemos para ocultarlo despues
	fondo.style.top = "0px";
	fondo.style.left = "0px";
	fondo.style.width = anchoPagina()+"px";
	fondo.style.height = altoPagina()+"px";
	fondo.style.background = "url(/img/fondo-trans.gif)";
    fondo.style.zIndex = 100;
	//mantenemos la ventana en la misma posicion independientemente del scroll
	if(esIE()){
		fondo.style.position = "absolute";
		fondo.style.top = obtenerScrollY()+"px";
		fondo.style.left = obtenerScrollX()+"px";
		window.onscroll = function(){
			fondo.style.top = obtenerScrollY()+"px";
			fondo.style.left = obtenerScrollX()+"px";
		}
	}else{
		fondo.style.position = "fixed";
		fondo.style.top = "0px";
		fondo.style.left = "0px";
	}
	
	//intentamos centrar la ventana en base a su ancho
	objVentana.style.position = "absolute";
	if(objVentana.style.width.indexOf("px")>0){
		var ancho = objVentana.style.width;
		ancho = ancho.substring(0,ancho.length-2);
		objVentana.style.left = parseInt(window.screen.width/2-ancho/2)+"px";
	}
	
	//intentamos centrar la ventana en base a su alto
	if(objVentana.style.height.indexOf("px")>0){
		var alto = objVentana.style.height;
		alto = alto.substring(0,alto.length-2);
		objVentana.style.top  = parseInt(window.screen.height/2-alto/2-100)+"px";
	}
	
	//ponemos el nodo al fondo y al documento
	fondo.appendChild(objVentana);
	//ponemos invisibles todos los fondos anteriores
	cerrarVentana();
	document.body.appendChild(fondo);
}

/**
@funcion	cerrarVentana()
@desc	Cierra la última ventana mostrada
*/
function cerrarVentana(){
	for(var i=0;i<document.body.childNodes.length;i++){
		var nodo = document.body.childNodes[i];
		if(nodo && nodo.className && nodo.className.indexOf("divFondoVentana")>-1)
			nodo.style.display = "none";
	}
}

/**
@funcion    esEnter(e)
@desc   Devuelve true si se ha pulsado el enter (llamar desde onkeypress='esEnter(event)')
@return True si se ha pulsado enter 
*/
function esEnter(e){
    var characterCode;                            
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4s which property
    }else{
        e = event;
        characterCode = e.keyCode; //character code is contained in IEs keyCode property
    }
    
    //comprobamos el enter
    if(characterCode == 13){ 
        return true;
    }                                
}

/**
@funcion    esperar(milisegundos)
@desc   Espera el número de milisegundos pasado
@param    milisegundos    Tiempo en milisegundos que debe esperar el navegador
*/
function esperar(milisegundos){
    var esperando = true;
    var ahora = new Date();
    var alarma;
    var comienzoSegs = ahora.getTime();
    while(esperando){
        alarma = new Date();                   
        if(alarma.getTime() - comienzoSegs > milisegundos)
            esperando = false; 
    }      
}
