function readcookie(name) {
  var i, j, s1, s2, s3="", s4="";
  s1=document.cookie; s2=s1.split(';'); j=s2.length;
  for (i=0;(i<j && s4=="");i++) {
    s3=s2[i].split('=');
    if (s3[0].substring(0,1)==' ') s3[0]=s3[0].substring(1,s3[0].length);
    if (s3[0]==name) s4=s3[1];
  };
  if (s4) return unescape(s4); else return "";
};

function writecookie(name,value,expire) {
  d=new Date();
  if(expire) {
    d.setTime(d.getTime()+expire);
    document.cookie=name+"="+escape(value)
     +"; expires="+d.toGMTString();
  } else {
    document.cookie=name+"="+escape(value);
  };
};

function navi() {
  this.isn=false; this.isie=false; this.win=false; this.version="";
  if ((navigator.appName=="Netscape") || (navigator.userAgent.indexOf("Opera")>=0)) {
    this.isn=true;
    if (navigator.appVersion[0]=='4') this.version=4; else this.version=5;
  } else {
    this.isie=true;
  };
  if (navigator.userAgent.indexOf("Win")>=0) this.win=true;
  this.idmachine=readcookie("idmachine");
  if (this.idmachine=="") this.idmachine=Math.round(Math.random()*90000000)+10000000;
  writecookie("idmachine",this.idmachine,31536000000000);
};

function getobject(document,name) {
  if (document.all) return document.all[name]; else return document.getElementById(name);
};

function getstyle(document,name) {
  if (document.all) return document.all[name].style; else return document.getElementById(name).style;
};

function windowcenter(w,h) {
  return "left="+Math.round((screen.availWidth-w)/2)+",top="+Math.round((screen.availHeight-h)/2)+",width="+w+",height="+h;
};

function windowsize(l,t,w,h) {
  return "left="+l+",top="+t+",width="+w+",height="+h;
};

function colortoselect(scselect,scshape,c) {
  var i, s;
  scselect.length=17;
  scselect.options[0].text='černá'; scselect.options[0].value="0";
  scselect.options[1].text='stříbrná'; scselect.options[1].value="12632256";
  scselect.options[2].text='tmavě šedá'; scselect.options[2].value="8421504";
  scselect.options[3].text='bílá'; scselect.options[3].value="16777215";
  scselect.options[4].text='kaštanová'; scselect.options[4].value="128";
  scselect.options[5].text='červená'; scselect.options[5].value="255";
  scselect.options[6].text='tmavě fialová'; scselect.options[6].value="8388736";
  scselect.options[7].text='fuchsiová'; scselect.options[7].value="16711935";
  scselect.options[8].text='zelená'; scselect.options[8].value="32768";
  scselect.options[9].text='žlutozelená'; scselect.options[9].value="65280";
  scselect.options[10].text='olivová'; scselect.options[10].value="32896";
  scselect.options[11].text='žlutá'; scselect.options[11].value="65535";
  scselect.options[12].text='námořnická modrá'; scselect.options[12].value="8388608";
  scselect.options[13].text='modrá'; scselect.options[13].value="16711680";
  scselect.options[14].text='šedozelená'; scselect.options[14].value="8421376";
  scselect.options[15].text='tyrkysová'; scselect.options[15].value="16776960";
  scselect.options[16].text='vlastní'; scselect.options[16].value=c;
  for (i=0;i<17;i++) {if (scselect.options[i].value==c) {scselect.selectedIndex=i; break;};};
  s=switchrb(c).toString(16); while (s.length<6) s="0"+s;
  scshape.style.background="#"+s;
};

function colortoshape(scselect,scshape) {
  var s;
  s=switchrb(scselect.options[scselect.selectedIndex].value).toString(16); while (s.length<6) s="0"+s;
  scshape.style.background="#"+s;
};

function ns(i) {
  return (i>0?true:false);
};

function sn(l) {
  return (l?1:0);
};

function selectedindexbyvalue(document,name,value,defaultindex) {
  var i,s=getobject(document,name);
  s.selectedIndex=-1;
  if (value==undefined) return;
  if (s.options.length>0) for (i=0;i<s.options.length;i++) {
    if (s.options[i].value==value) {s.selectedIndex=i; break;};
  };
  if (s.selectedIndex==-1 && defaultindex!=undefined) s.selectedIndex=defaultindex;
};

function getselectedvalue(document,name) {
  s=getobject(document,name);
  if (s.selectedIndex==-1) return ""; else return s.options[s.selectedIndex].value;
};

function sni(s) {
  return (s==""?0:s);
};

function nis(i) {
  return (i==0?"":i);
};

