//&& Permite revisar el estatus del chat de forma indefinida
function loadChk(){
   var timerID = setInterval("getStatus(modalidad,clave)", 300000);
   return timerID;
}

function enlinea(){}

//&& Permite la opertura de la ventana de chat
function openChat(oLiga){
	var aIdentifica = oLiga.name.split(":");
	var cUrl = "http://chat-cosmos.com/chat.php?ide="+aIdentifica[1]+"&cvem="+aIdentifica[0];
	var cTarget = "asesor" + aIdentifica[1];
	vu=window.open(cUrl, cTarget, 'width=800,height=400,scrollbars=no,left=250, top=200');
	vu.focus();
	return false;
}

//&& Define a partir del idioma, el grafico a desplegar
function defineGrafico(nModo){
   var cRetorno;
   cRetorno = "";
   //&& determinamos el idioma
   if (idiomaInterfaz){
      if (idiomaInterfaz=="ESP"){
         cImgOnline = "http://www.cosmos.com.mx/img/chatonline.gif";
         cImgOver = "http://www.cosmos.com.mx/img/chatover.gif";      
      }
      else{
         cImgOnline = "http://www.cosmos.com.mx/img/chatonlineing.gif";
         cImgOver = "http://www.cosmos.com.mx/img/chatovering.gif";      
      }
   }
   else{
      cImgOnline = "http://www.cosmos.com.mx/img/chatonline.gif";
      cImgOver = "http://www.cosmos.com.mx/img/chatover.gif";
   }
   //&& Dependiendo de la modalidad, regresamos lo que nos piden
   if (nModo == 0){
      cRetorno = cImgOnline;
   }
   else{
      cRetorno = cImgOver;
   }
   return cRetorno;
}

//&& Procesa la respuesta del servidor sobre el status de los chats
function proceso(cRespText){
   cRespText = cRespText.replace(/\r\n\r\n/,"");
   var aListado = cRespText.split(",");
   var oStatus = false;
   var oStatus2 = false;
   var cIdentifica = "";
	var cIdentiMaca = "";
	var aIdentifica = null;
   var RegularExp = /\empty/;
   
   //&& blanqueamos status
   if (memoChat.length>0){
      for (var i=0; i<memoChat.length; i++){
         aIdentifica = memoChat[i].split(":");
         cIdentifica = aIdentifica[0];
         cIdentiMaca = aIdentifica[1];
         oStatus = document.getElementById("lnk"+cIdentifica);
         oStatus2 = document.getElementById("img"+cIdentifica);
         if (oStatus){
            oStatus2.src="http://www.cosmos.com.mx/img/chatoff.gif";
				oStatus.style.display = "none";
         }
      }
   }
   memoChat = cRespText.split(",");
   
   //&& reasignamos status
   if (!cRespText.match(RegularExp)){
      for (var i=0; i<aListado.length; i++){
         aIdentifica = aListado[i].split(":");
         cIdentifica = aIdentifica[0];
         cIdentiMaca = aIdentifica[1];
         oStatus = document.getElementById("lnk"+cIdentifica);
         oStatus2 = document.getElementById("img"+cIdentifica);
         if (oStatus){
            oStatus2.src = defineGrafico(0);
            oStatus2.onmouseover = function(){this.src = defineGrafico(1);}
            oStatus2.onmouseout = function(){this.src = defineGrafico(0);}
				oStatus.name = aListado[i];
				oStatus.style.display = "inline";
         }
      }
   }
}

//&& Mi soporte para AJAX
function AjaxHandler(url, eventAttach, params){
   var self = this;
   this._Async = true;
   this._method = "GET";
   this._req = null;
   this._url = url;
   this._eventAttach = eventAttach;
   this._params = params;
   this._successful = true;
   this._systemMessage = "";
   this.readyState = 0;
   this.lxml = false;

   //&& Código del evento onreadystatechange
   function processReqChanges(){
      self.readyState = self._req.readyState;
      if (self._req.readyState == 4){
         self.readyState = 0;
         if (self._req.status == 200){
            if(self.lxml){
               self._eventAttach(self._req.responseXML);
            }
            else{
               self._eventAttach(self._req.responseText);
            }
         }
         else{
            this._systemMessage = "Hubo un problema al procesar la soliciud:\n" + self._req.statusText;
            this._successful = false;
         }
      }
   }
   
   //&& Envía una petición vía XMLHttpRequest() o su equivalente
	this.SendRequest = function(){
      if (window.XMLHttpRequest){this._req = new XMLHttpRequest()}
      else if (window.ActiveXObject){this._req = new ActiveXObject("Microsoft.XMLHTTP")}
      if (this._req){
         this._req.onreadystatechange = processReqChanges;
         this._req.open(this._method, this._url, this._Async);
         if (this._method = "POST"){
            this._req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            this._req.setRequestHeader("Content-length", this._params.length);
            this._req.setRequestHeader("Connection", "close");
            this._req.send(this._params);
         }
         else if (this._method = "GET"){
            this._req.send(null);
         }
      }
      else{
         this._systemMessage = "Al parecer, no hay soporte para XMLHttpRequest";
         this._successful = false;
      }
   }
}

//&& Obtener estado
function getStatus(cM,nC){
   oAnterior = document.getElementById("chkchat");
   oScript = document.createElement("script");
   oScript.type="text/javascript";
   oScript.src="http://cosmos.net.mx/chat/syncchat.vfp?c_m="+cM+"&n_c="+nC;
   oScript.id="chkchat";
   if (oAnterior){
      document.body.replaceChild(oScript,oAnterior);
   }
}

function sobrecarga(){
   //var iframeCarga = document.createElement("iframe");
   //iframeCarga.style.display = "none";
   //iframeCarga.src="http://cosmos.mx/producto.php?n_prod="+clave;
   //document.body.appendChild(iframeCarga);
}
