/* Funcion para añadir eventos al cargar la pagina */

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/* Función para el Paginado */

function paginadoListado(mipagina) {
	var ff=window.document.bava;
	ff.pagina.value=mipagina;
	ff.submit();
}

/* Funcion para el Pop-Up Genérico */

function VPrint() {

	var patron = "";
	patron = new RegExp("contenido-corto");
	if (patron.test(document.getElementById("contenedor").innerHTML)==true){
		width = 471+72;
	}

	patron = new RegExp("contenido-largo");
	if (patron.test(document.getElementById("contenedor").innerHTML)==true){
		width = 647+72;
	}

	patron = new RegExp("contenido-sin-derecha");
	if (patron.test(document.getElementById("contenedor").innerHTML)==true){
		width = 786+72;
	}



	window.open("/vprint.html", "","resizable=1,SCROLLBARS=YES,HEIGHT=697,WIDTH="+width);
}


/* Funcion para el Pop-Up */

function Zoom(idioma,link,parametros,ancho,alto) {
	window.open("/"+link+".html?"+parametros+"&Idioma="+idioma, "","resizable=1,SCROLLBARS=YES,HEIGHT="+alto+",WIDTH="+ancho);
}

function ZoomG(ruta,sPicURL) { 
	window.open("/zoom.php?Dir="+ruta+"&Doc="+sPicURL,"","resizable=1,HEIGHT=200,WIDTH=200");
}

/* Funcion para comprobar el Email */

function comprobarEmail(texto){

	var mailres = true;            
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
	var arroba = texto.indexOf("@",0);
	
	if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
	
	var punto = texto.lastIndexOf(".");      
	for (var contador = 0 ; contador < texto.length ; contador++){
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
			mailres = false;
			 break;
		}
	}

	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
		mailres = true;
	else
		mailres = false;

	return mailres;
}

function SimpleSwap(el, which) {
    el.src = el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup() {
    var x = document.getElementsByTagName("img");
    for (var i = 0; i < x.length; i++) {
        var oversrc = x[i].getAttribute("oversrc");
        if (!oversrc) {
            continue;
        }
        x[i].oversrc_img = new Image;
        x[i].oversrc_img.src = oversrc;
        x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
        x[i].onmouseout = new Function("SimpleSwap(this);");
        x[i].setAttribute("origsrc", x[i].src);
    }
}

function H4_Replace_Setup(etiqueta) {
    var x = document.getElementsByTagName(etiqueta);
    for (var i = 0; i < x.length; i++) {
        var texto = x[i].innerHTML;
        if (!texto) {
            continue;
        }
        mi_id = x[i].id = etiqueta+"_"+i;
        H1_x_Div(x[i]);

	link = x[i].getAttribute("href");
	width = str_replace("px","",getStyle(mi_id,'width'));
	height = str_replace("px","",getStyle(mi_id,'height'));

        H4_Flash(x[i].id,x[i].className,texto,width,height,link);
    }
}

function H1_x_Div(x){
	var replace = document.createElement('div');
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id)
		{
			var y = replace.cloneNode(true);
			x[i].replaceChild(y,x[i].firstChild);
		}
	}
}

function H4_Flash(id,estilo,texto,width,height,link) {
	var so = new SWFObject("/media/Flash/"+estilo+".swf", "f"+id, width, height, "8", "#FFFFFF");
	so.addVariable("mitexto", texto);
	so.addVariable("miid", id);
	so.addVariable("miurl", link);
	so.addParam("wmode", "transparent");
	so.addParam("menu", "false");
	so.write(id);
}


function getStyle(el,styleProp){
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function str_replace(busca, repla, orig){
	str 	= new String(orig);
	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);
	str = new String(str.replace(rExp, newS));
	return str;
}

function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length; i++)
            {
                  if (charSearchFor == Mid(strSearch, i, 1))
                  {
                        return i;
                  }
            }
            return -1;
}


function url_nula() {
	void(0);
}

/* Funcion para buscar una cadena dentro de una variable */

