function openTargetPopupWindow(href,tar,w,h) {
 msgWindow = window.open(href, tar, 'resizable=0,scrollbars=1,status=0,title=0,toolbar=0,location=0,width='+w+',height='+h);
 if (msgWindow.opener == null) msgWindow.opener = self;
}

function openResizableTargetPopupWindow(href,tar,w,h) {
 msgWindow = window.open(href, tar, 'resizable=1,scrollbars=1,status=0,title=0,toolbar=0,location=no,width='+w+',height='+h);
 if (msgWindow.opener == null) msgWindow.opener = self;
 msgWindow.focus();
}

function openPopupWindow(href,w,h) {
 randomNumber = Math.round(Math.random()*100000000);
 return openTargetPopupWindow(href,'popup_'+randomNumber,w,h);
}

function openResizablePopupWindow(href,w,h) {
 randomNumber = Math.round(Math.random()*100000000);
 return openResizableTargetPopupWindow(href,'popup_'+randomNumber,w,h);
}

function foreignKey(cls,nam,ide,val) {
 return openPopupWindow('foreignKey?cls='+cls+'&nameSource='+nam+'&idSource='+ide+'&val='+val,400,500);
}

function confirmRemoveP(id,param) {
 if(confirm('Really remove the item?'))
  {window.location='?'+param+'='+id};
}

function confirmRemove(id) {
 confirmRemoveP(id,'removeId');
}

function confirmedRemove(id,message) {
	confirmedRemoveP(id,'removeId',message);
}
function confirmedRemoveP(id,param,message) {
 if(confirm(message))
  {window.location='?'+param+'='+id};
}

function confirmSubmitP(id,message) {
	if(confirm(message))
	{this.form[id].value=1; this.form.submit()};
}

function confirmSubmit(id) {
	confirmSubmitP(id,'Really remove?');
}

function reloadFrames() {
 if (parent.main.document.form != null) parent.main.document.form.cancelVariable.value=1;
 if (parent.main.document.form != null) parent.main.document.form.submit();
 if (parent.menu.document.form != null) parent.menu.document.form.submit();
}

function toggle(pass, tit0, tit1) {
 var sign = document.getElementById('sign_'+pass);
 if(sign.src.substr(sign.src.length-9,5)=='minus') {
	sign.src='/images/plus.gif';
	sign.title=tit0;
 } else {
	sign.src='/images/minus.gif';
	sign.title=tit1;
 }
 var rows = document.getElementsByName(pass);
 for(i=0;i<rows.length;i++) {
  if(rows[i].style.display == 'none') {
   rows[i].style.display='';
  } else {
   rows[i].style.display='none';
  }
 }
}

function toggle_eq(pass) {
 var sign = document.getElementById('sign_'+pass);
 if(sign.src.substr(sign.src.length-9,5)=='minus') {
	sign.src='/images/plus.gif';
 } else {
	sign.src='/images/minus.gif';
 }
 var rows = document.getElementsByName(pass);
 for(i=0;i<rows.length;i++) {
  if(rows[i].style.display == 'none') {
   rows[i].style.display='';
  } else {
   rows[i].style.display='none';
  }
 }
}

function chooseMenu(opt) {
    var ms;
    ms = opt.split("?");
    parent.parent.main.window.location=opt;
    parent.parent.menu.window.location='menu?menuSelection='+ms[0];
}

function refreshOpener(opener,loc) {
 if (opener == null) alert("opener isNil");
 opener.location=loc;
}

function imgSwitch(elid) {
  if(document.getElementById(elid).value=="true") {
   document.getElementById(elid).value="false";
  } else {
   document.getElementById(elid).value="true";
  }
  setRow(elid);
};

function setRow(elid) {
 if (document.getElementById(elid).value=="true") {
  document.getElementById(elid+"ON").style.backgroundColor="#00CC00";
  document.getElementById(elid+"ON").style.fontWeight="bold";
  document.getElementById(elid+"ON").style.color="#FFFFFF";
  document.getElementById(elid+"OFF").style.backgroundColor="transparent";
  document.getElementById(elid+"OFF").style.fontWeight="normal";
  document.getElementById(elid+"OFF").style.color="#000000";
  document.getElementById(elid+"IMG").src="/images/switch_l.gif";
 } else {
  document.getElementById(elid+"ON").style.backgroundColor="transparent";
  document.getElementById(elid+"ON").style.fontWeight="normal";
  document.getElementById(elid+"ON").style.color="#000000";
  document.getElementById(elid+"OFF").style.backgroundColor="CC0000";
  document.getElementById(elid+"OFF").style.fontWeight="bold";
  document.getElementById(elid+"OFF").style.color="#FFFFFF";
  document.getElementById(elid+"IMG").src="/images/switch_r.gif";
 }
};

