function gE(id) {
	return document.getElementById(id);
}

//-------------kopie php funkci co v JS chybí---------------------
function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function number_format(a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}
//-------------kopie php funkci co v JS chybí---------------------end

var CisloUploadIframe='';
var MenRozmer='';
//multi upload
function MultiUpload(soubor,cislo) {
	gE('soubor').value+='--- '+soubor+' !nahravam!';
	gE('soubor'+cislo).value=soubor+' !nahravam!';
}

function RozniDalsiUpload(cislo) {
	var PocetUploadu = gE('maxpocetiframu').value;
	if(cislo==CisloUploadIframe&&cislo<(PocetUploadu-1)) {
		CisloUploadIframe++;
		gE('idupload'+CisloUploadIframe).style.display='';

		if (PocetUploadu<5)
			var PrvniAktivator=PocetUploadu;
		else
			var PrvniAktivator=5;
		if (cislo==0){ //naplni dalši 4 uploady
			for (var i=1;i<PrvniAktivator;i++) {
				gE('idupload'+i).src='upload.php?'+gE('iframepromenne').value+'&cisloframu='+i; //alert('1-'+i);
			}
		}
		if (cislo==1){ //naplní zbytek uploadu
			for (var i=5;i<PocetUploadu;i++) {
				gE('idupload'+i).src='upload.php?'+gE('iframepromenne').value+'&cisloframu='+i; //alert('2-'+i);
			}
		}
		if (MenRozmer!='') ZmenaRozmeru(MenRozmer);
	}
}

function MultiUploadSoubor(soubor,soubor_size,cislo) {
	gE('soubor'+cislo).value=soubor;
	gE('soubor_size'+cislo).value=soubor_size;
	try { ResizeJPG(soubor); } catch(e)	{} //ResizeJPG je jenom u spravce
	var celkovysoubor='';
	var celkovavelikost=0;
	for (var i=0;i<=CisloUploadIframe;i++) {
		if (gE('soubor'+i).value!='') {
			celkovysoubor+='--- '+gE('soubor'+i).value;
			celkovavelikost+=parseInt(gE('soubor_size'+i).value);
		}
	}
	if (celkovavelikost>gE('maxvelikostsouboru').value&&gE('maxvelikostsouboru').value!='') {
		alert(TextJS[14]);
		parent.window.eval('upload'+cislo).gE('formmazani').submit();
		MultiUploadSoubor('','',cislo);
		return false;
	}
	else
		gE('soubor').value=celkovysoubor;

	try {
		if (gE('soubor').value!="" && gE('soubor').value.indexOf("!nahravam!")==-1) {
			gE('upload_img').src="html/pic/upload2.jpg";
			gE('upload_but').disabled=false;
			gE('upload_but_tab').style.border='2px solid #009900';
		}
		else {
			gE('upload_img').src="html/pic/upload1.jpg";
			gE('upload_but').disabled=true;
			gE('upload_but_tab').style.border='2px solid #E8E5DE';
		}
	}
	catch(e) {}
}
function UzJeUpload(nazevsouboru) {
	return gE('soubor').value.indexOf(nazevsouboru);
}
//simple upload
function SimpleUpload(soubor,dodatekknazvu) {
	gE('soubor'+dodatekknazvu).value=soubor;
	try {
		if (gE('soubor'+dodatekknazvu).value!="" && gE('soubor'+dodatekknazvu).value.indexOf("!nahravam!")==-1) {
			gE('upload_img').src="html/pic/upload2.jpg";
			gE('upload_but').disabled=false;
			gE('upload_but_tab').style.border='2px solid #009900';
		}
		else {
			gE('upload_img').src="html/pic/upload1.jpg";
			gE('upload_but').disabled=true;
			gE('upload_but_tab').style.border='2px solid #E8E5DE';
		}
	}
	catch(e) {}
}
function Uploaduje() {
	if (arguments.length>0)
		var dodatekknazvu=arguments[0];
	else
		var dodatekknazvu='';

	if (gE('soubor'+dodatekknazvu).value.indexOf("!nahravam!")!=-1) {
		alert(TextJS[0]);
		return true;
	}
	else
		return false;
}
function RadioButton(kde,hodnota) {
	gE(kde).value=hodnota;
}
function CheckBox(kde,hodnota) {
	if (hodnota)
		gE(kde).value=1;
	else
		gE(kde).value=0;
}
function CheckBoxValue(kde,hodnota,vloz) {
	if (hodnota)
		gE(kde).value=vloz;
	else
		gE(kde).value='';
}


function PrepinacOnOff(name) {
	if (gE(name).value=='1') {
		gE('prepinac_zap_'+name).style.display='none';
		gE('prepinac_vyp_'+name).style.display='';
		gE(name).value='0';
		return false;
	}
	else {
		gE('prepinac_vyp_'+name).style.display='none';
		gE('prepinac_zap_'+name).style.display='';
		gE(name).value='1';
		return false;
	}
}

