function ajax(url, target, msg) {
	document.getElementById(target).innerHTML = msg;
	if (window.XMLHttpRequest) {//native XMLHttpRequest object
		req = new XMLHttpRequest();
		req.onreadystatechange = function() {ajaxDone(target);};
		req.open("GET", url, true);
		req.send(null);
	} else if (window.ActiveXObject) {//IE/Windows Activex version
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	    if (req) {
	        req.onreadystatechange = function() {ajaxDone(target);};
	        req.open("GET", url, true);
	        req.send();
	    }
	}
}
function ajaxDone(target) {
	if (req.readyState == 4) {//only if red is "loaded"
		if (req.status == 200) {//only if "OK"
			results = req.responseText;
			document.getElementById(target).innerHTML = results;
		}else {
			document.getElementById(target).innerHTML = "Problemas! (" + req.statusText + "). Contacte o administrador";
		}
	}
}
////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
function atualiza(url, valores, target, action){
	document.getElementById(target).innerHTML = "Aguarde...";
    req = null;

// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject))
	{
    	try
		{
			req = new XMLHttpRequest();
        } catch(e) {
			req = null;
        }
    }
    // branch for IE/Windows ActiveX version
	else
	{
		if(window.ActiveXObject)
		{
       		try
			{
        		req = new ActiveXObject("Msxml2.XMLHTTP");
      		} 
			catch(e)
			{
        		try
				{
          			req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					req = null;
				}
			}
    	}
	}
	
	if(req)
	{
		req.onreadystatechange = function() {processReqChange(target, action);};
		
		requisicaoAssincrona = true;
		navegador = navigator.appName;
		if(navegador == 'Netscape' && navigator.userAgent.indexOf('Firefox') == -1) requisicaoAssincrona = false;
		
		req.open("POST",url,requisicaoAssincrona);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
		req.send(valores);
	}
	else
	{
		
	}
}

function processReqChange(target, action)
{
   if (req.readyState == 4)
   {
        if (req.status == 200)
		{
			var text = req.responseText;
			//alert(text);
			//var ScriptJs=unescape(req.responseText.replace(/\+/g," "));
		   	//extraiScript(ScriptJs);
			if(trim(text, "\n")=="logado")
			{
				window.location.reload();
			}
			else
			{
				document.getElementById(target).innerHTML = text;
			}			
        }
		else
		{
            alert("Houve um problema ao obter os dados:\n" + req.statusText);
        }
    }
}
/////////////////////////////////////////
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function extraiScript(texto){
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            eval(codigo);
        }
    }
}
