//var xmlhttp;
var vtout;
var vjob;
vjob=0;
function carregar(url, tagId)
{
//if (vjob==1){return}
    document.getElementById(tagId).innerHTML='<div style="position:relative;background-color:#ffffcc;width:100%;text-align:center;">Processando...</div>';
    goAjax( url , "GET", true , tagId,"",0,false);
}
function carregarsilent(url, tagId)
{
//if (vjob==1){return}
     goAjax( url , "GET", true , tagId,"",1,false);
}
function enviar(url, tagId,parametros)
{
//if (vjob==1){return}
    document.getElementById(tagId).innerHTML="<img src=/img/carreg.gif border=0>";
    goAjax( url , "POST", true , tagId,parametros,0,false);
}
function adicionar(url,tagId,parametros)
{
    goAjax( url , "POST", true , tagId,parametros,1,true);
}



function goAjax(url, metodo, modo, tagRetorno, parametros,silent,adiciona) {
url=url.replace(' ','+');
if (url.indexOf('?')==-1){alert('Incluir interrogação no fim da URL!!!')}
vjob=1;
tr1=document.getElementById(tagRetorno)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(ex)
	{
	try
	{
	xmlhttp = new XMLHttpRequest();
	}
	catch(exc)
		{
		alert("Esse browser usa tecnologia Ajax, mas seu navegador não dá suporte. É necessário atualizar seu navegador para navegar neste site.");
		xmlhttp = null;
		}
}

            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200){
				if(typeof(vtout)!='undefined'){clearTimeout(vtout)}
        	        	retorno=xmlhttp.responseText
				if (adiciona==false)
				{
		                	tr1.innerHTML=retorno
				}else{
					tr1.innerHTML=tr1.innerHTML+retorno
				}
			}else{
				if (silent==0){tr1.innerHTML="Ocorreu um erro ao tentar carregar a página.<BR>"+xmlhttp.statusText+"<BR><A href=javascript:goAjax('"+url+"','"+metodo+"','"+modo+"','"+tagRetorno+"','"+parametros+"','"+silent+"')>Clique aqui para tentar novamente.</a>"}
			}
		vjob=0;
                }
            }

            if(metodo == "GET") {
                xmlhttp.open("GET", url+"&rnd="+Math.random(), modo);
            } else {        
                xmlhttp.open("POST", url, modo);
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
                xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
                xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
                xmlhttp.setRequestHeader("Pragma", "no-cache");
            }    
           
	   vtout=setTimeout("abortar()",35000);
           if(metodo == "GET") {
                xmlhttp.send(null);
            } else {        
                xmlhttp.send(parametros);
            }
}
function abortar(){
xmlhttp.abort();
vjob=0;
}

function execScript(codigoHTMLcomScript)
{
   // Cria elemento de script
   var scriptObj = document.createElement('script');

   // Divide o responseText em 2 partes, sendo a segunda a parte onde irá iniciar o script
   // então desconsidera a primeira parte.
   tmpScriptCode = codigoHTMLcomScript.split('<script language="javascript" type="text/javascript">');

   // divide a parte relevante novamente em 2 partes, sendo que agora a primeira
   // parte conterá o script, a segunda parte é descartada.
   scriptCode = tmpScriptCode[1].split('</script>');

   // Define parâmetro language=javascript para o objeto de script 
   scriptObj.setAttribute('language', 'javascript');

   // Atribui o código-fonte do script ao obj de script
   scriptObj.text = scriptCode[0];

   // Executar o script
   document.body.appendChild(scriptObj);

}
