var StrikePHP = function() {

	var IE = navigator.userAgent.indexOf('MSIE')>-1;

	function PHPAccess(url) {
		this.url = url;
		this.pending = new Array();
	}
	function PHPCall(access,afterFunction,phpFunction,obj) {
		this.access = access;
		this.access.pending.push(this);
		this.afterFunction = afterFunction;
		this.phpFunction = phpFunction;
		this.obj = obj;
		_callGateway(this);
	}
	PHPCall.prototype.afterFunction = function(){}
	PHPCall.prototype.destroy = function() {
		this.afterFunction = null;
		if (!IE) {
			this.access.pending.splice(this.access.pending.indexOf(this),1);
		}
		this.access = null;
		this.obj = null;
	}
	
	function PHPResult(result) {
		this.result = result;
	}
	
	function _callGateway(phpcall) {
		var req;
		req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest && !(window.ActiveXObject)) {
			try {
				req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					req = false;
				}
			}
		}
		if(req) {
			req.onreadystatechange = function () {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {
						// ...processing statements go here...
						var res = JSON.parse(req.responseText);
						if (res.error) {
							alert(res.error);
						} else {
							phpcall.afterFunction(new PHPResult(JSON.parse(req.responseText)));
							phpcall.destroy();
						}
					} else {
						alert("There was a problem retrieving the XML data:\n" +
							req.statusText);
					}
				}
			}
			var vdat = 'v='+encodeURIComponent(JSON.stringify({fun:phpcall.phpFunction,dat:phpcall.obj}));
			req.open("POST", phpcall.access.url, true);
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", vdat.length);
			req.setRequestHeader("Connection", "close");
			req.send(vdat);
		}
	}
	return {
			PHPAccess:PHPAccess, 
			PHPCall:PHPCall, 
			PHPResult:PHPResult
			};
}()
