/**
* Třída Socket
* Slouží pro vytvoření ajaxového requestu
*/
function Socket(){
	// jména atributů která se budou předávat cílovému skriptu
	this.attribNames = Array('ip', 'port', 'data');
	// adresa cílového skriptu
	this.url = "/apps/sock.php";
	
	// Vytvoříme instanci XMLXttpRequestu 
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		this.request = new XMLHttpRequest();
		if (this.request.overrideMimeType) {
			this.request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			this.request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!this.request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	this.timeout = function (ms){
		var _self = this;
		this.timeoutId = setTimeout(function(ms){
			_self.request.abort();
		}, ms);
	};

	this.makeRequest = function (){
		if(!this.request) return false;
		if(this.showData){
			result = document.getElementById(this.showDataElementId);
			result.className = 'wait';
			result.innerHTML = '<img src="/img/wait.gif" /><br />Počkejte prosím.';
		}
		this.timeout(10000);
		url = this.url;
		params = "";
		for(var i = 0; i < this.attribs.length; i++){
			params += this.attribNames[i] + "=" + encodeURI(this.attribs[i]);
			params += "&";
		}
		var _self = this;
		this.request.onreadystatechange = function () { _self.alertContents(); };
		this.request.open('POST', url, true);
		this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.request.setRequestHeader("Content-length", params.length);
		this.request.setRequestHeader("Connection", "close");
		this.request.send(params);
		return true;
	};
	
	this.alertContents = function() {
		try{
			if (this.request.readyState == 4) {
				if (this.request.status == 200) {
					clearTimeout(this.timeoutId);
					var xmlDoc = this.request.responseXML;
					var tree = xmlDoc.documentElement;
					var succ = tree.getElementsByTagName("success")[0];
					var result = tree.getElementsByTagName("result")[0];
					this.success = ((succ.firstChild.data === "false") ? false : true);
					this.data = document.createTextNode(decodeURIComponent(result.firstChild.data));
					if(this.showData){
						target = document.getElementById(this.showDataElementId);
						target.className = '';
						target.innerHTML = "";
						if(this.success){
							target.appendChild(this.data);
						} else {
							target.innerHTML = "Nepodařilo se připojit.";
						}
					}
					if(this.success){
						var img = document.getElementById(this.targetImageId);
						img.setAttribute('src', '/img/online.gif');
						img.setAttribute('alt', 'online');
						img.setAttribute('title', 'online');
					} else {
						var img = document.getElementById(this.targetImageId);
						img.setAttribute('src', '/img/offline.gif');
						img.setAttribute('alt', 'offline');
						img.setAttribute('title', 'offline');
					}
				} else {
					if(this.showData){
						target = document.getElementById(this.showDataElementId);
						target.className = '';
						target.innerHTML = "Došlo k chybě.";
					}
				}
			}
		} catch (e) {
			if(this.showData){
				target = document.getElementById(this.showDataElementId);
				target.className = '';
				target.innerHTML = "Došlo k timeoutu.<br />" + e.name + "<br />" + e.message;
			}
		}
	};
	
	this.stopRequest = function () { this.request.abort();};
	this.setImageId = function (id){this.targetImageId = id;};
	this.setShowData = function (show){this.showData = show;};
	this.setShowDataElementId = function (id){this.showDataElementId = id;};
	this.setAttribs = function (att){this.attribs = att;};
	this.setAttribNames = function (names){this.attribNames = names;};
	this.setUrl = function (url) {this.url = url;};
}
