function getE(id)
{
   if (typeof id != "string")
   {
         return id;
   }
   else if (Boolean(document.getElementById))
   {
         return document.getElementById(id);
   }
   else if (Boolean(document.all))
   {
         return eval("document.all."+id);
   }
   else if (Boolean(document.ids))
   {
         return eval("document.ids."+id);
   }
   else
   {
         return null;
   }
}

function wo(link,name,attr,dat)
{
      if (getE(dat)!=null)
      {
            if (getE(dat).value != "")
            {
                  var sd = getE(dat).value;

                  while(sd.search(String.fromCharCode(10)) != -1)
                  {
                     sd = sd.replace(String.fromCharCode(10),"$");
                  }

                  link = link + "&sd=" + sd;
            }
      }

     nove_okno = window.open(link,name,attr);

     if (name != '')
     {
           nove_okno.focus();
     }
}

function dlg_clear(id)
{
   if (getE(id)!=null)
   {
         SetValKomu(id,"");
   }

   if (getE(id+"_nazev")!=null)
   {
         SetValKomu(id+"_nazev"," - ");
   }
}

function SetValKomu(komu,val)
{
   if (getE(komu).tagName=="INPUT" || getE(komu).tagName=="TEXTAREA")
   {
         getE(komu).value = val;
   }
   else if (getE(komu).tagName=="SPAN" || getE(komu).tagName=="DIV")
   {
         getE(komu).innerHTML = val;
   }
}

function SetVal(kod,nazev,kod_val,nazev_val)
{
   if (kod != ""   && getE(kod)!=null)
   {
         SetValKomu(kod, kod_val);
   }

   if (nazev != "" && getE(nazev)!=null)
   {
         SetValKomu(nazev, nazev_val);
   }
}

function ParentSetVal(kod,nazev,kod_val,nazev_val,close)
{
   try
   {
         self.opener.SetVal(kod,nazev,kod_val,nazev_val);
   }
   catch(e)
   {
   }

   if (close=="a")
   {
         self.close();
   }
}

function ParentRefresh(close)
{
   try
   {
         self.opener.document.location.reload(true);
   }
   catch(e)
   {
   }

   if (close=="a")
   {
         self.close();
   }
}

function switch_filtr()
{
   _div  = getE("filtr");
   _href = getE("filtr_href");

   if (_div == null || _href == null) return;

   if (_div.style.display == "")
   {
         _div.style.display = "none";
         _href.className = "filtr_off";
   }
   else
   {
         _div.style.display = "";
         _href.className = "filtr_on";
   }
}

function ShowHide(div,stav)
{
   var d = getE(div);
   var _stav = stav;

   if (d == null) return;

   if (stav == 3)
   {
         if (d.style.display == "") _stav = 0;
         else                       _stav = 1;
   }

   if (_stav == 0)
   {
         d.style.visibility = "hidden";
         d.style.display    = 'none';
   }
   else
   {
         d.style.visibility = "visible";
         d.style.display    = '';
   }
}

/*global alert, document, window, class_session, getE, str_session_expired  */

/**
 *  Hlida vyprseni session
 *  @class class_session
 */
class_session = function()
{
   this.sessionCD    = 3600; // vyprseni session
   this.timer        = null;      // reference na casovac
   this.interval     = 1000;    // interval skoku casovace [ms]
   this.warnExpired  = false; // varovat pri vyprseni session
   this.step  = 1;     // po kolika sekundach skakat

   this.start = function()
   {
      if (document.uniqueID) // msie crazy timer
      {
         var t = this;
         this.timer = window.setInterval(function(){t.runSessionCountDown();}, this.interval, this);
      }
      else
      {
         this.timer = window.setInterval(function(self) {self.runSessionCountDown.call(self);} ,this.interval,this);
      }
   };

   this.runSessionCountDown = function()
   {
      if (this.sessionCD === 0)
      {
         str_session_expired = "Sezení je ukončeno. Pro další práci s IS se znovu přihlašte.\nVeškerá neuložená data budou ztracena.";
         window.status = str_session_expired;
         getE("session").innerHTML = "00:00";
         window.clearTimeout(this.timer);

         if (this.warnExpired)
         {
            alert(str_session_expired);
         }
      }
      else
      {
         var m   = Math.floor(this.sessionCD/60);
         var s   = Math.floor((this.sessionCD % 60) % 60);
         var cas = "";

         if (m<10)
         {
            cas = cas+"0";
         }
         cas = cas+m+":";
         if (s<10)
         {
            cas = cas+"0";
         }

         cas = cas+s;
         getE("session").innerHTML = cas;

         this.sessionCD = this.sessionCD - this.step;
      }
   };
};