function checkAll(){
 for (i=0;i<document.forms[0].elements.length;i++)
 {
  var e=document.forms[0].elements[i];
  if ((e.name != 'allbox') && (e.type=='checkbox'))
  {
   e.checked=document.forms[0].allbox.checked;
  }
 }
};

function openTID1czWindow(tid) {
window.open('http://www.justice.cz/xqw/xervlet/insl/index?sysinf.@typ=or&sysinf.@strana=searchResults&hledani.@typ=subjekt&hledani.podminka.subjekt=&hledani.podminka.ico=' + tid + '&hledani.podminka.obec=&hledani.podminka.spisZnacka.oddil=&hledani.podminka.spisZnacka.vlozka=&hledani.podminka.spisZnacka.soud=0&hledani.format.pocet_polozek=50&hledani.format.trideni=netridit&hledani.format.typHledani=x*&hledani.format.obchodniJmeno=platne&hledani.podminka.pravniForma=-1&hledani.podminka.ulice=', 'tid', 'resizable=1,scrollbars=1,status=0,title=0,toolbar=0,width=800,height=700')
};

function openTID2czWindow(tid) {
 window.open('http://wwwinfo.mfcr.cz/cgi-bin/ares/ares_subj.cgi?JS=1&jazyk=cz&obch_jm=&ico=' + tid + '&cestina=cestina', 'tid', 'resizable=1,scrollbars=1,status=0,title=0,toolbar=0,width=800,height=700')
};

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function resizeIFrame() {
 v = getInsideWindowHeight() - findPos(document.getElementById('iframe').parentNode)[1];
 if (v < 50) v = 50;
 document.getElementById('iframe').style.height=v;
// alert(findPos(document.getElementById('iframex'))[1]);
};

function setDevices() {
 k = window.parent.myKaMap;
 o = k.objs;
 result = '';
 for (i=0;i<o.length;i++) {
  c = iframe.document.getElementById('eyeId_'+o[i].id);
  if(c != null) {
    if (c.checked) 
    result = result + o[i].id + ';';
  };
 };
 document.getElementById('eyes').value=result;
 this.form.submit();
};

function openDevices() {
 k = window.parent.myKaMap;
 o = k.objs;
 result = '';
 for (i=0;i<o.length;i++) {
  c = iframe.document.getElementById('eyeId_'+o[i].id);
  if(c != null) {
    if (c.checked) 
    result = result + o[i].id + ';';
  };
 };
 openResizableTargetPopupWindow('?fullscreen=1&setDevices=1&eyes='+result,'fullscreen',800,600);
}

function openDevicesL() {
 k = window.parent.myKaMap;
 o = k.objs;
 result = '';
 for (i=0;i<o.length;i++) {
  c = document.getElementById('eyeId_'+o[i].id);
  if(c != null) {
    if (c.checked) 
    result = result + o[i].id + ';';
  };
 };
 openResizableTargetPopupWindow('?fullscreen=1&setDevices=1&eyes='+result,'fullscreen',800,600);
}

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

function dateString(aDate) {
 if(aDate == null) return '';
 t = new Date();
 res = sprintf("%02u:%02u",aDate.getHours(),aDate.getMinutes());
 if(t.getDate()==aDate.getDate() && t.getMonth()==aDate.getMonth() && t.getFullYear()==aDate.getFullYear()) return res;
 res = sprintf("%02u.%02u.%02u",aDate.getDate(),aDate.getMonth()+1,aDate.getFullYear()%100) + ' ' + res;
 return res;
}

function intervalString(anInterval) {
 t = anInterval;
 r = '';
 if(anInterval == null) return '';
 if(t < 120) return 'OK';
 if(t > 86400*31) return '>30d';
 if(t > 86400) {r = r + Math.floor(t / 86400) + 'd '; t = t % 86400;}
 if(t > 3600) {r = r + sprintf("%02u:",Math.floor(t / 3600)); t = t % 3600;}
 r = r + sprintf("%02u:%02u",Math.floor(t/60),t%60);
 return r;
}

function asIntervalString(anInterval) {
 t = anInterval;
 r = '';
 if(anInterval == null) return '';
 if(t > 86400) {r = r + Math.floor(t / 86400) + 'd '; t = t % 86400;}
 if(t > 3600) {r = r + sprintf("%02u:",Math.floor(t / 3600)); t = t % 3600;}
 r = r + sprintf("%02u:%02u",Math.floor(t/60),t%60);
 return r;
}


function fcgiCopy(what) {
 for (i in what) {
  this[i] = (typeof what[i] == "object") ? new fcgiCopy(what[i]) : what[i];
 }
}

