function xDef(){
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

function xNum(){
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
}

function xStr(s){
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
}

function xGetElementById(e){
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

function xPageY(e){
  var y = 0;
  e = xGetElementById(e);
  while (e) {
    if (xDef(e.offsetTop)) y += e.offsetTop;
    e = xDef(e.offsetParent) ? e.offsetParent : null;
  }
  return y;
}

function xPageX(e){
  var x = 0;
  e = xGetElementById(e);
  while (e) {
    if (xDef(e.offsetLeft)) x += e.offsetLeft;
    e = xDef(e.offsetParent) ? e.offsetParent : null;
  }
  return x;
}

function xGetComputedStyle(oEle, sProp, bInt){
  var s, p = 'undefined';
  var dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(oEle,'');
    if (s) p = s.getPropertyValue(sProp);
  }
  else if(oEle.currentStyle) {
    // convert css property name to object property name for IE
    var i, c, a = sProp.split('-');
    sProp = a[0];
    for (i=1; i<a.length; ++i) {
      c = a[i].charAt(0);
      sProp += a[i].replace(c, c.toUpperCase());
    }
    p = oEle.currentStyle[sProp];
  }
  else return null;
  return bInt ? (parseInt(p) || 0) : p;
}

function xMoveTo(e,x,y){
  xLeft(e,x);
  xTop(e,y);
}

function xResizeTo(e,w,h){
  xWidth(e,w);
  xHeight(e,h);
}

function xOffsetLeft(e){
  if (!(e=xGetElementById(e))) return 0;
  if (xDef(e.offsetLeft)) return e.offsetLeft;
  else return 0;
}

function xOffsetTop(e){
  if (!(e=xGetElementById(e))) return 0;
  if (xDef(e.offsetTop)) return e.offsetTop;
  else return 0;
}

function xScrollLeft(e, bWin){
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && e) w = e;
    if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
    else if(w.document.body && xDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
  }
  else {
    e = xGetElementById(e);
    if (e && xNum(e.scrollLeft)) offset = e.scrollLeft;
  }
  return offset;
}

function xScrollTop(e, bWin){
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && e) w = e;
    if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
    else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
  }
  else {
    e = xGetElementById(e);
    if (e && xNum(e.scrollTop)) offset = e.scrollTop;
  }
  return offset;
}

function xTop(e, iY){
  if(!(e=xGetElementById(e))) return 0;
  var css=xDef(e.style);
  if(css && xStr(e.style.top)) {
    if(xNum(iY)) e.style.top=iY+'px';
    else {
      iY=parseInt(e.style.top);
      if(isNaN(iY)) iY=xGetComputedStyle(e,'top',1);
      if(isNaN(iY)) iY=0;
    }
  }
  else if(css && xDef(e.style.pixelTop)) {
    if(xNum(iY)) e.style.pixelTop=iY;
    else iY=e.style.pixelTop;
  }
  return iY;}

function xLeft(e, iX){
  if(!(e=xGetElementById(e))) return 0;
  var css=xDef(e.style);
  if (css && xStr(e.style.left)) {
    if(xNum(iX)) e.style.left=iX+'px';
    else {
      iX=parseInt(e.style.left);
      if(isNaN(iX)) iX=xGetComputedStyle(e,'left',1);
      if(isNaN(iX)) iX=0;
    }
  }
  else if(css && xDef(e.style.pixelLeft)) {
    if(xNum(iX)) e.style.pixelLeft=iX;
    else iX=e.style.pixelLeft;
  }
  return iX;
}

function xClientWidth(){
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}

function xClientHeight(){
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientHeight)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
    v=w.innerHeight;
    if(d.width>w.innerWidth) v-=16;
  }
  return v;
}