function stristr( haystack, needle) {
    var pos = 0;
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if( pos == -1 ){
    	return false;
    }
    else{
	return true;
    }
}



/********************* MENUS **************************************/

function Menu(){
	var menu = new Array();
	var valores = window.document.getElementById("mi_menu");
	var x = valores.getElementsByTagName("a");
	for(i=0;i<x.length;i++){
		menu[i] = x[i].innerHTML+";"+x[i].href+";"+x[i].name+";"+x[i].target;
	}
	return(menu);
}

function Submenu(){
	var menu = new Array();
	var valores = window.document.getElementById("mi_submenu");
	var x = valores.getElementsByTagName("a");
	for(i=0;i<x.length;i++){
		menu[i] = x[i].innerHTML+";"+x[i].href+";"+x[i].name+";"+x[i].target;
	}
	return(menu);
}

function fl_menu_superior() {
	
	var so = new SWFObject("/media/flash/menu_superior.swf", "menu", "786", "28", "8");
	so.addParam("wmode", "transparent/opaque");
	so.write("menu_superior");
	
}

function fl_menu_inferior() {
	
	var so = new SWFObject("/media/flash/menu_inferior.swf", "submenu", "786", "28", "8");
	so.addParam("wmode", "transparent/opaque");
	so.write("menu_inferior");
	
}



/***************************** JS ********************************/

function abrirDocumento(carpeta,documento){
	if(documento>0 || documento!=0){
		document.location.href = "/descargar.php?carpeta="+carpeta+"&doc="+documento;
	}
}

function abrirDocumento2(documento){
	if(documento>0 || documento!=0){
		aux = documento.split(":");
		carpeta = aux[0];
		documento = aux[1];
		nombre = aux[2];
		location.href = "/descargar.php?carpeta="+carpeta+"&doc="+documento+"&nom="+nombre;
	}
}

function getRadio(ctrl){
	for(i=0;i<ctrl.length;i++)
		if(ctrl[i].checked) return ctrl[i].value;
}

function vaciar(campo){
	campo.value="";
}

function EnJuego(tipo,fechas,pagina) {

	var myConn2 = new XHConn();
	if (!myConn2)
		alert("XMLHTTP not available. Try a newer/better browser.");
	var EnJuegoFunc = function (oXML) {
		if (oXML.responseText!=''){
			datos = oXML.responseText;
		}
		window.document.getElementById("div-enjuego").innerHTML = datos;
	};

	EJ(tipo,fechas,pagina);
		
	function EJ(tipo,fechas,pagina) {
		myConn2.connect("/inc/incAjaxEnJuego.php", "GET", "Fechas="+fechas+"&Pagina="+pagina+"&Tipo="+tipo, EnJuegoFunc);
	}

}

function strtr(str,from,to) {

    var fr = '', i = 0, j = 0, lenStr = 0, lenFrom = 0;
    var tmpFrom = [];
    var tmpTo   = [];
    var ret = '';
    var match = false;

    if (typeof from === 'object') {
        this.krsort(from);
        for (fr in from) {
            tmpFrom.push(fr);
            tmpTo.push(from[fr]);
        }

        from = tmpFrom;
        to   = tmpTo;
    }
    
    lenStr  = str.length;
    lenFrom = from.length;
    for (i = 0; i < lenStr; i++) {
        match = false;
        for (j = 0; j < lenFrom; j++) {
            if (str.substr(i, from[j].length) == from[j]) {
                match = true;

                // Fast forward
                i = (i + from[j].length)-1;
                
                break;
            }
        }
        
        if (false !== match) {
            ret += to[j];
        } else {
            ret += str[i];
        }
    }

    return ret;
}

