
function http_request(parent, data, structure_root) {
	this.inheritFrom = collection_list_item;
	this.inheritFrom();

	this.inheritFrom = structure_class;
	this.inheritFrom();
	
	var self = this;
	
	this.parent = parent; // parent collector
	this.data = data; // user data

	this.request = null;

	this.evt_oncreate = null;
	this.evt_ondone = null;

	this.construct = function() {
		if (window.ActiveXObject) {
			this.request = context.object_create(new ActiveXObject("Microsoft.XMLHTTP"));
		} else {
			this.request = context.object_create(new XMLHttpRequest());
		}
		
		this.request.onreadystatechange = function() {
			self.state_changed();
		}
		
		if (structure_root == null) {
			this.flush();
		} else {
			this.root = structure_root;
		}
	}

	this.state_changed = function() {
		switch (this.request.readyState) {
			case 4 : // complete
				if (this.evt_ondone != null)
					this.evt_ondone(this.data, this);
				
				this.flush();
				this.parent.dequeue(this);
				break;
		}
	}
	
	this.make = function() {
		if (this.parent != null) {
			if (this.evt_oncreate != null)
				this.evt_oncreate(this.data, this);
			
			this.request.open("POST", this.parent.interface, true);
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.request.send("input=" + encodeURI(this.compose("request_data")));
		} else {
			context.error("Unable to process request, no parent collector specified!");
		}
	}
	
	this.get_response = function() {
		xml = this.request.responseXML;
		
		if (xml != null) {
			var head = xml.getElementsByTagName("head")[0];
			var body = xml.getElementsByTagName("body")[0];

			if (head != null && body != null) {
				// je chyba?
				var errors = head.getElementsByTagName("error");
				if (errors.length == 0) {
					return new Array(head, body);
					
				} else {
					var error_str = "";
					
					for (f = 0; f < errors.length; f++) {
						if (f != 0) error_str += "\r\n";
						error_str += context.element_value(errors[f]);
					}
					
					context.error(error_str);
				}
			} else {
				context.error("Document does not contain base elements! output: " + this.request.responseText);
			}
		} else {
			context.error("Invalid response document! output: " + this.request.responseText);
		}
		
		return null;
	}
	
	this.construct();
}

function http_request_collector(interface) {
	this.inheritFrom = collection_list;
	this.inheritFrom();

	this.interface = interface;
	this.first = null;
	this.end = null;
	
	this.enqueue = function(data, evt_oncreate, evt_ondone, structure_root) {
		var req = context.object_create(new http_request(this, data, structure_root));
		
		req.evt_oncreate = evt_oncreate;
		req.evt_ondone = evt_ondone;
		
		this.enqueue_ref(req);
	}
	
	this.enqueue_ref = function(req) {
		if (this.collection_add(req) != null)
			req.make();
	}
	
	this.dequeue = function(req) {
		if ((req = this.collection_rem(req)) != null) {
			context.object_destroy(req);
			req = null;
		} else {
			context.error("Unable to dequeue, nothing in request collector!");
		}
	}
}
