//Abre una ventana de popup

function ventana(url,nombre,ancho,alto,posx,posy,scroll) {
 if(ancho == null || ancho == '' ) {
 		ancho = 600;
 }
 
 if(alto == null || alto == '' ) {
 		alto = 400;
 }
 
 if(posx == null || posx == '' ) {
 		posx = 0;
 }
 
 if(posy == null || posy == '' ) {
 		posy = 0;
 }
 
 if(scroll == null || scroll == '' ) {
 		scroll = 'yes';
 }
 
 vent_popup=window.open(url,nombre,'height='+alto+',width='+ancho+',top='+posy+',left='+posx+',menubar=no,scrollbars='+scroll+',status=no,toolbar=yes,resizable=yes');
 vent_popup.focus();
};

//Abre una ventana de popup maximizada

function ventana_max(url,nombre) {
var vent_popup = window.open(url,nombre,'height=400,width=600,top=0,left=0,menubar=no,scrollbars=yes,status=no,toolbar=yes,resizable=yes');
vent_popup.resizeTo(screen.availWidth,screen.availHeight);
vent_popup.focus();
};

//Abre una ventana de popup con todas las barras

function ventana_ext(url,nombre) {
 vent_popup=window.open(url,nombre,'height=400,width=600,top=0,left=0,menubar=yes,scrollbars=yes,status=yes,toolbar=yes,location=yes,resizable=yes');
 vent_popup.focus();
};

//Carga el iframe principal y sustituye la imagen de fondo de la derecha
function ir(inputurl,imagen,target){
// var displaymode=0
//alert(document.images["marco-derecho"].src);
//return;
var padre = "";

if (target == "cuerpo") {
		window.parent.cuerpo.document.getElementById("marco-derecho").style.backgroundImage = 'url(' + imagen + ')';
		padre ="window.parent.cuerpo.";
		window.parent.cuerpo.scrollTo(0, 0);
} else if (target == "opener") {
		padre = "window.opener.";
		window.opener.document.getElementById("marco-derecho").style.backgroundImage = 'url(' + imagen + ')';
		window.opener.scrollTo(0, 0);

} else if (target == "padre") {
		padre = "window.opener.parent.";
		window.opener.parent.document.getElementById("marco-derecho").style.backgroundImage = 'url(' + imagen + ')';
		window.opener.parent.focus();
		window.opener.parent.scrollTo(0, 0);
} else {
			if(imagen != document.getElementById("marco-derecho").style.backgroundImage) {
				// alert("Imagenes distintas " + document.getElementById("marco-derecho").style.backgroundImage);
	  		document.getElementById("marco-derecho").style.backgroundImage = 'url(' + imagen + ')';
	  	}
		//document.images["marco-derecho"].src = imagen;
		self.scrollTo(0, 0);

}

documento = eval(padre + 'document');

if (documento.getElementById)
	documento.getElementById("principal").src=inputurl
else if (documento.all)
	documento.all.principal.src=inputurl
	else{
		if (!window.win2||win2.closed)
			win2=window.open(inputurl)
//Con navegador antiguo si la ventana existe se utiliza la misma ventana
		else{
			win2.location=inputurl
			win2.focus()
	}
}

}

function ir_organismo(form,organismo) {

  var enlace = form.options[form.options.selectedIndex].value;
  if (enlace != 0) {
  	ventana_ext(enlace,organismo);
  }

};

function ventana_padre(inputurl,imagen,target) {
	ir(inputurl,imagen,target);
	window.close();
}

//Función para la búsqueda de términos

function buscar_texto() {
	cad_buscar = document.forms["formBuscar"].buscar.value;
	//self.scrollTo(0, 0);
	parent.cuerpo.document.getElementById("principal").src="/php/buscar/search.php?q=" + cad_buscar + "&r=4";
}

// Colapsar el borde derecho
// #############################################################################
// let's find out what DOM functions we can use
var vbDOMtype = '';
if (document.getElementById)
{
	vbDOMtype = "std";
}
else if (document.all)
{
	vbDOMtype = "ie4";
}
else if (document.layers)
{
	vbDOMtype = "ns4";
}


// catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

// make an array to store cached locations of objects called by fetch_object
var vBobjects = new Array();

// #############################################################################
// function to emulate document.getElementById
function fetch_object(idname, forcefetch)
{
	if (forcefetch || typeof(vBobjects[idname]) == "undefined")
	{
		switch (vbDOMtype)
		{
			case "std":
			{
				vBobjects[idname] = document.getElementById(idname);
			}
			break;

			case "ie4":
			{
				vBobjects[idname] = document.all[idname];
			}
			break;

			case "ns4":
			{
				vBobjects[idname] = document.layers[idname];
			}
			break;
		}
	}
	return vBobjects[idname];
}


function toggle_collapse(objid)
{
	if (!is_regexp)
	{
		return false;
	}
	
	obj = fetch_object("collapseobj_" + objid);	
	img = fetch_object("collapseimg_" + objid);
	cel = fetch_object("collapsecel_" + objid);
	
	if (!obj)
	{
		// nothing to collapse!
		if (img)
		{
			// hide the clicky image if there is one
			img.style.display = "none";
		}
		return false;
	}

	if (obj.style.display == "none")
	{
		obj.style.display = "";
		//save_collapsed(objid, false);
		if (img)
		{
			img_re = new RegExp("_collapsed\\.gif$");
			img.src = img.src.replace(img_re, '.gif');
		}
		if (cel)
		{
			cel_re = new RegExp("^(thead|tcat)(_collapsed)$");
			cel.className = cel.className.replace(cel_re, '$1');
		}
	}
	else
	{
		obj.style.display = "none";
		//save_collapsed(objid, true);
		if (img)
		{
			img_re = new RegExp("\\.gif$");
			img.src = img.src.replace(img_re, '_collapsed.gif');
		}
		if (cel)
		{
			cel_re = new RegExp("^(thead|tcat)$");
			cel.className = cel.className.replace(cel_re, '$1_collapsed');
		}
	}
	return false;
}