function xAddEventListener(e,eT,eL,cap){
  if(!(e=xGetElementById(e))) return;
  eT=eT.toLowerCase();
  if (e==window && !e.opera && !document.all) { // simulate resize and scroll events for all except Opera and IE
    if(eT=='resize') {
      e.xPCW=xClientWidth(); e.xPCH=xClientHeight();
      var pREL = e.xREL ;
      e.xREL= pREL ? function() { eL(); pREL(); } : eL;
      xResizeEvent(); return;
    }
    if(eT=='scroll') {
      e.xPSL=xScrollLeft(); e.xPST=xScrollTop();
      var pSEL = e.xSEL ;
      e.xSEL=pSEL ? function() { eL(); pSEL(); } : eL;
      xScrollEvent(); return; }
  }
  if(e.addEventListener) e.addEventListener(eT,eL,cap);
  else if(e.attachEvent) e.attachEvent('on'+eT,eL);
  else {
    var pev = e['on'+eT];
    e['on'+eT]= pev ? function() { eL(); typeof(pev) == 'string' ? eval(pev) : pev(); } : eL;
  }
}

function xResizeEvent(){ // called only from the above
  if (window.xREL) setTimeout('xResizeEvent()', 100);
  var w=window, cw=xClientWidth(), ch=xClientHeight();
  if (w.xPCW != cw || w.xPCH != ch) { w.xPCW = cw; w.xPCH = ch; if (w.xREL) w.xREL(); }
}

function xScrollEvent(){ // called only from the above
  if (window.xSEL) setTimeout('xScrollEvent()', 100);
  var w=window, sl=xScrollLeft(), st=xScrollTop();
  if (w.xPSL != sl || w.xPST != st) { w.xPSL = sl; w.xPST = st; if (w.xSEL) w.xSEL(); }
}

function xOpacity(e, o){
  var set = xDef(o);
  //  if (set && o == 1) o = .9999; // FF1.0.2 but not needed in 1.5
  if(!(e=xGetElementById(e))) return 2; // error
  if (xStr(e.style.opacity)) { // CSS3
    if (set) e.style.opacity = o + '';
    else o = parseFloat(e.style.opacity);
  }
  else if (xStr(e.style.filter)) { // IE5.5+
    if (set) e.style.filter = 'alpha(opacity=' + (100 * o) + ')';
    else if (e.filters && e.filters.alpha) { o = e.filters.alpha.opacity / 100; }
  }
  else if (xStr(e.style.MozOpacity)) { // Gecko before CSS3 support
    if (set) e.style.MozOpacity = o + '';
    else o = parseFloat(e.style.MozOpacity);
  }
  else if (xStr(e.style.KhtmlOpacity)) { // Konquerer and Safari
    if (set) e.style.KhtmlOpacity = o + '';
    else o = parseFloat(e.style.KhtmlOpacity);
  }
  return isNaN(o) ? 1 : o; // if NaN, should this return an error instead of 1?
}

function xAniOpacity(xa, o, tt, at, qc, oed, oea, oef) {
  var a = xa.init(xa.e, at, qc, tt, onRun, onRun, oed, oea, oef);
  a.x1 = xOpacity(a.e); // start opacity
  a.x2 = o; // end opacity
  if (a.as) a.start();
  return a;
  function onRun(o) { xOpacity(o.e, o.x); }
}

function xSlideTo(e, x, y, uTime){
  if (!(e=xGetElementById(e))) return;
  if (!e.timeout) e.timeout = 25;
  e.xTarget = x; e.yTarget = y; e.slideTime = uTime; e.stop = false;
  e.yA = e.yTarget - xTop(e); e.xA = e.xTarget - xLeft(e); // A = distance
  if (e.slideLinear) e.B = 1/e.slideTime;
  else e.B = Math.PI / (2 * e.slideTime); // B = period
  e.yD = xTop(e); e.xD = xLeft(e); // D = initial position
  var d = new Date(); e.C = d.getTime();
  if (!e.moving) _xSlideTo(e);
}

