function openAjax()
{
    var Ajax;
	
    try
    {  Ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers mais populares, como: Firefox, Safari, dentre outros.
    }
    catch(ee)
    {	try
	{  Ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
	}
	catch(e)
	{   try
            {  Ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
            }
            catch(e)
            {	Ajax = false;
            }
	}
    }
		
    return Ajax;
}

/**** Ajax que espera o termino da execu��o ***/
function executaAjax(URL)
{
    xmlHttp = openAjax();
    xmlHttp.open("POST", URL, false);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");
    xmlHttp.send(null);
    //return xmlHttp.responseText;
}

function executaAjaxRetorno(URL)
{
    xmlHttp = openAjax();
    xmlHttp.open("POST", URL, false);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");
    xmlHttp.send(null);
    return xmlHttp.responseText;
}

function executaAjaxMsg(URL, Layer, MSG)
{
    Layer.innerHTML=MSG;
    xmlHttp = openAjax();
    xmlHttp.open("POST", URL, false);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");
    xmlHttp.send(null);
    Layer.innerHTML="";
    return xmlHttp.responseText;
}

function executaAjaxLayer2(URL, Layer, MSG)
{
    if (MSG == "")
    {  //Layer.innerHTML="<div align='center'><img src='imagens/loading.gif' width=32 height=32></div>";
    }
    else
    {  Layer.innerHTML=MSG;
    }
    xmlHttp = openAjax();
    xmlHttp.open("POST", URL, false);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");
    xmlHttp.send(null);
    Layer.innerHTML=xmlHttp.responseText;
}





function executaAjaxLayer(URL, Layer, MSG)
{

    //$(Layer).load(URL);

    $.ajax({
        type: "GET",
        url: URL,

         beforeSend: function(){
                // mostro a div loading
                //$('#loading').show();

                //  html(): equivalente ao innerHTML
                //$(Layer).html("<img src='img/ajaxLoader.gif'>");
                //$(Layer).html("<img src='img/loadingAnimation.gif'>");
                //$(Layer).html("<img src='img/loading.gif'>");
                $(Layer).html("<center><img src='img/lightbox-ico-loading.gif'></center>");
                //$(Layer).html("Carregando...");
            },


        success: function(ret){
            jQuery(Layer).html(ret);
            
        }
    });


}


/**********************************
 ***** FUNCOES TEMPORARIAS ********
 **********************************/



	
function carregaAjax(id, MSG, pagina)
{	var ExibirMSG;

   if (MSG=='')
	{  ExibirMSG='Carregando...';
	}
	else
	{  ExibirMSG=MSG;
	}

   if (document.getElementById)
	{	// Para os browsers complacentes com o DOM W3C.
		var exibeResultado = document.getElementById(id);
		// div que exibir� o resultado.
		var Ajax = openAjax();
		// Inicia o Ajax.
		Ajax.open("GET", pagina, true);
		// Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
      // Ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
      // Ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
      // Ajax.setRequestHeader("Pragma", "no-cache");
		// fazendo a requisi��o
		Ajax.onreadystatechange = function()
		{  if (Ajax.readyState == 1)
			{	// Quando estiver carregando, exibe: carregando...
				exibeResultado.innerHTML = "<div align='center'><strong>" + ExibirMSG + "</strong></div>";
			}
			if (Ajax.readyState == 4)
			{	// Quando estiver tudo pronto.
				if (Ajax.status == 200)
				{	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
					resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
					resultado = unescape(resultado); // Resolve o problema dos acentos 
					exibeResultado.innerHTML = resultado;
				}
				else
				{	exibeResultado.innerHTML = "Erro: .";
				}
			}
		}
		
		Ajax.send(null); // submete
	}
}

function carregaAjaxSync(id, MSG, pagina)
{	var ExibirMSG;

   if (MSG=='')
	{  ExibirMSG='Carregando...';
	}
	else
	{  ExibirMSG=MSG;
	}

   if (document.getElementById)
	{	// Para os browsers complacentes com o DOM W3C.
		var exibeResultado = document.getElementById(id);
		// div que exibir� o resultado.
		var Ajax = openAjax();
		// Inicia o Ajax.
		Ajax.open("GET", pagina, false);
		// Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
      // Ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
      // Ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
      // Ajax.setRequestHeader("Pragma", "no-cache");
		// fazendo a requisi��o
		Ajax.onreadystatechange = function()
		{  if (Ajax.readyState == 1)
			{	// Quando estiver carregando, exibe: carregando...
				exibeResultado.innerHTML = "<div align='center'><strong>" + ExibirMSG + "</strong></div>";
			}
			if (Ajax.readyState == 4)
			{	// Quando estiver tudo pronto.
				if (Ajax.status == 200)
				{	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
					resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
					resultado = unescape(resultado); // Resolve o problema dos acentos 
					exibeResultado.innerHTML = resultado;
				}
				else
				{	exibeResultado.innerHTML = "Erro: .";
				}
			}
		}
		
		Ajax.send(null); // submete
	}
}

