
function ajaxIni(){
try{
	xmlhttp = new XMLHttpRequest();
}catch(ee){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){	
			xmlhttp = false;
		}
	}
}
}

fila=[];
ifila=0;
errBoxPre = '<table width="300" height="50" border="0" style="font-size: 14px; font-family: Arial, Helvetica, sans-serif; color: #990000; background-color: #FFCCCC; border: 1px solid #990000;"><tr><td align="center">';
errBoxPos = '</td></tr></table>';

function carregarDados(url, destino){
	document.getElementById(destino).innerHTML="Carregando...";
	fila[fila.length]=[url,destino];
	if((ifila+1)==fila.length)ajaxRun();
}

function ajaxRun(){
	url = fila[ifila][0];
	destino = fila[ifila][1];
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			if(xmlhttp.status != 200){
				switch(xmlhttp.status){					
					case 400:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 400<br />Erro de sintaxe."+errBoxPos;
						break
					case 401:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 401<br />N&atilde;o autorizado."+errBoxPos;
						break
					case 403:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 403<br />Acesso negado."+errBoxPos;
						break
					case 404:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 404<br />Arquivo n&atilde;o encontrado."+errBoxPos;
						break
					case 500:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 500<br />Erro interno do servidor."+errBoxPos;
						break
					case 501:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 501<br />N&atilde;o suportado."+errBoxPos;
						break
					case 502:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 502<br />Servidor temporariamente indispon&iacute;vel."+errBoxPos;
						break
					case 503:
						document.getElementById(destino).innerHTML=errBoxPre+"ERRO 503<br />Erro no retorno dos dados."+errBoxPos;
						break
				}
			} else {
				texto=unescape(xmlhttp.responseText.replace(/\+/g," "));
				document.getElementById(destino).innerHTML=texto;
			}
			ifila++;
			if(ifila<fila.length)setTimeout("ajaxRun()",20);
		}
	}
	xmlhttp.send(url);
}

function enviaForm(frmNome, url, destino){
	f = document.getElementById(frmNome);
	
	var query=url;
	
	for (i=0;i<f.elements.length;i++){
		query += query.search('\\?')!=-1 ? '&' : '?';
		if (f.elements[i].type == "radio") {
			if (f.elements[i].checked) {
				query += f.elements[i].name + '=' + f.elements[i].value;
			}
		} else {
			query += f.elements[i].name + '=' + f.elements[i].value;
		}
	}
	ajaxIni();
	carregarDados(query, destino);
}
//classe de requisição do ajax
	function Request(url){//classe que trata requisições HTML
	this._url = url;
	this._method = 'POST';
	this._sincrono = true;
	this._strPar = '';
	this._reqContentType = 'application/x-www-form-urlencoded';
	this._reqCharset = 'ISO-8859-1';
	this._onResponseArgs = new Array();
	if (window.XMLHttpRequest){
		this._r = new XMLHttpRequest();
	}
	if(!this._r){
		this._r = new ActiveXObject('Microsoft.XMLHTTP');
		if(!this._r){
			this._r = new ActiveXObject('Msxml2.XMLHTTP');
		}
	}
}
Request.prototype.setCharset = function(charset){
	this._reqCharset = charset;
}
Request.prototype.getCharset = function(){
	return this._reqCharset;
}
Request.prototype.setContentType = function(contenttype){
	this._reqContentType = contenttype;
}
Request.prototype.getContentType = function(){
	return this._reqContentType;
}
Request.prototype.setUrl = function(url){
	this._url = url;
}
Request.prototype.setMetodo = function(metodo){
	this._method = metodo;
}
Request.prototype.setSincrono = function(sincrono){
	this._sincrono = sincrono;
}
Request.prototype.setStrParam = function(str){
	this._strPar = str;
}
Request.prototype.getUrl = function(){
	return this._url;
}
Request.prototype.getMetodo = function(){
	return this._method;
}
Request.prototype.getSincrono = function(){
	return this._sincrono;
}
Request.prototype.getStrParam = function(){
	return this._strPar;
}
Request.prototype._onErro = function(msg,cod){
	if(msg!=""){
		msg = ": " + msg;
	}
	if(cod==null){
		cod = "???";
	}
	alert("Erro "+cod+msg);
}
Request.prototype.setEventoErro = function(funcao){
	this._onErro = funcao;
}
Request.prototype._onResponse = function(response){
	return true;
}
Request.prototype.setEventoResponse = function(funcao){
	for(var i=1;i<arguments.length;i++){
		this._onResponseArgs.push(arguments[i]);
	}
	this._onResponse = funcao;
}
Request.prototype._onIniciaLoad = function(){
}
Request.prototype.setEventoIniciaLoad = function(funcao){
	this._onIniciaLoad = funcao;
}
Request.prototype.cancelar = function(){
	try{
		this._r.abort();
	}catch(e){
	}
}

Request.prototype.ativar = function(){
	var r = this._r;
	if(!r){
		this._onErro('Atualize seu browser para obter todas as funcionalidades desta aplicação.');
	}else{
		r.open(this._method,this._url,this._sincrono);
		r.setRequestHeader('content-type',this._reqContentType + '; charset=' + this._reqCharset);
		r.send(this._strPar);
		var caller = this;
		r.onreadystatechange = function(){
			if(r.readyState==2){
				caller._onIniciaLoad();
			}else if(r.readyState==4){
				if(r.status==200){
					var response = new Object();
					response.text = r.responseText;
					response.xml = r.responseXML;
					if(caller._onResponseArgs.length==0){
						caller._onResponse(response);
					}else{
						var arg = "response";
						for(var a=0;a<caller._onResponseArgs.length;a++){
							arg += ",caller._onResponseArgs["+a+"]";
						}
						eval("caller._onResponse(" + arg + ")");
					}
				}else if(r.status==404){
					caller._onErro('Documento não encontrado',r.status);
				}else{
					caller._onErro('',r.status);
				}
			}
		}
	}
}