var JSBroadcaster = {};

function XMLRequest(){
	this.Url = arguments[0];
	this.Method = arguments[1];
	this.Async = arguments[2];
	this.ReturnType = arguments[3];
	this.Data = null;
	this.HttpReq = new HttpRequest();
	this.Broadcaster = {};
	JSBroadcaster.initialize(this.Broadcaster);
	this.Broadcaster.addListener(this);

	XMLRequest.prototype.Execute = function(){
		if(this.Data != null){
			this.Data = this.Data.join("&");
		}
		StartRequest(this);
	}
	XMLRequest.prototype.AddVariable = function(name, value){
		if(this.Data == null){
			this.Data = new Array();
		}
		this.Data[this.Data.length] = name + "=" + value;
	}
}

function CheckStatus(XMLReq){
	if(XMLReq.HttpReq.readyState == 4){
		XMLReq.Broadcaster.broadcastMessage("onComplete", XMLReq.HttpReq);
	}
}

function StartRequest(XMLReq){
	XMLReq.Broadcaster.broadcastMessage("onRequest");
	XMLReq.HttpReq.onreadystatechange = function(){
		CheckStatus(XMLReq);
	}
	XMLReq.HttpReq.open(XMLReq.Method, XMLReq.Url, XMLReq.Async);
	XMLReq.HttpReq.send(XMLReq.Data);
}

function HttpRequest(){
	this.Instance = false;
	if(window.XMLHttpRequest){
		this.Instance = new XMLHttpRequest();
		if(this.Instance.overrideMimeType){
			this.Instance.overrideMimeType('text/xml');
		}
	}else if(window.ActiveXObject){
		try
		{
			this.Instance = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try
			{
				this.Instance = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(ex){
			}
		}
	}
	return this.Instance;
}

JSBroadcaster.initialize = function(obj){
	obj._listeners = new Array();

	obj.addListener = function(obj){
		for (var i=0;i<this._listeners.length; i++){
			if(this._listeners[i] == obj){
				return false;
			}
		}
		this._listeners.push( obj );
		return true;
	}

	obj.removeListener = function(obj){
		for (var i=0;i<this._listeners.length; i++){
			if ( this._listeners[i] == obj){
				this._listeners.splice(i, 1);
				return true;
			}
		}
		return false;
	}

	obj.broadcastMessage = function(){
		for(var i=0; i<this._listeners.length; i++){
			if(typeof this._listeners[i][arguments[0]] == "function"){
				this._listeners[i][arguments[0]](arguments[1]);
			}
		}
	}
}