function FaktStornoSmaz(cislotxt) {
	return confirm(TextJS[cislotxt]);
}

function htmlSpecialChars(tmp) {
	var iStringLength = tmp.length;
    var sModifiedString = '';
    for (var i = 0; i < iStringLength; i++) {
        switch (tmp.charCodeAt(i)) {
            case 34 : sModifiedString += '&quot;'; break;
            case 38 : sModifiedString += '&amp;' ; break;
            case 39 : sModifiedString += '&#39;' ; break;
            case 60 : sModifiedString += '&lt;'  ; break;
            case 62 : sModifiedString += '&gt;'  ; break;
            default : sModifiedString += tmp.charAt(i);
        }
    }
    return sModifiedString;
}

function RychlaNavigace(tmp) {
	if (tmp!="") {
		document.location.href=tmp;
	}
}

var zobrazenMalyAlert='';

function Alert(text) {
	var cil=arguments[1];

	//vypnu zobrazeny maly alert nebo velky alert
	//trochu me to mate, teoreticky nemusi byt: if (zobrazenMalyAlert!='' || !cil) 
	AlertOff(); 
	
	//zkusim vypsat chybu v cily nebo nahore
	if (cil) {
		gE("A_"+cil).style.display='';		
		gE("A_"+cil).innerHTML=text;
		zobrazenMalyAlert=cil;
	}
	//roznu horni chybu
	else {		
		gE('obal_chyba_js').style.display='';
		gE('obal_chyba_js_text').innerHTML=text;
	}
}
function AlertOff() {
	//zhasnu stare chyby a zpravy nahore vygenerovane phpkem
	try { 
		gE('ChybaVypis').style.display='none';
	} catch(e) {}
	
	//zhasnu horni chybu
	try { 
		gE('obal_chyba_js_text').innerHTML='';
		gE('obal_chyba_js').style.display='none';		
	} catch(e) {}

	//pokusi se zhasnout predchozi maly Alert (uz nemusi byt)
	try { 
		gE("A_"+zobrazenMalyAlert).innerHTML='';
		gE("A_"+zobrazenMalyAlert).style.display='none';		
	} catch(e) {}
	
	zobrazenMalyAlert='';
}

var generovat_okno;
function Generovat(cas) {
	generovat_okno='';
	try {
		generovat_okno=window.open('generovat.php','generovat_okno');
		//alert("volani java");
		Generovat_focus();
		Generovat_cyklus(cas);
		return false;
	}
	catch(e) {}		
	if (generovat_okno=='') {
		Generovat_cyklus(cas);
		//alert("volani klasika");
		return true;
	}
	else
		return false;
}
function Generovat_cyklus(i) {
	try {			
		gE('generovat_fake').innerHTML=TextJS[1]+" "+i+"s";
		gE('generovat_fake').style.display='';
		gE('generovat').style.display='none';
		i--;
		if(i>0) {		
			setTimeout(function() {
				Generovat_cyklus(i);
			}, 1000);
		}	
		else {
			gE('generovat_fake').style.display='none';
			gE('generovat').style.display='';
		}
	}
	catch(e) {}
}
function Generovat_focus() {
	try {		
		generovat_okno.focus();	
	}
	catch(e) {}	
}

function SpustFlash(doID,dir,flash,width,height,bg,bgalt,borderalt) {
	if (!bg) bg='E8E5DE';
	if (!bgalt) bgalt='efece5';
	if (!borderalt) borderalt='ccc';
	var hasRightVersion = DetectFlashVer(9, 0, 124);
	if(hasRightVersion) {  // if we've detected an acceptable version
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0',
			'width', width,
			'height', height,
			'src', dir+flash,
			'quality', 'high',
			'pluginspage', 'http://www.adobe.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'false',
			'scale', 'showall',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', flash,
			'bgcolor', '#'+bg,
			'name', 'stats_banner',
			'menu', 'false',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', dir+flash,
			'salign', '',
			'doID',doID
		); //end AC code
	} else {  // flash is too old or we can't detect the plugin
		NespustilFlash(doID,width,height,bgalt,borderalt);
	}
	return false;
}
function NespustilFlash(doID,width,height,bgalt,borderalt) {
	gE(doID).innerHTML="<div class='textchyba' align='center' style='width:"+(width-2)+"px;height:"+(height-2)+"px;border:1px solid #"+borderalt+";background-color:#"+bgalt+";'><div style='padding:"+((height/2)-50)+"px 0px 0px 0px;'>"+TextJS[56]+"</div><a href=\"http://get.adobe.com/flashplayer/\" target=\"_self\"><img src=\"html/pic/alternate.gif\" alt=\"flashplayer\" width=\"88\" height=\"31\" vspace=\"5\" border=\"0\"></a><br/>("+TextJS[57]+")</div>";
}