function ensurerange(i,r1,r2) {
  return Math.min(Math.max(i,r1),r2);
};

function nameid(nameid) {
  return " name='"+nameid+"' id='"+nameid+"' ";
};

function switchrb(c) {
  return ((c & 0x0000FF) << 16)+(c & 0x00FF00)+((c & 0xFF0000) >> 16);
};

function radiocheck(document,name,checkedname) {
  var b=(name==checkedname?true:false);
  getobject(document,name).checked=b;
  return b;
};

function getmultipleselect(document,name) {
  var s=getobject(document,name),r="",i;
  if (s.options.length>0) for (i=0;i<s.options.length;i++) {
    if (s.options[i].selected) r+=(r==""?"":",")+s.options[i].value;
  };
  return r;
};

function setmultipleselect(document,name,values) {
  var s=getobject(document,name),a,v=","+values+",";
  if (s.options.length>0) for (i=0;i<s.options.length;i++) {
    if (v.indexOf(","+s.options[i].value+",")!=-1) s.options[i].selected=true;
  };
};

function clearmultipleselect(document,selectname) {
  var s=getobject(document,selectname),i;
  if (s.options.length>0) for (i=0;i<s.options.length;i++) s.options[i].selected=false;
};

function showmultipleselect(document,selectname,spanname) {
  var s=getobject(document,selectname),r="",i;
  if (s.options.length>0) for (i=0;i<s.options.length;i++) {
    if (s.options[i].selected) r+=s.options[i].text+"<br>";
  };
  getobject(document,spanname).innerHTML=r+(r==""?"":"<br><br>");
};

function fillselect(document,selectname,values) {
  var s=getobject(document,selectname),i,j=0;
  s.options.length=Math.floor(values.length/2);
  if (values.length>1) {
    for (i=0;i<values.length-1;i+=2) {
      s.options[j].text=values[i];
      s.options[j].value=values[i+1];
      j++;
    };
    s.selectedIndex=0;
  };
};

function simplefillselect(document,selectname,values) {
  var s=getobject(document,selectname),i;
  s.options.length=Math.floor(values.length);
  if (values.length>1) {
    for (i=0;i<values.length;i++) {
      s.options[i].text=values[i];
      s.options[i].value=i;
    };
    s.selectedIndex=0;
  };
};

function clearselect(document,selectname,values) {
  var s=getobject(document,selectname);
  s.options.length=0;
};

function getradiocheckedvalue(document,name) {
  var i=1,rb;
  while (true) {
    rb=getobject(document,name+i);
    if (rb==undefined) break;
    if (rb.checked) return rb.value;
    i++;
  };
};

function getcheckboxcheckedvalues(document,name) {
  var i=1,cb,r="";
  while (true) {
    cb=getobject(document,name+i);
    if (cb==undefined) break;
    if (cb.checked) r+=(r==""?"":",")+cb.value;
    i++;
  };
  return r;
};

function clearcheckboxes(document,name) {
  var i=1,cb;
  while (true) {
    cb=getobject(document,name+i);
    if (cb==undefined) break;
    cb.checked=false;
    i++;
  };
};

function enable(document,name,enabled) {
  var s=getobject(document,name);
  s.disabled=(enabled==undefined?false:!enabled);
};

function disable(document,name) {
  var s=getobject(document,name);
  s.disabled=true;
};

function escapesl(si) {
  var i,so="",ch;
  for (i=0;i<si.length;i++) {
    ch=si.charAt(i); switch (ch) {
      case " ": so+="%20"; break;
      case ",": so+="%2C"; break;
      case "&": so+="%26"; break;
      case "?": so+="%3F"; break;
      default: so+=ch;
    };
  };
  return so;
};

function sp(s) {
  return ((s="") || (s==undefined)?"&nbsp;":s);
};

function ps(s) {
  return (s=="&nbsp;"?"":s);
};

function selcolor() {
  return "paleturquoise";
};

function showwait(d) {
  var o;
  o=getobject((d==undefined?document:d),"work");
  if (o!=undefined) o.style.display="none";
  o=getobject((d==undefined?document:d),"wait");
  if (o!=undefined) o.style.display="block";
};

function negn(n) {
  return (n==1?0:1);
};

/**************************** APP FUNCTIONS ****************************/

function genmapid() {
  return Math.round(Math.random()*1000000000);
};

function map(mapid) {
  this.mapid=(mapid==undefined?genmapid():mapid);
  this.layers=new Array();
  this.wms=new Array();
  this.i=0;
  this.xc=1999999999; this.yc=1999999999; this.pbr=1;
  this.panl=1; this.panb=1;
  this.selchange=true;
};