function showSession(sek, varovat)
{
   var sis_timer = new class_session();
   sis_timer.sessionCD   = sek;
   sis_timer.warnExpired = varovat;
   sis_timer.runSessionCountDown();
   sis_timer.start();
}

function invert_form(_frm, _stav, classname)
{
   if (getE(_frm) == null) return;
   if (!classname) classname=false;

   var _form = getE(_frm).getElementsByTagName("input");

   for (var i = 0; i < _form.length; i++)
   {
         var e = _form[i];

         if (e.type == "checkbox")
         {
               if ((classname==false) || (e.className==classname))
               {
                     if (_stav == 1)
                     {
                           e.checked = true;
                     }
                     else if (_stav == 0)
                     {
                           e.checked = false;
                     }
                     else
                     {
                           e.checked = !e.checked;
                     }
               }
               else
               {

               }
         }
   }

   return false;
}

function dopln_datum(did)
{
   var t = getE(did).value;
   var den,dden,mesic,mmesic,rok,p;

   p = t.indexOf(".");
   if (p == -1) return;

   dden  = t.substring(0,p);
   den   = parseInt(dden);
   mesic = t.substring(p+1,t.length);

   p = mesic.indexOf(".");

   if (p == -1) return;

   rok   = mesic.substring(p+1,mesic.length);
   mmesic= mesic.substring(0,p);
   mesic = parseInt(mmesic);

   if (isNaN(den) == true || isNaN(mesic) == true || den != dden || mesic != mmesic) return;

   if (den < 1 || den > 31 || mesic < 1 || mesic > 12) return;

   if (rok.length == 0)
   {
         var dt = new Date();
         var rok=dt.getFullYear();
         getE(did).value = getE(did).value + rok;
   }
}

function dl_select_dual(__obj, __checked, __sel_array)
{
   var obj_select = getE(__obj);

   if (!obj_select || obj_select.type != "select-one") return;

	obj_select.options.length = 0;

   if (__checked == true)
   {
         var opt = __sel_array[1];
   }
   else
   {
         var opt = __sel_array[0];
   }

	obj_select.options[0] = new Option('---', '');

	for (i=0; i < opt.length; i+=2)
	{
         obj_select.options[(i/2 + 1)] = new Option(opt[i+1], opt[i]);
	}
}

function dl_select_povinn(select_id, obj_data)
{
      var obj_select = document.getElementById(select_id);

      if (!obj_select || obj_select.type != "select-one") return;

      var obj_value = obj_select.options[obj_select.selectedIndex].value;

      obj_select.options.length = 0;

      if (obj_data.mode == "kod")
      {
            for (key in obj_data.data)
            {
                  obj_select.options[obj_select.length] = new Option(obj_data.data[key] + ' - ' + key, key);
            }

            obj_data.mode = 'text';

            var items = obj_select.options.length;
            var tmpArray = new Array(items);

            for (var i=0; i < items; i++ )
            {
                  tmpArray[i] = new Option(obj_select.options[i].text,obj_select.options[i].value);
            }

            tmpArray.sort(dl_select_povinn_sort);

            obj_select.options[0] = new Option('---', '');

            for (i = 0; i < items; i++ )
            {
                  obj_select.options[i + 1] = new Option(tmpArray[i].text,tmpArray[i].value);
            }
      }
      else
      {
            obj_select.options[0] = new Option('---', '');

            for (key in obj_data.data)
            {
                  obj_select.options[obj_select.length] = new Option(key + ' - ' + obj_data.data[key], key);
            }

            obj_data.mode = 'kod';
      }

      if (obj_value != '')
      {
            obj_select.value = obj_value;
      }
}


function dl_select_povinn_sort(a,b)
{
   return a.text != b.text ? a.text < b.text ? -1 : 1 : 0;
}

