/*
XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08
Code licensed under Creative Commons Attribution-ShareAlike License
http://creativecommons.org/licenses/by-sa/2.0/

AjaxConn - Aleksandar Vacic, www.aplus.co.yu
ver 1.2 - Aug 15 2005
*/

function AjaxConn() {
	var self = this;

	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { xmlhttp = false; }}}
	if (!xmlhttp) return null;

	//	is connection free to use?
	this.bIsFree = true;
	
	//	true = async request, false = sync request
	this.bIsAsync = true;
	
	//	result returned from server
	this.responseText = "";
	this.status = "";

	//	set that connection is free to be used again. save responseText
	function _Done() {
		self.bIsFree = true;
		self.responseText = xmlhttp.responseText;
		self.status = xmlhttp.status;
		self.Done();
	}
	//	allows executing specific actions after operation has completed
	this.Done = function() {};

	this.connect = function(sMethod, sURL, sVars) {
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		
		this.bIsFree = false;
		this.responseText = "";
		this.status = "";

		//	Hack for stupid IE and its caching or URLs and responses
		if (sVars && sVars != "")
			sVars += "&";
		sVars += "h4sie=" + (new Date()).getTime();

		try {
			if (sMethod == "GET") {
				sURL += "?" + sVars;
				xmlhttp.open(sMethod, sURL, this.bIsAsync);
			} else {
				xmlhttp.open(sMethod, sURL, this.bIsAsync);
				xmlhttp.setRequestHeader("Method", "POST " + sURL + " HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && !bComplete) {
					bComplete = true;
					_Done();
				}
			};
			xmlhttp.send(sVars);
		} catch(z) { return false; }

		return true;
	};

	return self;
}



