// HttpRequest library by Juerg Lehni, http://www.scratchdisk.com/
//
// Inspired by:
//
//   XHConn - Simple XMLHTTP Interface by bfults@gmail.com
//   HTMLHttpRequest v1.0 beta2, (c) Angus Turnbull, TwinHelix Designs http://www.twinhelix.com
//   PORK.iframe by Jelle Ursem, http://beta.zapguide.nl/iframe/

var ua = navigator.userAgent, av = navigator.appVersion;
var is = {
	win: /Win/.test(ua),
	mac: /Mac/.test(ua),
	ie: !window.opera && /MSIE/.test(ua),
	ie5: !window.opera && /MSIE 5.0/.test(av)
};

var _requests = {};
var _requestId = 0;

function HttpRequest() {
 	this.id = 'request_' + _requestId++;
	_requests[this.id] = this;
	this.body = document.body ? document.body : document.getElementsByTagName('body').item(0);
	var d = document.createElement('div');
    d.style.position = "absolute";
	d.style.top = "0";
	d.style.marginLeft = "-10000px";
	this.body.appendChild(d);
	if (is.ie) {
		d.innerHTML = '<iframe name="' + this.id + '" id="' + this.id + '" src="about:blank" onload="_requests[\'' + this.id + '\'].onLoad()"></iframe>';
	} else {
		var f = document.createElement("iframe");
		f.setAttribute("name", this.id);
		f.setAttribute("id", this.id);
		var obj = this;
		f.addEventListener("load", function() { obj.onLoad(); }, false);
		d.appendChild(f);
	}
	this.frame = window.frames[this.id] || document.getElementById(this.id);
	this.frameDiv = d;
}

HttpRequest.prototype.send = function(url, method, params, onComplete) {
	method = method ? method.toUpperCase() : "GET";
	var form;
	params = params || {};
	if (params.elements) { // a form
		form = params;
	} else {
		var pos = url.indexOf('?');
		if (pos > 0) {
			// convert query strings back to param lists, as the values would be ignored on some browsers otherwise
			var ps = url.substring(pos + 1).split('&');
			for (var i in ps) {
				var p = ps[i].split('=');
				params[p[0]] = unescape(p[1]);
			}
			url = url.substring(0, pos);
		}
		// create form entries for each value
		var s = '';
		for (var i in params)
			s += '<input type="hidden" name="' + i + '" value="' + params[i] + '">';
		var d = document.createElement('div');
		d.style.visibility = 'hidden';
		d.innerHTML = '<form>' + (s || '<input type="hidden">') + '</form>'; // min. one empty input for MACIE
		form = d.firstChild;
		this.body.appendChild(d);
		this.formDiv = d;
	}
 	// Opera fix: force the frame to render
	this.frameDiv.offsetWidth;
	form.target = this.id;
	form.action = url;
	form.method = method;
	if (is.ie5) {
		if (is.mac)
			form.enctype = method == "GET" ? "application/x-www-form-urlencoded" : "multipart/form-data";
		this.checkRef = "_requests['" + this.id + "'].checkFrame()";
		setTimeout(this.checkRef, 50);
	}
	this.url = url;
	this.onComplete = onComplete;
	form.submit();
}

HttpRequest.prototype.onLoad = function() {
	var docs = [
		this.frame.contentDocument, // NS6
		this.frame.contentWindow, 	// IE5.5 and IE6
		this.frame					// IE5
	];
	var text;
	for (var i in docs) {
		try {
			var d = docs[i].document;
			text = d.body.innerHTML;
			d.close();
			break;
		} catch(e) {}
	}
	if (text == null) {
		try {
			text = window.frames[this.id].document.body.innerText;
		} catch(e) {}
	}
	if (text != null) {
		// first tag in IE ends up in <head>, safe it
		this.onComplete(text.replace(/<TITLE><\/TITLE>/gi, "").replace(/^(<HEAD>([\s\S]*)<\/HEAD>\s*<BODY>|<BODY>)([\s\S]*)<\/BODY>$/gi, "$2$3"));
		if (this.formDiv) {
			this.body.removeChild(this.formDiv);
			this.formDiv = null;
		}
		var obj = this;
		setTimeout(function() { obj.body.removeChild(obj.frameDiv) }, 5000); // some browsers need this object around for a while...
	}
}

HttpRequest.prototype.checkFrame = function() {
	var l = this.frame.location;
	if ((l && l.href ? l.href.match(this.url) : 1) && this.frame.document.readyState == 'complete') {
		this.onLoad();
	} else {
		setTimeout(this.checkRef, 50);
	}
}

