
function CreateObject() {
	var obj = new Object();
	//var msxmlhttp = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
	var msxmlhttp = new Array('Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP');

	for (var i = 0; i < msxmlhttp.length; i++) {
		try {
			obj = new ActiveXObject(msxmlhttp[i]);
			break;
		} catch (e) {
			obj = null;
		}
	}
	
	if(!obj && typeof XMLHttpRequest != "undefined") {
		obj = new XMLHttpRequest();
	}
	
	return obj;
}

function Init_Ajax() {
	var glb_obj;
	glb_obj = CreateObject();
	
	this.SetOption 		= ""; 		/* Ex.: op=nome */
	this.SetUri 		= "";		/* functions.inc.php */
	this.SetMethod 		= "";		/* GET ou POST */
	this.isXML			= false;	/* bool se o retorno ser em XML */
	this.msg			= "";
	this.msgobj			= null;
	this.objFieldName   = Array();
	this.objValue		= Array();
	var MSGWait 		= "";		/* Mensagem para aguardar carregamento */
	var MSGObj			= null;		/* Objeto que exibir a mensagem enquanto aguarda retorno */
	var func			= "";		/* Funo que ser executada quando o processo retornar */
	var Par				= "";
  var seturi            = "";
  
  this.antiCacheRand = function(aurl) {
	 var dt = new Date();
	 if(aurl.indexOf("?") >= 0) {
		 return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
	 } else {
		 return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());
	 }
  }
  
	if( (this.SetMethod != "GET") && (this.SetMethod != "POST") ) {
		this.SetMethod = "GET";
	}
	
	this.Wait = function() {
		if(glb_obj.readyState == 4 || glb_obj.readySate == "complete") {
			if(glb_obj.status == 200) {
				if(MSGObj)
						MSGObj.innerHTML = "&nbsp;";
					eval(func + "(glb_obj)");
			} else {
				MSGObj.innerHTML = "Erro ao carregar arquivo: " + seturi + ", retornou:" + glb_obj.status;
			}
		} else {
			if(MSGObj)
				MSGObj.innerHTML = MSGWait;
		}	
	}

	this.Parametros = function() {
		var txt = "";
		var tot = this.objFieldName.length;
		if(tot > 0) {
			for(var i = 0; i < tot; i++) {
				txt += this.objFieldName[i] + "=" + this.objValue[i] + (i < (tot - 1) ? "&" : "" );
			}
		}
		return txt;
	}
	
	this.Execute = function(f) {
		var txt, url;
		func = f;
		
		MSGWait = this.msg;
		MSGObj  = this.msgobj;
		
		if (glb_obj != null) {
			if( (this.SetMethod != '') && (this.SetUri != '') ) {
				glb_obj.onreadystatechange = this.Wait;
				
				if(this.SetOption != '') {
					url = this.SetUri + "?" + this.SetOption;
				} else {
					url = this.SetUri;
				}
				seturi = this.SetUri;
				url = this.antiCacheRand(url);

				glb_obj.open(this.SetMethod, url, true);
				
				if (this.SetMethod == "POST") {
					glb_obj.setRequestHeader("SetMethod", "POST " + this.SetUri + " HTTP/1.1");
				}
	
				glb_obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
				glb_obj.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
				glb_obj.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
				glb_obj.setRequestHeader("Pragma", "no-cache");

				if(this.isXML) {
					if(glb_obj.overrideMimeType)
						glb_obj.overrideMimeType('text/xml');
				}
				glb_obj.send(this.Parametros());
			} else {
				alert("Parâmetros insuficientes para execuçao!");
			}
		} else {
			alert("Erro ao inicializar Objeto!");
		}
	}
}

function executarAJAX(method, objFieldName, objValue, URI, msg, objMsg, functionReturnName) {
	ajax 				= new Init_Ajax();
	ajax.SetMethod 		= method;
	ajax.objFieldName 	= objFieldName;
	ajax.objValue     	= objValue;
	ajax.SetUri 		= URI;
	ajax.msg			= msg;
	ajax.msgobj			= objMsg;
	ajax.Execute(functionReturnName);
	delete ajax;			
}
