﻿    var urlNames = [];
    var urlObjects = [];
    
    function urlRemove(windowName) {
        for (i=0; i<urlNames.length; i++) {
            if (urlNames[i] == windowName) {
                urlNames.splice(i,1);
                urlObjects.splice(i,1);
                break;
            }
        }
    }
    
    function urlContains(windowName)
    {
        for (i=0; i<urlNames.length; i++) {
            if (urlNames[i] == windowName) 
                return true;
        }
        return false;
    }
    
    function urlGetWindowObject(windowName)
    {
        for (i=0; i<urlNames.length; i++) {
            if (urlNames[i] == windowName)
                return urlObjects[i];
        }
        
        return null;
    }
    
    function urlAddWindow(windowObject, windowName)
    {
        if (urlContains(windowName)) {
            urlRemove(windowName);
        }

        urlNames.push(windowName);
        urlObjects.push(windowObject);
    }
    
    function openWindow(windowUrl, windowName, showUrlBar, reload)
    {
    
        var windowObject = urlGetWindowObject(windowName);
        
        if (windowObject != null) {
            if (windowObject.closed) {
                if (showUrlBar)
                    var windowObject = window.open(windowUrl, windowName, 'status=1,toolbar=0,location=1,menubar=0,resizable=1,scrollbars=1');
                else
                    var windowObject = window.open(windowUrl, windowName, 'status=1,toolbar=0,location=0,menubar=0,resizable=1,scrollbars=1');
                
                urlRemove(windowName);
                urlAddWindow(windowObject, windowName);
            }
            else {
                if (reload) {
                    var windowObject = window.open(windowUrl, windowName, 'status=1,toolbar=0,location=0,menubar=0,resizable=1,scrollbars=1');
                    windowObject.focus();
                }
                else
                    windowObject.focus();
            }
        }
        else {
            if (showUrlBar == 1)
                var windowObject = window.open(windowUrl, windowName, 'status=1,toolbar=0,location=1,menubar=0,resizable=1,scrollbars=1');
            else
                var windowObject = window.open(windowUrl, windowName, 'status=1,toolbar=0,location=0,menubar=0,resizable=1,scrollbars=1');

            urlRemove(windowName);
            urlAddWindow(windowObject, windowName);
        }
    }



    // -----------------------------------------------------------



    var PDFimageOriginal = null;
    var PDFOnMouseDown = null;
    var PDFOnMouseOver = null;
    var PDFOnMouseOut = null;

    function disablePrintPDF() {
        //set icon to loading inficator
        var toolbarImage = document.getElementById("Toolbar1PrintPDFImage");
        if (toolbarImage != null) {
            PDFimageOriginal = toolbarImage.src;
            toolbarImage.src = "images/callbackActivityIndicator.gif";
        }

        //disable events
        var toolbarTD = document.getElementById("Toolbar1PrintPDF");
        if (toolbarTD != null) {
            PDFOnMouseDown = toolbarTD.onmousedown;
            toolbarTD.onmousedown = "";
            PDFOnMouseOver = toolbarTD.onmouseover;
            toolbarTD.onmouseover = "";
            PDFOnMouseOut = toolbarTD.onmouseout;
            toolbarTD.onmouseout = "";
        }
    }

    function enablePrintPDF() {
        //set icon back to print icon
        var toolbarImage = document.getElementById("Toolbar1PrintPDFImage");
        if (toolbarImage != null) {
            toolbarImage.src = PDFimageOriginal;
        }

        //disable events
        var toolbarTD = document.getElementById("Toolbar1PrintPDF");
        if (toolbarTD != null) {
            toolbarTD.onmousedown = PDFOnMouseDown;
            toolbarTD.onmouseover = PDFOnMouseOver;
            toolbarTD.onmouseout = PDFOnMouseOut;
        }
    }

    //clears kvrsearch task inputs
    function cleanInputs() {
        document.getElementById('TaskManager1_kvr_input0').value = ''; //Pavadinimas
        document.getElementById('TaskManager1_kvr_input1')[0].selected = true; //Savivaldybe combo
        document.getElementById('TaskManager1_kvr_input2').value = ''; //IP
        document.getElementById('TaskManager1_kvr_input3').value = ''; //LA
        document.getElementById('TaskManager1_kvr_input4').value = ''; //PMK
        document.getElementById('TaskManager1_kvr_input5').value = ''; 
        document.getElementById('TaskManager1_kvr_input6').value = ''; //vietove
        document.getElementById('TaskManager1_kvr_input7')[0].selected = true; //Kilnojamas
        document.getElementById('TaskManager1_kvr_input8')[0].selected = true; //Tipas combo
        document.getElementById('TaskManager1_kvr_input9')[0].selected = true; //Potipis combo

        document.getElementById('TaskManager1_kvr_input8').disabled = true;
        document.getElementById('TaskManager1_kvr_input9').disabled = true;
    }


    //used to launch TIFF generation sequence. Bad name, but does the job
    function printPDF() {

        disablePrintPDF();

        var message = "ControlID=Map1&ControlType=Map&EventArg=PrintPDF";
        var context = map.controlName;
        eval(map.identifyCallbackFunctionString);
    }


    //reloads iframe for user would be presented with download dialog
    function loadDownload() {

        enablePrintPDF();

        var ifrm = document.getElementById("downloadFrame");
        ifrm.src = 'Pages/Controls/FileDownload.aspx';
    }

    //   ------------  Paieskom  ----------------


    function showObject(id, lid) {
        if (map) {
            //if (getSessionLapse() < maximumLapseTime)
            //{
            //          hbShowMapBusyIndicator(null);
            var context = '';
            var argument = 'EventArg=showObject&objID=' + id + '&layerID=' + lid;

            //      alert( argument );
            eval(searchUtilityCallbackName);
            //}
            //else
            //    showLapseAlert()
        }
    }

    //  ---------------- po 09.08.xx pakeitimu (pridejus 'Rusi')


    function toggleKVRSearchByKilnojamas() {
        var selectKilnojamas = document.getElementById('TaskManager1_kvr_input7');
        var selectTipas = document.getElementById('TaskManager1_kvr_input8');
        var selectPotipis = document.getElementById('TaskManager1_kvr_input9');

        var selectedKilnojamas = selectKilnojamas.selectedIndex;

        //lets decide by index, because value is not present ant text is internationalized
        if ((selectedKilnojamas == 0)) { //Visi
            selectTipas.selectedIndex = 0;
            selectTipas.disabled = true;
            selectPotipis.selectedIndex = 0;
            selectPotipis.disabled = true;
        } else if (selectedKilnojamas == 1) {//Kilnojamas
            selectTipas.selectedIndex = 0;
            selectTipas.disabled = false;
            fillCombo('TaskManager1_kvr_input8', 'TaskManager1_kvr_label_tipk');

            selectPotipis.selectedIndex = 0;
            selectPotipis.disabled = true;
        } else if (selectedKilnojamas == 2) {//Nekilnojamas
            selectTipas.selectedIndex = 0;
            selectTipas.disabled = false;
            fillCombo('TaskManager1_kvr_input8', 'TaskManager1_kvr_label_tip');

            selectPotipis.selectedIndex = 0;
            selectPotipis.disabled = false;
        }
    }

    function fillCombo_old(comboID, labelID) {
        var arrTipas = document.getElementById(labelID).innerHTML.split('|');
        var combo = document.getElementById(comboID);

        for (var count = combo.options.length - 1; count >= 0; count--) {
            combo.options[count] = null;
        }

        for (var i = 0; i < arrTipas.length; i++) {
            var oOpt = document.createElement('option');
            oOpt.setAttribute('value', arrTipas[i]);
            oOpt.appendChild(document.createTextNode(arrTipas[i]));
            combo.appendChild(oOpt);
        }
    }

    function fillCombo(lbox, arr) {

        var arrTipas = document.getElementById(arr).innerHTML.split('|');
        var sleSarasiuks = document.getElementById(lbox);
        sleSarasiuks.options.length = 0;

        if (arrTipas.length > 0) {
            for (var mi = 0; mi < arrTipas.length; mi++) {
//                var midx = temp[mi].indexOf("=");
//                var laukas = temp[mi].substring(0, midx);
//                var reiksme = temp[mi].substring(midx + 1);

                var reiksme = arrTipas[mi];
                
                var lsmi = sleSarasiuks.options.length;
                sleSarasiuks.options[lsmi] = new Option(reiksme, reiksme);

                //       alert( laukas + " - " + reiksme );
            }
        }
    }


    var progressEN = "Loading in progress. Please wait...";
    var progressLT = "Kraunamas žemėlapis. Prašome palaukti...";
    var lapseMessageEN = "Session has expired. Page will reload for you could continue working with the map!";
    var lapseMessageLT = "Baigėsi jūsų sesijos laikas. Kad galėtumėte toliau dirbti su žemėlapiu puslapis bus perkrautas!";


    function setProgressLoadingText() {
        var labelProgress = document.getElementById('lbProgress');
        if (labelProgress == null)
            return;

        if (getLangFromQString() == "en") {
            labelProgress.innerText = progressEN; //for IE
            //labelProgress.innerHtml = progressEN;
            labelProgress.textContent = progressEN; //for FF
            //labelProgress.childNodes[0] = progressEN;
        }
        else {
            labelProgress.innerText = progressLT;
            labelProgress.textContent = progressLT;
        }
    }


    function getLangFromQString() {
        var lang = getQStringParam("lang");

        if (lang != null) {
            if (lang.toLowerCase() == "en")
                return "en";
            else
                return "lt";
        }
        else
            return "lt";
    }



    function getQStringParam(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
            return "";
        else
            return results[1];
    }


    function checkLapseTime() {
        if (getSessionLapse() > maximumLapseTime) {
            showLapseAlert();
        }
        //else
        //{
        //reset session timeout for this action would extend session lapse
        //    setSessionLapse();
        //}
        return false;
    }

    function openResultsKVRPanel() {
        var panel2 = $get("ResultsKVR_Panel_Collapse");
        if (panel2 != null) panel2.style.display = "block";

        window.setTimeout("arcgisWebApp.adjustMapSize();", 500);
    }

    function closeVTRezultatai() {
        var vtrez = $get('VTRezultatai_Panel');
        vtrez.style.visibility = "hidden";
    }

    function openVTRezultatai() {
        var vtrez = $get('VTRezultatai_Panel');
        vtrez.style.visibility = "visible";
    }    
    