function ovmap(mapid) {
  this.mapid=(mapid==undefined?genmapid():mapid);
  this.layers=new Array();
  this.wms=new Array();
  this.i=0;
  this.xc=1999999999; this.yc=1999999999; this.pbr=1;
  this.panl=1; this.panb=1;
};

function addlayertrun(map,kdbrec,idtable,vi,se) {
  this.index=map.i++;
  this.kdbrec=kdbrec; this.idtable=idtable; this.idtheme=0; this.vi=vi; this.se=se; this.la=0; this.tr=0; this.scalefrom=0; this.scaleto=0; this.coef=1;
  this.lascalefrom=0; this.lascaleto=0; this.latype=0; this.lacolor=5263440; this.labgcolor=16777215; this.lasize=18; this.lafont=1; this.lastyle=0; this.ladatatype=0; this.ladatafieldname="ID"; this.ladatasqltext="";
  this.sqlfilter="";
  this.wf=null; this.wn="";
};

function addlayer(map,kdbrec,idtable,idtheme,vi,se,la,tr,scalefrom,scaleto,coef) {
  this.index=map.i++;
  this.kdbrec=kdbrec; this.idtable=idtable; this.idtheme=idtheme; this.vi=vi; this.se=se; this.la=la; this.tr=tr; this.scalefrom=scalefrom; this.scaleto=scaleto; this.coef=coef;
  this.lascalefrom=0; this.lascaleto=0; this.latype=0; this.lacolor=5263440; this.labgcolor=16777215; this.lasize=18; this.lafont=1; this.lastyle=0; this.ladatatype=0; this.ladatafieldname="ID"; this.ladatasqltext="";
  this.sqlfilter="";
  this.wf=null; this.wn="";
};

function addlayerext(map,index,kdbrec,idtable,idtheme,vi,se,la,tr,scalefrom,scaleto,coef,lascalefrom,lascaleto,latype,lacolor,labgcolor,lasize,lafont,lastyle,ladatatype,ladatafieldname,ladatasqltext,sqlfilter) {
  this.index=index;
  this.kdbrec=kdbrec; this.idtable=idtable; this.idtheme=idtheme; this.vi=vi; this.se=se; this.la=la; this.tr=tr; this.scalefrom=scalefrom; this.scaleto=scaleto; this.coef=coef;
  this.lascalefrom=lascalefrom; this.lascaleto=lascaleto; this.latype=latype; this.lacolor=lacolor; this.labgcolor=labgcolor; this.lasize=lasize; this.lafont=lafont; this.lastyle=lastyle; this.ladatatype=ladatatype; this.ladatafieldname=ladatafieldname; this.ladatasqltext=ladatasqltext;
  this.sqlfilter=sqlfilter;
  this.wf=null; this.wn="";
};

function addwms(index,checked,expanded,urlgetcapabilities,rangesjtsk,formatlist,format,caption,scalefrom,scaleto) {
  this.index=index;
  this.checked=checked;
  this.expanded=expanded;
  this.urlgetcapabilities=urlgetcapabilities;
  this.rangesjtsk=rangesjtsk;
  this.formatlist=formatlist;
  this.format=format;
  this.caption=caption;
  this.scalefrom=scalefrom;
  this.scaleto=scaleto;
  this.layers=new Array();
};

function addwmslayer(checked,queryable,name,title) {
  this.checked=checked;
  this.queryable=queryable;
  this.name=name;
  this.title=title;
};

function maptostr(map) {
  return "mapid="+map.mapid+"&xc="+map.xc+"&yc="+map.yc+"&pbr="+map.pbr;
};

