// --- Multi sort ---

function classHtml(opt)
{
var root = this;
this.name = 'classHtml';
this.sel_all   = '(vše)';
this.sel_empty = '(prázdné)';
this.func = {};


this.pageGetSelect = function(form,name,bool_values)
	{
//alert(document.forms.length)
	var i, i_end, item_all, sel;
	var form = form ? form : (document.forms[0] ? document.forms[0] : null);
//alert([1,form])
sel = [];
	if (!form || !form[name])
		{
		return sel;
		}
//alert([2,form[name]])
	item_all = form[name].getElementsByTagName('OPTION');
	i_end    = item_all.length;
	j = 0;
	for (i=0;i<i_end;i++)
		{
		if (item_all[i].selected && item_all[i].value>=0)	// vynech value=-1 (text=vse)
			{
			sel[j] = !bool_values ? item_all[i].value : item_all[i].innerHTML.replace(root.sel_empty,'');
			j++;
			}
		}
	return sel;
	}

this.pageGetSelectCheckBox = function(form,name,bool_values)
	{
	var i, i_end, item_all, sel;
	var form = form ? form : (document.forms[0] ? document.forms[0] : null);
	sel = [];
	if (!form || !form[name])
		{
		return sel;
		}
	item_all = form[name];
	i_end    = item_all.length;
	j = 0;
	for (i=0;i<i_end;i++)
		{
		if (item_all[i].checked)
			{
//			sel[j] = item_all[i].value;
			sel[j] = !bool_values ? item_all[i].value : item_all[i].parentNode.innerHTML.replace(/\s*[<].*?[>]\s*/g,'').replace(root.sel_empty,'');
			j++;
			}
		}
	return sel;
	}

this.func.createListSelectBoxItem = function(key,value,selected)
	{
	var j, j_end, str;
	j_end = selected.length;
	for(j=0;j<j_end;j++)
		{
		if (key==selected[j])
			{
			break;
			}
		}
	value = value!='' ? value : root.sel_empty;
	if (j<j_end)
		{
		str = '<option value="'+key+'" selected="selected">'+value+'<\/option>\n';
		return str;
		}
	str = '<option value="'+key+'">'+value+'<\/option>\n';
	return str;
	}

this.func.createListSelectBox = function(params,data,selected)
	{
	var str,str_p,i,i_end;
//	tpl = {};
//	tpl.select   = '<select>{0}<\/select>';
//	tpl.opt      = '<option value="{0}">{1}<\/option>';
//	tpl.selected = '<option value="{0}" selected="selected">{1}<\/option>';
	str = '';
	i_end = data.length;
	if (selected.length==0 && i_end>0)	// select default value
		{
		selected = [-1];
		}
	str += root.func.createListSelectBoxItem(-1,root.sel_all,selected);
	for(i=0;i<i_end;i++)
		{
		str += root.func.createListSelectBoxItem(i,data[i],selected);
		}
	if (str!='')
		{
		str_p = '';
		for(i in params)
			{
			str_p += ' '+i+'="'+params[i]+'"';
			}
		str = '<select'+str_p+'>\n'+str+'<\/select>\n';
		}
	return str;
	}

this.func.createListCheckBoxItem = function(key, value, selected, str_p, pad)
	{
	var j, j_end, str, pad;
	pad   = isExist(pad) ? pad : '<br>';
	j_end = selected.length;
	for(j=0;j<j_end;j++)
		{
		if (key==selected[j])
			{
			break;
			}
		}
	value = value!='' ? value : root.sel_empty;
	if (j<j_end)
		{
		str = '<label><input type=checkbox value="'+key+'" checked="checked"'+str_p+'>'+value+'<\/label>\n'+pad;
		return str;
		}
	str = '<label><input type=checkbox value="'+key+'"'+str_p+'>'+value+'<\/label>\n'+pad;
	return str;
	}

this.func.createListCheckBox = function(params, data, selected, pad)
	{
	var str,str_p,i,i_end;
	str   = '';
	str_p = '';
	for (i in params)
		{
		str_p += ' '+i+'="'+params[i]+'"';
		}
	i_end = data.length;
	if (selected.length==0 && i_end>0)	// select default value = all
		{
		selected = [-1];
		}
	str += root.func.createListCheckBoxItem(-1,root.sel_all.replace(/\s/g,'&nbsp;'),selected,str_p,pad);
	for(i=0;i<i_end;i++)
		{
		str += root.func.createListCheckBoxItem(i,data[i].replace(/\s/g,'&nbsp;'),selected,str_p,pad);
		}
	return str;
	}

this.func.createListCheckBoxItemBoolean = function(key, value, selected, str_p, pad)
	{
	var str;
	var pad   = isExist(pad) ? pad : '<br>';
	value = value!='' ? value : root.sel_empty;
	if (selected)
		{
		str = '<label><input type=checkbox value="'+key+'" checked="checked"'+str_p+'>'+value+'<\/label>\n'+pad;
		return str;
		}
	str = '<label><input type=checkbox value="'+key+'"'+str_p+'>'+value+'<\/label>\n'+pad;
	return str;
	}

this.func.createListCheckBoxBoolean = function(params, data, selected_bool, pad)
	{
	var str,str_p,i,i_end, bool, val;
	str   = '';
	str_p = '';
	for (i in params)
		{
		str_p += ' '+i+'="'+params[i]+'"';
		}
	i_end = data.length;
	for(i=0;i<i_end;i++)
		{
		bool = isExist(selected_bool[i]) ? selected_bool[i] : false;
		val  = data[i].replace(/\s/g,'&nbsp;');
		str += root.func.createListCheckBoxItemBoolean(i,val,bool,str_p,pad);
		}
	return str;
	}
/*
	selected_all = false;
	i_end = data.length;
	if (selected_word.length==0 && i_end>0)	// select default value = all
		{
		selected_all = data.length;
		}
	str += root.func.createListCheckBoxItemBoolean(-1,root.sel_all.replace(/\s/g,'&nbsp;'),selected_all==data.length,str_p,pad);
// toto _all_ opravit

*/
this.func.createTable = function(schema, body_data, header_data, footer_data, start, end, col_id, header_data2)
	{
	root.func.log(root.name + '.func.createTable (' + body_data.length + ')');
	var i, i_end, tpl, str;
	var schema = isExist(schema) ? schema : 0;
	var start  = isExist(start)  ? start * 1 : 0;
	var end    = isExist(end)    ? end   * 1 : 0;
	var header_data2 = isArray(header_data2) ?  header_data2 : ['',''];
	end = end<=body_data.length  ? end : body_data.length;
	tpl = {};
	tpl.table  = '';
	tpl.table0 = '<table class="tbl"><thead>{0}<\/thead><tbody>{1}<\/tbody><tfoot>{2}<\/tfoot><\/table>';
	tpl.table1 = '<table><thead>{0}<\/thead><tbody>{1}<\/tbody><tfoot>{2}<\/tfoot><\/table>';
	tpl.table2 = '<table><tbody>{1}<\/tbody><\/table>';
	tpl.header = {};
	tpl.header.tr = '<tr>{0}<\/tr>\n';
	tpl.header.th = '<th style="text-align:left;">{0}<\/th>';
	tpl.body = {};
	tpl.body.tr = '<tr>{0}<\/tr>\n';
	tpl.body.td = '<td>{0}<\/td>';
	tpl.body.th = '<th style="text-align:right;font-weight:normal;">{0}<\/th>';
/*
	tpl.body.row = '\
<td>{5}<\/td>\
<td>{6}<\/td>\
<td class="num">{7}<\/td>\
<td>{8}<\/td>\
<td class="num">{0}<\/td>\
<td class="num">{1}<\/td>\
<td class="num">{2}<\/td>\
<td class="num">{3}<\/td>\
<td class="num">{4}<\/td>\
<td class="num">{10}<\/td>\
<td>{9}<\/td>\
';
	tpl.body.row2 = '\
<td>{0}<\/td>\
<td class="num">{1}<\/td>\
<td>{2}<\/td>\
';
	tpl.footer = {};
	tpl.footer.tr = '<tr>{0}<\/tr>';
	tpl.footer.td = '<td><b>{0}<\/b><\/td>';
	tpl.footer.row = '\
<td>{0}<\/td>\
<td>{1}<\/td>\
<td class="num">{2}<\/td>\
<td>{3}<\/td>\
<td class="num">{4}<\/td>\
<td class="num">{5}<\/td>\
<td class="num">{6}<\/td>\
<td class="num">{7}<\/td>\
<td class="num">{8}<\/td>\
<td class="num">{9}<\/td>\
<td>{9}<\/td>\
';
*/

	tpl.table  = tpl.table0;
//	if (schema==3)
//		{
//		tpl.body.row = tpl.body.row2;
//		}
//	if (schema==1)
//		{
//		tpl.body.row = tpl.body.row1;
//		}
	head = '';
	body = '';
	foot = '';
	if (header_data.length>0)
		{
		i_end = header_data.length;
		j_end = isExist(header_data[0]) ? header_data[0].length : 0;
		for (i=0;i<i_end;i++)
			{
			row   = '';
			if (schema==1)
				{
//<input type="button" onlick="this.func.tableRefresh();" value="Apply">\
				row += render(tpl.header.th,{0:header_data2[i]});
				}
			if (schema==2)
				{
				row += render(tpl.header.th,{0:''});
				}
			for (j=0;j<j_end;j++)
				{
				row += render(tpl.header.th,{0:header_data[i][j]});
				}
			head += render(tpl.header.tr,{0:row});
			}
		}
//	if (body_data.length>0 && (schema==0 || schema==3))
//		{
//		row = '';
//		i_end = body_data.length;
//		for (i=0;i<i_end;i++)
//			{
//			row   = render(tpl.body.row,body_data[i]);
//			body += render(tpl.body.tr,{0:row});
//			}
//		}
	if (end-start>0 && schema==0)	// normal tahle
		{
		i_end = end;
		j_end = body_data[start].length;
		for (i=start;i<i_end;i++)
			{
			row   = '';
			for (j=0;j<j_end;j++)
				{
				row += render(tpl.body.td, {0:body_data[i][j]});
				}
			body += render(tpl.body.tr, {0:row});
			}
		}
	if (end-start>0 && (schema==1 || schema==2))	// table with head col
		{
		i_end = end;
		j_end = body_data[start].length;
		for (i=start;i<i_end;i++)
			{
			row   = '';
			row  += render(tpl.body.th, {0:i});
			for (j=0;j<j_end;j++)
				{
				if (j==col_id)
					{continue;}
				row += render(tpl.body.td, {0:body_data[i][j]});
				}
			body += render(tpl.body.tr, {0:row});
			}
		}
//	if (footer_data.length>0 && schema==0)
//		{
//		row = '';
//		i_end = footer_data.length;
//		for (i=0;i<i_end;i++)
//			{
//			row   = render(tpl.footer.row,footer_data[i]);
//			foot += render(tpl.footer.tr,{0:row});
//			}
//		}
	if (isArray(footer_data) && footer_data.length>0)	// && (schema==1 || schema==2)
		{
		row = '';
		i_end = footer_data.length;
		j_end = isExist(footer_data[0]) ? footer_data[0].length : 0;
		for (i=0;i<i_end;i++)
			{
			row = '';
			for (j=0;j<j_end;j++)
				{
				row += render(tpl.footer.td,{0:footer_data[i][j]});
				}
			foot += render(tpl.footer.tr,{0:row});
			}
		}
//alert(tpl.table.toSource())
	return render(tpl.table,{0:head,1:body,2:foot});
	}

this.func.init = function(opt)
	{
	if (root.func.log)
		{
		root.func.log(root.name + '.func.init');
		}
	var bool;
	bool = isObject(opt);
	root.func.log = bool && isFunction(opt.log) ? opt.log : null;
	}

this.func.init(opt);
}

var HTML = new classHtml({log:SCREEN.log});