/**  XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08    **
 **    modified by Stancu Florin (nifostancu@gmail.com) - 2007-10-25      **
 **  Code licensed under Creative Commons Attribution-ShareAlike License  **
 **  http://creativecommons.org/licenses/by-sa/2.0/                       **/
function XHConn() {
	var xmlhttp, bComplete = false;
	this.init = function() {
		var tmpxmlhttp;
		try { tmpxmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { tmpxmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { try { tmpxmlhttp = new XMLHttpRequest(); }
		catch (e) { tmpxmlhttp = false; }}}
		return tmpxmlhttp;
	}
	xmlhttp=this.init();
	if (!xmlhttp) return null;
	this.connect = function(sURL, sMethod, sVars, fnDone) {
		if (!xmlhttp) {xmlhttp=this.init();}
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		try {
			if (sMethod == "GET") {
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			} else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function() {if ((xmlhttp.readyState==4||xmlhttp.readyState=='complete') && !bComplete) {
				bComplete = true;
				fnDone(xmlhttp);
			}};
			xmlhttp.send(sVars);
		}
		catch(z) {return false;}
		return true;
	};
	return this;
}

/**  AJAX IFRAME METHOD (AIM)     **
 **  http://www.webtoolkit.info/  **/
AIM = {
	frame: function(c) {
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
		return n;
	},
	form: function(f, name) {
		f.setAttribute('target', name);
	},
	submit: function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
	loaded: function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) { var d = i.contentDocument; }
		else if (i.contentWindow) { var d = i.contentWindow.document; }
		else { var d = window.frames[id].document; }
		if (d.location.href == "about:blank") { return; }
		if (typeof(i.onComplete) == 'function') { i.onComplete(d.body.innerHTML); }
	}
}