/* Instancia o componente em vários browsers compativeis com xmlhttp */
function createAjax(){
	var ajax;
	try{
		ajax = new ActicveXObject("Microsoft.XMLHTTP");
	}
	catch(e){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex){
			try{
				ajax = new XMLHttpRequest();
				ajax.overrideMimeType("text/xml");
			}
			catch(exc){
				alert('Seu navegador não suporta os recursos da tecnologia Ajax!');
				ajax = null
			}
		}
	}
	return ajax;
}

function Ajax(divId,url,msgCarregando,fontColor,objForm){
	var prevCache;
	var previneCache;
	var objAjax = new createAjax();
	var d = document.getElementById(divId);
	var resultado;

	// Verifica sinal para adicionar prevCache a url.
	if(url.indexOf('?') > 0){
		prevCache = '&';
	}
	else{
		prevCache = '?';
	}

	// Pega a data e a hora para gerar uma requisição nova a cada chamada.
	previneCache = new Date().getTime();
	
	if(objAjax){
		objAjax.open("POST", url + prevCache + previneCache, true);
		objAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
		objAjax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		objAjax.setRequestHeader("Content-Length", "0"); 
		objAjax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		objAjax.setRequestHeader("Pragma", "no-cache");

		objAjax.onreadystatechange = function(){
			if(objAjax.readyState == 4 && objAjax.status == 200){
				resultado = unescape(objAjax.responseText.replace(/\+/g," "));
				d.innerHTML = resultado
				anexaJavaScript(resultado);
			}
			else{
				d.innerHTML = '<img src="imagens/img_ajax/aguarde.gif" border="0"><span style="font-family:tahoma; font-size:10px; color:' + fontColor + '; font-weight:bold;">' + msgCarregando + '...</span>';
			}
		
		}
		objAjax.send(objForm);
	}
}

function anexaJavaScript(strHtml){
	var inicio = 0;
	var fim;
	var novo;
	while(inicio != -1){
		inicio = strHtml.indexOf('<script', inicio);
		if(inicio >= 0){
			inicio = strHtml.indexOf('>', inicio) + 1;
			fim = strHtml.indexOf('</script>', inicio);
			codigo = strHtml.substring(inicio,fim);
			novo = document.createElement('script');
			novo.type = 'text/javascript';
			novo.language = 'javascript';
			novo.text = codigo;
			document.body.appendChild(novo);
		}
	}
}
