function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function paginadoListado(mipagina) {
	var ff=window.document.bava;
	ff.pagina.value=mipagina;
	ff.submit();
}

function ZoomG(ruta,sPicURL) { 
	window.open("/zoom.php?Dir="+ruta+"&Doc="+sPicURL,"","resizable=1,HEIGHT=200,WIDTH=200");
}

function VPrint(idioma) {
	window.open("/vprint.html?Idioma="+idioma, "","resizable=1,SCROLLBARS=YES,HEIGHT=600,WIDTH=653");
}

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 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 stristr( haystack, needle) {
    var pos = 0;
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if( pos == -1 ){
    	return false;
    }
    else{
	return true;
    }
}

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 getRadio(ctrl){
	for(i=0;i<ctrl.length;i++)
		if(ctrl[i].checked) return ctrl[i].value;
}

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 vaciar(dato){
	dato.value = "";
}

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;
}


addLoadEvent(SimpleSwapSetup);

function ReemplazarCufon(){
	Cufon.replace("h2",{ fontFamily:"BabelSans" });
	Cufon.replace("h3",{ fontFamily:"BabelSans", hover: true, hoverables: { a:true } });
}

function RedimensionarContenido(){

	if(window.document.getElementById("print")){
		heightIzq = window.document.getElementById("print").offsetHeight;
		heightDer = window.document.getElementById("derecha").offsetHeight;
		
		var margen = 4 + 12 + 2;
				
		if(heightIzq > heightDer){
			heightIzq -= margen;
			window.document.getElementById("derecha").style.height = heightIzq + "px";
		}
	}
	

	else if(window.document.getElementById("zp")){
		heightIzq = window.document.getElementById("zp").offsetHeight;
		heightDer = window.document.getElementById("derecha").offsetHeight;
		
		var margen = 4 + 12 + 2;
				
		if(heightIzq > heightDer){
			heightIzq -= margen;
			window.document.getElementById("derecha").style.height = heightIzq + "px";
		}
	}
	
}

function fl_banner(tabla,idioma) {
	var so = new SWFObject("/media/flash/slide.swf", "pbl_"+tabla, "184", "99", "7", "#FFFFFF");
	so.addVariable("imglist_fn", "/slide.php");
	so.addVariable("tabla", tabla);
	so.addVariable("idioma", idioma);
	so.addVariable("interval", "3500");
	so.addVariable("w", "184");
	so.addVariable("h", "99");
	so.addParam("wmode", "opaque");
	so.addParam("menu", "false");
	so.write("fl_"+tabla);
}

function ReloadCategorias(categoria,idioma){

	var myConn2 = new XHConn();
	if (!myConn2) { alert("XMLHTTP not available. Try a newer/better browser."); }
	if(categoria>0) {
		var ReloadCategoriasFunc = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText;
				window.document.getElementById("videos-ajax").innerHTML = datos;
			}
		};
		RCF(categoria,idioma);
	}
	function RCF(categoria,idioma) {
		myConn2.connect("/inc/incAjaxReloadCategorias.php", "GET", "Categoria="+categoria+"&Idioma="+idioma, ReloadCategoriasFunc);
	}

}

function ReloadVideo(video,idioma){

	var myConn2 = new XHConn();
	if (!myConn2) { alert("XMLHTTP not available. Try a newer/better browser."); }
	var ReloadVideoFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
			window.document.getElementById("video-grande").innerHTML = datos;
		}
	};
	RVF(video,idioma);
	function RVF(video,idioma) {
		myConn2.connect("/inc/incAjaxReloadVideo.php", "GET", "Enlace="+video+"&Idioma="+idioma, ReloadVideoFunc);
	}

}
