function trim(str)
{
    return str.replace(/^\s+|\s+$/g,'');
}

function zobraz(id){
	document.getElementById(id).style.display = 'block';
}

function skry(id){
	el=document.getElementById(id).style.display = 'none';
}

function collapse_menu(id)
{
	document.getElementById(id).style.display = document.getElementById(id).style.display == 'none' ? 'block' : 'none';
}

function add_rk()
{
	var spravny = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	with(window.document.addrk)
	{
		if (trim(inzerat_name.value)=='')
		{
			alert(hlaska['fill'] + ": " + hlaska['inzerat_name']);
			inzerat_name.focus();
			return false;
		}
		else if (trim(description.value)=='')
		{
			alert(hlaska['fill'] + ": " + hlaska['description']);
			description.focus();
			return false;
		}
		else if (trim(cena.value)=='')
		{
			alert(hlaska['fill'] + ": " + hlaska['price']);
			cena.focus();
			return false;
		}
		else if (trim(email.value)=='')
		{
			alert(hlaska['fill'] + ": " + hlaska['email']);
			email.focus();
			return false;
		}
		else if(!(spravny.test(email.value)))
		{
			alert(hlaska['incorrect_email']);
			email.focus();
			email.select();
			return false;
		}
	}
	return true;
}


function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, true);
		return true;
	} 
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} 
	else 
	{
		return false;
	}
}

document.getElementsByClassName = function (needle)
{
  var         my_array = document.getElementsByTagName("*");
  var         retvalue = new Array();
  var        i;
  var        j;

  for (i = 0, j = 0; i < my_array.length; i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}

addEvent(window, 'load', function()
{
	var counts = document.getElementsByClassName('maxlength');
	var i, count, matches, countHolder;
	
	for (i=0; i<counts.length; i++)
	{
		count = counts[i];	
		matches = count.className.match(/max_([0-9]+)/);
		count.maxVal = RegExp.$1;
		count.holder = document.getElementById(count.id + 'Count');
		if (count.holder)
		{
			count.holder.innerHTML = count.maxVal - count.value.length;
			count.onkeyup = function()
			{
				if (this.value.length > this.maxVal)
					this.value = this.value.substring(0, this.maxVal);
	
				this.holder.innerHTML = this.maxVal - this.value.length;		
			}
		}
	}	
});