function confirmRequest(a,b,c,anId,d) {
s = new String(d);
s = s.replace('%1',a);
s = s.replace('%2',b);
if (confirm(s)) {
 cframe.window.location.href='?cmd=4&par=' + c + '&dev=' + anId;
};
}

function select_all() {
 v = document.getElementById("select_all_box").checked;
 for(i=0;i<document.form.elements.length;i++) {
  n = document.form.elements[i].name;
  if(n.substr(0,9)=="selected_")
   document.form.elements[i].checked=v;
 }
}

Date.prototype.setISO8601 = function(dString){ 
var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/; 
if (dString.toString().match(new RegExp(regexp))) { 
var d = dString.match(new RegExp(regexp)); 
var offset = 0; 
this.setUTCDate(1); 
this.setUTCFullYear(parseInt(d[1],10)); 
this.setUTCMonth(parseInt(d[3],10) - 1); 
this.setUTCDate(parseInt(d[5],10)); 
this.setUTCHours(parseInt(d[7],10)); 
this.setUTCMinutes(parseInt(d[9],10)); 
this.setUTCSeconds(parseInt(d[11],10)); 
if (d[12]) 
this.setUTCMilliseconds(parseFloat(d[12]) * 1000); 
else
this.setUTCMilliseconds(0); 
if (d[13] != 'Z') { 
offset = (d[15] * 60) + parseInt(d[17],10); 
offset *= ((d[14] == '-') ? -1 : 1); 
this.setTime(this.getTime() - offset * 60 * 1000); 
} 
} 
else { 
this.setTime(Date.parse(dString)); 
} 
return this; 
};

function isoAsDate(s) {
 if(!s) return null;
 n = new Date();
 n.setISO8601(s);
 return n;
}

function prefUpdateMain(nm) {
 var mc = document.getElementById(nm).checked;
 var pr = document.getElementById('privileges').getElementsByTagName('input');
 for(var i=0; i<pr.length; i++) {
  if(pr[i].name.match(new RegExp(nm,''))) pr[i].checked=mc;
 }
}

function prefResetMain(nm) {
 var pr = document.getElementById('privileges').getElementsByTagName('input');
 var allset = true;
 for(var i=0; i<pr.length; i++) {
  if(pr[i].name.match(new RegExp(nm,''))) allset = allset && pr[i].checked;
 }
 if(!allset) document.getElementById(nm).checked = allset;
}

function distance(lat1,lon1,lat2,lon2) {
 var r = 6378700;
 var p = 57.295778666662;
 var la1 = (lat1 / p / 3600000);
 var la2 = (lat2 / p / 3600000);
 var lo1 = (lon1 / p / 3600000);
 var lo2 = (lon2 / p / 3600000);
 var x = Math.sin(la1) * Math.sin(la2) + Math.cos(la1) * Math.cos(la2) * Math.cos(lo2-lo1);
 if(x>1) x = 1;
 return r * Math.acos(x);
}

var pokracuj = 1;

function alignTable(aHeader, aTable, w, aBottom) {
	   var b = document.getElementById(aTable);
	   var h = document.getElementById(aHeader);
	   if(b && h) {
	   for(var k = 0; k < 1; k++) {
		   		 
		   if(aBottom != null) {
			   var hg = getInsideWindowHeight()-getOffsetTop(b) - aBottom;
		       if (hg<50) hg = 50;
		       b.parentNode.parentNode.style.height= hg + "px";
		   }
		   var rowh = h.getElementsByTagName('tr');
		   var rowb = b.getElementsByTagName('tr');
		   if (rowb.length == 0 || rowh.length == 0) return;
		   var hh = rowh[0].getElementsByTagName('th');
		   var bh = rowb[0].getElementsByTagName('td');
		   var bs = b.insertRow(0);
		   for(var i = 0; i < hh.length; i++) {
		    var c = bs.insertCell(-1);
		    c.innerHTML=hh[i].innerHTML;
		   }
		 
		   for(var i = 0; i < hh.length; i++) {
		    hh[i].width=bh[i].offsetWidth-3;
		    bh[i].width=bh[i].offsetWidth-3;
		   }
		   
		   b.deleteRow(0);
		 
		   }
	   }
}

function getInsideWindowWidth() {
    if (window.innerWidth) {
        return window.innerWidth;
    }
    return 0;
}
// Return the available content height space in browser window
function getInsideWindowHeight() {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (document.body.parentElement.clientHeight) {
        return document.body.parentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}

function getOffsetTop(aTag) {
	var off = 0;
	var el = aTag;
	while(el.offsetParent) {
		off += el.offsetTop;
		el = el.offsetParent
	}
	return off;
}

navigator.sayswho= (function(){ 
  var N= navigator.appName, ua= navigator.userAgent, tem; 
  var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); 
  if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; 
  M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?']; 
  return M; 
 })(); 

