function AjaxGetPageClass()
{
	// constants
	this.divWindowTopMargin = 50;
	this.defaultWindowWidth = 900;
	this.defaultWindowHeight = 600;
	
	
	// variables
	this.div = null;
	this.bgDiv = null;
	this.contentDiv = null;
	this.currentWindowWidth = this.currentWindowWidth;
	this.currentWindowHeight = this.defaultWindowHeight;
	
	
	// functions
	
	this.getWindowWidth = function()
	{
		return document.body.clientWidth;
	}

	this.getWindowHeight = function()
	{
		return document.body.clientHeight;
	}

	this.getWindowScrollX = function()
	{
		if (document.body.scrollLeft == 0)
		{
			if (document.documentElement.scrollLeft && document.documentElement.scrollLeft > 0)
				return document.documentElement.scrollLeft;
		}
		return document.body.scrollLeft;
	}

	this.getWindowScrollY = function()
	{
		if (document.body.scrollTop == 0)
		{
			if (document.documentElement.scrollTop && document.documentElement.scrollTop > 0)
				return document.documentElement.scrollTop;
		}
		return document.body.scrollTop;
	}
	
	this.getDocumentWidth = function()
	{
		alert(window.innerWidth + window.scrollMaxX);
		alert(document.body.scrollWidth);
		alert(document.body.offsetWidth);
		if (window.innerHeight && window.scrollMaxX) // Firefox
			return window.innerWidth + window.scrollMaxX;
		else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
			return document.body.scrollWidth;
		else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			return document.body.offsetWidth;
	}
	
	this.getDocumentHeight = function()
	{
		if (window.innerHeight && window.scrollMaxY) // Firefox
			return window.innerHeight + window.scrollMaxY;
		else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
			return document.body.scrollHeight;
		else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			return document.body.offsetHeight;
	}
	
	this.isDivOpen = function()
	{
		return (this.div.style.visibility == 'visible');
	}
	
	this.createDivs = function()
	{
		var bgDiv = document.createElement('div');
		bgDiv.className = 'ajaxOpenWindowBackground';
		
		var resultDiv = document.createElement('div');
		resultDiv.className = 'ajaxOpenWindow';
		resultDiv.setAttribute('class', 'ajaxOpenWindow');
		
		var closeDiv = document.createElement('div');
		closeDiv.className = 'ajaxClosePanel';
		closeDiv.setAttribute('class', 'ajaxClosePanel');
		if (closeDiv.attachEvent)
		{
			closeDiv.attachEvent('onclick', function() { ajaxGetPage.close(); });
		}
		else
		{
			closeDiv.setAttribute('onclick', 'ajaxGetPage.close();');
		}
		resultDiv.appendChild(closeDiv);
		
		var closeAnchor = document.createElement('a');
		closeAnchor.innerHTML = 'Zatvoriť';
		closeAnchor.href = "javascript: void(0);";
		if (closeAnchor.attachEvent)
		{
			closeAnchor.attachEvent('onclick', function () { ajaxGetPage.close(); });
		}
		else
		{
			closeAnchor.setAttribute('onclick', 'ajaxGetPage.close();');
		}
		closeDiv.appendChild(closeAnchor);
		
		var contentDiv = document.createElement('div');
		resultDiv.appendChild(contentDiv);
		document.body.appendChild(bgDiv);
		document.body.appendChild(resultDiv);
		
		this.bgDiv = bgDiv;
		this.div = resultDiv;
		this.contentDiv = contentDiv;
	}
	
	this.setDivLoadingHtml = function()
	{
		this.contentDiv.innerHTML =
			'<div class="ajaxLoading"></div>'
	}
	
	this.setDivPosition = function()
	{
		this.div.style.top = (this.divWindowTopMargin + this.getWindowScrollY()) + 'px';
		this.div.style.left = (this.getWindowScrollX() + (this.getWindowWidth() - this.div.offsetWidth) / 2) + 'px';
	}
	
	this.setBgDivPosition = function()
	{
		this.bgDiv.style.top = this.getWindowScrollY() + 'px';
		this.bgDiv.style.left = this.getWindowScrollX() + 'px';
	}
	
	this.showDiv = function()
	{
		this.setDivPosition();
		this.setBgDivPosition();
		this.div.style.visibility = 'visible';
		this.bgDiv.style.visibility = 'visible';
	}
	
	this.hideDiv = function()
	{
		this.div.style.visibility = 'hidden';
		this.bgDiv.style.visibility = 'hidden';
	}
	
	this.openPageGetAsyncResult = function(request)
	{
		if (request.readyState == 4 && this.isDivOpen())
		{
			this.contentDiv.innerHTML = request.responseText;
			this.showDiv();
		}
	}
	
	this.getXMLHttpRequestObject = function()
	{
		if (typeof XMLHttpRequest == "undefined")
		{
			try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
			try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
			return null;
		}
		else
		{
			return new XMLHttpRequest();
		}
	}
	
	this.sendRequest = function(url)
	{
		var request = this.getXMLHttpRequestObject();
		//request.overrideMimeType('text/html; charset=windows-1250');
		request.open("GET", url, true);
		request.setRequestHeader("Content-Type", "application/x-javascript;");
		
		var obj = this;
		request.onreadystatechange = function()
		{
			obj.openPageGetAsyncResult(request);
		}
		request.send('');
	}
	
	this.translateUrlAgainstCaching = function(url)
	{
		var rndNumber = Math.round(Math.random() * 10000);
		if (url.indexOf('?') >= 0)
			return url + '&anticache=' + rndNumber;
		else
			return url + '?anticache=' + rndNumber;
	}
	
	this.setWindowSize = function(width, height)
	{
		if (!width || !height)
		{
			this.div.style.width = this.defaultWindowWidth + 'px';
			this.div.style.height = this.defaultWindowHeight + 'px';
			this.currentWindowWidth = this.defaultWindowWidth;
			this.currentWindowHeight = this.defaultWindowHeight;
		}
		else
		{
			this.div.style.width = width + 'px';
			this.div.style.height = height + 'px';
			this.currentWindowWidth = width;
			this.currentWindowHeight = height;
		}
	}
	
	this.openPage = function(url, width, height)
	{
		this.setWindowSize(width, height);
		this.setDivLoadingHtml();
		this.showDiv();
		this.sendRequest(this.translateUrlAgainstCaching(url));
	}
	
	this.toQueryComponent = function(input)
	{
		if (!input.name || input.disabled)
			return "";

		var n = this.urlencode(input.name);

		switch (input.type)
		{
			case "text":
			case "password":
			case "submit":
			case "hidden":
				return n + "=" + this.urlencode(input.value);
			case "textarea":
				// normalize line breaks as CR LF pairs as per RFC 1866
				var v = input.value.split(/\r\n|\r|\n/).join("\r\n");
				return n + "=" + this.urlencode(v);
			case "checkbox":
			case "radio":
				if (!input.checked)
					return "";
				var v = this.getRealValue(input);
				if (v === null) v = "on";
				return n + "=" + this.urlencode(v);
			case "select-one":
			case "select-multiple":
				var nvp = [];
				var opt, i = 0;
				while ((opt = input.options[i++]) != null)
				{
					if (opt.selected)
					{
						var v = getRealValue(opt);
						if (v === null) v = opt.text;
						nvp[nvp.length] = n + "=" + urlencode(v);
					}
				}
				return nvp.join("&");
			default:
				return "";
		}
	}

	this.urlencode = function(str)
	{
		var v;
		try { v = encodeURIComponent(str); } catch (e) { v = escape(str); }
		return v.replace(/%20/g,"+");
	}

	this.getRealValue = function(input)
	{
		var attr = input.getAttributeNode("value");
		return (attr && attr.specified) ? input.getAttribute("value") : null;
	}
	
	this.getFormQueryString = function(frm)
	{
		var qs = '';
		for (var i = 0; i < frm.elements.length; i++)
		{
			var val = this.toQueryComponent(frm.elements[i]);
			if (val != '')
			{
				if (qs != '')
					qs += "&" + val;
				else
					qs += val;
			}
		}
		
		if (qs != '')
			qs = '?' + qs;

		return qs;
	}
	
	this.submitForm = function(frm)
	{
		if (frm)
		{
			var url = frm.action + this.getFormQueryString(frm);
			this.openPage(url, this.currentWindowWidth, this.currentWindowHeight);
		}
		this.setDivLoadingHtml();
		this.showDiv();
		return false;
	}
	
	this.close = function()
	{
		this.hideDiv();
	}
	
	this.onWindowChange = function()
	{
		this.setDivPosition();
		this.setBgDivPosition();
	}
	
	this.attachEvents = function()
	{
		if (window.attachEvent)
		{
			window.attachEvent('onresize', function() { ajaxGetPage.onWindowChange(); });
			window.attachEvent('onscroll', function() { ajaxGetPage.onWindowChange(); });
		}
		if (document.attachEvent)
		{
			document.attachEvent('onresize', function() { ajaxGetPage.onWindowChange(); });
			document.attachEvent('onscroll', function() { ajaxGetPage.onWindowChange(); });
		}
		if (document.body.attachEvent)
		{
			document.body.attachEvent('onresize', function() { ajaxGetPage.onWindowChange(); });
			document.body.attachEvent('onscroll', function() { ajaxGetPage.onWindowChange(); });
		}
		else
		{
			document.body.setAttribute('onresize', 'ajaxGetPage.onWindowChange();');
			document.body.setAttribute('onscroll', 'ajaxGetPage.onWindowChange();');
		}
	}
	
	this.createDivs();
	this.attachEvents();
}

var ajaxGetPage;

function ajaxGetPageAttach()
{
	if (document && document.body && document.body.appendChild)
	{
		ajaxGetPage = new AjaxGetPageClass();
	}
	else
	{
		setTimeout("ajaxGetPageAttach();", 100);
	}
}

ajaxGetPageAttach();

