// ********************* Funciones Para los menus que siempre te salgan en pantalla con el nombre tabla_desplazar
function findScrollTop() {
	if (window.pageYOffset != null)
		return window.pageYOffset;
	if (document.body.scrollWidth != null)
		return document.body.scrollTop;
	return null;
}
var offsetTablaInicial = null;

function miOnScroll() {
	var top;
	//alert("scroll");
	if ( offsetTablaInicial==null) {
		offsetTablaInicial = document.all["tabla_desplazar"].offsetTop;
	} else {
		top = findScrollTop();
		if (top!=null) {
			if (top>offsetTablaInicial) {
				document.all["tabla_desplazar"].style.top = top;				
			} else {
				document.all["tabla_desplazar"].style.top = offsetTablaInicial;
			}
		}				
	}
}

function eliminarEntradaAsiento(numero) {
	if (confirm("¿Seguro que desea eliminar esta linea?")) {
		eval("document.formulario.importe_"+numero+".value='';");
		eval("document.formulario.cuenta_"+numero+".value='';");
	}
	return;
}


function numDigitos( valor, ancho) {
		var res, i;
		res = valor +"";
		for (i=(res+"").length; i<ancho; i++) {
			res ="0"+ res;
		}
		return res;
	}

function changeDisplay(id) {
	//Permite ocultar o ver un elemento:
	var nodo=document.all[id];
	if (nodo.style.display=="none") {
		nodo.style.display="block";
	} else {
		nodo.style.display="none";
	}
}

function openWindow(url, ancho, alto) {
	strancho=ancho;
	stralto=alto;
	if (ancho==null) strancho = 600;
	if (alto==null) stralto = 360;
	window.open(url,"","resizable=yes,status=1,scrollbars=1,width="+strancho+",height="+stralto,false);
}

function esVacio(cadena) {
	if (trim(cadena)=="") return true;
	else return false;
}

function openLogin() {
	var url="popup_login.php";
	window.open(url,"","resizable=0,status=0,scrollbars=0,width=200,height=140",false);
	//window.open(url,"","",false);
}

function openBigWindow(url) {
	window.open(url,"","resizable=yes,status=1,scrollbars=1,width=540,height=480",false);
}



function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function resaltarGuardar() {
	var element = document.getElementById("boton_guardar");
	if (element!=null) {
		element.innerHTML ="* Guardar *";
	}
}

function abrirMail() {
	if (event.ctrlKey) {
		event.returnValue=false;
		window.open('http://www.varaderoweb.com/cgi-bin/sqwebmail');
		return false;
	}
}

function getElement( id ) {
	var element;
	if (document.getElementById==null) {
		element = document.all[id];
	} else {
		element =document.getElementById(id);
	}
	return element;
}

function getElementDoc( ndoc, id) {
	var doc;
	doc = ndoc;
	if (doc==null) doc = document;
	var element;
	if (doc.getElementById==null) {
		element = doc.all[id];
	} else {
		element =doc.getElementById(id);
	}
	return element;
}


function toggleOcultacion(partner) {
	var table = getElement(partner);
	if (table!=null) {
		if (table.style.display=="none") table.style.display="block";
		else table.style.display="none";
	}
}

function mostrarOcultacion(partner) {
	var table = getElement(partner);
	table.style.display="block";	
}

function ocultarOcultacion(partner) {
	var table = getElement(partner);
	table.style.display="none";	
}


function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = true;
function preloadImages() {
	if (document.images) {
		preloadFlag = true;
	}
}

function onchange_data() {
	var estado="";

	var f = document.formulario;
	var idtipo_recurso=f.idtipo_recurso.value;
	var seleccionar=f.seleccionar.value;
	var tipo=f.tipo.value;
	if (f["estado"]!=null) {
		estado=f["estado"].value;
	}
	var q=f.q.value;
	window.location.href="gestion_de_recursos.asp?seleccionar="+seleccionar+"&idtipo_recurso="+idtipo_recurso+"&q="+q+"&estado="+estado+"&tipo="+tipo;
}

