function MyAjax() {
	var xhr;
	try {
		xhr = new XMLHttpRequest();
	}

	catch (e) {
		var MSXmlVerze = new Array('MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.5.0',
				'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.3.0',
				'MSXML2.XMLHttp.2.0', 'Microsoft.XMLHttp');
		for ( var i = 0; i <= MSXmlVerze.length; i++) {
			try {
				xhr = new ActiveXObject(MSXmlVerze[i]);
				break;
			} catch (e) {
				//vzniklou chybu ignoruji a test dalsi verze  
			}
		}
	}
	if (!xhr)
		alert("Chyba vytvoreni objektu XMLHttpRequest!");

	this.xhr = xhr;
}

MyAjax.prototype.GetXmlResponse = function() {
	if (this.xhr.readyState == 4) { //funkci provedeme, pokud je požadavek ve stavu dokončeno  
		if (this.xhr.status == 200) { //pokračuji, jen když je vše OK  
			try {
				var XMLRes = this.xhr.responseXML;

				// zachycení chyb IE a Opery
				if (!XMLRes || !XMLRes.documentElement) {
					throw ("Chybná struktura XML OP:\n" + this.xhr.responseText);
				}
				//zachycení chyb ohnivé lišky :-)  
				var rootNodeName = XMLRes.documentElement.nodeName;
				if (rootNodeName == "parsereerror") {
					throw ("Chybná struktura XML FF:\n" + this.xhr.responseText);
				}

				return XMLRes.documentElement;

			} catch (e) {
				alert("Chyba při čtení odpovědi:" + e.toString());
			}
		}
	}
	return '';
}

MyAjax.prototype.SendRequestFunc = function(url, func) {
	this.xhr.open("POST", url);
	this.xhr.onreadystatechange = func; // dojde-li ke změně, volej funkci
	// volanaFunkce
	this.xhr.send(null);
}

MyAjax.prototype.ReadyStateChange = function() {
	if (this.xhr.readyState == 1) { //zobrazení točidla
		this.toElement.innerHTML = '<img src="/styly/wwww/prikazy/circle.gif" alt="Nahravám..." />';
	}
	if (this.xhr.readyState == 4) { //funkci provedeme, pokud je požadavek ve stavu dokončeno  
		if (this.xhr.status == 200) { //pokračuji, jen když je vše OK  
			try {
				if (this.xhr.responseText) {
					this.toElement.innerHTML = this.xhr.responseText;
				}

			} catch (e) {
				alert("Chyba při čtení odpovědi:" + e.toString());
			}
		}
	}
}

MyAjax.prototype.SendRequestToElement = function(url, element) {
	if (typeof(element)=="string") {
		this.toElement = document.getElementById(element);
	} else {
		this.toElement = element;
	}
	this.xhr.open("GET", url);
	this.xhr.onreadystatechange = function(o) {
		return function() {
			o.ReadyStateChange();
		}
	}(this); // dojde-li ke změně, volej funkci
	this.xhr.send(null);

}

MyAjax.prototype.SendRequestToElementPozice = function(url, elementId,
		poziceElementId, krok) {
	var element = document.getElementById(elementId);
	var pozice = document.getElementById(poziceElementId);
	var p = parseInt(pozice.innerHTML);
	if (krok != 0) {
		p = p + parseInt(krok);
		if (p < 0)
			p = 0;
	} else {
		p = 0;
	}
	pozice.innerHTML = p;
	this.SendRequestToElement(url + '&pozice=' + p, element);
}

MyAjax.prototype.SendPostRequestToElement = function(url, params, element) {
	this.toElement = element;
	this.xhr.open("POST", url);

	this.xhr.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	this.xhr.setRequestHeader("Content-length", params.length);
	this.xhr.setRequestHeader("Connection", "close");

	this.xhr.onreadystatechange = function(o) {
		return function() {
			o.ReadyStateChange();
		}
	}(this); // dojde-li ke změně, volej funkci
	this.xhr.send(params);

}

MyAjax.prototype.readyState = function() {
	return this.xhr.readyState
}

MyAjax.prototype.status = function() {
	return this.xhr.status
}

MyAjax.prototype.responseXML = function() {
	return this.xhr.responseXML
}

function toUtf(string) {
	string = string.replace(/\r\n/g, "\n");
	var utftext = "";

	for ( var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if ((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

function fromUtf(utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while (i < utftext.length) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		} else if ((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i + 1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = utftext.charCodeAt(i + 1);
			c3 = utftext.charCodeAt(i + 2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6)
					| (c3 & 63));
			i += 3;
		}

	}

	return string;
}