function _xSlideTo(e){
  if (!(e=xGetElementById(e))) return;
  var now, s, t, newY, newX;
  now = new Date();
  t = now.getTime() - e.C;
  if (e.stop) { e.moving = false; }
  else if (t < e.slideTime) {
    setTimeout("_xSlideTo('"+e.id+"')", e.timeout);
    s = e.B * t;
    if (!e.slideLinear) s = Math.sin(s);
//    if (e.slideLinear) s = e.B * t;
//    else s = Math.sin(e.B * t);
    newX = Math.round(e.xA * s + e.xD);
    newY = Math.round(e.yA * s + e.yD);
    xMoveTo(e, newX, newY);
    e.moving = true;
  }  
  else {
    xMoveTo(e, e.xTarget, e.yTarget);
    e.moving = false;
    if (e.onslideend) e.onslideend();
  }  
}

function xWidth(e,w){
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(w)) {
    if (w<0) w = 0;
    else w=Math.round(w);
  }
  else w=-1;
  var css=xDef(e.style);
  if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    w = xClientWidth();
  }
  else if(css && xDef(e.offsetWidth) && xStr(e.style.width)) {
    if(w>=0) {
      var pl=0,pr=0,bl=0,br=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = xGetComputedStyle;
        pl=gcs(e,'padding-left',1);
        if (pl !== null) {
          pr=gcs(e,'padding-right',1);
          bl=gcs(e,'border-left-width',1);
          br=gcs(e,'border-right-width',1);
        }
        else if(xDef(e.offsetWidth,e.style.width)){
          e.style.width=w+'px';
          pl=e.offsetWidth-w;
        }
      }
      w-=(pl+pr+bl+br);
      if(isNaN(w)||w<0) return;
      else e.style.width=w+'px';
    }
    w=e.offsetWidth;
  }
  else if(css && xDef(e.style.pixelWidth)) {
    if(w>=0) e.style.pixelWidth=w;
    w=e.style.pixelWidth;
  }
  return w;
}

function xHeight(e,h){
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(h)) {
    if (h<0) h = 0;
    else h=Math.round(h);
  }
  else h=-1;
  var css=xDef(e.style);
  if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    h = xClientHeight();
  }
  else if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
    if(h>=0) {
      var pt=0,pb=0,bt=0,bb=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = xGetComputedStyle;
        pt=gcs(e,'padding-top',1);
        if (pt !== null) {
          pb=gcs(e,'padding-bottom',1);
          bt=gcs(e,'border-top-width',1);
          bb=gcs(e,'border-bottom-width',1);
        }
        else if(xDef(e.offsetHeight,e.style.height)){
          e.style.height=h+'px';
          pt=e.offsetHeight-h;
        }
      }
      h-=(pt+pb+bt+bb);
      if(isNaN(h)||h<0) return;
      else e.style.height=h+'px';
    }
    h=e.offsetHeight;
  }
  else if(css && xDef(e.style.pixelHeight)) {
    if(h>=0) e.style.pixelHeight=h;
    h=e.style.pixelHeight;
  }
  return h;
}

/*
Other properties of xAnimation:
  x,y // instantaneous point
  af // acceleration factor
  ap // acceleration period
  t1 // start time
  et // elapsed time
  tmr // timer handle
  x1,y1 // start point or angle
  x2,y2 // end point or angle
  xm,ym // component magnitudes or arc length
  as // auto-start
Other properties used only by callbacks:
 xEllipse:
  xr,yr // x and y radii
  xc,yc // arc center point
 xSlideCorner:
  corner // corner string
 xParaEq:
  xs,ys // expression strings
  inc // T increment
*/

function xAnimation(e, at, qc, tt, orf, otf, oed, oea, oef) { // Object Prototype

  this.init(e, at, qc, tt, orf, otf, oed, oea, oef);
  var a = xAnimation.instances;
  var i;
  for (i = 0; i < a.length; ++i) { if (!a[i]) break; } // find an empty slot
  a[i] = this;
  this.idx = i;
}

