var IE;
var emod;
var maq = 0;
var porta = 2001;
var salaNum = 0;
var canalNome = "";
var subCanalNome = "";
var salaNome = "";
var erroMsg = 0;
var oXml; 
var who = "";
var erroMsgStr = new Array(
  "",
  "Ops! Este apelido já está sendo usado.\nEscolha outro e tente de novo. :D",
  "Ops! A sala está cheia.\nQue tal tentar entrar em outra sala do mesmo canal? :D",
  "Ops! Por favor, digite o código corretamente. :D");
if(document.all)
{
  IE = true;
  emod = "IE4+";
}
else
{
  IE = false;
  emod = "W3C"; 
}
function LTrim( value ) 
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
function trim( value ) 
{
	return LTrim(RTrim(value));
}

function extractFieldValue(ajaxData,fieldId)
{
  var tmp;
  if(!fieldId) return trim(ajaxData.firstChild.data);
  if((tmp = ajaxData.getElementsByTagName(fieldId)[0]) && tmp.firstChild)
  {
    return trim(tmp.firstChild.data);
  }
  else return "";
}
function sysMsg(msg)
{
  return '<center><table class="tableSysMsg"><tr><td nowrap class="tableSysMsg">'+msg+'</td></tr></table></center>';
}
function autoResize(src)
{
  var newheight;
  var frObj = document.getElementById('mainright');  
  frObj.height = "400px";
  try
  {
    newheight = frObj.contentWindow.document.body.scrollHeight;
  }
  catch(err)
  {
    // DOC: dominio do iframe diferente
    newheight = 1200;
  }
  
  
  if(getWho() == 'Chat no celular') { newheight = 670;}
  
  frObj.height = (newheight) + "px";
}
function numToStr(n)
{
  nStr = n.toString();
  len = nStr.length;
  s = "";
  for(i=0;i<len;i++)
  {
    if((len - i)%3 == 0 && i != 0) s += '.';
    s += nStr.charAt(i);
  }
  return s;
}

function reloadHome()
{
  location.replace(location.protocol+'//'+location.hostname+'/');
}
function escreveCanais()
{
  oXml = document.getElementById('canaisXML');
  canais = new Array();
  SEP = '<#>';
      
  for(i=0;(tmp=oXml.getElementsByTagName('canal')[i]);i++)
  {
	if((id = unescape(tmp.getElementsByTagName('id')[0].firstChild.data)) >= 100) continue;
    canais[i] = unescape(tmp.firstChild.data);
    canais[i] += SEP + id;
  }
  canais.sort()
  for(i=0;i < canais.length;i++)
  {
    tmp = canais[i].split(SEP);
    if(tmp[1] >= 100) continue; // Convidados
    document.write('<a href="javascript: void(0)" class="td03Item" id="canal'+tmp[1]+'" name="'+tmp[0]+'" onclick="javascript: setWho(\''+tmp[0]+'\');"><img src="imgs/setamenu.gif" class="setamenuClass">'+tmp[0]+'</a>')
  }
}

function escreveMenu(menu,setaId)
{
  oXml = document.getElementById(menu);
  menus = new Array();
  SEP = '<#>';

  for(i=0;(tmp=oXml.getElementsByTagName('item')[i]);i++)
  {
    menus[i] = unescape(tmp.getElementsByTagName('nome')[0].firstChild.data);
    menus[i] += SEP + unescape(tmp.getElementsByTagName('ref')[0].firstChild.data);
  }
  menus.sort();
  
  for(i=0;i < menus.length;i++)
  {
    tmp = menus[i].split(SEP);
    document.write('<a href="javascript: void(0)" class="td04Item" id="link'+i+'" name="'+tmp[1]+'" onclick="javascript: setWho(\''+tmp[0]+'\');"><img src="imgs/setamenu'+setaId+'.gif" class="setamenuClass">'+tmp[0]+'</a>')
  }
}

function setWho(who) {
	this.who = who;
}

function getWho() {
	return this.who;
}

function onclickH(e)
{
  var itemClicked;
  if(emod == "IE4+")
  {
    e = event;
    itemClickedID = e.srcElement.id;
    itemClickedName = e.srcElement.name;
  }
  else
  {
    itemClickedID = e.target.id;
    itemClickedName = e.target.name;
  }
  if(itemClickedID.substring(0,5) == 'canal')
  {
  	mainright.location.href = "canal.shtm?"+itemClickedID.substring(5);
  }
  else if(itemClickedID.substring(0,4) == 'link')
  {
    switch(itemClickedName)
    {
      case "faleconosco":
        open('mailto:suporte_igpapo@ig.com.br?subject=Fale%20conosco');
        break;
      case "bug":
        open('mailto:suporte_igpapo@ig.com.br?subject=Reporte%20de%20bugs');
        break;
      case "sugirasala":
        open('mailto:moderadora@ig.com?subject=Sugest&atilde;o%20de%20sala');
        break;
      case "chatcelular":
        open('http://mobilechat.brturbo.com.br/chat/topRoom.do?method=display&ag=torpedochat','igcelular','width=575,height=425');
        break;
      default:
  	    mainright.location.href = itemClickedName;
        break;
    }
  }
  return true;
}

function processUserData(ajaxData)
{ 
  document.getElementById('totpessoas').innerHTML = numToStr(extractFieldValue(ajaxData,"pessoas"));
  document.getElementById('totsalas').innerHTML = numToStr(extractFieldValue(ajaxData,"salas"));
}
function ajaxDataReceived(http) 
{
  if(http.readyState == 4) 
  {
    if(http.status == 200) 
    {
      processUserData(http.responseXML.documentElement);
    } 
    else 
    {
      //alert('Problema com a resposta. '+http.status);
    }
  }
}
function ajaxCall()
{ 
  var http = null; 
  if(window.XMLHttpRequest)      http = new XMLHttpRequest(); 
  else if (window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP"); 
  http.onreadystatechange = function() {ajaxDataReceived(http);};
  http.open("GET",location.protocol+"//"+location.host+"/cgi-lib/stat.xml", true); 
  http.setRequestHeader('Content-Type',  "text/xml");
  http.send(null);
    
  setAjaxTimer();
}
function setAjaxTimer()
{
   ajaxTimer = setTimeout('ajaxCall()',60000); 
}
function onloadH()
{
  setAjaxTimer()
}
function goBusca()
{
  Obj = document.getElementById('proc');
	apelido = Obj.value;

  if(apelido.length <= 2)
  {	
		alert("Escreva um nick de no mínimo 3 caracteres.");
		Obj.focus();
    return;
	}

  Obj.value = "";
	mainright.location.href='procura.shtm?'+escape(apelido);
}
function escrevePessoasSalas(qual)
{
  document.write(numToStr(document.getElementById('pessoasSalasXML').getElementsByTagName(qual)[0].firstChild.data));
}
document.onclick = onclickH;