function onchange_data_taxonomias() {
	var f = document.formulario;
	var idtipo_recurso=f.idtipo_recurso.value;
	var seleccionar=f.seleccionar.value;
	var q=f.q.value;
	window.location.href="gestion_de_taxonomias.asp?idtipo_recurso="+idtipo_recurso+"&q="+q;
}

function onchange_data_filtros() {
	var f = document.formulario;
	var tipo=f.tipo.value;
	window.location.href="cm_popup_filtros.asp?tipo="+tipo;
}


function onchange_data_categorias() {
	var f = document.formulario;
	var idportal=f.idportal.value;
	var seleccionar=f.seleccionar.value;
	var tipo=f.tipo.value;
	window.location.href="gestion_de_categorias.asp?seleccionar="+seleccionar+"&idportal="+idportal+"&tipo="+tipo;
}


function getElement( id ) {
	var element;
	if (document.getElementById==null) {
		element = document.all[id];
	} else {
		element =document.getElementById(id);
	}
	return element;
}

function toggleOcultacion(partner) {
	var table = getElement(partner);
	if (table!=null) {
		if (table.style.display=="none") table.style.display="block";
		else table.style.display="none";
	}
}

function mostrarOcultacion(partner) {
	var table = getElement(partner);
	table.style.display="block";	
}

function ocultarOcultacion(partner) {
	var table = getElement(partner);
	table.style.display="none";	
}

function sinImagen(imagen) {
	var nodo;
	nodo = getElement("ubicacion_"+imagen);
	if (nodo!=null) nodo.innerHTML="-- Sin imágen --";
	nodo = getElement("imagen_"+imagen);
	if (nodo!=null) nodo.src="img/punto_claro.gif";
	nodo = getElement(imagen+"_bancoimagenes");
	if (nodo!=null) nodo.value="-1";
		
}

function sinDocumento(doc) {
	var nodo;
	nodo = getElement("ubicacion_"+doc);
	if (nodo!=null) {
		nodo.innerHTML="-- Sin documento --";
		nodo.href="#"
	}
	nodo = getElement(doc+"_eliminar");
	if (nodo!=null) nodo.value="S";		
}


function categorias_cambio_tipo_contenidos() {
	var f = document.formulario;
	var tipo=f.tipo_contenidos.value;
	var nodo;
	if (tipo=="-3") {
		nodo = getElement("mostrar_banco_filtros");
		if (nodo!=null) nodo.style.display = "block";
		nodo = getElement("mostrar_nombre_filtro")
		if (nodo!=null) nodo.style.display = "block";
	} else {
		nodo = getElement("mostrar_banco_filtros");
		if (nodo!=null) nodo.style.display = "none";
		nodo = getElement("mostrar_nombre_filtro")
		if (nodo!=null) nodo.style.display = "none";
	}
}


function edicionDestacado( obj, tipo, idcat) {
	if (obj.previousSibling.value=="") {
		alert("Debe guardar la categoria para poder editar este recurso.");
	} else {
		openWindow("cm_popup_destacados.asp?tipo="+tipo+"&idcat="+idcat+"&numero="+obj.previousSibling.value,670,480);
	}
}

function construyeQuery( url, nombre, valor) {
	var inicio;
	//var re = new RegExp('(?|&)'+nombre+'=[^&]*','g');
	var v1 = url.split('?');
	var res="";
	var v2 = v1[1].split('&');
	var i;
	inicio = false;
	for (i=0;i<v2.length;i++) {
		var v3 = v2[i].split("=");
		if (res!="") {
		} else {
			res = v1[0]+"?";
			inicio = true;
		}
		if ( (v3[0]!=nombre) &&(v3[0]!="") ) {
			if (!inicio) {
				res +="&";
			} else {
				inicio = false;
			}
			res += v3[0]+"="+v3[1];
		} else {
		}
	}
	res += "&" + nombre + "=" + valor;
	return res;
}

