<script>
function serialize(data,separ)
{
var arr,key;
var sep = separ ? separ : [' ','=','"'];
for (key in data)
	{
	arr[arr.length] = data[key]!='' ? key + sep[1] + sep[2] + data[key] + sep[2] : '';
	}
return arr.join(sep[0]);
}


function classHtml()
{

this.draw = function (type,name,params,text)
	{
	switch (type)
		{
		case 'img'     : return '<'+name+' ' + serialize(params) + '/>'; break;			// single tag
		case 'button'  :
		case 'text'    :
		case 'hidden'  : return '<input type="'+name+'" ' + serialize(params) + '\/>'; break;
		case 'radio'   :
		case 'checkbox': return '<input type="'+name+'" ' + serialize(params) + '\/>'+draw('label', {onclick: params.onclick, title: params.title}, text);
		default	       : return '<'+name+' '+serialize(params)+'>'+ text +'<\/'+name+'>';	// double tag
		}
	}
this.init = function ()
	{
	for(i in this.tags)
		{window[i] = function (params,text) {return this.draw(this.tags[i],i,params,text);};}
	}

this.templates =
	{
	'input': '<input type="text" size="6" name="%%%name%%%" value="%%%value%%%" onchange="%%%func%%%"/>',
	'th'   : "\n"+'<th>%%%text%%%<\/th>',
	'td'   : "\n"+'<td%%%bg%%%%%%fg%%%>%%%text%%%<\/td>',
	}
this.draw = function (str,data)
	{
	for (i in data)
		{
		str = str.replace('%%%'+i+'%%%',data[i]);
		}
	return str;
	}
this.drawTpl = function (name,data)
	{
	if (this.templates && this.templates[name])
		{
		return(this.draw(this.templates[name],data));
		}
	return '';
	}
}

myrender = new drawhtml();
alert(myrender.drawTpl('td',{'bg':1,'fg':2,'text':'ahoj'}));


</script>