var xmlHttp_zemelapiui = null;
var xmlHttp_zemelapioPopup = null;

var row_num = 20;// page size (rows)

function sugeneruoti_lentele(page)
{
  xmlHttp_zemelapiui = GetXmlHttpObject();
  var url = "return.php";
  var method = "get";
  var data = null;
  var siusti = true;

  var zem_tipas = document.getElementById("zem_tipas").value;
  var koord_sis = document.getElementById("koord_sis").value;
  var mastelis = document.getElementById("mastelis").value;
  var rektifikuotas = document.getElementById("rektifikuotas").value;
  var publikuojamas = document.getElementById("publikuojamas").value;
  var duom_tipas = document.getElementById("duom_tipas").value;
  var formatas = document.getElementById("formatas").value;
//-- Get
  if(document.getElementById("istoriniai").checked) url += "?type=lentele&istorinis=true";
  else url += "?type=lentele&istorinis=false&tipas=" + zem_tipas;
  url += "&sistema=" + koord_sis;
  url += "&mastelis=" + mastelis;
  url += "&rektifikuotas=" + rektifikuotas;
  url += "&publikuojamas=" + publikuojamas;
  url += "&pletinys=" + formatas;
  url += "&dtipas=" + duom_tipas;
  url += "&row_num=" + row_num;
  if(page) url += "&page=" + page;
  else url += "&page=1";
  document.getElementById("showyears").value = "off"; // reikia WFS'ui
  if(document.getElementById("showyears").checked)
  {
    var metai_nuo = document.getElementById("metai_nuo").value;
    var metai_iki = document.getElementById("metai_iki").value;
    if (!IsNumeric(metai_nuo) || metai_nuo == "") { alert("Parametro \"nuo\" reikšmė turi būti ne neigiamas skaičius"); siusti = false; }
    if (!IsNumeric(metai_iki) || metai_iki == "") { alert("Parametro \"iki\" reikšmė turi būti ne neigiamas skaičius"); siusti = false; }
    url += "&metai_nuo=" + metai_nuo + "&metai_iki=" + metai_iki;
    document.getElementById("showyears").value = "on"; // reikia WFS'ui
  }
  document.getElementById("istoriniai").value = "on";
  document.getElementById("showBBoxForm").value = "off"; // reikia WFS'ui
  if(!document.getElementById("istoriniai").checked)
  {
    if(document.getElementById("showBBoxForm").checked)
    {
      if (!IsNumeric(document.getElementById("lonMin").value) || "" == document.getElementById("lonMin").value) { alert("Parametro \"Ilguma nuo\" reikšmė turi būti ne neigiamas skaičius"); siusti = false; }
      if (!IsNumeric(document.getElementById("latMin").value) || "" == document.getElementById("latMin").value) { alert("Parametro \"Platuma nuo\" reikšmė turi būti ne neigiamas skaičius"); siusti = false; }
      if (!IsNumeric(document.getElementById("lonMax").value) || "" == document.getElementById("lonMax").value) { alert("Parametro \"Ilguma iki\" reikšmė turi būti ne neigiamas skaičius"); siusti = false; }
      if (!IsNumeric(document.getElementById("latMax").value) || "" == document.getElementById("latMax").value) { alert("Parametro \"Platuma iki\" reikšmė turi būti ne neigiamas skaičius"); siusti = false; }
      var bbox = document.getElementById("lonMin").value+","+document.getElementById("latMin").value+","+document.getElementById("lonMax").value+","+document.getElementById("latMax").value;
      url += "&bbox=" + bbox;
      document.getElementById("showBBoxForm").value = "on"; // reikia WFS'ui
    }
    document.getElementById("istoriniai").value = "off";
  }

  if (siusti)
  { 
    xmlHttp_zemelapiui.onreadystatechange = sugeneruota_lentele;
    xmlHttp_zemelapiui.open(method, url, true);
    document.getElementById("zemelapiu_lentele").innerHTML = "";
    document.getElementById("krauna").style.display = "block";
    xmlHttp_zemelapiui.send(data);
  }
}

var pdivGlobal;

