/*
The Bozena's JavaScript Code Library
(c) 2008 The Bozena
*/

var bJCL= {
	Version:  '1.0.0',
	Browser: {
    	IE:     !!(window.attachEvent && !window.opera),
	    Opera:  !!window.opera,
	    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
	    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
	    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
  	},
	getVersion: function() {return this.Version},
	getFormData: function(frmname) {return Serialize(frmname)}
};

Object.extend = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};

Object.extend(Object, {
	  isString: function(object) {
		return typeof object == "string";
	  }
});

function $(element) {
	//vraci ukazatel na objekt pomoci vyhledani jeho id
	if (Object.isString(element)) 
	{
		element = document.getElementById(element);
	}
  	return element;
};

function $N(element) {
	//vraci ukazatel na objekt pomoci vyhledani jeho jmena
	if (Object.isString(element)) 
	{
		element = document.getElementsByName(element)[0];
	}
  	return element;
};

function $F(element) {
	//vraci hodnotu elementu
	return $(element).value;
};

function Serialize(hForm)
{
	var theForm = $(hForm);
	var els = theForm.elements;
	var result = new Array();
	var arrcount=0;
	for(i=0; i<els.length; i++){
		if (els[i].tagName.toLowerCase()=='input' || els[i].tagName.toLowerCase()=='select'  || els[i].tagName.toLowerCase()=='textarea')
		{
			if (!els[i].disabled && els[i].name)
			{
				switch (els[i].type.toLowerCase()) 
				{
					case 'checkbox':
						els[i].checked ? result[arrcount]=els[i].name+'='+encodeURIComponent(els[i].value):result[arrcount]=els[i].name+'=0';
						arrcount++;
						break;
					case 'radio':		
						if (els[i].checked) 
						{
							result[arrcount]=els[i].name+'='+encodeURIComponent(els[i].value);
							arrcount++;
						}
						break;
					default:
					  	result[arrcount]=els[i].name+'='+encodeURIComponent(els[i].value);
						arrcount++;
						break;
				}
			}
		}
	}
	return result.join('&');
};

//AJAX funkce
function bAjax() {
	this.req = null;
	this.url = null;
	this.method = 'POST';
	this.encoding = 'UTF-8';
	this.async = 'false';
	this.status = null;
	this.statusText = '';
	this.sendData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.showErrors = false;
	this.responseFormat = 'text', // 'text', 'xml', 'object'
	this.init = function() 
	{
		if (!this.req) {
	   		try 
			{
		 		// Firefox, Safari, IE7.
		 		this.req = new XMLHttpRequest();
			}
	   		catch (e) 
			{
		 		try 
				{
		   			//IE6.
		   			this.req = new ActiveXObject('MSXML2.XMLHTTP');
		 		}
		 		catch (e) 
				{
		   			try 
					{
			 			//predchozi IE.
			 			this.req = new ActiveXObject('Microsoft.XMLHTTP');
		   			}
		   			catch (e) 
					{
			 			//nelze XMLHttpRequest objekt vytvorit.
			 			return false;
		   			}
		 		}
	   		}
	 	}
	 	return this.req;
	};  //end init fce
	this.doReq = function() 
	{
		if (!this.init()) 
		{
   			alert('Nebylo možno vytvořit XMLHttpRequest objekt !!!');
   			return;
 		}
		if (this.method.toLowerCase()=='post')
		{
			//volame stranku pomoci POST
	 		this.req.open(this.method, this.url, this.async);
			this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset="+this.encoding.toLowerCase());
			this.req.setRequestHeader("Content-length", this.sendData.length);
			this.req.setRequestHeader("Connection", "close");
		}
		else
		{
			//volame pomoci GET
	 		this.req.open(this.method, this.url+'?'+this.sendData, this.async);
		}
		var self = this; 
 		this.req.onreadystatechange = function() 
		{
			var resp = null;
			if (self.req.readyState == 4) 
			{
				switch (self.responseFormat) 
				{
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}
				if (self.req.status >= 200 && self.req.status <= 299) 
				{
					self.handleResp(resp);
				}
				else 
				{
					self.handleErr(resp);
				}
			} 
 		}; //end onreadystatechange fce
 		this.req.send(this.sendData);
	}; //end request funkce
	this.handleErr = function() 
	{
		if (this.showErrors)
		{
	   		alert('Chyba:\n'
		 	+ 'Kód: ' + this.req.status + '\n'
		 	+ 'Popis: ' + this.req.statusText);
	 	}
	}; //end fce pro chyby
	this.abort = function() 
	{
		if (this.req) 
		{
   			this.req.onreadystatechange = function() { };
   			this.req.abort();
   			this.req = null;
 		}
	};
	this.Request = function(url, hand, format) 
	{
 		this.url = url;
 		this.handleResp = hand;
 		this.responseFormat = format || 'text';
 		this.doReq();
	};
}