function elimina_acentos(cadena){
	tofind = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ,;.:-_´¨+*`[]'¡?¿ºª!·$%&/()=?¿|@#~’";
	replac = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn                                   ";
	return(strtr(cadena,tofind,replac));
}

function Arregla_Rewrite(texto) {
	texto = elimina_acentos(texto);
	texto = str_replace('"',"",texto);
	texto = str_replace("  "," ",texto);
	texto = str_replace("  "," ",texto);
	texto = str_replace("  "," ",texto);
	texto = str_replace("  "," ",texto);
	texto = str_replace(" ","_",texto);
	return texto;
}

function getHeight(div){
	return document.getElementById(div).offsetHeight;
}

function getWidth(div){
	return document.getElementById(div).offsetWidth;
}

function setHeight(div,num){
	document.getElementById(div).style.height = num+"px";
}

function setWidth(div,num){
	document.getElementById(div).style.width = num+"px";
}

function reducirDiv(div,max){
	if(getHeight(div)<max){
		setHeight(div,max);
	}
}

function borrarDocumento(id,valor){

	if(confirm("¿Desea realmente borrar este documento?")){

		var myConn2 = new XHConn();
		if (!myConn2)
			alert("XMLHTTP not available. Try a newer/better browser.");
		var borrarDocumentoFunc = function (oXML) {
			if (oXML.responseText!=''){
				datos = oXML.responseText;
			}
			window.document.getElementById("ruta_Documento").innerHTML = "<span id='completado'>Documento eliminado correctamente.</div>";
			window.document.getElementById("nombres").style.display = "none";
			window.document.getElementById("Documento").value = "";
		};
		BD(id,valor);
	}

	function BD(id,valor) {
		myConn2.connect("/borrar_documento.php", "GET", "Id="+id+"&Valor="+valor, borrarDocumentoFunc);
	}
	
}

function Reordenar(campo,tipo){

	var myConn2 = new XHConn();
	if (!myConn2)
		alert("XMLHTTP not available. Try a newer/better browser.");
	var ReordenarFunc = function (oXML) {
		if (oXML.responseText!=''){
			datos = oXML.responseText;
		}
		location.href = location.href;
	};

	RO(campo,tipo);

	function RO(campo,tipo) {
		myConn2.connect("/inc/incAjaxReordenar.php", "GET", "Campo="+campo+"&Tipo="+tipo, ReordenarFunc);
	}

}


/** NUEVO MENU **/

function RedimensionarMenu(){

	if(window.document.getElementById("menu_nuevo_superior")){

		var widthTotal = $("#menu_nuevo_superior").width();
		var enlaces = $("#menu_nuevo_superior a");
		var total = enlaces.length;
		var widthMenu = 0;
		for(i=0; i<total; i++){
			widthMenu += $(enlaces[i]).width();
		}

		if(widthTotal > widthMenu){
			var resto =  widthTotal - widthMenu;
			var elem = $("#sinmenu").width();
			var pRight = parseInt($("#sinmenu").css("padding-right"));
			total = elem + resto + pRight;
			$("#sinmenu").width(total);
			$("#sinmenu").css("padding-right","0px");
			alert(widthTotal + " " + widthMenu + " " + total);
		}

	}

	if(window.document.getElementById("menu_nuevo_inferior")){

		var widthTotal = $("#menu_nuevo_inferior").width();
		var enlaces = $("#menu_nuevo_inferior a");
		var total = enlaces.length;
		var widthMenu = 0;
		for(i=0; i<total; i++){
			widthMenu += $(enlaces[i]).width();
		}

		if(widthTotal > widthMenu){
			var resto =  widthTotal - widthMenu;
			var elem = $("#sinsubmenu").width();
			var pRight = parseInt($("#sinsubmenu").css("padding-right"));
			total = elem + resto + pRight;
			$("#sinsubmenu").width(total);
			$("#sinsubmenu").css("padding-right","0px");
		}

	}

}

//addLoadEvent(RedimensionarMenu);

addLoadEvent(SimpleSwapSetup);

var H_Replace =(window.onload)? window.onload : function(){};
window.onload = function(){ H_Replace(); H4_Replace_Setup('h2'); }
//window.onload = function(){ H_Replace(); H4_Replace_Setup('h3'); H4_Replace_Setup('h2'); H4_Replace_Setup('h1'); }
//window.onload = function(){ fl_menu_superior(); fl_menu_inferior(); H_Replace(); H4_Replace_Setup('h3'); H4_Replace_Setup('h2'); H4_Replace_Setup('h1'); }