function eliminarProyecto(strid) {
	var nodo = getElement("mostrar_otros_proyectos_"+strid);
	nodo.outerHTML="";

	var valorPrevio = document.formulario.otros_proyectos.value;
	var v = valorPrevio.split(",");
	var res="";
	var i;
	for (i=0;i<v.length;i++) {
		if (v[i]!="") {
			if (v[i]!=strid) {
				res = res +","+v[i];
			}
		}
	}
	if (res!="") res=res.substring(1);
	document.formulario.otros_proyectos.value = res;
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function abrirEnlaceCamino(url, idcamino) {
	var str_buffer=getElement( idcamino ).innerHTML;
	var text = getElement( idcamino ).innerText;
	if (text!="") {
		openWindow(url);
		var vWinCal = window.open("", "Camino", "width=200,height=210,status=no,resizable=yes,top=200,left=200");
		vWinCal.opener = self;
		var calc_doc = vWinCal.document;
		calc_doc.write (str_buffer);
		calc_doc.close();
	} else {
		openWindow(url);
	}
	return false;
}

function abrirDescripcion(idtexto) {
	var str_buffer=getElement( idtexto ).innerHTML;
	var text = getElement( idtexto ).innerText;
	if (text!="") {
		var vWinCal = window.open("", "Descripcion", "width=400,height=210,status=no,resizable=yes,scrollbars=1,top=200,left=200");
		vWinCal.opener = self;
		var calc_doc = vWinCal.document;
		calc_doc.write (str_buffer);
		calc_doc.close();
	} 
	return false;
}

function construyeUrlMenosMas(url,lista,itemsMas) {
	var res,urlBase,resto,w,x,i,atributo,valor,nresto;
	var v = url.split("?");
	if (v.length==1) {
		res = url+"?"+itemsMas;
	} else {
		nresto="";
		urlBase = v[0];
		resto = v[1];
		w = resto.split("&");
		for (i=0;i<w.length;i++) {
			if (w[i]!=""){
				x = w[i].split("=");
				atributo = x[0];
				valor="";
				if (x.length>0) valor = x[1];
				if (valor!="") {
					if (!perteneceLista(atributo,lista)) {
						nresto = nresto + "&" + atributo + "=" + valor;
					}
				}
			}
		}
		nresto += "&" + itemsMas;
		if (nresto!="") nresto=nresto.substring(1);
		res = urlBase + "?" + nresto;
	}
	return res;
}

function perteneceLista2(valor,lista) {
        var res=false;
        pos =(";"+lista+";").indexOf(";"+valor+";");
        if (pos!=-1) {
                res=true;
        }
        return res;
}

function perteneceLista(valor,lista) {
        var res=false;
        pos =(","+lista+",").indexOf(","+valor+",");
        if (pos!=-1) {
                res=true;
        }
        return res;
}

function tieneAlgunoRellenoPatron(patron) {
	var f = document.formulario;
	res=false;
	for(i=0;i<f.all.length;i++) {	
		var campo = f[i];
		if (campo!=null) {
			var nombreCampo = campo.name;
			if (nombreCampo.substring(0, patron.length)==patron) {
				if (campo.checked) res=true;
			}
		}
	}
	return res;
}

function perteneceCadena(valor,lista) {
        var res=false;
        pos =(" "+lista+" ").indexOf(" "+valor+" ");
        if (pos!=-1) {
                res=true;
        }
        return res;
}


function mostrarTrMenu(strid, strelemento, doc) {
	var res=false;
	var nodo2 = getElement("menu_"+strelemento, doc);
	
	if (nodo2!=null) {
		if (nodo2.style.display=="block") res=true;
	}
	return res;
}



function expandirMenuNuevo(tipoMenu) {
	//alert (tipoMenu);
	var nodo, display;
	nodo = getElement("menu_nuevo");
	
	if (nodo!=null) {
		display = nodo.style.display;	
		if ( (display=="block") || (display=="") ) {
			nodo.style.display="none";
			nodo.innerHTML="";
		} else {			
			nodo.style.display="block";
			nodo = getElement("menu_nuevo");
			if (nodo!=null) {
				//nodo.innerHTML="Cargando datos ...";
				urlXml="xml_html_popup_tnavegacion";
				strIdNodo="menu_nuevo";
				url="xml_html_popup_tnavegacion.php?bloque="+tipoMenu;
				//alert (url);
				getXml(url);
			}
		}
	}

	//Mostrar la tr
	var res=false;
	var nodo2 = getElement("menu_nuevo");
	if (nodo2!=null) {
		if (nodo2.style.display=="block") res=true;
	}
	return res;

}


function expandirMenu(stroculto, strelemento, strid, strMenu, tipo, doc) {
	var nodo, display;
	nodo = getElement("menu_"+strelemento,doc);
	if (nodo!=null) {
		display = nodo.style.display;		
		if ( (display=="block") || (display=="") ) {
			nodo.style.display="none";
			nodo.innerHTML="";
		} else {			
			nodo.style.display="block";
			nodo = getElement("menu_"+strelemento,doc);
			if (nodo!=null) {
				nodo.innerHTML="Cargando datos ...";
				urlXml="xml_html_popup_tnavegacion";
				strIdNodo="menu_"+strelemento;
				url="xml_html_popup_tnavegacion.php?id="+strid+"&idoculto=th_"+stroculto+"&idelemento="+strelemento+"&idmenu="+strMenu;
				//alert (url);
				getXml(url);
			}
		}
	}
	mostrarTrMenu(stroculto, strelemento, doc);
}


function getXml(url, ndoc) {
	var doc =ndoc;
	if (doc==null) doc=document;
	var nodo = getElement("xmlisland",doc);
	
	nodo.src=url;
	
}

function loadXml() {
	var res;
	var doc = document;
	var xmlNode = xmlisland.XMLDocument;
	//alert("loadXml: "+xmlNode.text);

	if (urlXml=="xml_html_popup_tnavegacion") {
		
		nodo = getElement(strIdNodo);
		//alert(strIdNodo);
		if (nodo!=null) {
			nodo.innerHTML=xmlNode.text;
		}
		//alert("loadXml2: "+nodo.innerHTML);
	} else if (urlXml=="xml_html_popup_item") {
		nodo = getElement(strIdNodo);
		if (nodo!=null) {
			nodo.innerHTML=xmlNode.text;
		}
	} else if (urlXml=="xml_html_popup_ponencia") {
		
		nodo = getElement(strIdNodo);
		if (nodo!=null) {
			nodo.innerHTML=xmlNode.text;
		}
	} else if (urlXml=="xml_html_popup_detalleformulario"){
		//alert (strIdNodo);
		nodo = getElement(strIdNodo);
		//alert(nodo.outerHTML);
		if (nodo!=null) {
			nodo.innerHTML=xmlNode.text;
			
		}
		
	} else if (urlXml=="xml_html_popup_menus"){
		//alert ("strIdNodo:"+strIdNodo);
		nodo = getElement(strIdNodo);

		//alert("nodo: " +nodo);
		if (nodo!=null) {
			//nodo.innerHTML="//";
			//alert (xmlNode.text);
			nodo.innerHTML=xmlNode.text;
		}			
	}
	urlXml="";

}


function cambiaValorTipocontenidos(item) {

	var valor = getRightToLast(item.id,"_",1);

	var nodoPagina = getElement("div_paginas_"+valor);
	var nodoEnlace = getElement("div_enlace_"+valor);

	nodoPagina.style.display="none";
	nodoEnlace.style.display="none";
	if (item.checked) {
		if (item.value=="P" ) nodoPagina.style.display="block";
		else if (item.value=="E" ) nodoEnlace.style.display="block";
	}
}



function getRightToLast(cadena, delim, num) {
        var pos;
        var res="";
        var tmp=cadena;
        for(i=0;i<num;i++) {
                pos = tmp.lastIndexOf(delim);
                if (pos!=-1) {
                        res = tmp.substring(pos) + res;
                        tmp = tmp.substring(0,pos);
                } else {
                        res = tmp + res;
                        tmp = "";
                }
        }
        if (res.length>=delim.length) {
                if (res.substring(0,delim.length)==delim) {
                        res = res.substring(delim.length);
                }
        }
        return res;
}

/* Separa la cadena por el delimitador y coge el elemento n*/
function getDelim(cadena, delim, num) {
        var res="";
		try
		{
			var v = cadena.split(delim);
			res = v[num];			
		}
		catch (e){}
        return res;
}