function sugeneruota_lentele() 
{ 
  if ((xmlHttp_zemelapiui.readyState == 4) && (xmlHttp_zemelapiui.status == 200))
  {
    document.getElementById("krauna").style.display = "none";
    // Jei atsakymas ateina sugeneruotas XML'u
    var xmlDoc = xmlHttp_zemelapiui.responseXML;
    var n = xmlDoc.getElementsByTagName("ROW").length;
    var id = xmlDoc.getElementsByTagName("ROW");
    
    var input = document.createElement("input");
    input.type = "hidden"; 
    input.id   = "rows";
    input.name = "rows";
    input.value = id.length;
    var webServiceForm_form = document.getElementById("webServiceForm_form");
    webServiceForm_form.innerHTML = "";
    webServiceForm_form.appendChild(input);
    for (var i = 1; i <= n; i++)
    {
      var input = document.createElement("input");
      input.type = "hidden"; 
      input.id   = "Id_"+i;
      input.name = "Id_"+i;
      input.value = id[i-1].attributes.getNamedItem("id").value;
      webServiceForm_form.appendChild(input);
    }

    config.objects.webServiceForm.submitForm();
        
    var borderColl = "collapse";
    var borderClr = "#AAAAAA"
    var fontSize = "xx-small";
    var fontFamily = "Verdana,arial,sans-serif";
    
    document.getElementById("uzklausos_rez").innerHTML = "";

    if (xmlDoc.getElementsByTagName("WARNING").length)  // Warning
    {
      var ispejimas = xmlDoc.getElementsByTagName("WARNING")[0].childNodes[0].nodeValue;
      var p = document.createElement("p");
      p.style.color = "#FF8800";
      font.innerHTML = "Įspėjimas (warning): "+ispejimas;
      document.getElementById("uzklausos_rez").appendChild(p);
    }
    if (xmlDoc.getElementsByTagName("ERROR").length)  // Error
    {
      var klaida = xmlDoc.getElementsByTagName("ERROR")[0].childNodes[0].nodeValue;
      var p = document.createElement("p");
      p.style.color = "#FF0000";
      p.innerHTML = "Klaida (error): "+klaida;
      document.getElementById("uzklausos_rez").appendChild(p);
    } 
    if(xmlDoc.getElementsByTagName("TOTAL_COUNT")[0].childNodes[0]) // TOTAL_COUNT 
      var viso = xmlDoc.getElementsByTagName("TOTAL_COUNT")[0].childNodes[0].nodeValue;
    var p = document.createElement("p");
    p.style.color = "#008800";
    p.innerHTML = "Užklausą tenkino "+viso+" įrašų(-ą, -ai).";

    document.getElementById("uzklausos_rez").appendChild(p);
    var p = document.createElement("p");
    p.style.color = "#0000FF";
    p.innerHTML = "Užklausa grąžino "+n+" įrašus(-ą, -ų).";
    document.getElementById("uzklausos_rez").appendChild(p);  
    var row                = xmlDoc.getElementsByTagName("ROW");
    var mastelis           = xmlDoc.getElementsByTagName("MASTELIS");
    var nomenklaturinis_nr = xmlDoc.getElementsByTagName("NOMENKL_NR");
    var koord_sist         = xmlDoc.getElementsByTagName("KOORD_SIST");
    var zemelapio_metai    = xmlDoc.getElementsByTagName("ZEMELAPIO_METAI");
    var pavadinimas        = xmlDoc.getElementsByTagName("PAVADINIMAS");
    var inventorinis_nr    = xmlDoc.getElementsByTagName("INVENTORINIS_NR");
    var tipas              = xmlDoc.getElementsByTagName("TIPAS");
    var rektifikuotas      = xmlDoc.getElementsByTagName("REKTIFIKUOTAS");
    var publikuojamas      = xmlDoc.getElementsByTagName("PUBLIKUOJAMAS");
    var www_nuoroda        = xmlDoc.getElementsByTagName("WWW_NUORODA");
    var download           = xmlDoc.getElementsByTagName("DOWNLOAD");

    if ("Microsoft Internet Explorer" == navigator.appName) document.getElementById("zemelapiu_lentele").innerHTML = "";
    else document.getElementById("zemelapiu_lentele").innerHTML = "<br />";
    
//---------------------- puslapiavimas --------------------------------
 
    if(viso > row_num) // tikrinti ar reikia puslapiavimo
    {
      var visoPuslapiu = Math.floor((viso - 1)/row_num) + 1;
      if (xmlDoc.getElementsByTagName("PAGE")[0]&&
          xmlDoc.getElementsByTagName("PAGE")[0].childNodes[0]) // tikrinti ar žinomas puslapio numeris
        activePage = eval(xmlDoc.getElementsByTagName("PAGE")[0].childNodes[0].nodeValue);
      else activePage = 1; // kadangi nežinomas puslapio numeris, priskiriamas 1 puslapis

      var pdiv = document.createElement("div");
      pdiv.style.textAlign = "center";
	  pdiv.style.margin = "0 0 0 0;";
      pdiv.style.padding = "3px";
      pdiv.innerHTML = '<span class="paging_text"><b>' + activePage + "</b> puslapis iš " + visoPuslapiu + '</span>';
	  document.getElementById('foundItems').innerHTML = '<span class="paging_text">Viso rasta <b>'+viso+'</b> įrašų.</span>';
      
      var div = document.createElement("div");
      div.style.textAlign = "center";
	  div.className = 'paging_links';
	  pdivGlobal = pdiv;
      
      for (i = -10; i <= 10; i++) // 10 prieš + einamasis + 10 po puslapių 
      {
        var currentPage = activePage+i; 
        if (currentPage > 0 && currentPage <= visoPuslapiu) // negali būti neigiamas puslapis
        {
          if (i == 0 ) // einamasis puslapis be nuorodos
          {
            var font = document.createElement("font"); // text'as
            font.innerHTML = currentPage + "&nbsp;";
            font.style.color = "#FF2200";
            div.appendChild(font);
          }
          else
          {
            var font = document.createElement("font"); // text'as
            font.innerHTML = currentPage;
            font.style.color = "#0022FF";
            var a = document.createElement("a"); // nuoroda
            a.appendChild(font);
            a.href = "javascript:sugeneruoti_lentele("+currentPage+");";
            //a.innerHTML = currentPage;
            div.appendChild(a);
            var font = document.createElement("font"); // nuoroda
            font.innerHTML = "&nbsp;";
            div.appendChild(font);
          }
        }
      }
      
      document.getElementById("zemelapiu_lentele").appendChild(div);
    }
    
//------------------------- end puslapiavimas -------------------------------    
    
    var thead = document.createElement("thead");
    var tr = document.createElement("tr");
    var b = document.createElement("b");
    b.innerHTML = "Pavadinimas";
    var th = document.createElement("th");
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily; 
    tr.appendChild(th);
    b = document.createElement("b");
    b.innerHTML = "Tipas";
    th = document.createElement("th");
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    tr.appendChild(th);
    b = document.createElement("b");
    b.innerHTML = "Koord. sis.";
    th = document.createElement("th");
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    tr.appendChild(th);
    b = document.createElement("b");
    b.innerHTML = "Mastelis";
    th = document.createElement("th");
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    tr.appendChild(th);
    b = document.createElement("b");
    b.innerHTML = "Metai";
    th = document.createElement("th");
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    tr.appendChild(th);
    b = document.createElement("b");
    b.innerHTML = "Nuoroda";
    th = document.createElement("th");
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    tr.appendChild(th);
    b = document.createElement("b");
    th = document.createElement("th");
    th.setAttribute("class", "sorttable_nosort");
    th.setAttribute("className", "sorttable_nosort"); // IE hack
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    var img = document.createElement("img");
    img.setAttribute("src", "images/issaugoti.gif");
    img.setAttribute("title", "Atsisiųsti žemėlapį");
    th.appendChild(img);
    tr.appendChild(th);
    b = document.createElement("b");
    th = document.createElement("th");
    th.setAttribute("class", "sorttable_nosort");
    th.setAttribute("className", "sorttable_nosort"); // IE hack    
    th.appendChild(b);
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    tr.appendChild(th);
    
    th = document.createElement("th");
    th.setAttribute("class", "sorttable_nosort");
    th.setAttribute("className", "sorttable_nosort"); // IE hack
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    img = document.createElement("img");
    img.setAttribute("src", "images/search_20px.gif");
    img.setAttribute("title", "Išryškinti");
    th.appendChild(img);
    var input = document.createElement("input");
    input.type = "checkbox";
    input.id = "highlightAll";
    input.onclick = function()
    {
      var inp = document.getElementsByTagName("input");
      l = inp.length;
      for (var i = 0; i < l; i++)
      {
        if ("checkbox" == inp[i].type && "highlightFeatures" == inp[i].name)
        {
          inp[i].checked = this.checked;
          check(inp[i]);
        }
      }
    }
    th.appendChild(input);
    tr.appendChild(th);

    th = document.createElement("th");
    th.setAttribute("class", "sorttable_nosort");
    th.setAttribute("className", "sorttable_nosort"); // IE hack
    th.style.borderCollapse = borderColl;
    th.style.borderColor = borderClr;
    th.style.fontSize = fontSize;
    th.style.fontFamily = fontFamily;
    img = document.createElement("img");
    img.setAttribute("src", "images/akis_12px.gif");
    img.setAttribute("title", "Rodyti");
    th.appendChild(img);
    var input = document.createElement("input");
    input.type = "checkbox";
    input.id = "viewAll";
    input.onclick = function()
    {
      var inp = document.getElementsByTagName("input");
      l = inp.length;
      for (var i = 0; i < l; i++)
      {
        if ("checkbox" == inp[i].type && "viewFeatures" == inp[i].name)
        {
          inp[i].checked = this.checked;
          view(inp[i]);
        }
      }
    }
    th.appendChild(input);
    tr.appendChild(th);
    
    thead.appendChild(tr);
    var tbody = document.createElement("tbody");
    for (var i = 0; i < n; i++)
    {
      tr = document.createElement("tr");

      td = document.createElement("td");
      if (pavadinimas[i].childNodes[0]) td.innerHTML = pavadinimas[i].childNodes[0].nodeValue;
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;
      td.style.fontSize = fontSize;
      td.style.fontFamily = fontFamily;
      tr.appendChild(td); // pavadinimas
      td = document.createElement("td");
      if (tipas[i].childNodes[0]) td.innerHTML = tipas[i].childNodes[0].nodeValue;
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;
      td.style.fontSize = fontSize;
      td.style.fontFamily = fontFamily;
      tr.appendChild(td); // tipas
      td = document.createElement("td");
      if (koord_sist[i].childNodes[0]) td.innerHTML = koord_sist[i].childNodes[0].nodeValue;
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;
      td.style.fontSize = fontSize;
      td.style.fontFamily = fontFamily;
      tr.appendChild(td); // koord_sist
      td = document.createElement("td");
      if (mastelis[i].childNodes[0]) td.innerHTML = mastelis[i].childNodes[0].nodeValue;
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;
      td.style.fontSize = fontSize;
      td.style.fontFamily = fontFamily;
      tr.appendChild(td); // mastelis
      td = document.createElement("td");
      if (zemelapio_metai[i].childNodes[0]) td.innerHTML = zemelapio_metai[i].childNodes[0].nodeValue;
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;
      td.style.fontSize = fontSize;
      td.style.fontFamily = fontFamily;
      tr.appendChild(td); // zemelapio_metai
      td = document.createElement("td");
      if(xmlDoc.getElementsByTagName("WWW_NUORODA")[i].childNodes[0])
        td.innerHTML = "<p><a href=\'"+xmlDoc.getElementsByTagName("WWW_NUORODA")[i].childNodes[0].nodeValue+"\' target=\'_blank\'>"+xmlDoc.getElementsByTagName("WWW_NUORODA")[i].childNodes[0].nodeValue+"</a></p>";
      else td.innerHTML = "<br />";
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;
      td.style.fontSize = fontSize;
      td.style.fontFamily = fontFamily;
      tr.appendChild(td); // nuoroda
           
      td = document.createElement("td");
      if (0 != download[i].childNodes[0].nodeValue)
      {
        var button = document.createElement("button");
        button.id = row[i].getAttribute("id");
        
        button.title = "Atsisiųsti žemėlapį";
        button.style.backgroundImage = "url(images/atsisiusti_16px.gif)";
        button.style.backgroundRepeat = "no-repeat";
		button.style.cursor = "pointer";
        button.style.width = "20px";
        button.style.height = "20px";
        
        button.style.border ="1px solid #226688";
        button.style.backgroundColor = "#BBDDBB";
   
        button.onclick = function()
        {
          document.getElementById("file_download_frame").src = "get_zip.php?num=" + this.id;
          //var url = "get_zip.php?num=" + this.id;
          //window.open(url, "_blank");
        }
        td.appendChild(button);
      }
      else td.innerHTML = "<br />";
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;     
      tr.appendChild(td); // žemėlapis
      
      var button = document.createElement("button");
      button.fid = xmlDoc.getElementsByTagName("ROW")[i].attributes.getNamedItem("id").value;
      
      button.title = "Visa informacija";
      button.style.width = "20px";
      button.style.height = "20px";
      button.style.border ="1px solid #999999";
      button.style.font = "bold 12px Times New Roman,serif";
      button.style.backgroundColor = "#FFFFFF";
      button.style.backgroundImage = "url(images/info_16px.gif)";
      button.style.backgroundRepeat = "no-repeat";
      button.style.cursor = 'pointer';

      button.onclick = function()
      {
        xmlHttp_zemelapioPopup = GetXmlHttpObject();
        var url = "return.php";
        var method = "get";
        url += "?type=lentele&fid=" + this.fid;
        var data = null;
        //ieskoti_paveiksliuku(this.fid);
        xmlHttp_zemelapioPopup.onreadystatechange = sugeneruotas_popup;
        xmlHttp_zemelapioPopup.open(method, url, true);
        xmlHttp_zemelapioPopup.send(data);
      }

      td = document.createElement("td");
      td.appendChild(button);
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;          
      tr.appendChild(td);
      
      var input = document.createElement("input");
      input.type = "checkbox";
      input.fid = "NZT_ZEMELAPIAI_VW." + row[i].getAttribute("id");
      input.id = "NZT_ZEMELAPIAI_VW." + row[i].getAttribute("id");
      input.name = "highlightFeatures";
      input.onclick = function() { check(this); }
      td = document.createElement("td");
      td.appendChild(input);
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;     
      tr.appendChild(td);
      
      var input = document.createElement("input");
      input.type = "checkbox";
      input.fid = "NZT_ZEMELAPIAI_VW." + row[i].getAttribute("id");
      input.id = "hide_NZT_ZEMELAPIAI_VW." + row[i].getAttribute("id");
      input.name = "viewFeatures";
      input.onclick = function()
      {
        view(this);
      }
      td = document.createElement("td");
      td.appendChild(input);
      td.style.borderCollapse = borderColl;
      td.style.borderColor = borderClr;     
      tr.appendChild(td);
      
      tbody.appendChild(tr);
    }
    var table = document.createElement("table");
    table.setAttribute("border", "0");
    table.width = "700px";
    table.style.borderCollapse = borderColl;
    table.style.borderColor = borderClr;

    table.appendChild(thead);
    table.appendChild(tbody);
    table.setAttribute("class", "sortable");
    
    sorttable.makeSortable(table);
    document.getElementById("zemelapiu_lentele").appendChild(table);
    document.getElementById("viewAll").checked = true;
    
    var inp = document.getElementsByTagName("input");
    l = inp.length;
    for (var i = 0; i < l; i++)
      if ("checkbox" == inp[i].type && "viewFeatures" == inp[i].name)
        inp[i].checked = true;
        
    document.getElementById("zemelapiu_lentele").style.display = "block";
    if (n > 0)
    {
      if (!document.getElementById("refresh"))
      {
        var refreshButton = document.createElement("div");
        refreshButton.id = "refresh";
        refreshButton.title = "Pašalinti užklausos rezultatus";
        refreshButton.style.backgroundImage = "url(images/refresh_24px.gif)";
        refreshButton.style.backgroundRepeat = "no-repeat";
        refreshButton.style.width = "24px";
        refreshButton.style.height = "24px";
        refreshButton.style.cssFloat = "left";
        refreshButton.style.border ="0px";        
        refreshButton.onclick = function()
        {
          if (config.objects.vectorGraphicsRenderer.targetModel.map) // remove and destroy layer
          {     
            if (config.objects.vectorGraphicsRenderer.olLayer)
            {
              config.objects.vectorGraphicsRenderer.model.setParam('gmlRendererLayer', null);
              if (config.objects.vectorGraphicsRenderer.targetModel.map == config.objects.vectorGraphicsRenderer.map)
              {
                config.objects.vectorGraphicsRenderer.olLayer.destroy();
                config.objects.vectorGraphicsRenderer.olLayer = null;
                document.getElementById("zemelapiu_lentele").innerHTML = "";
                this.style.display = "none";
                if (document.getElementById("infoButton")) document.getElementById("infoButton").style.display = "none";
              }
            }
          }
        }
        document.getElementById("mainButtonBar").appendChild(refreshButton);
      }      
      /*if (!document.getElementById("infoButton")) // idėti info klavisą
      {
        var infoButton = document.createElement("div");
        infoButton.id = "infoButton";
        infoButton.active = true;
        infoButton.title = "pasirinkti";
        infoButton.style.backgroundImage = "url(images/infoDisable.gif)";
        infoButton.style.backgroundRepeat = "no-repeat";
        infoButton.style.width = "24px";
        infoButton.style.height = "24px";
        infoButton.style.cssFloat = "left";
        infoButton.style.border ="0px";        
        infoButton.onclick = function()
        {
          if(infoButton.active)
          {
            config.objects.featureHighlighter.control.deactivate();
            this.style.backgroundImage = "url(images/infoEnable.gif)";
            this.active = false;
          }
          else
          {
            config.objects.featureHighlighter.control.activate();
            this.style.backgroundImage = "url(images/infoDisable.gif)";
            this.active = true;
          }
        }
        document.getElementById("mainButtonBar").appendChild(infoButton);
      }*/

      document.getElementById("refresh").style.display = "inline";
      //document.getElementById("infoButton").style.display = "inline";
    }
    else
    {
      //if (document.getElementById("infoButton")) document.getElementById("infoButton").style.display = "none";
      if (document.getElementById("refresh")) document.getElementById("refresh").style.display = "none";
    }
    if(pdivGlobal){
		pdivGlobal.style.margin = "0 0 15px 0;"
		document.getElementById("zemelapiu_lentele").appendChild(pdivGlobal);
	}
  }
}

