var box;
var content;
var contentHeight;
var boxHeight;
var contentTop;
var scrollIt;
var idBox;

if (!document.getElementById && document.all) document.getElementById = document.all;
function setSize() {
 var i, a;
 a=setSize.arguments;
 box = new Array();
 content = new Array();
 contentHeight = new Array();
 boxHeight = new Array();
 contentTop = new Array();
 
 for(i=0; i<a.length; i++) {
  box[a[i]] = document.getElementById("boxScroll"+a[i]);
  content[a[i]] = document.getElementById("boxObsah"+a[i]);
  contentHeight[a[i]] = content[a[i]].offsetHeight;
  boxHeight[a[i]] = box[a[i]].offsetHeight;
  contentTop[a[i]] = 0;
 }
 scrollIt = "";
}
function scroll(direction,p) {
 idBox = p;
 sign = "+" ;     //smir dolu (default)
 edge = (contentTop[idBox]<=0); //smir dolu (default)
 if (direction=="up"){    //podm?nka pro smir nahoru
  sign = "-";
  edge = ((contentHeight[idBox]+contentTop[idBox])>=0+boxHeight[idBox]); // jestli?e v krajn? poloze zvolen?ho smiru tak nic
 }
 if (edge) scrollIt = setInterval("scrollMe()",20);
}
function scrollMe (){
 contentTop[idBox] = eval("contentTop[idBox]"+sign+"=4");
 edge = (contentTop[idBox]<=0);  //smir dolu (default)
 if (sign=="-") edge = ((contentHeight[idBox]+contentTop[idBox])>=0+boxHeight[idBox]); //podm?nka pro smir nahoru
 if (edge){      // jestli?e v krajn? poloze zvolen?ho smiru tak ukone?me cyklus
  content[idBox].style.top = (contentTop[idBox]) + "px";
 }else{
  clearInterval(scrollIt);
 }
}
function e(){
}
function Highlight(variable,color){
 variable.style.backgroundColor = color; 
}
function HighlightI(variable,image){
 variable.style.backgroundImage = image; 
}

// zvyrazni polozku po prechode mysi 
function neon(_mode, el, _cssClass, _level){
  if ( spanIsEmpty ( el ) ) {
    //rozsviti (tu nastavis jak se ma chovat)
    if(_mode == 1) {
       el.className = _cssClass + "_L";
     } 
     // zhasne
     if(_mode == 0) {
      el.className = _cssClass;
     } 
   }
}

// testuje ci je posledni span daneho tagu prazdny
function spanIsEmpty ( _el ) {
  spans = _el.getElementsByTagName('span');
  ms = spans[ spans.length - 1 ] ;
//  return ( ms.innerText.length > 0 );
  try {
    text_odstavce = ms.firstChild;
    return ( text_odstavce.nodeValue.length > 0 );
  }
  catch(er) {
    return false;
  }
}

/*
 * getObj(name)
 *
 * sets object and style
 */
function getObj(name)
{
  if (document.getElementById){
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

function changeClass(objectName, newClass)
{

	object = new getObj(objectName);
	object.obj.className = newClass;
}

/*
getObjNN4(obj, name)

rekurzivni funkce pro hledani objektu jmena name v Netscapu se zanorenymi
vrstvami, divy.
*/
function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

/**
 *submit form
 */
function submitForm(formName)
{
	form = new getObj(formName);
	form.obj.submit();
}


/**
 * Opens a new window to display picture with specified
 * size and path to either the picture or a script to 
 * display it
 */
function openPictureWindow(path, width, height)
{
    var top  = screen.availHeight/2 - height/2;
    var left = screen.availWidth/2 - width/2;

	if (screen.availHeight < height)
	{
		top = 0;
		height=screen.availHeight;
	} else
	{
		height = height + 38;	
	}
	
	if (screen.availWidth < width)
	{
		left = 0;
		width = screen.availWidth;
	}else
	{
		width = width + 38;
	}	

	window.open(path, "window",'width=' + width + ',height=' + height +',top=' + top +',left=' + left);
}

/**
* gets all element with specified class name
* @param string clsName 
* @return array all elements with specified class name
*/	
function getElementsByClassName(clsName) 
{ 
	var arr = new Array(); 
	var elems = document.getElementsByTagName("*");
	for ( var cls, i = 0; ( elem = elems[i] ); i++ )
	{
		if ( elem.className.indexOf(clsName) != -1 )
		{
			arr[arr.length] = elem;
		}
	}
	return arr;
}

/**
 * checks if required fields are not empty
 * all required fields have to be in class named "required"
 * if the field has specified title than show title in alert - title should 
 * be in format Insert your name. Insert your address. Etc.
 */
function validateForm ()
{
	requiredFields = getElementsByClassName("required");
	
	for (var i=0;i<requiredFields.length;i++)
	{
		if ( trim (requiredFields[i].value) == "" ){
			if (requiredFields[i].title){
				alert (requiredFields[i].title);
			}
			else{
				alert ("Je třeba vyplnit všechny povinné položky!");
			}
			requiredFields[i].focus();
			return false;
		}
	}
	return true;
}


/**
 * trim function
 */
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}