// @desc   hlada ihlu v kope sena
// @param  needle - hladana hodnota
// @param  haystack - prehladavane pole
// @return boolean
function in_array(needle, haystack)
{	if (typeof haystack != "undefined")
	{	var count = haystack.length;
		// POZOR PRI JEDNOPRVKOVYCH POLIACH S NUM HODNOTOU PRVKU: x=new Array(10); x.length je 10
		var i;
		for (i = 0; i < count; i++)
		{	if (needle == haystack[i]) return true;
		}
	}
	return false;
}

// @desc   vyparsuje get argumenty v URL a vrtati ich v asoc poli
// @param  query (str) - GET cast url (zacinajuca ? /musi byt vratane ?/)
//                       to co vracia location.search
// @return object
function parse_get(query)
{	var out = new Object();
	var q_string = query.substring(1); // odrezeme prvy otaznik
	var pairs = q_string.split('&');
	for (var i = 0; i < pairs.length; i++)
	{	if (pairs[i].length > 0)
		{	var pos = pairs[i].indexOf('=');
			var key, value;
			if (pos == -1) // nenaslo sa "="
			{	key = pairs[i];
				value = "";
			}
			else
			{	key = pairs[i].substring(0, pos);
				value = pairs[i].substring(pos+1);
			}
			//alert(key + "=" + value);
			out[key] = value;
		}
	}
	return out;
}


function openW(url,meno,width,height){
	if (typeof width == "undefined") width = 400;
	if (typeof height == "undefined") height = 300;
	window.open(url,meno,'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
}


// @desc    selektne dany idx v option liste
function set_selected_idx(idx, form_name, select_name)
{	var slct = document.forms[form_name].elements[select_name];
	for (var i = 0; i < slct.options.length; i++ )
	{	if (slct.options[i].value == idx) slct.options[i].selected = true;
	}
}

function mail_to(domain, mailto)
{	var url = "mailto:"+mailto+"@"+domain;
	self.location.href = url;
	return false;
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function parseQueryStr(str) {
	var o = {}, a = [];
	
	// ocakava viac menej validny k=v&x=z format 
	if (str) {
		var kv = str.split('&');
	} else return o;
	
	for (var i = 0; i < kv.length; i++) {
		a = kv[i].split('=');
		if (a[0] && typeof a[1] != 'undefined') {
			o[a[0]] = unescape(a[1]);
		}
	}
	return o;	
}

// (c) --> http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
function encode_utf8(rohtext) {
	if (!rohtext) return '';
	// dient der Normalisierung des Zeilenumbruchs
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++) {
		// ermitteln des Unicodes des  aktuellen Zeichens
		var c=rohtext.charCodeAt(n);
		// alle Zeichen von 0-127 => 1byte
		if (c<128) {
			utftext += String.fromCharCode(c);
		} else if((c>127) && (c<2048)) { // alle Zeichen von 127 bis 2047 => 2byte
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		} else { // alle Zeichen von 2048 bis 66536 => 3byte
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		}
	}
	return utftext;
}

function decode_utf8(utftext) {
	var plaintext = ""; var i=0; var c=c1=c2=0;
	if (!utftext) return '';
	// while-Schleife, weil einige Zeichen uebersprungen werden
	while(i<utftext.length) {
		c = utftext.charCodeAt(i);
		if (c<128) {
			plaintext += String.fromCharCode(c);
			i++;
		} else if((c>191) && (c<224)) {
			c2 = utftext.charCodeAt(i+1);
			plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
			i+=2;
		} else {
			c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
			plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
			i+=3;
		}
	}
	return plaintext;
}

function getGCD(key, which)
{	if (typeof which == 'undefined') which = 'normal';
	var idx = (which == 'session') ? '_GCDS' : '_GCD';
	var q = parseQueryStr(getCookie(idx));
	return (typeof q[key] != 'undefined') ? q[key] : null;
}