var navegador = navigator.userAgent.toLowerCase();
var xmlhttp;

function objetoXML()
{
	if (navegador.indexOf('msie') != -1)
   {
		var controle = (navegador.indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
		try {
			xmlhttp = new ActiveXObject(controle); //Inicia o objeto no IE
		}
      catch (e) { }
	} else
   {
		xmlhttp = new XMLHttpRequest(); //Inicia o objeto no Firefox, Safari, Mozilla
	}
}

function EnviarPagina(url, campos, destino, tipoEnvio)
{
   var elemento = document.getElementById(destino);
	objetoXML();
	if (!xmlhttp)
   {
		elemento.innerHTML = 'Impossível iniciar o objeto XMLHttpRequest.';
		return;
	}
   else
   {
		elemento.innerHTML = '<img src=imagens/loading.gif>';
	}

	xmlhttp.onreadystatechange = function ()
   {
		if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0)
      {
			if (xmlhttp.status == 200)
         {
				elemento.innerHTML = xmlhttp.responseText;
            texto = unescape(xmlhttp.responseText.replace(/\+/g," "));
            document.getElementById(destino).innerHTML = texto;
            ExtraiScript(texto);
			}
         else
         {
				elemento.innerHMTL = 'Página não encontrada!';
			}
		}
	}

   if ( tipoEnvio == 'post' )
		xmlhttp.open('POST', url+'?'+campos, true);
   if ( tipoEnvio == 'get' )
	   xmlhttp.open('GET', url, true);
	xmlhttp.send(campos);
}

function ExtraiScript(texto)
{
	var ini = 0;
	while ( ini!=-1 ){
	   ini = texto.indexOf('<script', ini);
	   if ( ini >=0 )
      {
         ini = texto.indexOf('>', ini) + 1;
         var fim = texto.indexOf('</script>', ini);
         codigo = texto.substring(ini,fim);
         novo = document.createElement("script")
         novo.text = codigo;
         document.body.appendChild(novo);
      }
	}
}
