﻿function EmptyKml() {
    return '<?xml version="1.0" encoding="UTF-8"?>' +
              '<kml xmlns="http://www.opengis.net/kml/2.2">' +
              '    <Document>' +
              '        <name>' +
              '        </name>' +
              '        <description>' +
              '            <![CDATA[]]>' +
              '        </description>' +
              '    </Document>' +
              '</kml>';
}

/*
Displays the VisKort popup.
    
url: Url of Viskort component
editKml: Kml to be used as a template for the output placed in outputField
outputField: Id of field where Kml generated by VisKort is placed when the window is closed
viewKml: array of Kml to be displayed as read-only on the map
*/
function webformOpen(url, editKml, outputField, searchKml, viewKml) {
    var argsKml = {};
    if (editKml != null && editKml != "")
        argsKml.editKml = editKml;
    if (viewKml != null)
        argsKml.viewKml = viewKml;
    if (searchKml != null)
        argsKml.searchLayerKml = searchKml;

    var returnVal = window.showModalDialog(url, argsKml, "resizable:yes;dialogHeight:600px;dialogWidth:800px;");

    if (outputField != null && returnVal != null && returnVal.editKml != null && returnVal.editKml != '' && returnVal.editKml != EmptyKml()) {
        if (returnVal.editKml.toLowerCase().indexOf('placemark') != -1)
            outputField.value = escape(returnVal.editKml);

    }
    else
        outputField.value = '';

}

/*
Displays VisKort popup for inputting a position. If address information is present, 
it's passed along to VisKort to locate and display on map.
        
outputField: Id of control filled by VisKort with resulting Kml
addressField: Id of control holding street name and street number entered by user
postalCodeField: Id of control holding postal code entered by user
*/
function showFindCoordinatesDialog(outputField, addressField, postalCodeField) {
    var output = document.getElementById(outputField);
    var address = document.getElementById(addressField);
    var postalCode = document.getElementById(postalCodeField);

    // lookup address using the Address Web Service
    var addressMerged = ""
    if (address.value != "" || postalCode.value != "")
        addressMerged += "&AddressMerged=" + encodeURI(address.value + "," + postalCode.value);

    var url = '/VisKort/PopupMapDialog.aspx?CallingApp=DAFF&Resource=frekvens&' +
              'MapThemes=bg,sat,reg,kom,post,vindmpark&DefaultOn=bg&ActivePanelTabs=d&' +
              'ActivePaneId=d&pageid=def&MaxEditObjects=1&EditObjectTypes=point' + addressMerged
    var unescapedExistingKml = unescape(output.value);

    if (unescapedExistingKml == '')
        unescapedExistingKml = EmptyKml();

    webformOpen(url, unescapedExistingKml, output, null);
}

/*
Displays VisKort popup for inputting a polygon around a position. If latitude and
longitude of a position is passed along, the map zooms in to that location.
    
outputField: Id of control filled by VisKort with resulting Kml
viewKmlField: Id of control holding Kml containing position to display
*/
function showFindCoverageDialog(outputField, viewKmlField) {
    var output = document.getElementById(outputField);
    var input = document.getElementById(viewKmlField);
    var url = '/VisKort/PopupMapDialog.aspx?CallingApp=DAFF&Resource=frekvens&' +
              'MapThemes=bg,sat,reg,kom,post,vindmpark&DefaultOn=bg&ActivePanelTabs=d' +
              '&ActivePaneId=d&pageid=def&MaxEditObjects=1&EditObjectTypes=polygon'


    var unescapedPointKml = unescape(input.value);
    var unescapedAreaKml = unescape(output.value);

    if (unescapedAreaKml == '')
        unescapedAreaKml = EmptyKml();

    if (unescapedPointKml == '')
        webformOpen(url, unescapedAreaKml, output, null);
    else
        webformOpen(url, unescapedAreaKml, output, null, [unescapedPointKml]);
}

/*
Displays VisKort popup with a predefined area passed in and the ability to
edit the drawn multipolygon disabled.
    
viewKmlField: Html encoded Kml defining a predefined area
*/
function showPredefinedArea(outputField, viewKmlField) {
    var output = document.getElementById(outputField);
    var input = document.getElementById(viewKmlField);
    var url = '/VisKort/PopupMapDialog.aspx?CallingApp=DAFF&Resource=frekvens&ActivePanelTabs=d&MapThemes=bg,sat,reg,kom,post,vindmpark&DefaultOn=bg&ActivePaneId=d&pageid=def&MaxEditObjects=0&EditObjectTypes=polygon';

    // TODO: Disable drawing on map
    var unescapedKml = unescape(input.value);
    if (unescapedKml == '')
        webformOpen(url, EmptyKml(), output, null);
    else
        webformOpen(url, EmptyKml(), output, null, [unescapedKml]);

}

/*
Displays VisKort popup with a predefined area passed in and the ability to
edit the drawn multipolygon disabled.
    
viewKmlField: Html encoded Kml defining a predefined area
*/
function showGeographicalArea(inputField, outputField) {
    var input = document.getElementById(inputField);
    var output = document.getElementById(outputField);
    var url = '/VisKort/PopupMapDialog.aspx?CallingApp=DAFF&Resource=frekvens&ActivePanelTabs=d&MapThemes=bg,sat,reg,kom,post,vindmpark&DefaultOn=bg&ActivePaneId=d&pageid=def&MaxEditObjects=1&EditObjectTypes=polygon';

    var unescapedKml = unescape(input.value);
    if (unescapedKml == '')
        webformOpen(url, EmptyKml(), output, null);
    else
        webformOpen(url, unescapedKml, output, null);
}
/*
Displays VisKort popup in a non-editable view with extended data on the left side of the map (using SearchKml), Searchkml isn't encoded
*/
function showSearchResult(inputField) {
    var input = document.getElementById(inputField);
    var url = '/VisKort/PopupMapDialog.aspx?CallingApp=DAFF&amp;Resource=frekvens&amp;MapThemes=bg,sat,reg,kom,post,vindmpark&amp;DefaultOn=bg&amp;ActivePanelTabs=t&amp;ActivePaneId=t&amp;pageid=def&amp;searchlayerid=searchresult_Soeg';

    var unescapedKml = unescape(input.value);
    if (unescapedKml == '')
        webformOpen(url, EmptyKml(), null);
    else
        webformOpen(url, null, null, unescapedKml);
}

function showNonEditable(inputField) {
    var input = document.getElementById(inputField);
    var url = '/VisKort/PopupMapDialog.aspx?CallingApp=DAFF&amp;Resource=frekvens&amp;MapThemes=bg,sat,reg,kom,post,vindmpark&amp;DefaultOn=bg&amp;ActivePanelTabs=t&amp;ActivePaneId=t&amp;pageid=def&amp;searchlayerid=searchresultPredefinedArea';

    var unescapedKml = unescape(input.value);
    if (unescapedKml == '')
        webformOpen(url, EmptyKml(), null);
    else
        webformOpen(url, null, null, unescapedKml);
}


