//The browser detection function. comunicBack.js
//This function can be used for other purposes also.

//---
var popupHandle;
function closePopup() {
	if(popupHandle != null && !popupHandle.closed) popupHandle.close();
}
function displayPopup(position,url,name,height,width,evnt) {
// position=1 POPUP: makes screen display up and/or left, down and/or right depending on where cursor falls and size of window to open
// position=2 CENTER: makes screen fall in center
	var properties = "toolbar = 0, location = 0, height = " + height;
	properties = properties + ", width=" + width;
	var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
	if(at.ecmas) {
		screenY = document.body.offsetHeight;
		screenX = window.screen.availWidth;
	}
	else {
		screenY = window.outerHeight
		screenX = window.outerWidth
	}
	if(position == 1)	{ // if POPUP not CENTER
		cursorX = evnt.screenX;
		cursorY = evnt.screenY;
		padAmtX = 10;
		padAmtY = 10;
		if((cursorY + height + padAmtY) > screenY) {
		// make sizes a negative number to move left/up
			padAmtY = (-30) + (height * -1);
			// if up or to left, make 30 as padding amount
		}
		if((cursorX + width + padAmtX) > screenX)	{
			padAmtX = (-30) + (width * -1);	
			// if up or to left, make 30 as padding amount
		}
		if(at.ecmas) {
			leftprop = cursorX + padAmtX;
			topprop = cursorY + padAmtY;
		}else {
			leftprop = (cursorX - pageXOffset + padAmtX);
			topprop = (cursorY - pageYOffset + padAmtY);
	   	}
	}else{
		leftvar = (screenX - width) / 2;
		rightvar = (screenY - height) / 2;
		if(navigator.appName == "Microsoft Internet Explorer") {
			leftprop = leftvar;
			topprop = rightvar;
		}else {
			leftprop = (leftvar - pageXOffset);
			topprop = (rightvar - pageYOffset);
	   	}
	}
	if(evnt != null) {
	properties = properties + ", left = " + leftprop;
	properties = properties + ", top = " + topprop;
	}
	closePopup();
	popupHandle = open(url,name,properties);
}

//---



//if you want to create the frame or layer dynamically, do not
//specify a name, do something like this, new exchanger();

function comunicador(name){
  this.fakeid=0;
  this.name=name;
  if (at.ns4){
      this.lyr = window.document.layers[this.name];
  }
}

comunicador.prototype.enviarDatos=function(url){
  this.fakeid += 1;
  var newurl = "";
  if (url.indexOf("?") >= 0)
    newurl = url + "&fakeId=" + this.fakeid ;
  else
    newurl = url + "?fakeId=" + this.fakeid;

  if (at.ns4)
    this.lyr.src=newurl;
  else {
    if (at.ie || at.ns5 || at.ns6 || at.op)    {
      window.frames[this.name].document.location.replace(newurl);
    }
  }
}

// Limpiamos el comunicador
comunicador.prototype.limpiar=function(){
// alert("limpio");
  if (at.ns4)
//    this.lyr.src='about:blank';
//    this.lyr.src='&nbsp;';
	var abcd = "pp";
  else {
    if (at.ie || at.ns5 || at.ns6 || at.op)    {
      window.frames[this.name].document.location.replace('about:blank');
    }
  }
}

// Realizamos la peticion, indicando el formulario a submitir
comunicador.prototype.enviarFormulario=function(formulario){
  this.fakeid += 1;
  formulario.fakeId.value = this.fakeid ;
  formulario.target = this.name;
  formulario.submit();
}


comunicador.prototype.recuperarDato=function(varName)
{
  if (at.ns4)  {
    return eval("this.lyr." + varName);
  }else if (at.ie || at.ns5 || at.ns6 || at.op)  {
    return eval("window.frames['" + this.name + "']." + varName);
  }
}




// ---------------

var chequearRespuestaCada = 100; // en milisegundos
var timeOutRespuesta = 20000; // en milisegundos

var bufferUsu;

function inicializaU(frameOculta){
	bufferUsu = new comunicador(frameOculta);
}


var version4 = (navigator.appVersion.charAt(0) == "4");

function pararLectura() {
	clearInterval(cont);
	closePopup();
	// Mostramos el error
	mostrarError(true);
//	window.status="... No hay datos ...";
	bufferUsu.limpiar();

}

function leerRespuesta(idaYvuelta) {
	if (bufferUsu.recuperarDato("OK")=='true') {
		clearInterval(cont);
		window.clearTimeout(timeOutE);
//		window.status="... HECHO ...";
		closePopup();
		// Miramos si hay error y si lo hay lo mostramos
		if (bufferUsu.recuperarDato("TRANSACCION")=='ERROR'){
			mostrarError(false);
		}	
		// Si no hay error y idaYvielta es 2 llamamos a actualizaDatos()
		if (idaYvuelta==2)
			actualizarDatos();
		else
			bufferUsu.limpiar();
	}
}

function mostrarError(timeOut){
	if (timeOut){
		alert("El servidor no responde, intantalo de nuevo.");
	}else{
		alert(bufferUsu.recuperarDato("textoError"));
		//alert(bufferUsu.recuperarDato("textoTecnico"));
	}
}


function enviarAlServidor(idaYvuelta, urlDatos, urlEspera){
	displayPopup(2,urlEspera,'popup1',20,320,(version4 ? event : null));
//	window.status="... ACTUALIZANDO ...";
	bufferUsu.enviarDatos(urlDatos);

	cont = setInterval("leerRespuesta("+idaYvuelta+")", chequearRespuestaCada);
	timeOutE = window.setTimeout("pararLectura();", timeOutRespuesta);
}
		
function enviarAlFormularioServidor(idaYvuelta, formulario, urlEspera){
	displayPopup(2,urlEspera,'popup1',20,320,(version4 ? event : null));
//	window.status="... ACTUALIZANDO ...";
	bufferUsu.enviarFormulario(formulario);

	cont = setInterval("leerRespuesta("+idaYvuelta+")", chequearRespuestaCada);
	timeOutE = window.setTimeout("pararLectura();", timeOutRespuesta);
}
