Asyn = {};
Asyn.debugMode = false;
Asyn.requestType = "GET";
Asyn.simpleMode = false;
Asyn.urlEncoding = true;

Asyn.debug = function(text) {
	if (Asyn.debugMode)
		alert("Info: " + text);
}

Asyn.bF = function() {
	//Asyn.debug("Call function bF");
 	var A;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			A=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			A=null;
		}
	}
	if (!A && typeof XMLHttpRequest != "undefined")
		A=new XMLHttpRequest();
	if (!A)
		Asyn.debug("Could not create connection object.");
	return A;
}

Asyn.requestSrv = function (uri, funcReturn, args) {
	var x=null;
	var postData;
	args = (args) ? args : null;
	if (typeof args == "string" && this.requestType == "POST") {
		postData = args;
	} else {
		if (Asyn.requestType == "GET") {
			if (uri.indexOf("?") == -1 && args && typeof args == "array") {
				uri = uri + "?";
				for (a in args) {
					if (uri.indexOf("&") == -1) {
						uri = uri + "&" + a + "=" + escape(args[a]);
					} else {
						uri = uri + a + "=" + escape(args[a]);
					}
				}
			} else if (args && typeof args == "array") {
				Asyn.debug("Alert: Don't need pass array with arguments if the URI be complete with parameters.");
			}
			postData = null;
		} else {
			if (uri.indexOf("?") == -1 && args && typeof args == "array") {
				for (a in args) {
					if (postData.indexOf("&") == -1) {
						postData = postData + "&" + a + "=" + escape(args[a]);
					} else {
						postData = post_data + a + "=" + escape(args[a]);
					}
				}
			} else {
				if ((uri.indexOf("?") != -1) && (uri.indexOf("?") != uri.length-1))
					postData = uri.split("?")[1];
				else
					postData = null;
				Asyn.debug("Alert: The arguments is empty or the URI contain a query its.");
			}
		}
	}
	x=Asyn.bF();
	Asyn.debug("Parameters for open conection: METHOD - " + Asyn.requestType + " | URI - " + uri + " | POST - " + postData);
	x.open(Asyn.requestType,uri,Asyn.urlEncoding);
	if (Asyn.requestType == "POST") {
		x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
		x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	x.onreadystatechange = function() {
		if (x.readyState == 4) {
			if (x.status < 400) {
				if (Asyn.simpleMode) {
					var data;
					data = x.responseText;
					if (typeof funcReturn == "string")
						eval(funcReturn)(data);
					else
						funcReturn(data);
				} else {
					if (typeof funcReturn == "string")
						eval(funcReturn)(x);
					else
						funcReturn(x);
				}
			} else {
				Asyn.dedug("Error: No response - "+x.status+" - "+x.statusText);
			}
		}
	}
	x.send(postData);
	Asyn.debug(funcReturn + " uri = " + uri + " - post = " + postData);
	Asyn.debug(funcReturn + " waiting..");
}

Asyn.ahahCallTg = function(uri, target, dataSend) {
	var zB=null;
	var pData = null;
	var nodeTarget = null;
	if (dataSend && Asyn.requestType == "POST") {
		pData = dataSend;
	} else {
		if ((uri.indexOf("?") != -1) && (Asyn.requestType == "POST")) {
			var dv=uri.split("?");
			pData=dv[1];
		}
	}
	if (typeof target == "string" && document.getElementById(target)) {
		nodeTarget = document.getElementById(target);
	} else if (target && target.innerHTML) {
		nodeTarget = target;
	}
	if (nodeTarget) {
		if (Asyn.ahahMessage)
			nodeTarget.innerHTML = "Carregando...";
	} else {
		Asyn.debug("Error: Parameter target not exists.");
		return;
	}
	zB=Asyn.bF();
	Asyn.debug("Link para esta pagina: "+uri);
	zB.open(Asyn.requestType, uri, Asyn.urlEncoding);
	if (Asyn.requestType == "POST") {
		zB.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
		zB.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	if (zB == null) {
		Asyn.debug("Error: No object XMLHttpRequest in function ahahCallTg");
		return;
	}
	zB.onreadystatechange = function() {
		if (zB.readyState == 4) {
			if (zB.status < 400) {
				var rData;
				rData = zB.responseText;
				//Asyn.debug(zB.responseText);
				nodeTarget.innerHTML = rData;
				Asyn.executeJS(nodeTarget);
			} else {
				Asyn.debug("Error: No response - "+zB.status+" - "+zB.statusText);
			}
		}
	}
	zB.send(pData);
}

Asyn.reqGET = function(uri,funcReturn,args) {
	args = (args) ? args : null;
	Asyn.requestType = "GET";
	Asyn.requestSrv(uri,funcReturn,args);
}
Asyn.reqPOST = function(uri,funcReturn,args) {
	args = (args) ? args : null;
	Asyn.requestType = "POST";
	Asyn.requestSrv(uri,funcReturn,args);
}
Asyn.reqTgGET = function(uri,target,dataSend) {
	dataSend = (dataSend) ? dataSend : null;	
	Asyn.requestType = "GET";
	Asyn.ahahCallTg(uri,target,dataSend);
}
Asyn.reqTgPOST = function(uri,target,dataSend) {
	dataSend = (dataSend) ? dataSend : null;	
	Asyn.requestType = "POST";
	Asyn.ahahCallTg(uri,target,dataSend);
}

Asyn.executeJS = function(node) {
	var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
	var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
	var bMoz = (navigator.appName == 'Netscape');
	var st = node.getElementsByTagName('SCRIPT');
	var strExec;
	for (var i = 0;i < st.length; i++) {
		if (bSaf) {
			strExec = st[i].innerHTML
		} else if (bOpera) {
			strExec = st[i].text
		} else if (bMoz) {
			strExec = st[i].textContent
		} else {
			strExec = st[i].text
		}
		try {
			eval(strExec);
		} catch(e) {
			alert(e);
		}
	}
}