if (typeof aay == "undefined") {
	aay = {};
}

if (typeof aay.ecc == "undefined") {
	aay.ecc = {};
};

if (typeof aay.ecc.ajax == "undefined") {
	aay.ecc.ajax = {};
};

aay.ecc.ajax.AjaxEngine = function() {
	this.debug = false;
	this.currentRequest = null;
	this.requesting = false;
	this.requestQueue = new Array();
	this.xmlHttpReq = null;
};


aay.ecc.ajax.AjaxEngine.prototype = {
	sendRequest:function(request, override) {
		if (this.requesting && override != true) {
			this.addToQueue(request);
			return;
		}

		if (!request.validate()) {
			throw new Error("Request does not contain URL.");
		}
		
		this.isRequesting(true);
		this.setCurrentRequest(request);
		
		this.xmlHttpReq = aay.ecc.xml.XmlHttpRequestObject();
		this.xmlHttpReq.onreadystatechange = this.httpResponse;
		this.xmlHttpReq.open(this.currentRequest.method, this.currentRequest.url, true);
		this.xmlHttpReq.setRequestHeader("Content-Type", "text/xml");
		this.xmlHttpReq.send(this.currentRequest.postData);
		if (this.currentRequest.debug) {
			alert("XML Request sent\n\nURL: " + this.currentRequest.url + "\nPost data:\n" + this.currentRequest.postdata);
		}
	},

	httpResponse:function() {
		if (aay.ecc.ajax.Engine.xmlHttpReq == null) return;
		
		if (aay.ecc.ajax.Engine.xmlHttpReq.readyState == 4) {
			if (aay.ecc.ajax.Engine.currentRequest.debug) {
				alert("Response Status: " + aay.ecc.ajax.Engine.xmlHttpReq.status + "\n\nXML Response:\n" + aay.ecc.ajax.Engine.xmlHttpReq.responseText);
			}
			
			if (aay.ecc.ajax.Engine.currentRequest.returnFunction != null) {
				var responseReturn = "";
				if (aay.ecc.ajax.Engine.xmlHttpReq.status == 200) {
					responseReturn = (aay.ecc.ajax.Engine.currentRequest.returnXML) ? aay.ecc.ajax.Engine.xmlHttpReq.responseXML : aay.ecc.ajax.Engine.xmlHttpReq.responseText;
				}
				aay.ecc.ajax.Engine.currentRequest.returnFunction(responseReturn, aay.ecc.ajax.Engine.currentRequest);
			}

			aay.ecc.ajax.Engine.xmlResponseFinish();
		}
	},

	xmlResponseFinish:function xmlResponseFinish() {
		this.setCurrentRequest(null);
		if (this.requestQueue.length > 0) {
			this.sendRequest(this.requestQueue.shift(), true);
		}
		if (this.requestQueue.length == 0) this.isRequesting(false);
	},
	
	isRequesting:function(bool) {
		this.requesting = bool;
	},

	setCurrentRequest:function(request) {
		this.currentRequest = request;
	},

	addToQueue:function(request) {
		if (request.debug) alert("Adding request to queue");
		this.requestQueue.push(request);
	}
};



aay.ecc.ajax.Request = function() {
	if (typeof aay.ecc.ajax.Engine == "undefined") {
		aay.ecc.ajax.Engine = new aay.ecc.ajax.AjaxEngine();
	}

	this.url = null;
	this.returnFunction = null;
	this.postData = true;
	this.method = "GET";
	this.returnXML = true;
	this.debug = false;
};

aay.ecc.ajax.Request.prototype = {
	setUrl:function(url) {
		this.url = url;
	},
	setReturnFunction:function(returnFunction) {
		this.returnFunction = returnFunction;
	},
	setReturnXML:function(bool) {
		this.returnXML = bool;
	},
	validate:function() {
		return !(this.url == null);
	},
	send:function(postData) {
		if (postData != undefined) {
			this.postData = postData;
			this.method = "POST";
		}
		aay.ecc.ajax.Engine.sendRequest(this);
	},
	setDebug:function(bool) {
		this.debug = bool;
	}

};