function maptoform(map,document,formname) {
  var i, j, c=",", q='"', layers="", f, wms="";
  f=getobject(document,formname);
  for (i=0;i<map.layers.length;i++) {layers+=
    map.layers[i].index+c+map.layers[i].kdbrec+c+map.layers[i].idtable+c+map.layers[i].idtheme+c+map.layers[i].vi+c+map.layers[i].se+c+map.layers[i].la+c+map.layers[i].tr+c+map.layers[i].scalefrom+c+map.layers[i].scaleto+c+map.layers[i].coef+c+
    map.layers[i].lascalefrom+c+map.layers[i].lascaleto+c+map.layers[i].latype+c+map.layers[i].lacolor+c+map.layers[i].labgcolor+c+map.layers[i].lasize+c+map.layers[i].lafont+c+map.layers[i].lastyle+c+map.layers[i].ladatatype+c+map.layers[i].ladatafieldname+c+escapesl(map.layers[i].ladatasqltext)+c+
    q+escapesl(map.layers[i].sqlfilter)+q+c;
  };
  f.mapid.value=map.mapid;
  f.xc.value=map.xc;
  f.yc.value=map.yc;
  f.pbr.value=map.pbr;
  f.layers.value=layers;
  f.mapwindow.value=map.bgcolor+c+map.suppressedgescale+c+map.showscale+c+map.recursiveenabled+c+map.ovmap+c+map.panl+c+map.panb+c+map.wmsactive;
  f.mapgrid.value=map.showgrid+c+map.gridtype+c+map.showgridtype+c+map.gridcolor+c+map.showsignature+c+map.gridxc+c+map.gridyc+c+map.gridrectwidth+c+map.gridrectheight;
  for (i=0;i<map.wms.length;i++) {wms+=
    map.wms[i].index+c+map.wms[i].checked+c+map.wms[i].expanded+c+q+escapesl(map.wms[i].urlgetcapabilities)+q+c+q+escapesl(map.wms[i].rangesjtsk)+q+c+q+escapesl(map.wms[i].formatlist)+q+c+map.wms[i].format+c+
    q+escapesl(map.wms[i].caption)+q+c+map.wms[i].scalefrom+c+map.wms[i].scaleto+c+map.wms[i].layers.length+c;
    for (j=0;j<map.wms[i].layers.length;j++) {wms+=
      map.wms[i].layers[j].checked+c+map.wms[i].layers[j].queryable+c+map.wms[i].layers[j].name+c+q+escapesl(map.wms[i].layers[j].title)+q+c;
    };
  };
  f.mapwms.value=wms;
};

function ovmaptostr(map) {
  return "ovmapid="+map.mapid+"&ovxc="+map.xc+"&ovyc="+map.yc+"&ovpbr="+map.pbr;
};

function ovmaptoform(map,document,formname) {
  var i, c=",", q='"', layers="", f;
  for (i=0;i<map.layers.length;i++) {layers+=
    map.layers[i].index+c+map.layers[i].kdbrec+c+map.layers[i].idtable+c+map.layers[i].idtheme+c+map.layers[i].vi+c+map.layers[i].se+c+map.layers[i].la+c+map.layers[i].tr+c+map.layers[i].scalefrom+c+map.layers[i].scaleto+c+map.layers[i].coef+c+
    map.layers[i].lascalefrom+c+map.layers[i].lascaleto+c+map.layers[i].latype+c+map.layers[i].lacolor+c+map.layers[i].labgcolor+c+map.layers[i].lasize+c+map.layers[i].lafont+c+map.layers[i].lastyle+c+map.layers[i].ladatatype+c+map.layers[i].ladatafieldname+c+escapesl(map.layers[i].ladatasqltext)+c+
    q+escapesl(map.layers[i].sqlfilter)+q+c;
  };
  f=getobject(document,formname);
  f.ovmapid.value=map.mapid;
  f.ovxc.value=map.xc;
  f.ovyc.value=map.yc;
  f.ovpbr.value=map.pbr;
  f.ovlayers.value=layers;
  f.ovmapwindow.value=map.bgcolor+c+map.suppressedgescale+c+map.showscale+c+map.recursiveenabled+c+map.ovmap+c+map.panl+c+map.panb;
  f.ovmapgrid.value=map.showgrid+c+map.gridtype+c+map.showgridtype+c+map.gridcolor+c+map.showsignature+c+map.gridxc+c+map.gridyc+c+map.gridrectwidth+c+map.gridrectheight;
};

function framemaprefresh(ext,document,formname) {
  if (ext) {
    maptoform(parent.map,document,formname);
    getobject(document,formname).submit();
  } else {
    parent.framemap.location.replace(parent.framemaphtt+"?"+maptostr(parent.map));
  };
};

function getformdef(formlist,kdbrec,idtable,formdef) {
  formdef.recordurl="";formdef.listurl="";formdef.winname="";formdef.winparam="";
  var al,af,i;
  al=formlist.split("|");
  if (al.length>0) for (i=0;i<al.length;i++) if (al[i].indexOf(kdbrec+";"+idtable)>-1) {
    af=al[i].split(";");
    formdef.recordurl=af[2];
    formdef.listurl=af[3];
    formdef.winname=af[4];
    formdef.winparam=af[5];
    break;
  };
};

function getclist(document) {
  var i=0,oc,s="";
  while (true) {
    oc=getobject(document,"c"+i);
    if (oc==undefined) break;
    s+=(i==0?"":",")+oc.value;
    i++;
  };
  return s;
};

function noimp() {
  alert("Function still no implementation.");
};

