// Suporte ao Ajax
// Alexandre Tedeschi (d)
// alexandre arroba gmail.com
var xmlhttp = null;
var ajaxInstances = new Array();
var ajaxDebug     = false;

function ajaxInit(){
	if(xmlhttp != null)
		return;
	
	var msxmlhttp = new Array(
		'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0',
		'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP');
	for(var i = 0; i < msxmlhttp.length; i++){
		try{
			xmlhttp = new ActiveXObject(msxmlhttp[i]);
		}
		catch(e){
			xmlhttp = null;
		}
	}
	
	if(!xmlhttp && typeof XMLHttpRequest != "undefined")
		xmlhttp = new XMLHttpRequest();
}
function ajaxCall(link, parametros, callback){
	ajaxInit();
	
	xmlhttp.open("POST", link, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			if(!callback)
				top.window.status = xmlhttp.responseText;
			else{
				if(ajaxDebug) alert("[Resposta]:\n"+xmlhttp.responseText);
				callback(xmlhttp.responseText);
			}
		}
	}
	
	if(ajaxDebug)
		alert("[Request]\nURL: "+link+"\nParâmetros: "+parametros);
	xmlhttp.send(parametros);
	ajaxInstances[ajaxInstances.length] = xmlhttp;
}
function ajaxCancel(){
	if(xmlhttp == null)
		return;
	
	for(var i = 0; i < ajaxInstances.length; i++)
		ajaxInstances[i].abort();
}

