// Para controlar el cierre de ventanas emergentes
window.onunload=fin;
var g_objVentanas = new ArrayVentanas();

function ArrayVentanas(){
	this.array = new Array();
	this.length = ArrayVentanasLength;
	this.add = ArrayVentanasAdd;
	this.closeAll = ArrayVentanasCerrar;
	this.find = ArrayVentanasBuscar;
	this.hayAbiertasCriticas = ArrayVentanasHayAbiertasCriticas;
	this.borrar = ArrayVentanasBorrar;
}

function Ventana( ventana, critica ){
	this.ventana = ventana;
	this.critica = critica;
}

function ArrayVentanasLength(){
	return this.array.length;
}

function ArrayVentanasAdd( ventana, critica ){
	var posicion = this.find( ventana.name );
	var esCritica = false;

	if( ArrayVentanasAdd.arguments.length >= 2 ){
		esCritica = critica;
	}

	if( posicion == -1 ){
		this.array[this.array.length] = new Ventana( ventana, critica );
	}
}

function ArrayVentanasCerrar(){
	var cont = this.array.length;

	for( cont--; cont >= 0; cont-- ){
		this.array[cont].ventana.close();
		this.array[cont] = null;
	}

	this.array.length = 0;
}

function ArrayVentanasBuscar( nombre ){
	var cont = 0;
	var contAux = 0
	var arrayAux = new Array();

	for( cont=0; cont < this.array.length; cont++ ){
		if( !this.array[cont].ventana.closed ){
			arrayAux[contAux++] = this.array[cont];
		}
	}

	this.array = arrayAux;

	for( var cont=0; cont<this.array.length; cont++ ){
		if( nombre == this.array[cont].ventana.name ){
			return cont;
		}
	}

	return -1;
}

function ArrayVentanasBorrar( nombre ){
	var cont = 0;
	var ultimo = this.array.length - 1;
	var aux = null;

	for( cont=0; cont < this.array.length; cont++ ){
		if( nombre == this.array[cont].ventana.name ){
			this.array[cont]  = this.array[ultimo];
			this.array[ultimo] = null;
			this.array.length = ultimo;
		}
	}
}

function ArrayVentanasHayAbiertasCriticas(){
	for( var cont=0; cont<this.array.length; cont++ ){
		if( !this.array[cont].ventana.closed && this.array[cont].critica ){
			return true;
		}
	}

	return false;
}

function fin(){
	g_objVentanas.closeAll();
}

function permitirCerrarVentanas(){
	if( g_objVentanas.hayAbiertasCriticas() ){
		if( confirm( "Esta operación cerrará todas las ventanas emergentes que " +
				"dependen de la actual, lo que implica que se perderán los datos en ellas.\n¿Desea continuar?" )
		){
			g_objVentanas.closeAll();
		}else{
			return false;
		}
	}

	return true;
}

// Para el manejo de una ventana de selecci?n
function wseleccion ( nombre, win_location, ancho, alto, critico ) {
	var screen_width, screen_height;
	var win_top, win_left;
	var w_seleccion;
	var esCritico = false;

	if( wseleccion.arguments.length >= 5 ){
		esCritico = critico;
	}

	screen_height        = 0;     screen_width      = 0;
	win_top              = 0;     win_left          = 0;

	if (window.innerWidth) screen_width = window.innerWidth;
	if (window.innerHeight) screen_height = window.innerHeight;

	win_top  = screen_height - alto - 20;
	win_left = screen_width  - ancho  - 20;
  	w_seleccion  = window.open(
               win_location,
               nombre,
               'width='+ancho+
               ',height='+alto+
               ',top='+win_top+
               ',left='+win_left+
               ',scrollbars=yes,resizable=yes'
   	);
   	w_seleccion.focus();
   	w_seleccion.moveTo(30,30);

   	g_objVentanas.add( w_seleccion, esCritico );
}

  function getwseleccion ( nombre, win_location, ancho, alto, critico ) {

	var screen_width, screen_height;
	var win_top, win_left;
	var w_seleccion;
	var esCritico = false;
	if( getwseleccion.arguments.length >= 5 ){
		esCritico = critico;
	}

	screen_height        = 0;     screen_width      = 0;
	win_top              = 0;     win_left          = 0;

	if (window.innerWidth) screen_width = window.innerWidth;
	if (window.innerHeight) screen_height = window.innerHeight;

	win_top  = screen_height - alto - 20;
	win_left = screen_width  - ancho  - 20;
  	w_seleccion  = window.open(
               win_location,
               nombre,
               'width='+ancho+
               ',height='+alto+
               ',top='+win_top+
               ',left='+win_left+
               ',scrollbars=yes,resizable=yes'
   	);
   	w_seleccion.focus();
   	w_seleccion.moveTo(30,30);

   	g_objVentanas.add( w_seleccion, esCritico );

   	return w_seleccion;
   }