function KontrolaDatFormulare(typ,hodnota)
{
	if(typ=="mail")
	{
		var regObj= new RegExp('^([a-zA-Z0-9])+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9_-]+)+$');
	  if (! hodnota.match(regObj))
	    return false;
	}
	if(typ=="mail_prefix")
	{
		var regObj= new RegExp('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*$');
	  if (! hodnota.match(regObj))
	    return false;
	}
	if(typ=="subdomena")                                                                     //kontrola casti domenoveho jmena urcujici domenu tretiho radu (tzn.: KONTROLOVANE.neco.cz)
	{
		var regObj= new RegExp('^([a-z0-9]+[-]?)*[a-z0-9]+$');
	  if (! hodnota.match(regObj))
	    return false;
	}
	if(typ=="domena")
	{
		var regObj= new RegExp('^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$');
	  if (! hodnota.match(regObj))
	    return false;
	}
	if(typ=="domena2r")
	{
		var regObj= new RegExp('^[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+$');
	 if (! hodnota.match(regObj))  
	    return false;
	}
	if(typ=="heslo")
	{
		var regObj= new RegExp('^[a-zA-Z0-9@_]{5,30}$');
	  if (! hodnota.match(regObj))
	    return false;
	}
	if(typ=="vyplneno")
	{
	  if (hodnota.length<3)
	    return false;
	}
	if(typ=="cislo")
	{
		var regObj= new RegExp('^[0-9]+$');
	 if (! hodnota.match(regObj))  
	    return false;
	}
	return true;
}

function NactiText(cislo) {
	try {
		var tmp=gE("textyProJS").value;
		tmp = tmp.split("~");
		
		if(tmp[cislo-1000]!="") 
			return tmp[cislo-1000];
		else 
			return TextJS[999];
	}
	catch(e) { return TextJS[999]; }
}

function hexdec(hex_string) {
	hex_string = (hex_string+'').replace(/[^a-f0-9]/gi, '');
	return parseInt(hex_string, 16);
}
function OpacnaBarva(tmp) {
	tmp = tmp.replace(/#/gi, '');
	tmp = (hexdec(tmp.substr(0,2))+hexdec(tmp.substr(2,2))+hexdec(tmp.substr(4,2)))/3;
 	if (tmp>127.5) 
  		tmp="000000";
	else
    	tmp="ffffff";
	return tmp;
}

function ZakazEnter(e)
{
    var k = (navigator.appName=="Netscape") ? e : event.keyCode;
    if(k == 13)
		return false;
    else 
    	return true;
}

function str_replace(strFind, strReplace, strChk)
{
  var strOut = strChk;
  while (strOut.indexOf(strFind) > -1) {
    strOut = strOut.replace(strFind, strReplace);
  }
  return strOut;
}

String.prototype.trim = function()
{
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

var Utf8 = {
 
	// public method for url encoding
	encode : function (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;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		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;
			}
 
		}
 
		return string;
	},
	
	decodeArray : function (pole) {
		for(var key in pole)
		{
			try
			{
				if(key!='toJSONString')
					if (pole[key].constructor.toString().indexOf("Array") == -1)
					{
						if (pole[key].constructor.toString().indexOf("String") != -1)
							pole[key]=this.decode(pole[key]);
					}
					else
						pole[key]=this.decodeArray(pole[key]);
			}
			catch(e){}
		}
		return pole;
	}
 
};

function intval(mixed_var, base) {
	var tmp;
	var type = typeof( mixed_var );

	if (type === 'boolean') {
		return (mixed_var) ? 1 : 0;
	} else if (type === 'string') {
		tmp = parseInt(mixed_var, base || 10);
		return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
	} else if (type === 'number' && isFinite(mixed_var) ) {
		return Math.floor(mixed_var);
	} else {
		return 0;
	}
}

function preloader(preloaderid,containerid) {
	try
	{
		switch (preloaderid) {
			case 1 : document.getElementById(containerid).innerHTML="<table width='100%' height='98%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center'><img src='html/pic/loading.gif' width='50' height='50'></td></tr></table>"; break;
			//to same jak 1 ale s odsazenim zhora a zdola
			case 2 : document.getElementById(containerid).innerHTML="<div align='center' style='margin-top:164px;margin-bottom:164px'><img src='html/pic/loading.gif' width='50' height='50'></div>"; break;
			//vracim pro Direct Ajax
			case 3 : return "<div align='center' style='margin-top:20px;margin-bottom:20px;border:none'><img src='html/pic/preloader_napoveda.gif' width='32' height='32' border='0' style='border:none'></div>"; break;
			case 4 : return "<div align='center' style='margin-top:20px;margin-bottom:20px;border:none'><img src='html/pic/preloader.gif' width='32' height='32' border='0' style='border:none'></div>"; break;
		}
	}
	catch (e){
		alert(e);
	}
}

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}