//ptssyr


/*****************************************************************************
*
*
*****************************************************************************/
function hitch(C,D) {
 var B=Array.prototype.slice.call(arguments,2);
 function A() {
  var E=(C===null?this:C);
  var F=(typeof D=="function"?D:E[D]);
  return F.apply(E,B.concat(Array.prototype.slice.call(arguments)))
 }
 A.methodName=D;
 return A
}


/*****************************************************************************
*
*
*****************************************************************************/
Ajax=function() {
 this.m_httpListen=null;
 this.m_httpListenTimestamp=0;
 this.m_pendingRequests=0;
 this.m_eventPollDuration=45000;
 this.m_networkPollDuration=10000;
 this.m_serverTimeoutDuration=180000;
 this.m_messageRev=0;
 this.m_keepAliveListenerTimerId=-1;
 this.m_listenTimerId=-1;
 this.m_noResponseLimit=12;
 this.m_noResponses=0;
 this.m_secureDomain="";
 this.m_commandPath="cmd";
 this.m_modulePath="mcmd";
 this.m_timerArray=new Array();
 this.m_intervalArray=new Array();
 this.m_bTest=false;
 this.m_testXHR;
 this.m_sessionKey;
 this.m_sessionId=0;
 this.m_onResume;
 this.m_onFailTest;
 this.m_onStopAjax;
 this.m_onAbort;
 this.m_onEvent;
 this.m_onPostData;
 this.m_onEvalError
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getSessionKey=function() {
 return this.m_sessionKey||""
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.setSessionKey=function(A,B) {
 this.m_sessionKey=A;
 if (B) {
  this.m_messageRev=B
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getSessionId=function() {
 return this.m_sessionId||0
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.setSessionId=function(A) {
 this.m_sessionId=A
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.setCallbacks=function(E,G,D,B,A,C,F) {
 if (E) {
  this.m_onEvent=E
 }
 if (G) {
  this.m_onResume=G
 }
 if (D) {
  this.m_onFailTest=D
 }
 if (B) {
  this.m_onStopAjax=B
 }
 if (A) {
  this.m_onAbort=A
 }
 if (C) {
  this.m_onPostData=C
 } if (F) {
  this.m_onEvalError=F
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.onEvent=function(A) {
 if (this.m_onEvent) {
  this.m_onEvent(A)
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.onResume=function() {
 if (this.m_onResume) {
  this.m_onResume()
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.onStopAjax=function() {
 if (this.m_onStopAjax) {
  this.m_onStopAjax()
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.onFailTest=function(A) {
 if (this.m_onFailTest) {
  this.m_onFailTest(A)
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.onAbort=function() {
 if (this.m_onAbort) {
  this.m_onAbort()
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getTestFlag=function() {
 return this.m_bTest
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.setTestFlag=function(A) {
 this.m_bTest=A
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getPendingRequests=function() {
 return this.m_pendingRequests
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.incrementPendingRequests=function() {
 this.m_pendingRequests++
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.decrementPendingRequests=function() {
 this.m_pendingRequests--
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getCGIDomain=function(A) {
 return(A&&this.m_secureDomain||(typeof MeeboExtensionDomain!="undefined"?MeeboExtensionDomain:""))
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getCGIURL=function(C,B) {
 var A=this.getCGIDomain(B);
 if (A) {
  A=(B?"https://":"http://")+A
 }
 return A+"/"+(C?this.m_modulePath:this.m_commandPath)+"/"
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getMaxNoResponseLimit=function() {
 return this.m_noResponseLimit
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.clearTimeouts=function() {
 while (this.m_timerArray.length) {
  clearTimeout(this.m_timerArray.pop())
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.clearIntervals=function() {
 while (this.m_intervalArray.length) {
  clearInterval(this.m_intervalArray.pop())
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.addTimeout=function(A) {
 this.m_timerArray.push(A)
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.addInterval=function(A) {
 this.m_intervalArray.push(A)
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getLogout=function() {
 return !this.m_sessionKey
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.stopAjax=function() {
 this.onStopAjax();
 if (this.m_keepAliveListenerTimerId!=-1) {
  this.m_keepAliveListenerTimerId=-1
 }
 this.clearTimeouts();
 this.clearIntervals();
 if (this.m_httpListen) {
  this.m_httpListen=null;
  this.m_httpListenTimestamp=0
 }
 if (this.m_testXHR) {
  this.m_testXHR=null
 }
 this.m_messageRev=0;
 this.m_sessionKey=null;
 this.resetNoResponseCount();
 this.setTestFlag(false)
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.createListener=function() {
 if (this.m_httpListen) {
  this.m_httpListen=null;
  this.m_httpListenTimestamp=0
 }
 this.m_httpListen=this.getHTTPObject();
 this.ajaxListener()
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.initListener=function() {
 if (this.m_httpListen) {
  return
 }
 this.createListener()
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getHTTPObject=function() {
 var A=false;
 if (typeof XMLHttpRequest!="undefined") {
  try {
   A=new XMLHttpRequest();
   return A
  } catch (B) {
   A=false
  }
 } else {
  if (typeof Components!="undefined") {
   A=Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
   A.QueryInterface(Components.interfaces.nsIXMLHttpRequest)
  }
 }
 if (!A) {
  try {
   A=new ActiveXObject("Msxml2.XMLHTTP");
   return A
  } catch(B) {
   try {
    A=new ActiveXObject("Microsoft.XMLHTTP");
    return A
   } catch(B) {
    A=false
   }
  }
 }
 if (A==false) {
  alert("Su navegador no es compatible para utilizar este portal.")
 }
 return A
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.ajaxRequest=function(E,I,J,F,C,B) {
 var G=new Date().getTime();
 if (/(^|\?|&)sessionKey=/.test(E) && this.getLogout()) {
  this.handleAjaxRequestError(gLang.ajaxLogoutError,F,20,J);
  return false
 }
 if (!F&&(this.getNoResponseCount()>0)) {
  this.handleAjaxRequestError(gLang.ajaxConnectError,F,21,J);
  return false
 }
 if (F) {
  if (this.m_testXHR) {
   this.m_testXHR.abort();
   this.m_testXHR=null
  }
  this.incrementNoResponseCount();
  this.onFailTest(this.getNoResponseCount());
  if ((this.getNoResponseCount()>=this.m_noResponseLimit)&&!this.getLogout()) {
   this.reconnectNetwork("","handleNetworkError is requesting new connect when available");
   this.handleAjaxRequestError(gLang.ajaxError,F,22,J);
   return false
  }
 }
 var L=this.getHTTPObject();
 if (!L) {
  this.handleNetworkError(true,"ajaxRequest m_httpRequest is invalid",23);
  this.handleAjaxRequestError(gLang.ajaxError,F,23,J);
  return false
 }
 if (F) {
  this.m_testXHR=L
 }
 var D=true;
 var K;
 try{
  K=L.readyState
 } catch(H) {
  this.handleNetworkError(true,"ajaxRequest could not access readyState",24);
  this.handleAjaxRequestError(gLang.ajaxError,F,24,J);
  return false
 }
 var A=this.getCGIURL(C,B);
 if (L.readyState==4||L.readyState==0) {
  var M=setTimeout(hitch(this,"handleNetworkError",true,"ajaxRequest was not returned in time",25),this.m_eventPollDuration);
  this.addTimeout(M);
  L.open("POST",A+E,D);
  L.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  L.onreadystatechange=hitch(this,F?"callbackAjaxTest":"callbackAjaxRequest",L,E,J,M,new Date().getTime());
  try {
   L.send(I);
   this.incrementPendingRequests();
   networklog("ajaxRequest: took "+(new Date().getTime()-G)+" ms")
  } catch(H) {
   this.handleNetworkError(true,"ajaxRequest "+E+" send failed with args = "+I,26);
   this.handleAjaxRequestError(gLang.ajaxError,F,26,J);
   return false
  }
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.callbackAjaxRequest=function(xhr,request,callback,timeout,startTime) {
 if (typeof gAjax=="undefined"||!gAjax) {
  return
 }
 if (/(^|\?|&)sessionKey=/.test(request)&&this.getLogout()) {
  this.handleAjaxRequestError("Sorry, you are currently logged out.",false,1,callback);
  return
 }
 if (this.getNoResponseCount()) {
  this.handleAjaxRequestError("Sorry, meebo is having difficulties connecting to the network.",false,2,callback);
  return
 }
 clearTimeout(timeout);
 if (!xhr) {
  this.handleNetworkError(true,"callbackAjaxRequest could not get valid m_httpRequest",3);
  this.handleAjaxRequestError(gLang.ajaxError,false,3,callback);
  return
 }
 var connectivityTest;
 try {
  connectivityTest=xhr.readyState
 } catch(e) {
  this.handleNetworkError(true,"callbackAjaxRequest could not access readyState",4);
  this.handleAjaxRequestError(gLang.ajaxError,false,4,callback);
  return
 }
 if (xhr.readyState==4) {
  var status;
  try {
   status=xhr.status
  } catch(e) {
   this.handleNetworkError(true,"callbackAjaxRequest could not examine status",5);
   this.handleAjaxRequestError(gLang.ajaxError,false,5,callback);
   return
  }
  if ((status==404)||(status==500)) {
   this.handleAjaxRequestError(gLang.ajaxError,false,status,callback);
   return
  }
  if (status!=200) {
   this.handleAjaxRequestError(gLang.ajaxError,false,status,callback);
   return
  } try {
   var response=xhr.responseText;
   this.decrementPendingRequests()
  } catch(e) {
   this.handleNetworkError(true,"callbackAjaxRequest could not examine responseText",6);
   this.handleAjaxRequestError(gLang.ajaxError,false,6,callback);
   return
  }
  if (!response.length&&!this.getLogout()) {
   this.handleNetworkError(true,"callbackAjaxRequest received null response",7);
   this.handleAjaxRequestError(gLang.ajaxError,false,7,callback);
   return
  }
  if (!response.length) {
   this.handleNetworkError(true,"callbackAjaxRequest received null response after new line character",8);
   this.handleAjaxRequestError(gLang.ajaxError,false,8,callback);
   return
  }
  if (response=='"failure"'&&!this.getLogout()) {
   this.handleAjaxRequestError(gLang.ajaxError,false,9,callback);
   this.reconnectNetwork("","callbackAjaxListener received failure");
   return
  }
  if (callback) {
   var bFunc=typeof callback=="function";
   try{
    if (bFunc) {
     callback(eval("("+response+")"),true,0)
    } else {
     eval(callback+"("+response+", true, 0)")
    }
   } catch(e) {
    if (this.m_onEvalError) {
     this.m_onEvalError(e,bFunc,callback,response)
    }
    this.handleAjaxRequestError(gLang.ajaxError,false,10,callback);
    return
   }
   networklog("ajax loop: "+callback+" took "+(new Date().getTime()-startTime)+" ms");
   return
  }
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.handleAjaxRequestError=function(errorMsg,bTest,retCode,callback) {
 if (callback) {
  try { 
   if (typeof callback=="function") {
    callback(errorMsg,false,retCode)
   } else {
    eval(callback+'("'+errorMsg+'", false, '+retCode+")")
   }
  } catch(e) {
  }
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.callbackAjaxTest=function(E) {
 if (typeof gAjax=="undefined"||!gAjax) {
  return
 } 
 if (this.getLogout()) {
this.stopAjax();
  return
 } if (!E) {
  this.handleNetworkError(true,"callbackAjaxTest m_httpRequest is null",60);
  return
 }
 var A;
 try {
  A=E.readyState
 } catch(D) {
  this.handleNetworkError(true,"callbackAjaxTest could not access readyState",61);
  return
 } 
 if (E.readyState==4) {
  var C;
  try {
   C=E.status}catch(D) {
   this.handleNetworkError(true,"callbackAjaxTest could not examine status",62);
   return
  }
  if (C==404||C==500) {
   this.reconnectNetwork("","test request returned HTTP failure code");
   return
  }
  if (C!=200) {
   this.handleNetworkError(true,"callbackAjaxTest had non-200 status",1062);
   return
  } try {
   var B=E.responseText;
   this.decrementPendingRequests()
  } catch(D) {
   this.handleNetworkError(true,"callbackAjaxTest could not examine responseText",63);
   return
  }
  if (!B.length&&!this.getLogout()) {
   this.handleNetworkError(true,"callbackAjaxTest received null response",64);
   return
  }
  if (!B.length) {
   this.handleNetworkError(true,"callbackAjaxRequest received null response after new line character",65);
   return
  }
  if ((B=='"failure"')&&!this.getLogout()) {
   this.reconnectNetwork("","test request returned failure");
   return
  }
  this.handleNetworkResume(B);
  return
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.ajaxListener=function() {
 if (this.getLogout()) {
  return
 }
 if (this.getNoResponseCount()>0) {
  return
 }
 if (!this.m_httpListen) {
  this.handleNetworkError(false,"ajaxListener could not find valid m_httpListen",101);
  return
 }
 if (this.m_keepAliveListenerTimerId!=-1) {
  clearTimeout(this.m_keepAliveListenerTimerId);
  this.m_keepAliveListenerTimerId=-1
 }
 var B;
 try {
  B=this.m_httpListen.readyState
 } catch(D) {
  this.handleNetworkError(false,"ajaxListener could not access readyState",102);
  return
 }
 if (this.m_httpListen.readyState==4||this.m_httpListen.readyState==0) {
  try {
   this.m_httpListen.open("POST",this.getCGIURL(true)+"events",true);
   this.m_httpListen.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   this.m_httpListen.onreadystatechange=hitch(this,"callbackAjaxListener")
  } catch(D) {
   this.handleNetworkError(false,"ajaxListener could not configure httpListen",103);
   return
  }
  var A=this.m_onPostData?"&"+this.m_onPostData():"";
  var C="sessionKey="+this.getSessionKey()+"&rev="+this.m_messageRev+"&sessionId="+this.getSessionId()+A;
  this.m_keepAliveListenerTimerId=setTimeout(hitch(this,"handleNetworkError",false,"ajaxListener did not return callback fast enough",105),this.m_eventPollDuration);
  this.addTimeout(this.m_keepAliveListenerTimerId);
  this.m_httpListenTimestamp=new Date().getTime();
  try{
   this.m_httpListen.send(C);
   this.incrementPendingRequests()
  } catch(D) {
   this.handleNetworkError(false,"ajaxListener could not send request",106);
   return
  }
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.callbackAjaxListener=function() {
 if (typeof gAjax=="undefined" || !gAjax) {
  return
 }
 if (this.getLogout()) {
  return
 }
 if (!this.m_httpListen) {
  this.handleNetworkError(false,"callbackAjaxListener could not find valid m_httpListen",201);
  return
 }
 var A;
 try {
  A=this.m_httpListen.readyState
 } catch(D) {
  this.handleNetworkError(false,"callbackAjaxListener could not access readyState",202);
  return
 }
 if (this.m_httpListen.readyState==4) {
  var C;
  try {
   C=this.m_httpListen.status
  } catch(D) {
   this.handleNetworkError(false,"callbackAjaxListener could not access status",203);
   return
  }
  if (C==404) {
   this.reconnectNetwork("","callbackAjaxListener received 404");
   return
  }
  if (C==500) {
   this.handleNetworkError(false,"status was 500",1203);
   return
  }
  if (C!=200) {
   this.handleNetworkError(false,"status was not 200",1203);
   return
  }
  if (this.m_keepAliveListenerTimerId!=-1) {
   clearTimeout(this.m_keepAliveListenerTimerId);
   this.m_keepAliveListenerTimerId=-1
  }
  var B=this.m_httpListen.responseText;
  this.decrementPendingRequests();
  if (!B.length&&!this.getLogout()) {
   this.handleNetworkError(false,"callbackajaxListener received null response from server",204);
   return
  }
  if (!B.length) {
   this.handleNetworkError(false,"callbackAjaxListener received null response from server after character termination");
   return
  }
  if (B=='"failure"'&&!this.getLogout()) {
   this.reconnectNetwork("","callbackAjaxListener received failure");
   return
  } try {
   networklog("callbackAjaxListener: received response from server, response = "+B);
   if (B.length) {
    this.dispatchEvent(B)
   }
  } catch(D) {
  }
  setTimeout(hitch(this,"ajaxListener"),0)
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.dispatchEvent=function(response) {
 var ev;
 try {
  eval("ev = "+response+";")
 } catch(e) {
  this.handleNetworkError(false);
  return
 }
 if (this.isValidEvent(ev)) {
  networklog("dispatchEvents: message Rev = "+ev.rev);
  if (ev.rev&&(ev.rev>0)) {
   this.m_messageRev=ev.rev
  }
  this.onEvent(ev)
 } else {
  this.handleNetworkError(false)
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.isValidEvent=function(A) {
 try{
  if (!A) {
   this.stopAjax();
   this.onAbort();
   return false
  }
  if (!A.rev) {
   this.stopAjax();
   this.onAbort();
   return false
  }
 } catch(B) {
  return false
 }
 return true
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.handleNetworkError=function(D,B,C) {
 networklog("handleNetworkError: received network error, request - "+D+" error msg = "+B+" ret code="+C);
 networklog("handleNetworkError: logout:"+this.getLogout());
 if (this.getLogout()) {
  networklog("handleNetworkError stop Ajax");
  this.stopAjax();
  return
 }
 networklog("handleNetworkError: number of no responses is "+this.getNoResponseCount());
 if (this.getNoResponseCount()>0) {
  networklog("handleNetworkError: already doing some error handling, returning now");
  return
 }
 if (this.m_httpListenTimestamp&&new Date().getTime()-this.m_httpListenTimestamp>this.m_serverTimeoutDuration) {
  this.reconnectNetwork("","the session has been inactive for more than 3 minutes");
  return
 }
 var A=this.getTestFlag();
 this.setTestFlag(true);
 this.m_httpListen=null;
 this.m_httpListenTimestamp=0;
 networklog("handleNetworkError: canceling listener and restarting polling in "+this.m_networkPollDuration+" ms");
 if (!A) {

  this.setTestFlag(true);
  this.m_listenTimerId=setInterval(hitch(this,"ajaxRequest","test","sessionKey="+this.getSessionKey(),"",true,true),this.m_networkPollDuration);
  this.addInterval(this.m_listenTimerId);
  this.ajaxRequest("test","sessionKey="+this.getSessionKey(),"",true,true);
  this.onFailTest(0)
 }
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.handleNetworkResume=function(A) {
 if (this.getLogout()) {
  networklog("handleNetworkResume stop Ajax");
  this.stopAjax();
  return
 }
 this.clearIntervals();
 this.clearTimeouts();
 this.resetNoResponseCount();
 this.setTestFlag(false);
 this.onResume();
 this.createListener()
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.setNoResponseCount=function(A) {
 this.m_noResponses=A
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.getNoResponseCount=function() {
 return this.m_noResponses
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.incrementNoResponseCount=function() {
 this.m_noResponses++
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.resetNoResponseCount=function() {
 this.setNoResponseCount(0)
};


/*****************************************************************************
*
*
*****************************************************************************/
Ajax.prototype.reconnectNetwork=function(A,C,B) {
 networklog("reconnectNetwork: trying to logout (stopping AJAX)");
 this.stopAjax();
 this.onAbort();
 return
};


mjajax = new Ajax();

mjajax.initListener();