function gauti_popup_info(fid)
{
  if (typeof fid != "undefined" && fid != "")
  {
    xmlHttp_zemelapioPopup = GetXmlHttpObject();
    //ieskoti_paveiksliuku(fid);
    xmlHttp_zemelapioPopup.onreadystatechange = sugeneruotas_popup;
    xmlHttp_zemelapioPopup.open("get", "return.php?type=lentele&fid=" + fid, true);
    xmlHttp_zemelapioPopup.send(null);
  }
}

function sugeneruotas_popup()
{
  if (xmlHttp_zemelapioPopup.readyState == 4 && xmlHttp_zemelapioPopup.status == 200)
  {
    var xmlPopup = xmlHttp_zemelapioPopup.responseXML;
    var id                 = xmlPopup.getElementsByTagName("ROW");
    var mastelis           = xmlPopup.getElementsByTagName("MASTELIS");
    var nomenklaturinis_nr = xmlPopup.getElementsByTagName("NOMENKL_NR");
    var koord_sist         = xmlPopup.getElementsByTagName("KOORD_SIST");
    var zemelapio_metai    = xmlPopup.getElementsByTagName("ZEMELAPIO_METAI");
    var pavadinimas        = xmlPopup.getElementsByTagName("PAVADINIMAS");
    var inventorinis_nr    = xmlPopup.getElementsByTagName("INVENTORINIS_NR");
    var tipas              = xmlPopup.getElementsByTagName("TIPAS");
    var rektifikuotas      = xmlPopup.getElementsByTagName("REKTIFIKUOTAS");
    var publikuojamas      = xmlPopup.getElementsByTagName("PUBLIKUOJAMAS");
    var duom_tipas         = xmlPopup.getElementsByTagName("DUOMENU_TIPAS");
    var aprasymas          = xmlPopup.getElementsByTagName("APRASYMAS");
    var formatas           = xmlPopup.getElementsByTagName("DUOMENU_FORMATAS");
    var www_nuoroda        = xmlPopup.getElementsByTagName("WWW_NUORODA");
    var download           = xmlPopup.getElementsByTagName("DOWNLOAD");
    
    if (id[0].attributes.getNamedItem("id"))
    {
      fid = id[0].attributes.getNamedItem("id").value;
    }
        
    popupWindow = window.open('','_blank','scrollbars=1,width=500,height=600,resizable=yes');
    var boolReiksme = null;
    var body = "<html>\n";
    body += "  <head>\n";
    body += "    <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n";
    body += "    <title>Visa žemėlapio informacija</title>\n";
    body += "    <style>\n";
    body += "      body {\n";
    body += "        style.overflow;\n";
    body += "      }\n";
    body += "    </style>\n";
    body += "    <script type=\"text/javascript\" src=\"ajax/ajax.js\"></script>\n";
    body += "    <script type=\"text/javascript\">\n";
    body += "      var xmlHttp_zemelapioImages = null;\n";
    body += "      function download()\n";
    body += "      {\n";
    body += "        document.getElementById(\"file_download_frame\").src=\"get_zip.php?num="+fid+"\";\n"; // var url='get_zip.php?num="+fid+"';\n";
    //body += "        window.open(url, '_blank');\n";
    body += "      }\n";
    body += "      function ieskoti_paveiksliuku(fid)\n";
    body += "      {\n";
    body += "        if (typeof fid != \"undefined\" && fid != \"\")\n";
    body += "        {\n";
    body += "          xmlHttp_zemelapioImages = GetXmlHttpObject();\n";
    body += "          xmlHttp_zemelapioImages.onreadystatechange = gauti_paveiksliukai;\n";
    body += "          xmlHttp_zemelapioImages.open(\"get\", \"findMapImages.php?id=\" + fid, true);\n";    
    body += "          xmlHttp_zemelapioImages.send(null);\n";
    body += "        }\n";
    body += "      }\n";
    body += "      function gauti_paveiksliukai()\n";
    body += "      {\n";
    body += "        if(xmlHttp_zemelapioImages.readyState == 4 && xmlHttp_zemelapioImages.status == 200)\n";
    body += "        {\n";
    body += "          var xmlPopupImg = xmlHttp_zemelapioImages.responseXML;\n";
    body += "          var Images = xmlPopupImg.getElementsByTagName(\"PATH\");\n";
    body += "          n = Images.length;\n";
    body += "          imagesHtml = \"\";\n";
    body += "          for(i = 0; i < n; i++)\n";
    body += "          {\n";
    body += "            if(Images[i].childNodes[0])\n";
    body += "              imagesHtml += \"<img src='\"+Images[i].childNodes[0].nodeValue+\"' border='0' /><br />\";\n";
    body += "          }\n";
    body += "          if(\"\" != imagesHtml)\n";
    body += "          {\n";
    body += "            imagesDiv = document.getElementById(\"images\");\n";
    body += "            imagesDiv.innerHTML = \"<br />\" + imagesHtml;\n";
    body += "          }\n";
    body += "        }\n";
    body += "      }\n";
    body += "    </script>\n";
	body += "	 <link rel=\"StyleSheet\" type=\"text/css\" href=\"css/style.css\" />\n";
	body += "	 <link rel=\"StyleSheet\" type=\"text/css\" href=\"css/common.css\" />\n";
    body += "  </head>\n";


    body += "  <body style=\"background:#eff3ed;text-align:center;\" onload=\"parent.ieskoti_paveiksliuku("+fid+")\">\n";
    body += "    <div class=\"popup_logo\">\n";
    body += "      <a href=\"http://www.nzt.lt\" target=\"_blank\">\n";
    body += "        <img src=\"images/hdr/popup_logo.jpg\" border=\"0\" />\n";
    body += "      </a>\n";
    body += "    </div>\n";
    body += "    <table class='popup_table'>\n";
    body += "      <tbody>\n";
    if (id[0].attributes.getNamedItem("id"))
    {  
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>ID</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+id[0].attributes.getNamedItem("id").value+"</td>\n";
      body += "        </tr>\n";
    }
    if (pavadinimas[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Pavadinimas</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+pavadinimas[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }
    if (tipas[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Žemėlapio tipas</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+tipas[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }   
    if (koord_sist[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Koordinačių sistema</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+koord_sist[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }   
    if (mastelis[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Mastelis</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+mastelis[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }
    if (rektifikuotas[0].childNodes[0])
    {
      if ("0" == rektifikuotas[0].childNodes[0].nodeValue) boolReiksme = "Ne";
      else boolReiksme = "Taip";
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Ar orientuotas?</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+boolReiksme+"</td>\n";
      body += "        </tr>\n";
    }
    if (publikuojamas[0].childNodes[0])
    {
      if ("0" == publikuojamas[0].childNodes[0].nodeValue) boolReiksme = "Ne";
      else boolReiksme = "Taip";
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Ar viešas?</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+boolReiksme+"</td>\n";
      body += "        </tr>\n";
    }
    if (duom_tipas[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Duomenų tipas</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+duom_tipas[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }      
    if (formatas[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Formatas</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+formatas[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }
    if (zemelapio_metai[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Leidybos metai</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+zemelapio_metai[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }
    if (nomenklaturinis_nr[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Nomenklatūrinis numeris</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+nomenklaturinis_nr[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }
    if (inventorinis_nr[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>Inventorinis numeris</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">"+inventorinis_nr[0].childNodes[0].nodeValue+"</td>\n";
      body += "        </tr>\n";
    }
    if (www_nuoroda[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
      body += "            <b>www nuoroda</b>\n";
      body += "          </td>\n";
      body += "          <td style=\"border-color: #AAAAAA;border-collapse: collapse;\">\n";
      body += "            <a href=\""+www_nuoroda[0].childNodes[0].nodeValue+"\" target=\"_blank\">"+www_nuoroda[0].childNodes[0].nodeValue+"</a>\n";
      body += "          </td>\n";
      body += "        </tr>\n";
    }
    if (download[0].childNodes[0].nodeValue && download[0].childNodes[0].nodeValue != 0)
    {
      body += "        <tr>\n";
      body += "          <td class=\"popup_label\">\n";
	  body += "          <b>Parsisiųsti</b>";
	  body += "          </td>\n";
	  body += "          <td>\n";
      body += "            <button title=\"Atsisiųsti žemėlapį\" style=\"border: 1px solid #226688; cursor:pointer; width: 20px; height: 20px; background-color: #BBDDBB; background-repeat: no-repeat; background-image: url(images/atsisiusti_16px.gif);\" onclick=\"download()\"></button>\n";
      body += "          </td>\n";
      body += "        </tr>\n";
    }
    if (aprasymas[0].childNodes[0])
    {
      body += "        <tr>\n";
      body += "          <td colspan=\"2\" style=\"border-color: #AAAAAA;border-collapse: collapse;\">\n";
      body += "            <b>Aprašymas</b><br />"+aprasymas[0].childNodes[0].nodeValue+"\n";
      body += "          </td>\n";
      body += "        </tr>\n";
    }  
    body += "      </tbody>\n";
    body += "    </table>\n";
    body += "    <div class=\"popup_image\" id=\"images\"></div>\n";
	body += "	 <div class=\"popup_btn\">\n";
    body += "    <button style=\"cursor: pointer;background:url('images/hdr/close_btn.jpg');border:none;width:126px;height: 31px\" onclick=\"window.close()\">\n";
    body += "      &nbsp";
    body += "    </button>\n";
    body += "    <iframe id=\"file_download_frame\" name=\"file_download_frame\" src=\"about:blank\" style=\"display:none\"></iframe>\n";
	body += "    <div>\n";
    popupWindow.document.write(body);
    popupWindow.document.close();
    popupWindow.focus();
  }
}

function check(element)
{
  if (element.checked) config.objects.featureHighlighter.pazymeti(element.fid);
  else                 config.objects.featureHighlighter.atzymeti(element.fid);
}

function view(element)
{
  if (element.checked) config.objects.vectorGraphicsRenderer.rodyti(element.fid);
  else                 config.objects.vectorGraphicsRenderer.paslepti(element.fid);
}

function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
  for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
      IsNumber = false;
    }
  }
  return IsNumber;
}