function carregaAjaxPost(id, MSG, pagina)
{	var ExibirMSG;

   if (MSG=='')
	{  ExibirMSG='Carregando...';
	}
	else
	{  ExibirMSG=MSG;
	}

   if (document.getElementById)
	{	// Para os browsers complacentes com o DOM W3C.
		var exibeResultado = document.getElementById(id);
		// div que exibir� o resultado.
		var Ajax = openAjax();
		// Inicia o Ajax.
		Ajax.open("POST", pagina, true);
		Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");

		Ajax.onreadystatechange = function()
		{  if (Ajax.readyState == 1)
			{	// Quando estiver carregando, exibe: carregando...
				// exibeResultado.innerHTML = "<div align='center'><strong>Carregando...</strong></div>";
			}
			if (Ajax.readyState == 4)
			{	// Quando estiver tudo pronto.
				if (Ajax.status == 200)
				{	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
					resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
					resultado = unescape(resultado); // Resolve o problema dos acentos 
					exibeResultado.innerHTML = resultado;
				}
				else
				{  exibeResultado.innerHTML = "Erro: .";
				}
				AjaxOcupado=false;
			}
		}

		Ajax.send(null); // submete
	}
}

function executaAjax2(pagina)
{	var Ajax = openAjax(); 	// Inicia o Ajax.
	
	Ajax.open("GET", pagina, false); 
	// Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
   // Ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
   // Ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
   // Ajax.setRequestHeader("Pragma", "no-cache");
   // fazendo a requisi��o
	
	Ajax.send(null); // submete
		
	alert(Ajax.readyState);
	alert(Ajax.status);
	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
	resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
	resultado = unescape(resultado); // Resolve o problema dos acentos 
	alert(resultado);
	
	return resultado;
}

function executaAjax(pagina)
{	var Ajax = openAjax();  // Inicia o Ajax.
	
	Ajax.open("GET", pagina, false);

	Ajax.onreadystatechange = function()
	{  if (Ajax.readyState == 1)
		{	// Quando estiver carregando, exibe: carregando...
			// exibeResultado.innerHTML = "<div align='center'><strong>Carregando...</strong></div>";
		}
		if (Ajax.readyState == 4)
		{	// Quando estiver tudo pronto.
			if (Ajax.status == 200)
			{	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
				resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
				resultado = unescape(resultado); // Resolve o problema dos acentos 
				AjaxResultado = resultado;
			}
			else
			{  AjaxResultado = -1;
			}
			AjaxOcupado=false;
		}
	}
	
	AjaxOcupado=true;
	Ajax.send(null); // submete
}

function executaAjaxUpload(pagina, parametros)
{	var Ajax = openAjax();  // Inicia o Ajax.
	
	Ajax.open("POST", pagina, false);
	//Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");
	Ajax.setRequestHeader("Content-Type", "multipart/form-data; charset=iso-8859-1;");
	
	Ajax.onreadystatechange = function()
	{  if (Ajax.readyState == 1)
		{	// Quando estiver carregando, exibe: carregando...
			// exibeResultado.innerHTML = "<div align='center'><strong>Carregando...</strong></div>";
		}
		if (Ajax.readyState == 4)
		{	// Quando estiver tudo pronto.
			if (Ajax.status == 200)
			{	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
				resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
				resultado = unescape(resultado); // Resolve o problema dos acentos 
				AjaxResultado = resultado;
			}
			else
			{  AjaxResultado = -1;
			}
			AjaxOcupado=false;
		}
	}

	AjaxOcupado=true;
	Ajax.send(parametros); // submete
}



 

/* function executaAjaxPost(pagina, parametros)
{	var Ajax = openAjax();  // Inicia o Ajax.
			
	Ajax.open("POST", pagina, false);
	Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1;");
	
	Ajax.onreadystatechange = function()
	{  if (Ajax.readyState == 1)
		{	// Quando estiver carregando, exibe: carregando...
			// exibeResultado.innerHTML = "<div align='center'><strong>Carregando...</strong></div>";
		}
		if (Ajax.readyState == 4)
		{	// Quando estiver tudo pronto.
			if (Ajax.status == 200)
			{	var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa vari�vel
				resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
				resultado = unescape(resultado); // Resolve o problema dos acentos 
				AjaxResultado = resultado;
			}
			else
			{  AjaxResultado = -1;
			}
			AjaxOcupado=false;
		}
	}

	AjaxOcupado=true;
	Ajax.send(parametros); // submete
} */