xAnimation.instances = []; // static property

xAnimation.prototype.init = function(e, at, qc, tt, orf, otf, oed, oea, oef)
{
  this.e = xGetElementById(e); // e is object ref or id str
  this.at = at || 2; // acceleration type: 1=linear, 2=sine, 3=cosine
  this.qc = qc || 1; // acceleration quarter-cycles
  this.tt = tt; // total time
  this.orf = orf; // onRun function
  this.otf = otf; // onTarget function
  this.oed = oed; // delay before calling onEnd
  this.oea = oea; // onEnd argument
  this.oef = oef; // onEnd function
  this.to = 20; // timer timeout
  this.as = true; // auto-start
  return this;
};

xAnimation.prototype.start = function()
{
  var a = this;
  // acceleration
  if (a.at == 1) { a.ap = 1 / a.tt; } // linear
  else { a.ap = a.qc * (Math.PI / (2 * a.tt)); } // sine and cosine
  // magnitudes
  if (xDef(a.x1)) { a.xm = a.x2 - a.x1; }
  if (xDef(a.y1)) { a.ym = a.y2 - a.y1; }
  // end point if even number of cyles
  if (!(a.qc % 2)) {
    if (xDef(a.x1)) a.x2 = a.x1;
    if (xDef(a.y1)) a.y2 = a.y1;
  }
  if (!a.tmr) { // if not already running
    var d = new Date();
    a.t1 = d.getTime(); // start time
    a.tmr = setTimeout('xAnimation.run(' + a.idx + ')', 1);
  }
};

xAnimation.run = function(i) // static method
{
  var a = xAnimation.instances[i];
  if (!a) { /*alert('invalid index');*/ return; }
  var d = new Date();
  a.et = d.getTime() - a.t1; // elapsed time
  if (a.et < a.tt) {
    a.tmr = setTimeout('xAnimation.run(' + i + ')', a.to);
    // acceleration
    a.af = a.ap * a.et;  // linear
    if (a.at == 2) { a.af = Math.abs(Math.sin(a.af)); } // sine
    else if (a.at == 3) { a.af = 1 - Math.abs(Math.cos(a.af)); } // cosine
    // instantaneous point
    if (xDef(a.x1)) a.x = a.xm * a.af + a.x1;
    if (xDef(a.y1)) a.y = a.ym * a.af + a.y1;
    a.orf(a); // onRun
  }
  else {
    var rep = false; // repeat
    if (xDef(a.x2)) a.x = a.x2;
    if (xDef(a.y2)) a.y = a.y2;
    a.tmr = null;
    a.otf(a); // onTarget
    if (xDef(a.oef)) { // onEnd
      if (a.oed) setTimeout(a.oef, a.oed); // no args passed to oef if oed or oef is str
      else if (xStr(a.oef)) { rep = eval(a.oef); }
      else { rep = a.oef(a, a.oea); }
    }
    if (rep) { a.resume(true); } // there may be a problem here if the anim is paused and oef returns true
/*
    if (rep && a.tmr) {
      a.tmr = null;
      a.resume(true);
    }
    else a.tmr = null;
*/
  }
};

xAnimation.prototype.pause = function()
{
  var s = false;
  if (this.tmr) {
    clearTimeout(this.tmr);
    this.tmr = null;
    s = true;
  }
  return s;
};

xAnimation.prototype.resume = function(fromStart)
{
  var s = false;
  if (!this.tmr) {
    var d = new Date();
    this.t1 = d.getTime();
    if (!fromStart) this.t1 -= this.et; // new start time is current time minus elapsed time
    this.tmr = setTimeout('xAnimation.run(' + this.idx + ')', this.to);
    s = true;
  }
  return s;
};

