/*makePagesProducts 1.3.1 15.4.2011 Jan Verner - DIGITAL ART STUDIO j.verner@volny.cz

Rozdily ve verzi 1.3.1. proti 1.3:
Odstranena offline verze. Vse se dela jako online s presmerovanim vystupu do souboru, tim padem vypadl i parametr output_filename
Nemusi existovat soubor hlavicka a paticka. Pokud neni nebo ho nechceme davat do vystupu, tak se prislusne paramtry header_filename footer_filename nahradi klicovym slovem nofile.
Pole databaze Text, TextPred, TextZa, Obr a k nim prislusna pole s odkazy a jinymi relevantnimy parametry byla nahrazena Text0, TextPred0, TextZa0, Obr0 atd. Pole text0, text1 atd. bylo doplneno o HTML tag P0, P1 atd.
Do nazvu poli NazevSekce, NadpisRadky0 a Nadpsi radky pridany HTML tagy H1, H2, H3 v resp. poradi.
Byla vytvorena samostatna table pro UvodObr, aby se dal snadno umistovat ve stylech

Ukazka spusteni z bat:
java.exe -cp C:\Jenik\JavaClasses\makePagesProducts\ver131\programy makePagesProducts ..\Hlavicky\HTML-head_ID-deleni.txt nofile honzasport-ceniky ./ x -H Hlavni_table Hlavni_menu >../output.htm 2>log.txt



Rozdily ve verzi 1.3 proti 1.2.2:
-	V databazi v tabulce Sekce (anebo relevantni tabulce pod jinym jmenem) je nove textove pole UvodObr, kam se dava odkaz na obrazek, ktery se zobrazi v casti reference pod BlokUvod jako obrazek spolecny pro celou sekci. Pri zpracovani starsi verze databaze vznikne osetrena SQL chyba, pole je nutne do starsich dataabzi doplnit
-	Promenna ver v programu, obsahuje cislo aktualni verze - NEZAPOMEN MENIT
-	Funkcnost parametru -Vcislo nebo -Xcislo respektive -V!cislo nebo -X!cislo ve spoustecim batchi: tento prepinac je nadrazen prepinaci Publikovat v databazi. T.zn. je-li parametr Cislo uveden (ve forme celeho cisla) , tak pokud radka byla zaskrtnuta jako Publikovat=ano, tak dojde k jejimu vyjmuti pri nastaveni parametru na !(NEGACE). Naopak je-li cislo radku uvedeno bez !, je polozka zobrazena v menu jako jedina, ostatni polozky se  v menu nezobrazi
-	SQL error, ktery mohl vzniknout pri cteni databaze, pokud byl nastaven nespravne textovy odkaz (retezec v poli OdkazText nebyl nalezen nebo nebyl vubec uveden OdkazKamText (tyka se i ostatnich funkcne podobnych dvojic poli), byl osetren, t.zn. nedojde k zastaveni programu a je generovana chybova zprava, ktera pomuze chybne uvedeny odkaz v databazi lokalizovat.
-	Byly doplneny dalsi parametry CLASS u nekterych prvku TABLE a TD, ktere jeste nebyly takto popsany. Tyto je mozno vyuzit ke graficke uprave v souboru stylu (.css)
-	Na zaklade parametru NUMOFBLOCKCOLS a NUMOFBLOCKROWS lze nastavit formatovani vystupu a jeho rozvrzeni na stranku, pokud pocet radek databaze presahne pocet vystupu vygeneruje se menu odkazujici na dalsi stranky, ale jen v pripade, pokud parametr generovani menu neni -X
-	Formatovani ma za nasledek vygenerovani vice stranek se stejnym jumenem, ale s cislem napr. index.htm, index1.htm atd. Stranky jsou mezi sebou pristupne pomoci zvlastniho menu na konci stranky, jehoz popiska je udana parametrem MENUVICESTRANEKCAPTION. Menu se generuje pouze pokud parametr pro generovani menu v batchi NENI nastaven na -X
-	SPLITHEADLINES promenna pristupna  v CFG souboru umoznuje zalomeni rady nadpisu podle privatniho algoritmu vlozeneho do programu, pro poacet a moznsoti algoritmu nutno se podivat do programu, mozno pridavat dalsi algoritmy
-	FIELDCAPTION tamtez udava jestli se budou pred daty v polich NadpisRadky ... n ukazovat nazvy poli, 0=neukazovat, 1=ano a hledaji se dalsi parametry udavajici popisky jako napr. NADPISRADKYCAPTION. NADPISRADKY1CAPTION az NADPISRADKYnCAPTION podle poctu nadpisu, 2=ano, ale berou se default jmena poli

Rozdily verze 1.2.2 proti verzi 1.2.1:
Character encoding vystupniho souboru se nastavuje parametrem ENCODING v prislusnem .cfg souboru. Pokud neni, testuje se encoding pocitace, na kterem tento program bezi a nastavi se jako default
Byly pridany testovaci podminky, ktere odstrabi nektera prazdna pole z vystupniho souboru, pokud prislusna pole v databazi nejsou naplnena daty. Pri tom je treba davat pozot na to, ze odstraneni pole z vystupniho HTML souboru muze mit v urcitych pripadech za nasledek posunuti ostatnich poli a tim k rozhazeni grafickeho usporadani. V takove situaci je potreba prislusna pole databaze naplnit alespon retezcem &nbsp; (prazdny znak).

Pri spusteni je nutne volat :
v offline rezimu:
java [-cp classpath] classname output_filename header_filename footer_filename ODBCdatabase_name link_prefix service_number -V[[-H[-X]] [referencetablename] [sekcetablename]
Oddelovac parametru je mezera
Parametr -V nebo -H udava vertikalni nebo horizontalni usporadani, parametr -X vygeneruje stranku bez menu
K parametrum H a V lze pridat cislo sekce, ktere v pripade generovani menu bud vygeneruje pouze menu polozku s uvedenym cislem nebo naopak vygeneruje vsechnz polozky menu, vyjma te s uvedenym cilsem a to zalezi na tom, zda pred cislem je znak vykricnik (!) nebo ne / vykricnik udava negaci
Par. link_prefix udava to co bude pred odkazem uvedenym v databazi  (napr adresar nebo dalsi volani java pri online verzi

Priklady volani z batche:
java.exe -cp C:\JENIK\JavaClasses\TvorbaStranekZDatabaze\makePagesProducts122   makePagesProducts ..\VzoryVystupu\Sekce1H.htm ..\hlavicky\Hlavicka.txt ..\hlavicky\Paticka.txt ukazka-makePagProd12 ./ 1 -H

java.exe -cp C:\JENIK\JavaClasses\TvorbaStranekZDatabaze\makePagesProducts122   makePagesProducts ..\VzoryVystupu\Sekce1V.htm ..\hlavicky\Hlavicka.txt ..\hlavicky\Paticka.txt ukazka-makePagProd12 ./ 1 -V

java.exe -cp C:\JENIK\JavaClasses\TvorbaStranekZDatabaze\makePagesProducts122   makePagesProducts ..\VzoryVystupu\Sekce1X.htm ..\hlavicky\Hlavicka.txt ..\hlavicky\Paticka.txt ukazka-makePagProd12 ./ 1 -X

java.exe -cp C:\JENIK\JavaClasses\TvorbaStranekZDatabaze\makePagesProducts122   makePagesProducts ..\VzoryVystupu\Sekce1HStable.htm ..\hlavicky\Hlavicka.txt ..\hlavicky\Paticka.txt ukazka-makePagProd12 ./ 1 -H Stable

java.exe -cp C:\JENIK\JavaClasses\TvorbaStranekZDatabaze\makePagesProducts122   makePagesProducts ..\VzoryVystupu\Sekce1HStable.htm ..\hlavicky\Hlavicka.txt ..\hlavicky\Paticka.txt ukazka-makePagProd12 ./ 1 -H Stable MenuStable


Priklady volani v online rezimu z HTML stranky:
http://hostname/cgi-bin/java.exe?[-cp+classpath]+classname+header_filename+footer_filename+ODBCdatabase_name+link_prefix+service_number+-V||-H||-X+[referencetablename] [sekcetablename]

oddelovac parametru je +, pricemz jako oddelovac parametru v argumentu link_prefix slouzi znak# (%23)


ukazka volani z HTML dokumentu:
<a href="http://localhost/cgi-bin/java.exe?-cp+C:\Jenik\Hozman-arc\DatabazoveReseni\Programy+makePagesProducts+C:\Jenik\Hozman-arc\DatabazoveReseni\Hlavicky\SekceHlava.txt+C:\Jenik\Hozman-arc\DatabazoveReseni\Hlavicky\SekcePata.txt+arc+C:\Jenik\Hozman-arc\DatabazoveReseni\NaWeb\+1+-V+Stable">Zde klikni pro spusteni programu</a>


Pro online volani je cesta na ostatni soubory/adresare odvozovana relativne z hlediska cgi-bin (Windows 98 Personal web Server, z tohoto ohledu musi byt tedy sparvne nastavene cesty na images, style a pod. resp. spravne nakonfigurovane adresare web servru
Nezapomen nastavit spravne adresar cgi-bin pro spusteni interpreta javy (
Cfg soubor se pri online volani hleda, stejne jako u offline verze v adresari, odkud se spousti programy, tedy v tomto pripade v cgi-bin
*/


import java.net.*;
import java.sql.*;
import java.util.*;
import java.io.*;


public class makePagesProducts {
    //Funkce, ktera testuje Link a pridava LinkPrefix z paramatru
	public static String testLink(String odkaz, String linkPrefix){
	//String newLink se nastavi na null, protoze jinak neprobehen podminka testovani na null pri testovani konkretnich odkazu a zustava viset odkaz z predchoziho behu
	String newLink=null;
	
        				if (odkaz!= null) {
        					int test_odkaz=odkaz.indexOf("http");

        						if (test_odkaz!=-1){
        							linkPrefix="";
        						}
        						newLink = linkPrefix+odkaz;
        					//System.out.println("Link je: "+newLink);

        				}
        				return newLink;
     }  //Konec testLink

