/*	Cambler © 2009, http://cambler.pl/	*/

function HttpClient() { }
HttpClient.prototype = {
	requestType:'GET',
	isAsync:true,
	pcs:'pcs',
	xmlhttp:false,
	callback:false,
	
	onSend:function() {
		if(this.pcs) {
			o = document.getElementById(this.pcs);
			o.style.display = 'block';
			o.style.visibility  = 'visible';
		}
	},
	
	onLoad:function() {
		if(this.pcs) document.getElementById(this.pcs).style.display = 'none';
	},
	
	onError:function(error) {
		alert(error.message);
	},

	init:function() {
		try {
		    this.xmlhttp = new XMLHttpRequest();
		} catch (e) {
			var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP' );
			var success = false;
			for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
				try {
					this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
					success = true;
				} catch (e) {}
			}
			if (!success) {
				throw new Error('Nie moÅ¼na utworzyÄ‡ obiektu XMLHttpRequest.');
			}
		}
	},

	makeRequest: function(url,payload,contentType) {
		if (!this.xmlhttp) {
			this.init();
		}
		this.onSend();
		this.xmlhttp.open(this.requestType,url,this.isAsync);
		if (contentType) {
			this.xmlhttp.setRequestHeader('Content-Type',contentType);
		}
		var self = this;
		this.xmlhttp.onreadystatechange = function() { self._readyStateChangeCallback(); }

		this.xmlhttp.send(payload);
		if (!this.isAsync) {
			return this.xmlhttp.responseText;
		}
	},

	_readyStateChangeCallback:function() {
		switch(this.xmlhttp.readyState) {
			case 4:
				this.onLoad();
				if (this.xmlhttp.status == 200) {
					this.callback(this.xmlhttp.responseText);
				}
				else {
					//this.onError(new Error('BÅ‚Ä…d HTTP w czasie zgÅ‚aszania Å¼Ä…dania: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText));
					msg('BÅ‚Ä…d HTTP w czasie zgÅ‚aszania Å¼Ä…dania: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText, 1);
				}
			break;
		}
	}
}


