/*
 THIS CONTENT IS DERIVED FROM NETTE FRAMEWORK
	
 * Nette Framework
 *
 * Copyright (c) 2004, 2008 David Grudl (http://davidgrudl.com)
 *
 * This source file is subject to the "Nette license" that is bundled
 * with this package in the file license.txt.
 *
 * For more information please see http://nettephp.com/

*/

var najax = {

	ajax : null, 

	waiting : false,

	initAjax: function()
	{
		this.ajax = false;

		if (typeof XMLHttpRequest != 'undefined') {
			this.ajax = new XMLHttpRequest();
		}

		if (!this.ajax && window.ActiveXObject) {
			try {
				this.ajax = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					this.ajax = false;
				}
			}
		}
	},
	
	action: function(action, data)
	{
		if (this.waiting) return false;
	
		// create new AJAX request
		this.initAjax();
		if (!this.ajax) return false;

		try {
			var url = action;

			url += (action.indexOf('?') == -1) ? '?' : '&';
			url += 'ajaxrequest=yes';

			this.ajax.open("GET", url, true);
			this.ajax.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			//this.ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.ajax.onreadystatechange = function() { najax.ajaxHandler(); }
			this.ajax.send(data);
			
			this.waiting = true;
			
			return true;

		} catch (e) {
			return false;
		}
	},
	
	ajaxHandler: function()
	{
		if (this.ajax.readyState == 4) {
			// remove process indicator
			this.waiting = false;
			
			if (this.ajax.status == 200) {
				eval(this.ajax.responseText);
			} else  {
				alert("Chyba nacitani (" + this.ajax.status + ": " + this.ajax.statusText + ")");
			}

		}
	}
	
}

