function IniciaAjax(){  
  var HTTP_REQUEST;
  try {
	HTTP_REQUEST = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(e) {
	try {
	  HTTP_REQUEST = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(ex) {
	  try {
		HTTP_REQUEST = new XMLHttpRequest();
		HTTP_REQUEST.overrideMimeType('text/html');
	  } catch(exc)  {
		alert("Esse browser não tem recursos para uso do Ajax");
		HTTP_REQUEST = null;
	  }
	}
  }
  return HTTP_REQUEST;
}

function loadChamaAtend(url,fecha, aberto)
{
    var valor=0;
	var verifica = document.getElementById('ChamaAtend').style.display;
	if(verifica=='none'){
	  valor = 1;
	}
	ajax = IniciaAjax();
	if(fecha==1){
	  url = '../imosoft/chama_atendimento.php?';
	  var dados = 'x=1&valor='+valor;
	  if (ajax){
		ajax.onreadystatechange= function(){
		  if(ajax.readyState==1){
		  }
		  if(ajax.readyState==4){
			document.getElementById('ChamaAtend2').innerHTML=ajax.responseText;
		  }
		}
		ajax.open('POST', url, true);
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(dados);
	  }
	  document.getElementById('ChamaAtend').style.display = 'none';
	  document.getElementById('ChamaAtend2').style.display = 'none';
	} else {
	  url = '../imosoft/chama_atendimento.php?';
	  var dados = 'x=0&valor='+valor;
	  if (ajax){
		ajax.onreadystatechange= function(){
		  if(ajax.readyState==1){
		  }
		  if(ajax.readyState==4){
			document.getElementById('ChamaAtend2').innerHTML=ajax.responseText;
			if(ajax.responseText=='5'){
				aberto = 1;
			}
		  }
		}
		ajax.open('POST', url, true);
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(dados);
	  }
	  if(aberto==1){
	    document.getElementById('ChamaAtend').style.display = '';
	    document.getElementById('ChamaAtend2').style.display = '';
	    setInterval("loadChamaAtend('../imosoft/chama_atendimento.php', '0', '1')",30000);
	  } else {
	    setInterval("loadChamaAtend('../imosoft/chama_atendimento.php', '0', '0')",15000);
	  }
	}
}

loadChamaAtend("../imosoft/chama_atendimento.php",0,0);
