/**
 * function request
 * Copyright (C) 2006  Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *   Herltestraße 12
 *   D-01307, Germany
 *
 * @version  1.2
 */

/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
function XMLHttpRequest() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/

function request (url, method, data, callback) {
	var http = new XMLHttpRequest;
	if (!http)
		return false;
	if (data !== null && typeof data == 'object') {
		var _data = [];
		for (var i in data)
			_data.push (i + '=' + escape (data[i]));
		data = _data.join ('&');
		delete _data;
	}
	method = method.toUpperCase();
	if (method == 'POST') {
		http.open (method, url, true);
		http.setRequestHeader ('Method', 'POST '+url+' HTTP/1.1');
		http.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
	} else {
		if (data)
			url += '?' + data;
		data = '';
		http.open (method, url, true);
	}
	if (callback)
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				http.onreadystatechange = function(){};
				callback (http);
			}
		};
	http.send (data);
	return http;
}