	//Funkce, ktera generuje textovy odkaz
   	public static String makeAnchor(String Popis, String LinkTo, String LinkString, String OdkudSeSpousti, int Poradinum)
  	 {
					if (LinkTo !=null){
						String insertix="<A class=\""+OdkudSeSpousti+" "+OdkudSeSpousti+"Poradi"+Poradinum+"\" href=\""+LinkTo+"\">";
						if (LinkString!=null){
    						try {
    						//System.out.println ("Chyba");
    						int startpos=Popis.indexOf(LinkString);
    						int endpos=startpos+insertix.length()+LinkString.length();
    						//System.out.println ("Start Position="+startpos+", End Position="+endpos);
    						StringBuffer Popis_buff = new StringBuffer(Popis);
    						Popis_buff.insert(startpos, insertix);
    						Popis_buff.insert(endpos, "</A>");
    						Popis=Popis_buff.toString();
    						}
    						catch (Exception e)
    						{
								System.out.println(">>>Error in setting up anchor - String \""+Popis+"\" doesn`t contain the substring \""+LinkString+"\"");
    						}
						}
					}
					return Popis;
  	 }//Konec makeAnchor

//Funkce, ktera cte konfiguracni soubor databaze
	public static String readConfig(String cfgfilename, String parametername)
	{
		String line="null";
		String value="";
		int startpos;
		int endpos;
		try
		{
			//deprecated
			//DataInputStream dis = new DataInputStream(new FileInputStream(cfgfilename));
			BufferedReader dis = new BufferedReader(new FileReader(cfgfilename));

			while ((line = dis.readLine()) != null)
			{
				if ((line.indexOf(parametername))!=-1) {
    				if (!((line.substring(0,2)).equals("//"))){ //tam, kde jsou v cfg souboru na zacatku // je to jako komentar
    					String insertix=parametername+"=";
    					startpos=insertix.length();
    					endpos=line.length();
    					value=line.substring(startpos,endpos);
    					//System.out.println ("Start Position="+startpos+", End Position="+endpos+" Parametername="+parametername+" Value="+value);
    				}
				}
			}
		}
		catch (IOException e)
		{
				//Vystup zablokovan kvuli online verzi
				//System.out.println("Error opening "+cfgfilename);

		}

		return value;

	}//Konec readConfig




//Hlavni program
   public static void main (String args[]) {

	String ver="1.3.1";
	String whichURL="";//Jmeno databaze = systemove dsn ve Windows
   	String url;
	Connection db=null;
    Statement sq_stmt;
	ResultSet rs;
	String sql_str=null;

	FileOutputStream os=null;
	DataOutputStream dos=null;
	OutputStreamWriter osw;
	BufferedWriter out;
	String fileOut="";
	String fileOutPrev="";
	String fileOutNext="";
	String filehlavicka="";
	String filepaticka="";
	String link_prefix="";
	String service_string="";
	String layout="-k";
	String outputFileType="htm";//recognizable file type name
	char layoutchar='X';
	boolean negativepar=true;
	boolean subparexists=false;
	Integer menunumpar=new Integer(0);
	Integer service_num=new Integer(0);
	String enc="Cp1250";
	boolean online=true;
	boolean notend=true;
	boolean probehlo=false;
	boolean FieldExists=false;
	String option[]={"","","","","","","","",""};
	String nl="\n",cr="\r";

	String TableSQL="Reference";
	String TableMenuSQL="Sekce";

	//tato konfigurace je default, pokud neexistuje cfg file nebo pri online verzi
	int imgmax=5;  //default max pocet obrazku ve skupine obrazku (netykase Obr0)
	int headlinesmax=5; //default max pocet nadpisu na jedne radce vysledneho HTML souboru
	int numofblockcolsmax=1; //default pocet radek databaze na jedne radce vysledneho HTML souboru
	int numofblockrowsmax=0; //default pocet radek databaze na jedne strance vysledneho HTML souboru, 0=nekonecne dlouhy
	int numoftextfieldsmax=0; //default pocet odstavcu na jedne radce databaze, projevi se jako pocet odstavcu pod jednim cidlem poradi
	int reverseorder=0; //radi podle pole Poradi a NadpisRadky
	int textpred=0; //default pocet poli TextPred
	int textza=0; //default pocet poli TextZa
	int splitheadlines=0; //pokud je hodne poli NadpisRadky[i], deli se podle algoritmu na vice radek
	int fieldcaption=0; //nadpisy poli, 0=ne,1=ano, ale jen ty co jsou dale popsany v cfg, 2=default nazvy poli dalsi hodnoty reserve
	//Deklarace promennych pro fieldnamecaption viz dale, podle poctu prislusnych poli ziskanych z cfg

	//konec default konfigurace


	int maxargs;
	//String lastoption=new String("");

	String DescA="";
	String DescP="";



    	theEnd: if (notend) {
    		//System.out.println("ZDE");

    		//Prevod args na options resi situaci, kdy args neexistuji
    		for (maxargs=0; maxargs<args.length; maxargs++) {
    			//Vytvoreni noveho obsahu promenne option, ne ukazatele
    			option[maxargs]=new String(args[maxargs]);
    			//System.out.println("maxargs="+maxargs);
    			//System.out.println("option["+maxargs+"]="+option[maxargs]);

    		}
			//System.out.println("args.length="+args.length);
    		//System.out.println("maxargs="+maxargs);
    		//Zde se kontroluje jaky je posledni parametr a z toho se usuzuje zda chybi oznaceni tabulky Reference neb tabulky Sekce nebo oboji
    		// Pokud chybi, tak se pridava option do celkoveho poctu 8, aby fungovalo precteni paramatru
    		// je treba pocitat s tim, ze cislo args.length je o jednu vyssi nez option[maxargs], protoze pole se cisluje od 0


    		if (maxargs==6) {
    			option[maxargs]=TableSQL;
    			option[maxargs+1]=TableMenuSQL;
    			maxargs=maxargs+2;
    		}
    		if (maxargs==7) {
    			option[maxargs]=TableMenuSQL;
    			maxargs=maxargs+1;
    		}


    		if (maxargs==8) {
    			filehlavicka=new String (option[0]);
    			filepaticka=new String(option[1]);
    			whichURL=new String(option[2]);
    			link_prefix=new String(option[3]);
    			service_string=new String(option[4]);
    			layout=new String(option[5]);
    			TableSQL=new String(option[6]);
    			TableMenuSQL=new String(option[7]);

    		}else {
    			//System.out.println("notend");

    			notend=false;
    			break theEnd;

    		}

    		//System.out.println("service_string="+service_string);
    		//Zde se prevadi service_num na cislo, pokud je misto service_num x, bere se service_num=0 pro tabulku Sekce a vsechno z tabulky Reference
    		if (!(service_string.equals("x"))){
    		service_num=Integer.valueOf(service_string);
    		}//else {service_num=new Integer(0);}//neni treba je default
    		//System.out.println("service_num="+service_num);

    		//Zde se ctou parametry databaze z konfiguracniho sooboru databaze
    		//Pokud to nejde nastavi se implicitni hodnoty
    		//Funkce cte parametry jako string, prevod probiha az zde

    		String databasecfgname=whichURL+".cfg";

    		try {

    			Integer Imgmax=Integer.valueOf(readConfig(databasecfgname, "NUMOFIMAGES"));
    			imgmax=Imgmax.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {
    				System.out.println(">>>Error reading parameter NUMOFIMAGES, probably missing cfg file, setting initial value "+imgmax);
    			}
				*/
    		}


    		try {

    			Integer Maxheadlines=Integer.valueOf(readConfig(databasecfgname, "NUMOFHEADLINES"));
    			headlinesmax=Maxheadlines.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {
    				System.out.println(">>>Error reading parameter NUMOFHEADLINES, probably missing cfg file, setting initial value "+headlinesmax);
    			}
				*/
    		}

    		try {

    			Integer NumofTextFields=Integer.valueOf(readConfig(databasecfgname, "NUMOFPLUSTEXTFIELDS"));
    			numoftextfieldsmax=NumofTextFields.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {

    				System.out.println(">>>Error reading parameter NUMOFPLUSTEXTFIELDS, probably missing cfg file, setting initial value "+numoftextfieldsmax);
    			}
				*/
    		}

    		try {

    			Integer TextPredFields=Integer.valueOf(readConfig(databasecfgname, "FIELDTEXTPRED"));
    			textpred=TextPredFields.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {

    				System.out.println(">>>Error reading parameter FIELDTEXTPRED, probably missing cfg file, setting initial value "+textpred);
    			}
				*/
    		}


    		try {

    			Integer TextZaFields=Integer.valueOf(readConfig(databasecfgname, "FIELDTEXTZA"));
    			textza=TextZaFields.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {

    				System.out.println(">>>Error reading parameter FIELDTEXTZA, probably missing cfg file, setting initial value "+textza);
    			}
				*/
    		}



    		try {

    			Integer NumofBlockCols=Integer.valueOf(readConfig(databasecfgname, "NUMOFBLOCKCOLS"));
    			numofblockcolsmax=NumofBlockCols.intValue();
    			if (numofblockcolsmax==0) {numofblockcolsmax=1;}//Ochrana proti nekonecnemu cyklu pro numofblockcolsmax=0
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {
    				System.out.println(">>>Error reading parameter NUMOFBLOCKCOLS, probably missing cfg file, setting initial value "+numofblockcolsmax);
    			}
				*/
    		}


    		try {

    			Integer NumofBlockRows=Integer.valueOf(readConfig(databasecfgname, "NUMOFBLOCKROWS"));
    			numofblockrowsmax=NumofBlockRows.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {
    				System.out.println(">>>Error reading parameter NUMOFBLOCKROWS, probably missing cfg file, setting initial value "+numofblockrowsmax);
    			}
				*/
    		}


    		try {

    			Integer ReverseOrder=Integer.valueOf(readConfig(databasecfgname, "REVERSEORDER"));
    			reverseorder=ReverseOrder.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {

    				System.out.println(">>>Error reading parameter REVERSEORDER, probably missing cfg file, setting initial value "+reverseorder);
    			}
				*/
    		}

    			String Encoding=readConfig(databasecfgname, "ENCODING");
    			enc=new String(Encoding);

			try {

    			Integer Splitheadlines=Integer.valueOf(readConfig(databasecfgname, "SPLITHEADLINES"));
    			splitheadlines=Splitheadlines.intValue();
    		}
    		catch (Exception e)
    		{
   				//Od verze 131 neni vyuzivano - rezerva pro debug mode
            	/*
    			if (!(online)) {
    				System.out.println(">>>Error reading parameter SPLITHEADLINES, probably missing cfg file, setting initial value "+splitheadlines);
    			}
    			*/
    		}

             try {

    			Integer FieldCaption=Integer.valueOf(readConfig(databasecfgname, "FIELDCAPTION"));
    			fieldcaption=FieldCaption.intValue();
    		}
    		catch (Exception e)
    		{
    			//Od verze 131 neni vyuzivano - rezerva pro debug mode
   				/*
    			if (!(online)) {
    				System.out.println(">>>Error reading parameter FIELDCAPTION, probably missing cfg file, setting initial value "+fieldcaption);
    			}
   				*/
    		}

    		String NadpisRadkyCaption[]=new String[headlinesmax+1];//Caption pro pole NadpisRadky a NadpisRadky[]

    		if (fieldcaption==1){
    			for  (int i=0; i<=headlinesmax; i++) {
                    try {
                        //System.out.println("NadpisRadkyCaption["+i+"]="+NadpisRadkyCaption[i]);
                        if (i==0){
                           // NadpisRadkyCaption[i]="";
                        	NadpisRadkyCaption[i]=readConfig(databasecfgname, "NADPISRADKYCAPTION");

            			}else {
            			NadpisRadkyCaption[i]=readConfig(databasecfgname, "NADPISRADKY"+i+"CAPTION");}
           			 	//System.out.println("NadpisRadkyCaption["+i+"]="+NadpisRadkyCaption[i]);
                        //Nehazi chybu, protoze nikdy neni null, ale je prazdny retezec
            			//if (NadpisRadkyCaption[i]==null) {
            			//	NadpisRadkyCaption[i]="";
            			//}
            		}
            		catch (Exception e)
            		{
            			//Od verze 131 neni vyuzivano - rezerva pro debug mode
            			/*if (!(online)) {
            				if (i==0){
            					System.out.println(">>>Error reading parameter NADPISRADKYCAPTION");
            					}else { System.out.println(">>>Error reading parameter NADPISRADKY"+i+"CAPTION");}
            			}
            			*/
            		}
				}//for
    		}//if

    		//System.out.println("headlinesmax="+headlinesmax+" imgmax="+imgmax+" numoftextfieldsmax="+numoftextfieldsmax+" numofblockcols="+numofblockcolsmax+" reverseorder="+reverseorder+" textpred="+textpred+" textza="+textza);

    		String MenuViceStranekCaption=""; //To co predchazi menu s odkazy na dlasi stranku, je-li vice stranek vystupu
            if (fieldcaption==1){
            	try {
            		MenuViceStranekCaption=readConfig(databasecfgname, "MENUVICESTRANEKCAPTION");
           		}
           		catch (Exception e)
           		{
            		if (!(online)) {
            			System.out.println(">>>Error reading parameter MENUVICESTRANEKCAPTION");
           			}
   				}
             }
             //System.out.println("MenuViceStranekCaption="+MenuViceStranekCaption);

    		//Zde se prevadi oddelovac # na +
    		link_prefix=link_prefix.replace('#','+');
    		//System.out.println("link_prefix="+link_prefix);

           	int numofruns=0;
           	int rownum=0; //Aktualni radek
           	String fileOutOld=fileOut;//Uschovani puvodniho jmena souboru
           	//System.out.println("na zacatku fileOutOld="+fileOutOld);
           	int lastSlash=fileOutOld.lastIndexOf('\\');  //Separace fileOut po poslednim /, pro vytvoreni spravneho odkazu pro vicestrankovy vystup
           	String fileOutOldViceStranek=fileOutOld.substring(lastSlash+1);
           	fileOutOldViceStranek=link_prefix+fileOutOldViceStranek;
           	boolean tablefinished=true;
           	boolean lastset=false;
           	boolean neukoncenaradka=false;
            //System.out.println("tablefinished na zacatku="+tablefinished);
            int numofrunsInLastSet=0;

            //Bezi cyklus, dokud nejsou prectene vsechny zaznamy tabulky Reference
            numofrowsWhile:  while (tablefinished){
                //System.out.println ("Cyklus numofrowsWhile");
          		//System.out.println("numofruns=(numofrowsWhile)"+numofruns);


            	if (numofruns>0) {
            		fileOut=fileOutOld+numofruns+"."+outputFileType;
           		}else {fileOut=fileOutOld+"."+outputFileType;}

	            try {

        			if (!(online)) {
        				os = new FileOutputStream(fileOut);
        				dos = new DataOutputStream(os);
        			}
        			else {
        				dos = new DataOutputStream(System.out);
        			}
        			osw = new OutputStreamWriter(dos);
        			out = new BufferedWriter(osw);

        			//Testuje se encoding
        			if (enc.equals(""))	{
        				enc=osw.getEncoding();
        				if (!(online)) {
                        	System.out.println(">>>Error reading parameter ENCODING, probably missing cfg file, setting initial value "+enc);
        				}
        			}
        		}
        		catch (IOException e){
        			System.out.println("\n"+">>>Error creating OutputWriter. File "+fileOut+" is not writable or check the path");
        			notend=false;
        			break theEnd;
        		}
        		//System.out.println("ENCODING="+enc);
        		//Nastaveni Encoding, pokud se nepovede precist z config souboru na hodnotu enc. pocitace, na kterem bezi tento program
        		//je nutne, aby nezhavaroval dos.write(enc)

                //Pokud neni filehlavicka (pri spusteni musi byt parametr nastaven na nofile), tak se nehleda a do vystupniho souboru nic nepridava
                //System.out.println("filehlavicka = "+filehlavicka);
                if (!filehlavicka.equals("nofile")) {
            		try
            		{
            			FileInputStream is = new FileInputStream(filehlavicka);
            			DataInputStream dis = new DataInputStream(is);
            			InputStreamReader in = new InputStreamReader(dis);
            			int b=0;


                        //Od verze 131 neni pouzito, ale prevod bytes na string musi zustat, aby slo pouzit system.out
            			//if (online){
            				while ((b = in.read()) != -1)	{
            					//Nutny prevod na String aby slo pouzit dos.write i pro system.out
            					char bb=(char)b;
            					String bbb=String.valueOf(bb);
            					dos.write((bbb).getBytes(enc));
            				}
            			//}


            			dis.close();
            			is.close();

            		}
            		catch (IOException e){
            			System.out.println(">>>Error when reading data from header file "+filehlavicka);

            		}
                }

        		//Test spravnosti nastaveni kodove stranky
        		 try {
        				//System.out.println("Test spravnosti nastaveni kodove stranky");
        				dos.write(("\n"+"<!--Test spravnosti nastaveni kodove stranky ... "+enc+"//-->"+"\n").getBytes(enc));
        			} catch (IOException e){System.out.println(">>>Unsupported encoding "+enc);
        				//notend=false;
        				break theEnd;

        			}



               	//Set debug mode
                //DriverManager.setLogStream(java.lang.System.out);

        	    try {
              	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        		} catch (ClassNotFoundException e){System.out.println(">>>Java ODBC Driver Not FoundException  "+e);}

        		url = "jdbc:odbc:"+whichURL;



             	 	try {
              			db = DriverManager.getConnection(url);
          	      	}
                  	catch (SQLException e){
            				System.out.println("\n"+">>>Database name not found:"+whichURL);
            				notend=false;
            				break theEnd;
            		}
        			try {
        					//System.out.println("\n"+"Hi");
        					dos.write(("\n"+"\n"+"<!--Zacatek table Data z databaze "+whichURL+"-->"+"\n").getBytes(enc));
        					dos.write(("<DIV CLASS=\"Data Data"+whichURL+" "+whichURL+"\">"+"\n").getBytes(enc));
           					dos.write(("<TABLE CLASS=\"Data Data"+whichURL+" "+whichURL+"\">"+"\n").getBytes(enc));
        					dos.write(("<TR>"+"\n").getBytes(enc));
        			} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

        		//Try pro SQL query for table Sekce (menu)
				try {
        			//Generovani menu
        			int paramlenght = layout.length ();
        			//System.out.println("param length="+paramlenght);
        			layoutchar=layout.charAt(1);
        			//System.out.println("layout char="+layoutchar);
        			String layoutsubstring;

        			try {
        				if (paramlenght>2) {//Testuje se, jestli je za parametrem X,V nebo H jeste dalsi subparametr
        					subparexists=true;
        					char layoutnegchar=layout.charAt(2);
        					if (layoutnegchar == '!') {
        						layoutsubstring=layout.substring(3);
        						negativepar=false;
        					}else {
        						layoutsubstring=layout.substring(2);
        					}
        					//System.out.println("Substring layout="+layoutsubstring);
        					menunumpar=Integer.valueOf(layoutsubstring);
        					//System.out.println("menunumpar="+menunumpar+" negativepar="+negativepar);

        				}//Konec if (paramlenght>2)

        			}catch (Exception e) {System.out.println(">>>Wrong parameter "+layout);}

        			 	try {
        					if  (layoutchar == 'H'||layoutchar == 'V') {
        						dos.write(("<TD class=\"Sekce Sekce"+whichURL+" "+whichURL+" "+TableMenuSQL+"\">"+"\n"+"\n").getBytes(enc));
        						dos.write(("\n"+"\t"+"<!--Zacatek table "+TableMenuSQL+" z databaze "+whichURL+"-->"+"\n").getBytes(enc));
        						dos.write(("\t"+"<DIV CLASS=\"Sekce Sekce"+whichURL+" "+whichURL+" "+TableMenuSQL+"\">"+"\n").getBytes(enc));
        						if  (layoutchar == 'V') {
        							dos.write(("\t"+"<TABLE CLASS=\"Sekce Sekce"+whichURL+" "+whichURL+" "+TableMenuSQL+"\">"+"\n").getBytes(enc));
        						} else if  (layoutchar == 'H') {
        							dos.write(("\t"+"<TABLE CLASS=\"Sekce Sekce"+whichURL+" "+whichURL+" "+TableMenuSQL+"\">"+"\n"+"\t"+"<TR>"+"\n").getBytes(enc));
        						}
        					} else {
        						dos.write(("<TD class=\"Reference Reference"+whichURL+" Reference"+TableSQL+" "+TableSQL+"\">"+"\n").getBytes(enc));
        					}

        		  		} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        				}

        			sq_stmt = db.createStatement();
        			sql_str = "SELECT "+TableMenuSQL+".CisloSekce, "+TableMenuSQL+".Odkaz, "+TableMenuSQL+".MenuObrazek, "+TableMenuSQL+".AltMenuObrazek, "+TableMenuSQL+".MenuText, "+TableMenuSQL+".Publikovat FROM "+TableMenuSQL+" ORDER BY "+TableMenuSQL+".Poradi";

        			rs = sq_stmt.executeQuery(sql_str);
        			while (rs.next()){

        					Boolean Publ=rs.getBoolean("Publikovat");//Tato promenna pouze pro testovani hodnoty pole Publikovat v databazi, muze nabyvat 0 a 1
        					Integer CisloSekce = new Integer (rs.getInt("CisloSekce"));//prevadime int na Integer
        					String obrMenu = rs.getString("MenuObrazek");
        					String obrMenuAlt = rs.getString("AltMenuObrazek");
        					String menu_text = rs.getString("MenuText");
        					String menu_odkaz = rs.getString("Odkaz");
        					String Link="";

        					//System.out.println("subparexists="+subparexists);
                            //System.out.println("Cislo sekce="+CisloSekce+" Publ="+Publ);//test nastaveni pole Publikovat v databazi
                            //System.out.println("menu_odkaz="+menu_odkaz);

        					if (subparexists){
        						if (!negativepar) {
        							if (CisloSekce.equals(menunumpar)) {//nastaveni Publ podle negativpar...rozhodnuti jeatli prislusnou menu polozku zobrazit nebo ne
                                         Publ=negativepar;
               						}
       							}else {
                                    if (CisloSekce.equals(menunumpar)) {//nastaveni Publ podle negativpar...rozhodnuti jeatli prislusnou menu polozku zobrazit nebo ne
                                         Publ=negativepar;
               						} else {Publ=false;}
        						}
        					}
        					//System.out.println("Cislo sekce="+CisloSekce+" negativepar="+negativepar);
        					//System.out.println("Cislo sekce="+CisloSekce+" Vysledny Publ="+Publ);


        			if (Publ) {	//zobrazit jen ty menu polozky, ktere maji Publ=true at uz z pole publikovat v databazi nebo z parametru v e spoustecim batchi

        				//System.out.println("delam sekci "+CisloSekce);
        				//System.out.println("link_prefix="+link_prefix);
        				//System.out.println("menu_odkaz="+menu_odkaz);
        				Link=testLink (menu_odkaz, link_prefix);
        				
        				//System.out.println("Vytvoreni odkazu menu");
        				if (obrMenu!=null) {
        					if (menu_text==null) {menu_text="";}

        					if (obrMenuAlt==null) {

        						try {
        							if  (layoutchar == 'V') {
        								dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"Menu MenuCisloSekce"+CisloSekce+"\">"+"\n"+"\t"+"<A class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" href=\""+Link+"\"><IMG class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" name=Image"+CisloSekce+" SRC=\""+obrMenu+"\" ALT=\""+menu_text+"\"></A>"+"\n"+"\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n").getBytes(enc));
        							} else if  (layoutchar == 'H') {
        								dos.write(("\t"+"<TD class=\"Menu MenuCisloSekce"+CisloSekce+"\">"+"\n"+"\t"+"<A class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" href=\""+Link+"\"><IMG class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" name=Image"+CisloSekce+" SRC=\""+obrMenu+"\" ALT=\""+menu_text+"\"></A>"+"\n"+"\t"+"</TD>"+"\n").getBytes(enc));
        							}
        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        						}

        					} else {
        						try {
        							if  (layoutchar == 'V') {
        								dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"Menu MenuCisloSekce"+CisloSekce+"\">"+"\n"+"\t"+"<A class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" href=\""+Link+"\""+"\n"+"onmouseout=MM_swapImgRestore()"+"\n"+"onmouseover=\"MM_swapImage('document.Image"+CisloSekce+"','document.Image"+CisloSekce+"','"+obrMenuAlt+"', '# "+CisloSekce+" ' )\"><IMG class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" name=Image"+CisloSekce+" SRC=\""+obrMenu+"\" ALT=\""+menu_text+"\"></A>"+"\n"+"\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n").getBytes(enc));
        							} else if  (layoutchar == 'H') {
        								dos.write(("\t"+"<TD class=\"Menu MenuCisloSekce"+CisloSekce+"\">"+"\n"+"\t"+"<A class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" href=\""+Link+"\""+"\n"+"onmouseout=MM_swapImgRestore()"+"\n"+"onmouseover=\"MM_swapImage('document.Image"+CisloSekce+"','document.Image"+CisloSekce+"','"+obrMenuAlt+"', '# "+CisloSekce+" ' )\"><IMG class=\"MenuObrazek MenuObrazekCisloSekce"+CisloSekce+"\" name=Image"+CisloSekce+" SRC=\""+obrMenu+"\" ALT=\""+menu_text+"\"></A>"+"\n"+"\t"+"</TD>"+"\n").getBytes(enc));
        							}

        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        						}
        					}


        				}else {//ELSE od if (obrMenu!=null)

        					if (menu_text!=null) {

        						try {
        							if  (layoutchar == 'V') {
        								dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"Menu MenuCisloSekce"+CisloSekce+"\">"+"\n"+"\t"+"<A class=\"MenuText MenuTextCisloSekce"+CisloSekce+"\" href=\""+Link+"\">"+menu_text+"</A>"+"\n"+"\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n").getBytes(enc));
        							} else if  (layoutchar == 'H') {
        								dos.write(("\t"+"<TD class=\"Menu MenuCisloSekce"+CisloSekce+"\">"+"\n"+"\t"+"<A class=\"MenuText MenuTextCisloSekce"+CisloSekce+"\" href=\""+Link+"\">"+menu_text+"</A>"+"\n"+"\t"+"</TD>"+"\n").getBytes(enc));
        							}
        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        						}
        					}//Konec if (menu_text!=null)
        				}//Konec if (obrMenu!=null)
        			}//Konec if (Publ)

        			}//Konec while
        			//System.out.println("TU");

        			try {
        				if  (layoutchar == 'V') {
        					dos.write(("\t"+"</TABLE>"+"\n").getBytes(enc));
        				} else if  (layoutchar == 'H') {
        					dos.write(("\t"+"</TR>"+"\n"+"\t"+"</TABLE>"+"\n").getBytes(enc));
        				}//Konec table menu

        				if  (layoutchar == 'H'||layoutchar == 'V') {
        					dos.write(("\t"+"</DIV>"+"\n").getBytes(enc));
        					dos.write(("\t"+"<!--Konec table "+TableMenuSQL+" z databaze "+whichURL+"-->"+"\n"+"\n").getBytes(enc));
        				}
        			}catch (IOException e){
        				//System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        			}

        			//Nova TR nebo nova TD
        			try {
        				if  (layoutchar == 'V') {
        					dos.write(("</TD>"+"\n"+"<TD CLASS=\"Reference Reference"+whichURL+" Reference"+TableSQL+" "+TableSQL+"\">"+"\n"+"\n").getBytes(enc));
        				} else if  (layoutchar == 'H') {
        					dos.write(("</TD>"+"\n"+"</TR>"+"\n"+"<TR>"+"\n"+"<TD CLASS=\"Reference Reference"+whichURL+" Reference"+TableSQL+" "+TableSQL+"\">"+"\n"+"\n").getBytes(enc));
        				}
        			}catch (IOException e){
        				//System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        			}

                } catch (SQLException e){System.out.println(">>>Database query error =SQL query for table Sekce (menu)= - field names in the table "+TableMenuSQL+" of database "+whichURL+" may not be correct names, might be missing or this database may be locked or corrupted");}

                //Sql pro NazevSekce a BlokUvod for table Reference
				try {

        			//Otevreni tabulky Reference
        			try {
        				dos.write(("\n"+"\n"+"\t"+"<!--Zacatek table Reference "+TableSQL+" z databaze "+whichURL+ "-->"+"\n").getBytes(enc));
        				dos.write(("\t"+"<DIV class=\"Reference Reference"+whichURL+" Reference"+TableSQL+" "+TableSQL+"\">"+"\n").getBytes(enc));
        				dos.write(("\t"+"<TABLE class=\"Reference Reference"+whichURL+" Reference"+TableSQL+" "+TableSQL+"\">"+"\n").getBytes(enc));
        			}catch (IOException e){
        				//System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        			}
        			//System.out.println ("Reference");

        			//Generovani NazevSekce a BlokUvod
        			//Pokud je misto service_num x, je service_num=0 (default)
        			//System.out.println ("service_num pred Blokem Uvod="+service_num);

        			sq_stmt = db.createStatement();



					sql_str = "SELECT "+TableMenuSQL+".CisloSekce, "+TableMenuSQL+".NazevSekce, "+TableMenuSQL+".BlokUvod, "+TableMenuSQL+".UvodObr FROM "+TableMenuSQL+" WHERE "+TableMenuSQL+".CisloSekce="+service_num;

        			rs = sq_stmt.executeQuery(sql_str);
        			//System.out.println ("DUP");
        			while (rs.next()){

        				int CisloSekce = rs.getInt("CisloSekce");
        				String Nadpis = rs.getString("NazevSekce");
        				String uvodniText = rs.getString("BlokUvod");
        				String uvodniObr = rs.getString("UvodObr");

        				//System.out.println ("HUP");

        				if (Nadpis!=null) {

							int htagnum=1;
        					try {
        						dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"TableNazevSekce TableNazevSekceCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
								dos.write(("\t"+"\t"+"<TABLE class=\"NazevSekce NazevSekceCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
        						dos.write(("\t"+"\t"+"<TR>"+"\n"+"\t"+"\t"+"<TD class=\"NazevSekce NazevSekceCisloSekce"+CisloSekce+"\">"+"\n"+"<H"+htagnum+" class=\"CisloSekce"+CisloSekce+"\">"+Nadpis+"</H"+htagnum+">"+"\n"+"\t"+"\t"+"</TD>"+"\n"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
                                dos.write(("\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
                                dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n").getBytes(enc));

        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        					}
        				}
        				if (uvodniText!=null) {
        					try {
        						dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"TableBlokUvod TableBlokUvodCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
								dos.write(("\t"+"\t"+"<TABLE class=\"BlokUvod BlokUvodCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
        						dos.write(("\t"+"\t"+"<TR>"+"\n"+"\t"+"\t"+"<TD class=\"BlokUvod BlokUvodCisloSekce"+CisloSekce+"\">"+"\n"+uvodniText+"\n"+"\t"+"\t"+"</TD>"+"\n"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
                                dos.write(("\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
                                dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n").getBytes(enc));

        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        					}
        				}

        				if (uvodniObr!=null) {
        					try {
        						dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"TableUvodObr TableUvodObrCisloSekce"+CisloSekce+"\" COLSPAN="+numofblockcolsmax+">"+"\n").getBytes(enc));
                                dos.write(("\t"+"\t"+"<TABLE class=\"UvodObr UvodObrCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
        						dos.write(("\t"+"\t"+"<TR>"+"\n"+"\t"+"\t"+"<TD class=\"UvodObr UvodObrCisloSekce"+CisloSekce+"\">"+"<IMG  src=\""+uvodniObr+"\" CLASS=\"UvodObr UvodObrCisloSekce"+CisloSekce+"\" ALT=\"Uvodni obrazek cislo sekce "+CisloSekce+"\">"+"</TD>"+"\n"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
                                dos.write(("\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
                                dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n").getBytes(enc));
                                
        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        					}
        				}
	                	//Generovani tabulky Telo -zacatek
        				//System.out.println ("DUP");

            				try {
            					dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"SkupinaTelo SkupinaTeloCisloSekce"+CisloSekce+"\">"+"\n"+"\n").getBytes(enc));
            					dos.write(("\t"+"\t"+"<!--Zacatek bloku pro SkupinaTelo -->"+"\n").getBytes(enc));
            					dos.write(("\t"+"\t"+"<DIV class=\"SkupinaTelo SkupinaTeloCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
            					dos.write(("\t"+"\t"+"<TABLE class=\"SkupinaTelo SkupinaTeloCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
            				} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        				}
        				probehlo=true;//indikator, zda doslo k pruchodu timto blokem, pokud ne, nevytvorilo by se DIV skupinatelo





        			}//Konec while
        			//Generovani tabulky Telo pro pripad, ze nebylo vytvoreno v prechozim bloku kvuli publikovat!=1
        			//System.out.println ("DUP");
        				if (!probehlo){//indikator, zda doslo k pruchodu timto blokem, pokud ne, nevytvorilo by se DIV skupinatelo
        					Integer CisloSekce = service_num;
        					try {
        						dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD class=\"SkupinaTelo SkupinaTeloCisloSekce"+CisloSekce+"\">"+"\n"+"\n").getBytes(enc));
        						dos.write(("\t"+"\t"+"<!--Zacatek bloku pro SkupinaTelo -->"+"\n").getBytes(enc));
        						dos.write(("\t"+"\t"+"<DIV class=\"SkupinaTelo SkupinaTeloCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
        						dos.write(("\t"+"\t"+"<TABLE class=\"SkupinaTelo SkupinaTeloCisloSekce"+CisloSekce+"\">"+"\n").getBytes(enc));
        					} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        					}
        				}

        			//Generovani nadpisu a uvodu - konec


                } catch (SQLException e){System.out.println(">>>Database query error =Sql pro NazevSekce a BlokUvod=- field names in the table "+TableMenuSQL+" of database known by the ODBC name "+whichURL+" might not have correct names, might be missing or this database may be locked or corrupted."+"\n"+" For programme versions 1.3 and above the field \"UvodObr\" must be included into the database table mantioned above. Current version is "+ver+".");}
                //System.out.println ("numofrunsInLastSet="+numofrunsInLastSet);

				//SQL pro vse z tabulky Reference
				try {

        			//Generovani textu a obrazku
        			sq_stmt = db.createStatement();

        	     		//String sql_str = "SELECT * FROM "+TableMenuSQL+" WHERE ("+TableMenuSQL+".CisloSekce Like '%"+find_str+"%')";


        			/*Nasledujici SQL je vytvoreno tak, aby, kdyz prislusny parametr neexistuje, aby vubec nebyl v prikazu SQL, protoze SQL s prazdnymi parametry nefunguje. Proto jsou vsechyn metaSQL prohormade mezi nimi nesmi byt carka a posledni pred FROM nesmi byt meta (udelala by se tam carka), mezera a carka na konci pridavani argumentu do SQL stringu (pole) je nutna, oddeluje pole v sql_str
        			*/
        			//Tvorba meta SQL pro nadpisy (aby odpovidalo NUMOFHEADLINES v .cfg)
        			String NadpisySQL="";
        			//Promenna String TableSQL je definova globalne na zacatku programu


        			for  (int i=0; i<=headlinesmax; i++) {
        				if (i==0) {NadpisySQL=NadpisySQL+TableSQL+".NadpisRadky, ";}
        				else {NadpisySQL=NadpisySQL+TableSQL+".NadpisRadky"+i+", ";}
        			}
        			//System.out.println ("Nadpisy SQL");

        			//Tvorba meta SQL pro obrazky (aby odpovidalo NUMOFIMAGES v .cfg
        			String ObrazkySQL="";
        			String OdkazObrSQL="";
        			String NadpisObrSQL="";
        			String PopisObrSQL="";
        			String SirkaObrSQL="";
        			String VyskaObrSQL="";
        			//System.out.println ("Meta DUP");
        			for  (int i=0; i<=imgmax; i++) {
        				//Uprava cyklu pro  obr - odstranen obr bez [i]

						//System.out.println ("SQL DUP");
						ObrazkySQL=ObrazkySQL+TableSQL+".Obr"+i+", ";
						OdkazObrSQL=OdkazObrSQL+TableSQL+".OdkazObr"+i+", ";
						NadpisObrSQL=NadpisObrSQL+TableSQL+".NadpisObr"+i+", ";
						PopisObrSQL=PopisObrSQL+TableSQL+".PopisObr"+i+", ";
						SirkaObrSQL=SirkaObrSQL+TableSQL+".SirkaObr"+i+", ";
						VyskaObrSQL=VyskaObrSQL+TableSQL+".VyskaObr"+i+", ";

						//System.out.println ("ObrazkySQL="+ObrazkySQL);
						//System.out.println ("OdkazObrSQL="+OdkazObrSQL);
						//System.out.println ("NadpisObrSQL="+NadpisObrSQL);
						//System.out.println ("PopisObrSQL="+PopisObrSQL);
						//System.out.println ("SirkaObrSQL="+SirkaObrSQL);
						//System.out.println ("VyskaObrSQL="+VyskaObrSQL);


        			}
                    //System.out.println ("TADY");
        			//Tvorba meta SQL spolecne pro pole Text  TextPred a TextZa
        			String TextSQL="";
        			String OdkazTextSQL="";
        			String OdkazKamSQL="";
        			String NameofTextField="";
        			int lastj=2;//tolikrat kolik muze byt text skupin poli v databazi (0=Text az Texti, 1=TextPred, 2=TextZa)


        			int lasti;
        			for  (int j=0; j<=lastj; j++) {
        				lasti=0;
        				if (j==0){
        					lasti=numoftextfieldsmax+1;//v pripade ze by bylo vic poli Text, sem se doplni prislusna hodnota z cfg file
                            NameofTextField="Text";
        				//System.out.println ("lasti="+lasti);

        				}//PoleText

        				if (j==1){
        					if (textpred!=0) {
        						lasti=textpred;//v pripade ze by bylo vic poli TextPred, sem se doplni prislusna hodnota z cfg file
        						NameofTextField="TextPred";
        					}
        				//System.out.println ("lasti="+lasti);
        				}//PoleTextPred

        				if (j==2){
        					if (textza!=0) {
        						lasti=textza;//v pripade ze by bylo vic poli TextPred, sem se doplni prislusna hodnota z cfg file

        						NameofTextField="TextZa";
        					}
        				//System.out.println ("lasti="+lasti);
        				}//PoleTextZa

        				//System.out.println ("Meta DUP");
        				for  (int i=0; i<lasti; i++) {
        					//System.out.println ("SQL JUP");
        					//System.out.println ("1. i="+i+" j="+j);
       					 	//Uprava cyklu pro  text - odstranen text bez [i]
    						TextSQL=TextSQL+TableSQL+"."+NameofTextField+i+", ";
    						OdkazTextSQL=OdkazTextSQL+TableSQL+".Odkaz"+NameofTextField+i+", ";
    						OdkazKamSQL=OdkazKamSQL+TableSQL+".OdkazKam"+NameofTextField+i+", ";

        				}//Konec for pro i
        				j=j++;
        				//System.out.println ("2. j="+j);

        			}//Konec for pro j


        			//System.out.println ("TextSQL="+TextSQL);
        			//System.out.println ("OdkazTextSQL="+OdkazTextSQL);
        			//System.out.println ("OdkazKamSQL="+OdkazKamSQL);

        			//Razeni zaznamu
        			String descPoradi=new String("");
        			String descNadpisRadky=new String("");

        			if (reverseorder==1) { descPoradi="DESC";  descNadpisRadky="";}
        			else if (reverseorder==2) { descPoradi="";  descNadpisRadky="DESC";}
        			else if (reverseorder==3) { descPoradi="DESC";  descNadpisRadky="DESC";}
        			//System.out.println ("reverseorder ="+reverseorder+", descPoradi ="+descPoradi+", descNadpisRadky ="+descNadpisRadky);




        			//Pokud je misto service_num x, bere se service_num=0 pro tabulku Sekce a vsechno z tabulky Reference
        			//proto zde musi byt dva ruzne SQL dotazy, ten prvni je bez CislaSekce, druhy (po else) normalni

        			if (service_string.equals("x")){
        				sql_str = "SELECT "+NadpisySQL+" "+ObrazkySQL+" "+OdkazObrSQL+" "+NadpisObrSQL+" "+PopisObrSQL+" "+SirkaObrSQL+" "+VyskaObrSQL+" "+TextSQL+" "+OdkazTextSQL+" "+OdkazKamSQL+" "+TableSQL+".CisloSekce, "+TableSQL+".Poradi FROM "+TableSQL+" WHERE "+TableSQL+".Publikovat=1 ORDER BY "+TableSQL+".CisloSekce, "+TableSQL+".Poradi "+descPoradi+", "+TableSQL+".NadpisRadky "+descNadpisRadky;

        			}else {
        				sql_str = "SELECT "+NadpisySQL+" "+ObrazkySQL+" "+OdkazObrSQL+" "+NadpisObrSQL+" "+PopisObrSQL+" "+SirkaObrSQL+" "+VyskaObrSQL+" "+TextSQL+" "+OdkazTextSQL+" "+OdkazKamSQL+" "+TableSQL+".CisloSekce, "+TableSQL+".Poradi FROM "+TableSQL+" WHERE "+TableSQL+".Publikovat=1 AND "+TableSQL+".CisloSekce="+service_num+" ORDER BY "+TableSQL+".CisloSekce, "+TableSQL+".Poradi "+descPoradi+", "+TableSQL+".NadpisRadky "+descNadpisRadky;

        			}

        			//System.out.println ("sql_str ="+sql_str);

        			rs = sq_stmt.executeQuery(sql_str);


        					int pocetporadi=1;//Pocet radek z jednoho behu nextWhile pro potrebu NUMOFBLOCKCOLS
        					neukoncenaradka=false;
        					String lastNadpsiRadky=null;
                            int numofrunsInOneSet=0;
                            int lastrownum=rownum; //posun kurzorem o jednu radku, aby se na nove strance neopakovala posledni polozka z minule stranky
  							//System.out.println ("rownum PRED nextwhile ="+rownum);
  							//System.out.println ("lastrownum PRED nextwhile ="+lastrownum);
  							//System.out.println ("numofblockrowsmax="+numofblockrowsmax);

  							int lastnumofblockrowsmax=numofblockrowsmax;
                            if (numofblockrowsmax==0) {numofblockrowsmax=1;}//Opatreni proti numofblockrowsmax=0=>nekonecny vvypis

        			nextWhile:  while (rownum-lastrownum<numofblockrowsmax*numofblockcolsmax){
                            numofrunsInOneSet=numofrunsInOneSet+1;
                            //System.out.println ("numofrunsInOneSet="+numofrunsInOneSet);

                            //System.out.println ("Cyklus nextWhile");
                            if (lastnumofblockrowsmax==0)  {numofblockrowsmax=numofblockrowsmax+1;}//Opatreni proti numofblockrowsmax=0=>nekonecny vvypis


                            if ((lastrownum==0)&&(!lastset)) {
  								lastrownum=-1;
                                lastset=true;
							} //Opatreni pro prvni beh, aby se zaclo od prvniho zaznamu, ale nedoslo k nekonecne smycce, kdyz je numofblockcols=1 a numofblockrols=1

                            rownum=rs.getRow();
							//System.out.println ("rownum za nextwhile ="+rownum);
                            tablefinished=(rs.next());
                            numofrunsInLastSet=numofrunsInOneSet-(numofruns*numofblockcolsmax*numofblockrowsmax);

							if (rownum>lastrownum) {
						 		//System.out.println ("tiskneme ");
								String strednik=";";
            					String carka=",";
            					String Nadpis[]= new String [headlinesmax+1];//Pocet nadpisu v radce plus jeden nad
            					String fieldnameNadpis[]= new String [headlinesmax+1];//Pocet nadpisu v radce plus jeden nad
            					String Textfield[] = new String [numoftextfieldsmax+1];
            					String LinkString[] = new String [numoftextfieldsmax+1];
            					String LinkTo[] = new String [numoftextfieldsmax+1];
            					String Img[]=new String[imgmax+1];
            					String LinkImg[]=new String[imgmax+1];
            					String NadpisImg[]=new String[imgmax+1];
            					String PopisImg[]=new String[imgmax+1];
            					int SirkaImg[]=new int[imgmax+1];
            					int VyskaImg[]=new int[imgmax+1];
             					int poradi = rs.getInt("Poradi");





            					//Zacatek bloku pro jedno poradi
            					//System.out.println ("Pocet poradi="+pocetporadi);
            					//System.out.println ("Pocet poradi="+pocetporadi+" numofblockcolsmax="+numofblockcolsmax);
            					try {

            						if ((pocetporadi<=numofblockcolsmax)&&(pocetporadi!=1)){
            							dos.write(("\t"+"\t"+"<TD class=\"Poradi Poradi"+poradi+"\">"+"\n"+"\n").getBytes(enc));
            							//System.out.println ("1.if-mensi");
                                    }else{
            							dos.write(("\t"+"\t"+"<TR CLASS=\"mezi jednotlivymi poradi\">"+"\n"+"\t"+"\t"+"<TD class=\"Poradi Poradi"+poradi+"\">"+"\n"+"\n").getBytes(enc));
            							//System.out.println ("1.if-vetsi nebo rovno 1");

            						}
                                    dos.write(("\t"+"\t"+"\t"+"<!--Zacatek bloku pro poradi="+poradi+"-->"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"<TABLE class=\"Poradi Poradi"+poradi+"\">"+"\n").getBytes(enc));
        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}


								//Blok nadpisu
								try {
            						dos.write(("\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"<TD class=\"SkupinaNadpisRadky SkupinaNadpisRadkyPoradi"+poradi+"\">"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"\t"+"<TABLE class=\"SkupinaNadpisRadky SkupinaNadpisRadkyPoradi"+poradi+"\">"+"\n").getBytes(enc));
                                    dos.write(("\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));

            					} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

            					//Nadpis0
            					int i=0;
            					//v nasledujicim radku se nastavuje HTML H tag na H2...Hn podle toho, jestli je NadpisRadky0=H2 a ostatni na H3
            					int htagnum=2;

            					Nadpis[i] = rs.getString("NadpisRadky");
            					if (fieldcaption==1) {
            						fieldnameNadpis[i]=NadpisRadkyCaption[i];
            					}else if (fieldcaption==2){
            						fieldnameNadpis[i]="NadpisRadky";
                                }

								try {
									if (Nadpis[i]!=null) {//NadpisRadky je unikatni, porovnava se podle nej nadpis radku a pokud je stejny uz se nezobrazuje znovu

            							if  (!(Nadpis[i].equals(lastNadpsiRadky))) {

            								lastNadpsiRadky=Nadpis[i];//Hlavni nadpis i=0
            								int colspan=headlinesmax;
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"PodskupinaNadpisRadky PodskupinaNadpisRadkyPoradi"+poradi+"\">" +"\n").getBytes(enc));
            								//Otevreni tabulky nadpisu0
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"NadpisRadky NadpisRadky"+i+" NadpisRadky"+i+"Poradi"+poradi+"\">" +"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
            								if ((fieldcaption>0)&&(fieldcaption<3)&&(NadpisRadkyCaption[i]!="")){
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"NadpisRadkyCaption NadpisRadkyCaption"+i+" NadpisRadkyCaption"+i+"Poradi"+poradi+"\">"+ fieldnameNadpis[i]+"</TD>"+"\n").getBytes(enc));
            								}
            								
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"NadpisRadky NadpisRadky"+i+" NadpisRadky"+i+"Poradi"+poradi+"\">"+"<H"+htagnum+" class=\"Poradi"+poradi+"\">"+ Nadpis[i]+"</H"+htagnum+">"+"</TD>"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>" +"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
                                            dos.write(("\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));


            							}
            						}
            					} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

                                //Nadpisy[i]
            					FieldExists=false;
            					//v nasledujicim radku se nastavuje HTML H tag na H2...Hn podle toho, jestli je NadpisRadky0=H2 a ostatni na H3
            					htagnum=3;
            					for  (i=1; i<=headlinesmax; i++) {
                                        //Testuje se existence alespon jednoho z podnadpisu, jinak se vubec tabulka podnadpisu negeneruje
            							Nadpis[i] = rs.getString("NadpisRadky"+i); //podnadpisy
            							if (Nadpis[i]!=null){
            								FieldExists=true;
            							} else {Nadpis[i]="";}
            					} //konec for

            					if (FieldExists){
                					try {
                						i=1;
                						int lastsplit=0;//promenna pro oznaceni skupin podnandpisu na zvlastni radce pro styly
                						dos.write(("\t"+"\t"+"\t"+"\t"+"<TD class=\"PodskupinaNadpisRadky PodskupinaNadpisRadky"+(lastsplit+1)+" PodskupinaNadpisRadky"+(lastsplit+1)+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
                                       	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"PodskupinaNadpisRadky PodskupinaNadpisRadky"+(lastsplit+1)+" PodskupinaNadpisRadky"+(lastsplit+1)+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
                                       	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
                						for  (i=1; i<=headlinesmax; i++) {

											dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"TableNadpisRadky TableNadpisRadky"+i+" TableNadpisRadky"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));

                							if (fieldcaption==1) {
            									fieldnameNadpis[i]=NadpisRadkyCaption[i];
            								}else if (fieldcaption==2){
            									fieldnameNadpis[i]="NadpisRadky"+i;
                                			}dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"NadpisRadky NadpisRadky"+i+" NadpisRadky"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
                                         	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));

                                			if ((fieldcaption>0)&&(fieldcaption<3)&&(NadpisRadkyCaption[i]!=""))//mozne varianty fieldcaption viz cfg soubor
                                			{
                                				//System.out.println ("ZDE");
                                				if (splitheadlines==1) { //1=datura algoritmus - pridava /TR a TR za CAPTION pro nadpis[8]=kalendar, tak aby caption bylo nad daty
                                					if (i>=8){ //- pridava /TR a TR za CAPTION pro nadpis[8]

                                						//System.out.println ("TADY");
                                						dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"NadpisRadkyCaption NadpisRadkyCaption"+i+" NadpisRadkyCaption"+i+"Poradi"+poradi+"\">"+ fieldnameNadpis[i]+"</TD>"+"\n").getBytes(enc));
                               							dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
                               							dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
                               						} else {
           											//System.out.println ("ELSE");
           											dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"NadpisRadkyCaption NadpisRadkyCaption"+i+" NadpisRadkyCaption"+i+"Poradi"+poradi+"\">"+ fieldnameNadpis[i]+"</TD>"+"\n").getBytes(enc));
           											}
           										}
            								}
                                           	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"<TD class=\"NadpisRadky NadpisRadky"+i+" NadpisRadky"+i+"Poradi"+poradi+"\">"+"<H"+htagnum+" class=\"Poradi"+poradi+"\">"+ Nadpis[i]+"</H"+htagnum+">"+"</TD>"+"\n").getBytes(enc));
                                            dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
			                        		dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));

			       							if (splitheadlines==1) {//1=datura algoritmus
			       								if ((i==4)||(i==7)||(i==19)) { //1=datura algoritmus - pridava /TR a TR za nadpisy zde explicitne uvedene
			       								lastsplit=lastsplit+1;
			       								//System.out.println ("Split headlines i="+i);
			       									if (i<headlinesmax){//Ochrana proti pradnemu radku, pokud dojde k deleni zrovna na poslednim i
    		       										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
    		       										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
    		                        				 	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
    		                        				 	dos.write(("\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
    			       									dos.write(("\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
    			       									dos.write(("\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
    			       									dos.write(("\t"+"\t"+"\t"+"\t"+"<TD class=\"PodskupinaNadpisRadky PodskupinaNadpisRadky"+(lastsplit+1)+" PodskupinaNadpisRadky"+(lastsplit+1)+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
    	    	                                      	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"PodskupinaNadpisRadky PodskupinaNadpisRadky"+(lastsplit+1)+" PodskupinaNadpisRadky"+(lastsplit+1)+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
        	    	                                 	dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));

    	    	                                 	}
		       									}
			       							}

										}
										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
			       						dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
			       						dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>" +"\n").getBytes(enc));
									} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
                                }//konec if

                                try {
                                	dos.write(("\t"+"\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
                                	dos.write(("\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
                                	dos.write(("\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
                                    dos.write(("\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
            					} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
            					//Konec bloku nadpisu


                                //Zacatek bloku TextObr
            					try {
                                       		dos.write(("\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"<TD class=\"SkupinaTextObr SkupinaTextObrPoradi"+poradi+"\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<TABLE class=\"TextObr TextObrPoradi"+poradi+"\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));

                 				} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
            					//Blok Obr0 -je stejny jako pro Img1-Imgmax, pouze for je od 0 do 0


            					//Prace s obrazky
            					//Pouziva volnou promennou i


            					//Testujeme existenci Obr0
            					FieldExists=false;

            					for  (i=0; i<=0; i++) {

            						Img[i] = rs.getString("Obr"+i);
            						if (Img[i]!=null) {FieldExists=true;}
            						LinkImg[i] = rs.getString("OdkazObr"+i);

             						//Zde spravny odkaz
           							//System.out.println("link_prefix="+link_prefix);
               						//System.out.println("LinkImg["+i+"]="+LinkImg[i]);
               						Img[i]=testLink (Img[i], link_prefix);
                                	//System.out.println("Img["+i+"] po uprave je: "+Img[i]);
                                	LinkImg[i]=testLink (LinkImg[i], link_prefix);
                                	//System.out.println("LinkImg["+i+"] po uprave je: "+LinkImg[i]);

            						//System.out.println ("OdkazObr"+i+"="+LinkTo[i]);
            						NadpisImg[i] = rs.getString("NadpisObr"+i);
            						PopisImg[i] = rs.getString("PopisObr"+i);
            						if (NadpisImg[i]!=null) {
            							FieldExists=true;
            						}else {NadpisImg[i]="";}
            						if (PopisImg[i]!=null) {
            							FieldExists=true;
            						}else {PopisImg[i]="";}
            						SirkaImg[i] = rs.getInt("SirkaObr"+i);
            						VyskaImg[i] = rs.getInt("VyskaObr"+i);

            						//if (SirkaImg[i]==0) {SirkaImg[i]=620;}
            						//if (VyskaImg[i]==0) {VyskaImg[i]=620;}
            						//System.out.println ("SirkaObr"+i+"="+SirkaImg[i]);
            						//System.out.println ("VyskaObr"+i+"="+VyskaImg[i]);


            						//System.out.println ("Poradi="+poradi+" ImgExists="+FieldExists);



            						if (FieldExists){
            							try {

            								if (Img[i]!=null) {
            									dos.write(("\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"TableObr TableObr"+i+" TableObr"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\">" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"NadpisObr NadpisObr"+i+" NadpisObr"+i+"Poradi"+poradi+"\">"+ NadpisImg[i]+"</TD>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));

            									if (LinkImg[i]!=null){
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\">"+"<A CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\" TARGET=\"1\"  onclick=\"openNewWindow1(w="+SirkaImg[i]+",h="+VyskaImg[i]+",scrl=0,tgt=1)\" HREF=\""+LinkImg[i]+"\"><IMG  src=\""+Img[i]+"\" CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\" ALT="+PopisImg[i]+"></A>"+"</TD>"+"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
            									} else {
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\">"+"<IMG  src=\""+Img[i]+"\" CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\" ALT=\""+PopisImg[i]+"\">"+"</TD>"+"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
            									}//Konec if pro LinkImg[i]!=null
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"PopisObr PopisObr"+i+" PopisObr"+i+"Poradi"+poradi+"\">"+PopisImg[i]+"</TD>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
            								}//Konec if pro Img[i]!=null

            							} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
            						}//If FieldExists
            					}//Konec for pro obr0
            					//Konec bloku Obr0
                                //System.out.println ("ZDE");

            					//Jen text
            					FieldExists=false;
            					for  (i=0; i<=numoftextfieldsmax; i++) {
                                       //Uprava cyklu pro  text - odstranen text bez [i]
        								Textfield[i] = rs.getString("Text"+i);
        								LinkString[i] = rs.getString("OdkazText"+i);
        								LinkTo[i] = rs.getString("OdkazKamText"+i);
        								
        								//Testuj odkaz a pridej link_prefix
                                       // System.out.println("link_prefix="+link_prefix);
                                       // System.out.println("LinkTo["+i+"]="+LinkTo[i]);
                                        //System.out.println ("OdkazText"+i+"="+LinkString[i]+"OdkazKamText"+i+"="+LinkTo[i]);
                                        LinkTo[i]=testLink (LinkTo[i], link_prefix);
                                       // System.out.println("LinkTo["+i+"] po uprave="+LinkTo[i]);
            							if (Textfield[i]!=null) {FieldExists=FieldExists||true;}//Log. oper. nebo, aby se poznalo, ze bylo 1 a nepremazalo pri dalsim behu
                                        //if (Textfield[i]!=null) {FieldExists=true;}
                                        
            					if (Textfield[i]!=null){
            						try {
            							//System.out.println ("i="+i);
                                        //System.out.println ("TextField"+i+"="+Textfield[i]);
            							if (i>0){
            									dos.write(("\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
            									//Konec tabulky pro odstavec
            									dos.write(("\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"</TR CLASS=\"meziodstavci\">"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"<TR CLASS=\"meziodstavci\">"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"<TD CLASS=\"SkupinaText SkupinaText"+i+" SkupinaText"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
                                                //Nova tabulka pro odstavec
                            					dos.write(("\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"TableText TableText"+i+" TableText"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
                                 				dos.write(("\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
           								}


            							//System.out.println ("Tato radka");
            							dos.write(("\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"TableText TableText"+i+" TableText"+i+"Poradi"+poradi+"\">" +"\n").getBytes(enc));
                                        dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"Text Text"+i+" Text"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
            							dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));

            							
            								//volej funkce PRED -vraci DescA
            								DescA=makeAnchor(Textfield[i],LinkTo[i],LinkString[i],"OdkazText OdkazText"+i,poradi);
            								
            								if (Textfield[i]!=null){
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD class=\"Text Text"+i+" TextPoradi"+poradi+"\">"+"\n").getBytes(enc));
             								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<P class=\"Text Text"+i+" TextPoradi"+poradi+"\">"+DescA+"</P>"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
            								}
            							
            							dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
            							dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
            							dos.write(("\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
            							
            						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

            					}//Konec if (FieldExists)
                                }//Konec for
            					//Konec Jen text
            					try {

            						dos.write(("\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));

            					} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}


            					//Konec bloku TextObr

            					try {
            					dos.write(("\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));

            					} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}


            					//TextPred
            					//Pouziva volnou promennou i
            					FieldExists=false;
            					//For pro TextPred 1 az textpred
            					String PopisPred[]=new String[textpred];
            					String LinkStringPred[]=new String[textpred];
            					//String LinkTo[]=new String[textpred];
            					
            					

            					for  (i=0; i<textpred; i++) {


            						//System.out.println("i="+i);
            						//Uprava pro textpred0 - odstranen textpred bez [i]
        							//System.out.println("HUP");
        							//System.out.println("i="+i);

        							PopisPred[i] = rs.getString("TextPred"+i);
        							LinkStringPred[i] = rs.getString("OdkazTextPred"+i);
        							LinkTo[i] = rs.getString("OdkazKamTextPred"+i);
        							
        							//System.out.println("link_prefix="+link_prefix);
                               		//System.out.println("LinkTo["+i+"]="+LinkTo[i]);
                                	LinkTo[i]=testLink (LinkTo[i], link_prefix);
                                	//System.out.println("LinkTo["+i+"] po uprave="+LinkTo[i]);
            						//System.out.println("DUP");
            						
            						if (PopisPred[i]!=null) {FieldExists=FieldExists||true;}//Log. oper. nebo, aby se poznalo, ze bylo 1 a nepremazalo pri dalsim behu

            						if (PopisPred[i]==null) {PopisPred[i]="";}
                                    if (FieldExists) {
            							//volej funkce Za -vraci DescP
            							DescP=makeAnchor(PopisPred[i],LinkTo[i],LinkStringPred[i],"OdkazText OdkazTextPred",poradi);
            							try {
            								dos.write(("\t"+"\t"+"\t"+"</TR CLASS=\"mezitext a textpred\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"<TR CLASS=\"mezitext a textpred\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<TD class=\"Text TextPred TextPred"+i+" TextPred"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<P class=\"Text TextPred TextPred"+i+" TextPred"+i+"Poradi"+poradi+"\">"+DescP+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
                                    	} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
            						}

            					}//Konec For pro TextPred 0 az textpred
            					//Konec TextPred

                                 //For pro obrazky 1 az imgmax
            					FieldExists=false;
            					for  (i=1; i<=imgmax; i++) {
                                    Img[i] = rs.getString("Obr"+i);
            						if (Img[i]!=null) {FieldExists=true;}
            						LinkImg[i] = rs.getString("OdkazObr"+i);
            						//System.out.println ("OdkazObr"+i+"="+LinkImg[i]);
            						NadpisImg[i] = rs.getString("NadpisObr"+i);
            						PopisImg[i] = rs.getString("PopisObr"+i);
            						if (NadpisImg[i]!=null) {
            							FieldExists=true;
            						}else {NadpisImg[i]="";}
            						if (PopisImg[i]!=null) {
            							FieldExists=true;
            						}else {PopisImg[i]="";}
            						SirkaImg[i] = rs.getInt("SirkaObr"+i);
            						VyskaImg[i] = rs.getInt("VyskaObr"+i);
            						
            						//Zde spravny odkaz
           							//System.out.println("link_prefix="+link_prefix);
               						//System.out.println("LinkImg["+i+"]="+LinkImg[i]);
               						Img[i]=testLink (Img[i], link_prefix);
                                	//System.out.println("Img["+i+"] po uprave je: "+Img[i]);
                                	LinkImg[i]=testLink (LinkImg[i], link_prefix);
                                	//System.out.println("LinkImg["+i+"] po uprave je: "+LinkImg[i]);
            						//System.out.println("DUP");

            						//if (SirkaImg[i]==0) {SirkaImg[i]=620;}
            						//if (VyskaImg[i]==0) {VyskaImg[i]=620;}
            						//System.out.println ("SirkaObr"+i+"="+SirkaImg[i]);
            						//System.out.println ("VyskaObr"+i+"="+VyskaImg[i]);
            					}//Konec For  pro obrazky 1 az imgmax

                                 //Vystup pro obr1 az imgmax
                                 if (FieldExists){
                                 	try {
            							//Otevreni radky a tabulky Obr pro obrazky
            						dos.write(("\t"+"\t"+"\t"+"<TR>"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"<TD CLASS=\"SkupinaObr SkupinaObrPoradi"+poradi+"\">"+"\n").getBytes(enc));
                					dos.write(("\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"SkupinaObr SkupinaObrPoradi"+poradi+"\">"+"\n").getBytes(enc));
            						dos.write(("\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
								}
                                for  (i=1; i<=imgmax; i++) {
                                    if (FieldExists){
            							try {

            								dos.write(("\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"TableObr TableObr"+i+" TableObr"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
            								if (Img[i]!=null) {
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TABLE CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\">" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"NadpisObr NadpisObr"+i+" NadpisObr"+i+"Poradi"+poradi+"\">"+ NadpisImg[i]+"</TD>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));

            									if (LinkImg[i]!=null){
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\">"+"<A CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\" TARGET=\"1\"  onclick=\"openNewWindow1(w="+SirkaImg[i]+",h="+VyskaImg[i]+",scrl=0,tgt=1)\" HREF=\""+LinkImg[i]+"\"><IMG  src=\""+Img[i]+"\" CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\" ALT="+PopisImg[i]+"></A>"+"</TD>"+"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
            									} else {
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\">"+"<IMG  src=\""+Img[i]+"\" CLASS=\"Obr Obr"+i+" Obr"+i+"Poradi"+poradi+"\" ALT=\""+PopisImg[i]+"\">"+"</TD>"+"\n").getBytes(enc));
            										dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
            									}//Konec if pro LinkImg[i]!=null

            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TR>" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"<TD CLASS=\"PopisObr PopisObr"+i+" PopisObr"+i+"Poradi"+poradi+"\">"+PopisImg[i]+"</TD>"+"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TR>" +"\n").getBytes(enc));
            									dos.write(("\t"+"\t"+"\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));

            								}//Konec if pro Img[i]!=null

            							dos.write(("\t"+"\t"+"\t"+"\t"+"</TD>" +"\n").getBytes(enc));
            							//System.out.println ("Tato radka");

            							} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
            						}//Konec if (FieldExists)
            					}//Konec for pro obrazky 1 az imgmax

                            	//Konec radky za obrazky
                              	if (FieldExists){
            						try {
                						//Uzavreni tabulky SkupinaObr pro obrazky
                						dos.write(("\t"+"\t"+"\t"+"\t"+"</TR>"+"\n").getBytes(enc));
                						dos.write(("\t"+"\t"+"\t"+"\t"+"</TABLE>" +"\n").getBytes(enc));

                						//Uzavreni TD pro obrazky
                						dos.write(("\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
                						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
                              	}




            					//System.out.println (" DUP");

            					//TextZa
            					//Pouziva volnou promennou i
            					FieldExists=false;
            					//For pro TextZa
            					String PopisZa[]=new String[textza];
            					String LinkStringZa[]=new String[textza];
            					//String LinkToZa[]=new String[textza];

            					for  (i=0; i<textza; i++) {


            							PopisZa[i] = rs.getString("TextZa"+i);
            							LinkStringZa[i] = rs.getString("OdkazTextZa"+i);
            							LinkTo[i] = rs.getString("OdkazKamTextZa"+i);
            							
            							//System.out.println("link_prefix="+link_prefix);
                               			//System.out.println("LinkTo["+i+"]="+LinkTo[i]);
                                		LinkTo[i]=testLink (LinkTo[i], link_prefix);
                                		//System.out.println("LinkTo["+i+"] po uprave="+LinkTo[i]);
            							//System.out.println("DUP");

            						if (PopisZa[i]!=null) {FieldExists=FieldExists||true;}//Log. oper. nebo, aby se poznalo, ze bylo 1 a nepremazalo pri dalsim behu

            						if (PopisZa[i]==null) {PopisZa[i]="";}
                  				    if (FieldExists) {
            							//volej funkce Za -vraci DescP
            							DescP=makeAnchor(PopisZa[i],LinkTo[i],LinkStringZa[i],"OdkazText OdkazTextZa",poradi);
            							try {
            								dos.write(("\t"+"\t"+"\t"+"<TR CLASS=\"mezitextpred a textza\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<TD class=\"Text TextZa TextZa"+i+" TextZa"+i+"Poradi"+poradi+"\">"+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"\t"+"<P class=\"Text TextZa TextZa"+i+" TextZa"+i+"Poradi"+poradi+"\">"+DescP+"\n").getBytes(enc));
            								dos.write(("\t"+"\t"+"\t"+"</TD>"+"\n").getBytes(enc));
            							} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
                   					}
            					}//Konec For pro TextZa
            					//Konec TextZa

            					//System.out.println ("Pocetporadi2="+pocetporadi);
            					//Ukonceni tabulky pro jedno poradi
            					try {
            						dos.write(("\t"+"\t"+"\t"+"</TR CLASS=\"konec poradi\">"+"\n").getBytes(enc));//uzavreni radky pro obrazky nebo pro textza
            						dos.write(("\t"+"\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
                                    dos.write(("\t"+"\t"+"\t"+"<!--Konec bloku pro poradi="+poradi+"-->"+"\n"+"\n").getBytes(enc));


                                 //Pozor asi chyba pri vyhodnoceni poctu bloku, chybela tam radka s TR
            						if (pocetporadi<numofblockcolsmax){
            							dos.write(("\t"+"\t"+"</TD>"+"\n").getBytes(enc));
            							pocetporadi=pocetporadi+1;
            							neukoncenaradka=true;
            							//System.out.println ("mensi "+"pocetporadi="+pocetporadi);
            						}
            						else{
            						    //System.out.println ("Posledni /TD a /TR "+"pocetporadi="+pocetporadi);
            							dos.write(("\t"+"\t"+"</TD>"+"\n"+"\t"+"\t"+"</TR CLASS=\"konec poradi\">"+"\n").getBytes(enc));
                                        pocetporadi=1;

            							//System.out.println ("vetsi nebo rovno");
           							}
								} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

							}//Konec if(rownum)

      				}//Konec nextWhile


        			tablefinished=(rs.next());//jeste jednou posun ukazatelem, aby , kdyby nahodou nasobek rows*cols byl delitelny poctem zaznamu, nevytvorila se jedna prazdna stranka navic
           			//System.out.println ("posun rs.next pred catch SQL + tablefinished="+tablefinished);


            	} catch (SQLException e){
                        //System.out.println ("Jsme pred Generovani tabulky Telo - konec. Doplnuje se /TR, ktere, pokud se skonci chybou tam neni");
            		    //Doplnuje se /TR, ktere, pokud se skonci chybou tam neni
        				//try {
        					//dos.write(("\t"+"\t"+"</TR>"+"\n").getBytes(enc));
						//} catch (IOException f){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}


					if (tablefinished==true){//Podminka, ktera odlisuje, zda se koncilo chybou nebo tim, ze skoncili data - oproti NUMOFCOLS a NUMOFROWS
						System.out.println(">>>Database query error =SQL pro vse z tabulky Reference= - the parametres in the "+whichURL+".cfg file may not match the real number of fields used in the database known by the ODBC name "+whichURL+" in the "+TableSQL+" table (the number of fields specified in "+whichURL+".cfg file must be less or equal the number of fields in the real database  or must not exceed the default number, when the "+whichURL+".cfg does not exist), field names may not be correct or tables "+TableMenuSQL+" and/or "+TableSQL+" in this database may be locked or corrupted");
						//System.out.println ("Skoncilo SQL chybou + tablefinished="+tablefinished);
					}else {
						//System.out.println ("Necelistve nasobky Nevime, jak skoncilo - else + tablefinished="+tablefinished);
					}
					tablefinished=false;//Opatreni proti nekonecnemu cyklu, kdyz neni tabulka data v poradku
 					//System.out.println ("Pro jistotu nastavujem table finished na false... tablefinished="+tablefinished);
 					//Zde nemuze byt zadna chybova hlaska, protoze by se objevovala i pro spravnem ukonceni cyklu - nejsou zadna dalsi data
                }//Catch (SQLException e)

                //System.out.println ("Jsme pred Generovani tabulky Telo - konec");

    			//Generovani tabulky Telo - konec
    			//System.out.println ("Jsme pred Generovani tabulky Telo - konec, pokud  se neskoncilo chybou");
    			try {
    				//System.out.println ("neukoncenaradka="+neukoncenaradka);
    				//test jestli nechybi /TR nebo aby naopak nebylo 2x
    				if (neukoncenaradka)  {dos.write(("\t"+"\t"+"</TR>"+"\n").getBytes(enc));}
    				dos.write(("\t"+"\t"+"</TABLE>"+"\n").getBytes(enc));
    				dos.write(("\t"+"\t"+"</DIV>"+"\n").getBytes(enc));
                    dos.write(("\t"+"\t"+"<!--Konec bloku pro SkupinaTelo -->"+"\n"+"\n").getBytes(enc));
    				dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n"+"\n").getBytes(enc));
   				} catch (IOException f){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}



                 //Otevreni dalsi radky pro menu pokud se zalamuji stranky
      				//pokud menu neni, je radka prazdna
        			try {
        				dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD CLASS=MenuViceStranek colspan="+numofblockcolsmax+">"+"\n"+"\n").getBytes(enc));
        			} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
                    //Menu pri vicestrankvem vystupu (parametry NUMOFBLOCKCOLS a NUMOFBLOCKROWS)
                    if  (layoutchar != 'X') {
                            // if (tablefinished==true){
                                //System.out.println("numofruns=(fileLink)"+numofruns);
                                //System.out.println("dole v menu fileOutOldViceStranek="+fileOutOldViceStranek);
								fileOutPrev=fileOutOldViceStranek+(numofruns-1)+"."+outputFileType;
                                if ((numofruns-1)==0) {fileOutPrev=fileOutOldViceStranek+"."+outputFileType;}
                      	    	fileOutNext=fileOutOldViceStranek+(numofruns+1)+"."+outputFileType;
                                //System.out.println("fileOutPrev=(numofrowsWhile)"+fileOutPrev);
                                //System.out.println("fileOutNext=(numofrowsWhile)"+fileOutNext);
                                try {
        							dos.write(("\t"+"<TABLE CLASS=MenuViceStranek>"+"\n"+"\t"+"<TR>"+"\n").getBytes(enc));
                                    if ((fieldcaption>0)&&(fieldcaption<3)&&(MenuViceStranekCaption!="")){
                                    	dos.write(("\t"+"<TD class=\"MenuViceStranekCaption\">"+MenuViceStranekCaption+"</TD>"+"\n").getBytes(enc));

                                    }
                                    if (numofruns>=1) {
                                    	 dos.write(("\t"+"<TD class=\"MenuViceStranek MenuViceStranekPrev\">"+"<A CLASS=MenuViceStranek HREF=\""+fileOutPrev+"\">... "+numofruns+"</A>"+"</TD>"+"\n").getBytes(enc));

                                    }
                                    if (numofruns>=0){
                                    	if (!((numofruns==0)&&(!tablefinished)))//Podminka plati, pouze je-li vice stranek
                                    	 dos.write(("\t"+"<TD class=MenuViceStranek>"+(numofruns+1)+"</TD>"+"\n").getBytes(enc));

                                    }
                                    //tablefinished=(rs.next());//jeste jednou posun ukazatelem, aby , kdyby nahodou nasobek rows*cols byl delitelny poctem zaznamu, nevytvorila se jedna prazdna stranka navic
                                    if (tablefinished){//Podminka, ktera odlisuje, zda se koncilo chybou nebo tim, ze skoncili data - oproti NUMOFCOLS a NUMOFROWS
                                   		dos.write(("\t"+"<TD class=\"MenuViceStranek MenuViceStranekNext\">"+"<A CLASS=MenuViceStranek HREF=\""+fileOutNext+"\">"+(numofruns+2)+" ...</A>"+"</TD>"+"\n").getBytes(enc));
                                   	}

        							dos.write(("\t"+"</TR>"+"\n"+"\t"+"</TABLE>"+"\n"+"\n").getBytes(enc));
        						} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

    						//} //if  tablefinished
                    } //if layoutchar

    				//Zavreni radky pro menu pokud se zalamuji stranky
        			try {
        				dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n"+"\n").getBytes(enc));

        			} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

                //System.out.println ("numofruns="+numofruns);
           		//Try pro SQL query for table Sekce (menu na konci)
				try {

        			//Otevreni dalsi radky pro menu
        			//pokud menu neni, je radka prazdna
        			try {
        				dos.write(("\t"+"<TR>"+"\n"+"\t"+"<TD CLASS=SkupinaMenuDole colspan="+numofblockcolsmax+">"+"\n"+"\n").getBytes(enc));
        			} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}


        			//Generovani menu na konci stranky
        			if  (layoutchar != 'X') {


						//Menu z tabulky Sekce
        				try {
        					dos.write(("\t"+"<TABLE CLASS=MenuDoleText>"+"\n"+"\t"+"<TR>"+"\n"+"\t"+"<TD class=MenuDoleText>"+"\n").getBytes(enc));
        				} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

        				sq_stmt = db.createStatement();
        				//Puvodni SQL s WHERE
        	     			//sql_str = "SELECT "+TableMenuSQL+".CisloSekce, "+TableMenuSQL+".Odkaz,  "+TableMenuSQL+".MenuDoleText FROM "+TableMenuSQL+" WHERE "+TableMenuSQL+".Publikovat=1 ORDER BY "+TableMenuSQL+".Poradi";
        				sql_str = "SELECT "+TableMenuSQL+".CisloSekce, "+TableMenuSQL+".Odkaz,  "+TableMenuSQL+".MenuDoleText, "+TableMenuSQL+".Publikovat FROM "+TableMenuSQL+" ORDER BY "+TableMenuSQL+".Poradi";

        				rs = sq_stmt.executeQuery(sql_str);
        				while (rs.next()){

        					Boolean Publ=rs.getBoolean("Publikovat");//Tato promenna pouze pro testovani hodnoty pole Publikovat v databazi, muze nabyvat 0 a 1
        					Integer CisloSekce = new Integer (rs.getInt("CisloSekce"));//prevadime int na Integer
        					String Link = rs.getString("Odkaz");
        					String textMenu = rs.getString("MenuDoleText");

        					if (subparexists){
        						if (CisloSekce.equals(menunumpar)) {//nastaveni Publ podle negativpar...rozhodnuti jeatli prislusnou menu polozku zobrazit nebo ne
        						Publ=negativepar;
        						}
        					}
        					//System.out.println("Cislo sekce="+CisloSekce+" Publ="+Publ);//test nastaveni pole Publikovat v databazi


        					if (Publ) {	//zobrazit jen ty menu polozky, ktere maji Publ=true at uz z pole publikovat v databazi nebo z parametru v e spoustecim batchi

        						if (textMenu!=null) {
        							try {

        								dos.write(("\t"+"<A class=\"MenuDoleText MenuDoleTextCisloSekce"+CisloSekce+"\" HREF=\""+link_prefix+Link+"\">"+textMenu+"</A>"+"\n").getBytes(enc));

        							} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        							}
        						}
        					}//Konec if (Publ)

        				}//Konec while
        				try {
        					dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n"+"\t"+"</TABLE>"+"\n"+"\n").getBytes(enc));
        				} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}
        			}//Konec if layout
        			//Generovani menu na konci stranky - konec

        			//Zavreni radky pro menu
        			try {
        				dos.write(("\t"+"</TD>"+"\n"+"\t"+"</TR>"+"\n"+"\n").getBytes(enc));

        			} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}

        			//Uzavrereni tabulky Reference
        			try {
        				dos.write(( "\t"+"</TABLE>"+"\n").getBytes(enc));
        				dos.write(( "\t"+"</DIV>"+"\n").getBytes(enc));
        				dos.write(("\t"+"<!--Konec table Reference "+TableSQL+" z databaze "+whichURL+ "-->"+"\n"+"\n").getBytes(enc));
        				} catch (IOException e){
        			//System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        			}




        			//konec table Data
        			try {
        				dos.write(("</TD>"+"\n"+"</TR>"+"\n"+"</TABLE>"+"\n").getBytes(enc));
        				dos.write(("</DIV>"+"\n").getBytes(enc));
        				dos.write(("<!--Konec table Data z databaze "+whichURL+"-->"+"\n"+"\n").getBytes(enc));

        			} catch (IOException e){System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");}





          		} catch (SQLException e){System.out.println(">>>Database query error =Menu na konci=- field names in the table "+TableMenuSQL+" of database "+whichURL+" may not be correct names, might be missing or this database may be locked or corrupted");}

                //Pokud neni filepaticka (pri spusteni musi byt parametr nastaven na nofile), tak se nehleda a do vystupniho souboru nic nepridava
                //System.out.println("filepaticka = "+filepaticka);
                if (!filepaticka.equals("nofile")) {
                    try {
            			FileInputStream is = new FileInputStream(filepaticka);
            			DataInputStream dis = new DataInputStream(is);
            			InputStreamReader in = new InputStreamReader(dis);
            			int b=0;

            			//Od verze 131 neni pouzito, ale prevod bytes na string musi zustat, aby slo pouzit system.out
            			//if (online){
            				while ((b = in.read()) != -1)	{
            					//Nutny prevod na String aby slo pouzit dos.write i pro system.out
            					char bb=(char)b;
            					String bbb=String.valueOf(bb);
            					dos.write((bbb).getBytes(enc));
            				}
            			//}

            			dis.close();
            			is.close();

            		}
            		catch (IOException e){
            			System.out.println(">>>Error when reading data from footer file "+filepaticka);

            		}
        		}


          		try{
        			dos.close();
        		}
        		catch (IOException e){
        		//System.out.println(">>>The file "+fileOut+" must be writable for offline version of this output");
        		}
                numofruns=numofruns+1;
                //System.out.println ("numofblockrowsmax="+numofblockrowsmax);
				if (numofblockrowsmax==0) {tablefinished=false;}//Opatreni proti nekonecnemu cyklu


    		}//Konec numofrowsWhile


    	}//Konec if theEnd
    	if (!(notend)) {
    		System.out.println("\n"+"Usage:");

    		System.out.println("Offline version:");
    		System.out.println("java [-cp classpath] classname output_filename header_filename footer_filename ODBCdatabase_name link_prefix service_number -V[[!]number]||-H[[!]number]||-X [referencetablename] [sekcetablename]"+"\n");

    		System.out.println("Online version:");
    		System.out.println("http://hostname/cgi-bin/java.exe?[-cp+classpath]+classname+header_filename+footer_filename+ODBCdatabase_name+link_prefix+service_number+-V[[!]number]||-H[[!]number]||-X+[referencetablename]+[sekcetablename]"+"\n");

    		System.out.println("Parameter \"service_number\" should be any number from the first field of [sekcetablename] or character x, which means query all items");
    		System.out.println("Parameter \"-V\" specifies vertical layout of the menu and \"-H\" horizontal layout, if number is present, that means generate/exclude this specific number of menu item, depending on presence of \"!\", \"-X\" means do not generate menus at all, if parameter \"referencetablename\" or \"sekcetablename\" is not present, the table names \"Reference\" or \"Sekce\" are used as default. Valid combinations are \"referencetablename\" is not present and \"sekcetablename\" is not present, \"referencetablename\" is present and \"sekcetablename\" is not present, \"referencetablename\" is present and \"sekcetablename\" is present"+"\n");


    	}




   }//Konec main


}//Konec class