function dl_showhide(obj, obj_data)
{
      if (!obj || !(obj_data instanceof Array)) return;

      for (i in obj_data)
      {
            ShowHide(obj_data[i], 0);
      }

      if (obj.value in obj_data)
      {
            ShowHide(obj_data[obj.value], 1);
      }
}


/* :::::::::::::::: HINT ::::::::::::::::::: */

var tt_db = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body? document.body : null,
tt_n = navigator.userAgent.toLowerCase(),
tt_nv = navigator.appVersion;
var tt_op = !!(window.opera && document.getElementById),
tt_op6 = tt_op && !document.defaultView,
tt_op7 = tt_op && !tt_op6,
tt_ie = tt_n.indexOf("msie") != -1 && document.all && tt_db && !tt_op,
tt_ie6 = tt_ie && parseFloat(tt_nv.substring(tt_nv.indexOf("MSIE")+5)) >= 5.5;
tt_n4 = (document.layers && typeof document.classes != tt_u),
tt_n6 = (!tt_op && document.defaultView && typeof document.defaultView.getComputedStyle != tt_u),
tt_w3c = !tt_ie && !tt_n6 && !tt_op && document.getElementById;
var tt_u = "undefined";

function tt_Int(t_x)
{
	var t_y;
	return isNaN(t_y = parseInt(t_x))? 0 : t_y;
}

function getDivW(id)
{
   return tt_Int( tt_n4 ? getE(id).clip.width : (getE(id).style.pixelWidth || getE(id).offsetWidth) );
}
function getDivH(id)
{
   return tt_Int( tt_n4 ? getE(id).clip.height : (getE(id).style.pixelHeight || getE(id).offsetHeight) );
}

var timerId = null;
var t_id;
var t_mod;
var ex = 0;
var ey = 0;

function ShowHint(id,mod,tout,text)
{
   HideHint(id);
   t_id = id;

   if (text!=undefined && text!="")
   {
         mod = text;
   }
   else if (mod!=undefined && mod!="")
   {
         mod  = eval(mod);
   }

   if (mod!="")
   {
         t_mod = mod;

         if (tout == 0)
         {
               ShowHintTimeOut();
         }
         else
         {
               timerId = setTimeout("ShowHintTimeOut()", tout);
         }
   }
}

function ShowHintTimeOut()
{
   if (timerId!=null)
   {
         clearTimeout(timerId);
         timerId=null;
   }

   getE(t_id+"lbl").innerHTML = t_mod;
   ShowHide(t_id,1);

   if (getDivH(t_id)>winH())
   {
         getE(t_id).style.top = document.body.scrollTop + ((winH()-getDivH(t_id))/2) - 15;
   }
   else if ((ey+getDivH(t_id)+20)>winH())
   {
         if ((getDivH(t_id)+10) > ey)
         {
               getE(t_id).style.top = document.body.scrollTop + ((winH() - getDivH(t_id))/2) - 15;
         }
         else
         {
               getE(t_id).style.top = document.body.scrollTop + ey - getDivH(t_id) - 20;
         }
   }
   else
   {
         getE(t_id).style.top = document.body.scrollTop + ey + 4;
   }

   if (getDivW(t_id)>winW())
   {
         getE(t_id).style.left = document.body.scrollLeft+((winW()-getDivW(t_id))/2)-15;
   }
   else if ((ex+getDivW(t_id)+20)>winW())
   {
         if ((getDivW(t_id)+10)>ex)
         {
               getE(t_id).style.left = document.body.scrollLeft+((winW()-getDivW(t_id))/2)-15;
         }
         else
         {
               getE(t_id).style.left=document.body.scrollLeft+ex-getDivW(t_id)-20;
         }
   }
   else
   {
         getE(t_id).style.left=document.body.scrollLeft+ex+4;
   }
}

function HideHint(id)
{
   if (timerId != null)
   {
         clearTimeout(timerId);
         timerId=null;
   }

   ShowHide(id,0);
}

function setxy(e)
{
   ex = document.all?window.event.clientX:e.clientX;
   ey = document.all?window.event.clientY:e.clientY;
}

document.onmousemove=setxy;

