try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

var ajaxLastResult;
var ajaxLastResult_raw;
var ajaxLine = [];
var ajaxReady = true;
var ajax = {};

function ajaxAdd(func)
	{
		ajaxLine.push(func);
		ajaxWait();
	}

function ajaxWait()
	{
		if(ajaxReady == true)
			{
				ajaxReady = false;
				setTimeout(ajaxLine.shift(), 10);
			}
		else
			setTimeout('ajaxWait()', 10);
	}
	
function ajaxExecLast()
	{
		try {
			eval(ajaxLastResult);
		}
		catch(e) {
			errorShow('ajaxExecLast() ERROR<br>'+ajaxLastResult);
		}
	}

function ajaxGet(URL, func)
	{
		var texto;

		ajaxReady = false;

    	xmlhttp.open("GET", URL, true);
		xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4)
					{						
						texto = xmlhttp.responseText;
						
						ajaxLastResult_raw = texto;
						
						texto = texto.replace(/\+/g," ");
						texto = unescape(texto);
						
						ajaxLastResult = texto;
												
						setTimeout(func, 1);

						ajaxReady = true;
					}
			}
    	xmlhttp.send(null);
	}

function ajaxPost(URL, vars, func)
	{
		var texto;

		ajaxReady = false;

    	xmlhttp.open("POST", URL, true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4)
					{
						texto = xmlhttp.responseText;
						texto = texto.replace(/\+/g," ");
						texto = unescape(texto);
						
						ajaxLastResult = texto;
												
						setTimeout(func, 1);

						ajaxReady = true;
					}
			}

		xmlhttp.send(vars);
	}

function loadHTML(URL, obj)
	{
		var texto;
		ajaxReady = false;
		loading();

		xmlhttp.open("GET", URL, true);
		xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4)
					{
						texto = xmlhttp.responseText;
						texto = texto.replace(/\+/g," ");
						texto = unescape(texto);
						texto = texto.split('<!-- ENDJS -->');

						if(texto.length == 2)
							{
								obj.innerHTML = texto[1];
								eval(texto[0]);
							}
						else
							obj.innerHTML = texto[0];

						ajaxReady = true;
						if(typeof(ajax.onload) == 'function')
							window.setTimeout(ajax.onload, 100);
						loaded();
					}
			}
		xmlhttp.send(null);
	}

function loadHTML_bg(URL, obj)
	{
		var texto;
		ajaxReady = false;

		xmlhttp.open("GET", URL, true);
		xmlhttp.onreadystatechange=function()
			{
				if (xmlhttp.readyState==4)
					{
						texto = xmlhttp.responseText;
						texto = texto.replace(/\+/g," ");
						texto = unescape(texto);
						texto = texto.split('<!-- ENDJS -->');

						if(texto.length == 2)
							{
								obj.innerHTML = texto[1];
								eval(texto[0]);
							}
						else
							obj.innerHTML = texto[0];

						ajaxReady = true;
					}
			}
		xmlhttp.send(null);
	}

/* TRATAMENTO DE FORMULÁRIOS */
function formToPost (el) {
	var ret = '';
	campos = el.getElementsByTagName('INPUT');
	for(i=0;i<campos.length;i++) {
		if(campos[i].type == 'text' || campos[i].type == 'password'
			|| (campos[i].type == 'checkbox' && campos[i].checked == true) 
			|| (campos[i].type == 'radio' && campos[i].checked == true) 
			|| campos[i].type == 'hidden' )
			ret += campos[i].name+'='+escape(campos[i].value)+'&';
	}
	campos = el.getElementsByTagName('TEXTAREA');
	for(i=0;i<campos.length;i++) {
		ret += campos[i].name+'='+escape(campos[i].value)+'&';
	}
	campos = el.getElementsByTagName('SELECT');
	for(i=0;i<campos.length;i++) {
		ret += campos[i].name+'='+escape(campos[i].value)+'&';
	}

	ret = ret.substr(0, ret.length-1);
	return ret;
}