function getwseleccionJS ( nombre, win_location, ancho, alto, critico ){
	var screen_width, screen_height;
	var win_top, win_left;
	var w_seleccion;
	var esCritico = false;

	if( getwseleccionJS.arguments.length >= 5 ){
		esCritico = critico;
	}

	screen_height        = 0;     screen_width      = 0;
	win_top              = 0;     win_left          = 0;

	if (window.innerWidth) screen_width = window.innerWidth;
	if (window.innerHeight) screen_height = window.innerHeight;

	win_top  = screen_height - alto - 20;
	win_left = screen_width  - ancho  - 20;
	w_seleccion  = window.open(
	       win_location,
	       nombre,
	       'width='+ancho+
	       ',height='+alto+
	       ',top='+win_top+
	       ',left='+win_left+
	       ',scrollbars=yes,resizable=yes'
		);
	w_seleccion.focus();
	w_seleccion.moveTo(30,30);

	parent.datos.g_objVentanas.add( w_seleccion, esCritico );

	return w_seleccion;
}

   function getwseleccionMax ( nombre, win_location, critico, labelStatus ) {

	var screen_width, screen_height;
	var win_top, win_left;
	var w_seleccion;
	var esCritico = false;
	if( getwseleccionMax.arguments.length >= 3 ){
		esCritico = critico;
	}
	var labelParaStatus = 'no';
	if(getwseleccionMax.arguments.length >= 4){
		if(labelStatus != '')
			labelParaStatus = labelStatus;
	}
	
	screen_height        = 0;     screen_width      = 0;
	win_top              = 0;     win_left          = 0;

	if (window.innerWidth) screen_width = window.innerWidth;
	if (window.innerHeight) screen_height = window.innerHeight;

  	w_seleccion  = window.open(
  				win_location,
  				nombre,
  				'left=0,'+
  				'top=0,'+
  				'screenX=0,'+
  				'screenY=0,'+
  				'type=fullWindow,'+
  				'alwaysRaised=yes,'+
  				'directories=no,'+
  				'hotkeys=no,'+
  				'menubar=no,'+
  				'resizable=no,'+
  				'location=no,'+
  				'toolbar=no,'+
  				'status=' + labelParaStatus  + ',' +
  				'scrollbars=no,'+
  				'directories=no,'+
  				'status=yes,'+
  				'height='+ (screen.availHeight-50) +
  				',width=' + (screen.availWidth-10 ) +
  				',dependent=true'+
  				',titlebar=false'+
  				',z-lock=true'+
  				',channelmode=true'+
  				',innerWidth='+screen.width+
  				',innerHeight='+screen.height );
   	w_seleccion.focus();

   	g_objVentanas.add( w_seleccion, esCritico );

   	return w_seleccion;
   }

//2001-11-02
function seleccionar(){

	var forma = parent.opener.document.forms[0];
	var arrCamposRecuperados = seleccionar.arguments;

	if( !parent.opener || parent.opener.closed ){
		alert( "La ventana padre fue cerrada" );
		top.self.close();
		return;
	}

	if(document.forms[0].h_campos ){
		var arrCamposParent = document.forms[0].h_campos.value.split(",");
	}else if( document.forms[0].campos  ){
		var arrCamposParent = document.forms[0].campos.value.split(",");
	}
	var i = 0;
	var maximo = arrCamposParent.length < arrCamposRecuperados.length ? arrCamposParent.length : arrCamposRecuperados.length;
	for( i=0; i < maximo; i++ ){
		if ( arrCamposParent[i] != 'nulo' && arrCamposParent[i].length>0 ){
			if ( eval( "forma."+arrCamposParent[i]) ){
				eval( "forma."+arrCamposParent[i]+".value='"+JSMessage(arrCamposRecuperados[i])+"'" );
			}
		}
	}
	if ( eval( document.forms[0].h_campo_funcion && document.forms[0].h_campo_funcion.value.length > 0) ){
		eval( 'parent.opener.'+document.forms[0].h_campo_funcion.value );
	}
	if ( document.forms[0].accion ){
		if ( document.forms[0].accion.value == 'submit' ){
			forma.submit();
		}
	}

	top.self.close();
}


function JSMessage (mensaje)  {
	var sMensaje=mensaje;
	if ( sMensaje.indexOf("\n") != -1 ){
				var tmpsMensaje ="";
				for ( var i=0; i<sMensaje.length; i++){
						   if (sMensaje.charAt(i) == '\n' ){
									   tmpsMensaje += "\\n";
						   }else{
									   tmpsMensaje += sMensaje.charAt(i);
						   }
				}
				sMensaje=tmpsMensaje;
	}
	if ( sMensaje.indexOf("'") != -1 ){
				var tmpsMensaje ="";
				for ( var i=0 ; i<sMensaje.length ; i++){
						   if ( sMensaje.charAt(i) == '\'' ){
									   tmpsMensaje += "\\'";
						   }else{
									   tmpsMensaje += sMensaje.charAt(i);
						   }
				}
				sMensaje=tmpsMensaje;
	}
	return sMensaje;
}

function salir(urlRedireccionar){
	if(permitirCerrarVentanas())
		top.location.href = urlRedireccionar;
}

function abrirAplicacion(nombre,localizacion, labelStatus){
	var ventana = getwseleccionMax( nombre , localizacion, true, labelStatus);
}