function winH()
{
   if (window.innerHeight)
   {
         return window.innerHeight;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
   {
         return document.documentElement.clientHeight;
   }
   else if (document.body && document.body.clientHeight)
   {
         return document.body.clientHeight;
   }
   else
   {
         return null;
   }
}

function winW()
{
   if (window.innerWidth)
   {
         return window.innerWidth;
   }
   else if (document.documentElement && document.documentElement.clientWidth)
   {
         return document.documentElement.clientWidth;
   }
   else if (document.body && document.body.clientWidth)
   {
         return document.body.clientWidth;
   }
   else
   {
         return null;
   }
}

function js_confirm(text,url)
{
   if (confirm(text))
   {
         location.href = url;
   }
   else
   {
         return false;
   }
}

function textarea_check_limit(obj_ref, obj_len, lang_zbyva, lang_prebyva)
{
   obj_ref_id=obj_ref.id;

   obj_txt_zadano = document.getElementById('textarea_limit_'+obj_ref_id+'_zadano');
   obj_txt_zbyva  = document.getElementById('textarea_limit_'+obj_ref_id+'_zbyva');
   obj_txtlang_zbyva  = document.getElementById('textarea_limit_'+obj_ref_id+'_txt_zbyva');
   txt_length = obj_ref.value.replace(/\r/g,'').replace(/\n/g,'nl').length;

   if (txt_length > obj_len)
   {
      obj_ref.className='inp3';
      obj_txt_zadano.parentNode.parentNode.className = 'error';
      obj_txtlang_zbyva.innerHTML = lang_prebyva;
      obj_txt_zbyva.innerHTML  = (obj_len-txt_length)*(-1);
   }
   else
   {
      obj_ref.className='inp2';
      obj_txt_zadano.parentNode.parentNode.className = null;
      obj_txtlang_zbyva.innerHTML = lang_zbyva;
      obj_txt_zbyva.innerHTML  = obj_len-txt_length;
   }

   obj_txt_zadano.innerHTML = txt_length;
}


function in_array(value, arr)
{
   var i;

   if(value != null && arr != null && (arr instanceof Array))
   {
      for (i = 0; i < arr.length; i++)
      {
         if (arr[i] === value)
         {
            return true;
         }
      }
   }

   return false;
}


var registered_divs = new Array();

function addRegDiv(divid)
{
   registered_divs[registered_divs.length] = divid;
}

function HideAllRegisteredDivs()
{
   for(i=0; i<registered_divs.length; i++)
   {
      ShowHide(registered_divs[i], 0);
   }
}

function isChildOfRegDiv(child)
{
   var retval = false;

   if(typeof(ContainerObject) == "string")
   {
      child = document.getElementById(child);
   }

   if(child != null)
   {
      //alert(registered_divs.length);
      for(i=0; i<registered_divs.length; i++)
      {
         if(isChildOf(child, registered_divs[i]))
         {
            retval = true;
            break;
         }
      }
   }

   return retval;
}


function isChildOf(ChildObject, ContainerObject)
{
   var retval = false;
   var curobj;

   if(typeof(ContainerObject) == "string")
   {
      ContainerObject = document.getElementById(ContainerObject);
   }

   if(typeof(ChildObject) == "string")
   {
      ChildObject = document.getElementById(ChildObject);
   }

   if(ChildObject != null && ContainerObject != null)
   {
      curobj = ChildObject.parentNode;

      while(curobj != null)
      {
         if(curobj == document.body)
         {
            break;
         }

         if(curobj.id == ContainerObject.id)
         {
            retval = true;
            break;
         }
         curobj = curobj.parentNode;
      }
   }

   return retval;
}


function getEventSourceElement(event)
{
   if(Boolean(event.srcElement))
   {
      return event.srcElement;
   }
   else
   {
      return event.target;
   }
}

function multiple_select_title(select_id) 
{
   var dta="";
   el = document.getElementById(select_id).options;
   for (i=0;i<el.length;i++)
   {
      if (el[i].selected == true)
      {
         dta += el[i].innerHTML+"<br>";
      }
   }

   var hint = document.getElementById(select_id+"_hint");

   if (dta!="")
   {
      hint.style.visibility = "visible";
      hint.customdata = '<b>'+dl_lang['stev.js_selected_multiple']+'</b><br>'+dta;
   }
   else
   {
      hint.style.visibility = "hidden";
      hint.customdata = '';
   }
}