function sprintf () {
    var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;    var a = arguments, i = 0, format = a[i++];
 
    // pad()
    var pad = function (str, len, chr, leftJustify) {
        if (!chr) {chr = ' ';}
        var padding = (str.length >= len) ? '' : Array(1 + len - str.length >>> 0).join(chr);
        return leftJustify ? str + padding : padding + str;
    };
 
    // justify()    
    var justify = function (value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
        var diff = minWidth - value.length;
        if (diff > 0) {
            if (leftJustify || !zeroPad) {
                value = pad(value, minWidth, customPadChar, leftJustify);            } else {
                value = value.slice(0, prefix.length) + pad('', diff, '0', true) + value.slice(prefix.length);
            }
        }
        return value;
    };
 
    // formatBaseX()
    var formatBaseX = function (value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
        // Note: casts negative numbers to positive ones
        var number = value >>> 0;
        prefix = prefix && number && {'2': '0b', '8': '0', '16': '0x'}[base] || '';
        value = prefix + pad(number.toString(base), precision || 0, '0', false);
        return justify(value, prefix, leftJustify, minWidth, zeroPad);
    }; 
    // formatString()
    var formatString = function (value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
        if (precision != null) {
            value = value.slice(0, precision);
        }
        return justify(value, '', leftJustify, minWidth, zeroPad, customPadChar);
    };
 
    // doFormat()
    var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) {
        var number;
        var prefix;
        var method;
        var textTransform;
        var value;
 
        if (substring == '%%') {return '%';}
 
        // parse flags
        var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false, customPadChar = ' ';
        var flagsl = flags.length;
        for (var j = 0; flags && j < flagsl; j++) {
            switch (flags.charAt(j)) {
                case ' ': positivePrefix = ' '; break;
                case '+': positivePrefix = '+'; break;
                case '-': leftJustify = true; break;
                case "'": customPadChar = flags.charAt(j+1); break;
                case '0': zeroPad = true; break;
                case '#': prefixBaseX = true; break;
            }
        }
 
        // parameters may be null, undefined, empty-string or real valued
        // we want to ignore null, undefined and empty-string values
        if (!minWidth) {
            minWidth = 0;
        } else if (minWidth == '*') {
            minWidth = +a[i++];
        } else if (minWidth.charAt(0) == '*') {
            minWidth = +a[minWidth.slice(1, -1)];
        } else {
            minWidth = +minWidth;
        }
         // Note: undocumented perl feature:
        if (minWidth < 0) {
            minWidth = -minWidth;
            leftJustify = true;
        } 
        if (!isFinite(minWidth)) {
            throw new Error('sprintf: (minimum-)width must be finite');
        }
         if (!precision) {
            precision = 'fFeE'.indexOf(type) > -1 ? 6 : (type == 'd') ? 0 : undefined;
        } else if (precision == '*') {
            precision = +a[i++];
        } else if (precision.charAt(0) == '*') {
            precision = +a[precision.slice(1, -1)];
        } else {
            precision = +precision;
        }
         // grab value using valueIndex if required?
        value = valueIndex ? a[valueIndex.slice(0, -1)] : a[i++];
 
        switch (type) {
            case 's': return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
            case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
            case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
            case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
            case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
            case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
            case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
            case 'i':
            case 'd':
                number = parseInt(+value, 10);
                prefix = number < 0 ? '-' : positivePrefix;
                value = prefix + pad(String(Math.abs(number)), precision, '0', false);
                return justify(value, prefix, leftJustify, minWidth, zeroPad);
            case 'e':
            case 'E':
            case 'f':
            case 'F':
            case 'g':
            case 'G':
                number = +value;
                prefix = number < 0 ? '-' : positivePrefix;
                method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())];
                textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2];
                value = prefix + Math.abs(number)[method](precision);
                return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
            default: return substring;        }
    };
 
    return format.replace(regex, doFormat);
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    
    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        
        if (c < 128) {
            utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
 
	return utftext;
}
function decode(text) {
    var utftext = unescape(text);
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;
    
	while ( i < utftext.length ) {
        c = utftext.charCodeAt(i);
 
		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}
    string = string.replace(/\+/g," ");
	return string;
}
function createIsoString(string) {
	var pattern_czech = 'áäčďéěíĺľňóô öŕšťúů üýřžÁÄČĎÉĚÍĹĽŇÓÔ ÖŔŠŤÚŮ ÜÝŘŽ';
	var pattern_iso = 'aacdeeillnoo orstuu uyrzAACDEEILLNOO ORSTUU UYRZ'; 
	
	text = '';
	for(i = 0; i < string.length; i++) {
		if(pattern_czech.indexOf(string.charAt(i)) != -1)
			text += pattern_iso.charAt(pattern_czech.indexOf(string.charAt(i)));
		
		else 
			text += string.charAt(i);
	}
	return text;
}
function SetCookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
	var cookie_string = name + "=" + escape ( value );

	if(exp_y) {
		var expires = new Date ( exp_y, exp_m, exp_d );
		cookie_string += "; expires=" + expires.toGMTString();
	}
	
	if(path)
        cookie_string += "; path=" + escape ( path );
	
	if(domain)
		cookie_string += "; domain=" + escape ( domain );
  	
	if(secure)
        cookie_string += "; secure";
	
	document.cookie = cookie_string;
}
function DeleteCookie(cookie_name) {
	var cookie_date = new Date();
	cookie_date.setTime (cookie_date.getTime() - 1);
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function GetCookie(check_name) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 ) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if(!b_cookie_found ) {
		return null;
	}
}
function SaveSettings(key, value) {
	var current_date = new Date;
    var cookie_year = current_date.getFullYear() + 1;
    var cookie_month = current_date.getMonth();
    var cookie_day = current_date.getDate();
    
    SetCookie(key, value, cookie_year, cookie_month, cookie_day, '/');
}
function pic_window(file, width, height){
	window.open('/show-image/?filename='+file, 'mywindow', 'width='+width+', height='+height+'');
}
function show(eleId) {
	obj = document.getElementById(eleId);
	obj.style.display = 'block';
}
function hide(eleId) {
	obj = document.getElementById(eleId);
	obj.style.display = 'none';
}
function hideAll(key, count) {
	for(i=1; i<count+1; i++) {
		obj = GetObject(key+'_'+i);
		obj.style.display = 'none';
	}
}
function open_close(eleId) {
	obj = document.getElementById(eleId);
	if(obj.style.display == 'block') {
		obj.style.display = 'none'
	}
	else {
		obj.style.display = 'block'
	}
}
function insertIcon(eleId, data) {
	window.top.document.getElementById(eleId).value = data;
	parent.parent.GB_hide();
}
function GetObject(eleId) {
	obj = null;
	
	if(document.getElementById && document.getElementById(eleId)) {
		obj = document.getElementById(eleId);
	}
	
	else if (document.all && document.all(eleId)) {
		obj = document.all(eleId);
	}
	
	else if (document.layers && document.layers[eleId]) {
		obj = document.layers[eleId];
	}
	
	return obj;
}
function SetValue(key, value) {
	obj = GetObject(key);
	obj.value = value;
}
function ChangeBackground(eleId, className) {
    obj = GetObject(eleId);
    obj.className = className;
}
function CheckedAll(form, flag) {
	var inputs = form.getElementsByTagName('input');
	for(i=0; i<inputs.length; i++) {
		if(inputs[i].type == 'checkbox') {
			if(flag == 'checked') inputs[i].checked = true;
			else inputs[i].checked = false;
		}
	}
}
function saveStyles(eleId, form, input) {
	var text = '';
	var output = '';
	var parse = new Array();
	var inputs = form.getElementsByTagName('input');
	
	for(i=0; i< inputs.length; i++) {
		if(inputs[i].type == 'checkbox' && inputs[i].checked == 1) {
			label = GetObject('label_'+inputs[i].value);
			text += label.innerHTML+':';
		}
	}
	parse = text.split(':');
	for(i=0; i<parse.length-1; i++) {
		var length = output.length + parse[i].length;
		if(length < 25) {
			output += parse[i]+',';
		}
		else {
			var flag = true;
		}
	}
	output = output.substr(0, (output.length-1));
	if(flag)
		output += ',..';
	
	SetValue(input, output);
	hide(eleId);
	show('banner-250x250');
}
function CloseAdmin(header, list, count) {
	SaveSettings('admin_mode', 0);
	hide(header);
	hideAll(list, count);
}
function checkAkreData() {
  if(GetObject('akre_typy').value == '') {
     alert('Chyba: není vyplněn typ akreditace');
     return false;
  }
  
  if(GetObject('akre_pocet').value == '') {
    alert('Chyba: není vyplněn počet osob');
    return false;
  }
  
  return true;
}