xAnimation.prototype.kill = function()
{
  this.pause();
  xAnimation.instances[this.idx] = null;
};
// end xAnimation


// FADE IN-OUT ------------------------------------------------------------------------------

var d = .2; //incrementos del fade
var TimeOutFade = 40; //Timeout del fade

function fadeOutIn(id,action,final){
	var elem = xGetElementById(id);
	chkTmr(id);
	if(action=='in'){
		xOpacity(id, 0);
		elem.style.display = 'block';
		fadeIn(id,final);
	}else if(action=='out'){
		fadeOut(id,final);
	}
}

function chkTmr(id){
	gBox = xGetElementById(id);
  if (gBox.fadeTmr) {
    clearTimeout(gBox.fadeTmr);
    gBox.fadeTmr = null;
  }
}

function fadeOut(id,final){
	if(final==null){final=0;}
  var o = xOpacity(id);
  var e = xGetElementById(id);
  if (o - d > final){
    //e.fadeTmr = setTimeout("fadeOut('" + id + "','" + final + "')", TimeOutFade);
	e.fadeTmr = setTimeout("fadeOut('" + e.id + "','" + final + "')", TimeOutFade);
    xOpacity(id, o - d);
  } else {
    xOpacity(id, final);
    e.style.display = 'none';
    e.fadeTmr = null;
  }
}

function fadeIn(id,final){
	if(final==null){final=1;}
  var o = xOpacity(id);
  var e = xGetElementById(id);
  if (o + d < final){
		//e.fadeTmr = setTimeout("fadeIn('" + id + "','" + final + "')", TimeOutFade);
		e.fadeTmr = setTimeout("fadeIn('" + e.id + "','" + final + "')", TimeOutFade);
    xOpacity(id, o + d);
  } else {
		xOpacity(id, final);
    e.fadeTmr = null;
  }
}

// TAPA y MENSAJE -----------------------------------------------

var TransparenciaTapa = '.6';

function PonerTapa(elemento){
	FormatearTapa(elemento);
	PosicionarTapa(elemento);
	if(xGetElementById('tapa').style.display == 'none'){ fadeOutIn('tapa','in',TransparenciaTapa); }
	document.body.style.cursor = 'wait';
}

function FormatearTapa(elemento){
	xHeight('tapa',xHeight(elemento));
	xWidth('tapa',xWidth(elemento));
}

function PosicionarTapa(elemento){
	xMoveTo('tapa',xPageX(xGetElementById(elemento)),xPageY(xGetElementById(elemento)));
}

function SacarTapa(){
	if(xGetElementById('tapa').style.display != 'none'){ fadeOutIn('tapa','out'); }
	document.body.style.cursor = 'default';
}

function PonerMensaje(elemento){
	xGetElementById('tapa_mensajes').innerHTML = '<img src="'+Images_Protocolo+'://'+Images_HostName+'/'+idiomaActual+'/imagenes/spinner.gif" width="16" height="16" class="spinner" align="absmiddle" />&nbsp;Procesando&nbsp;datos...';
	if(xGetElementById('tapa_mensajes').style.display == 'none'){ fadeOutIn('tapa_mensajes','in'); }
	PosicionarMensaje(elemento);
}

function CambiarMensaje(mensaje,elemento){
	xGetElementById('tapa_mensajes').innerHTML = mensaje;
	PosicionarMensaje(elemento);
}

function PosicionarMensaje(elemento){
	xMoveTo('tapa_mensajes',((xPageX(xGetElementById(elemento)))+(((xWidth(elemento))-(xWidth('tapa_mensajes')))/2)),((xPageY(xGetElementById(elemento)))+(((xHeight(elemento))-(xHeight('tapa_mensajes')))/2)));
}

function SacarMensaje(){
	xGetElementById('tapa_mensajes').innerHTML = '';
	if(xGetElementById('tapa_mensajes').style.display != 'none'){ fadeOutIn('tapa_mensajes','out'); }
}