﻿function GoogleMapLoad() {
    //    GBWebService.GB_wscv.set_timeout(5000);
    var oLand = FindControl("cmbGoogleLand");
    var sLand = comboselectedText(oLand);
    var sLKZ = comboselectedValue(oLand);
    var sOrt = FindControl("dfsGoogleOrt").value;
    var sPLZ = FindControl("dfsGooglePLZ").value;
    var sStrasse = FindControl("dfsGoogleStrasse").value;
    var sNr = FindControl("dfsGoogleNr").value;
    var cXMLRequest = new XMLRequest();
    cXMLRequest.CallBackFunc(OnSucceededGoogleMapData);
    cXMLRequest.GoogleMapData(sLKZ, sLand, sOrt, sPLZ, sStrasse, sNr);
}

function OnSucceededGoogleMapData(result) {
    if (result != "") {
        var oOrt = FindControl("dfsGoogleOrt");
        if (oOrt != null) {
            oOrt.value = result;
        }
    }
    var now = new Date();
    var ticks = now.getTime();
//    var hRef = idFrame.location.href + "?ID=" + ticks;
    //    alert(hRef);
    window.frames.idFrame.document.forms[0].submit();  //.location.href = hRef;
}

function comboselectedText(oSelect) {
    if (oSelect.selectedIndex != -1)
        return oSelect.options[oSelect.selectedIndex].text;
    return "";
}
function comboselectedValue(oSelect) {
    if (oSelect.selectedIndex != -1)
        return oSelect.options[oSelect.selectedIndex].value;
    return "";
}

function enableAutoRetry() {
    document.getElementById('enableAutoRetryButton').disabled = true;

    Sys.Net.WebServiceProxy.retryOnFailure =
            function(result, userContext, methodName, retryParams, onFailure) {
                if (result.get_timedOut()) {
                    if (typeof retryParams != "undefined") {
                        debug.trace("Retry: " + methodName);
                        Sys.Net.WebServiceProxy.original_invoke.apply(this, retryParams);
                    }
                    else {
                        if (onFailure) onFailure(result, userContext, methodName);
                    }
                }
                else {
                    if (onFailure) onFailure(result, userContext, methodName);
                }
            }

    Sys.Net.WebServiceProxy.original_invoke = Sys.Net.WebServiceProxy.invoke;
    Sys.Net.WebServiceProxy.invoke =
            function Sys$Net$WebServiceProxy$invoke(servicePath, methodName, useGet,
                params, onSuccess, onFailure, userContext, timeout) {
                var retryParams = [servicePath, methodName, useGet, params,
                onSuccess, onFailure, userContext, timeout];

                // Call original invoke but with a new onFailure handler which does the auto retry
                var newOnFailure = Function.createDelegate(this,
                function(result, userContext, methodName) {
                    Sys.Net.WebServiceProxy.retryOnFailure(result, userContext,
                        methodName, retryParams, onFailure);
                });

                Sys.Net.WebServiceProxy.original_invoke(servicePath, methodName, useGet,
                params, onSuccess, newOnFailure, userContext, timeout);
            }
}

var GlobalCallQueue = {
    _callQueue: [],    // Maintains the list of webmethods to call
    _callInProgress: 0,    // Number of calls currently in progress by browser
    _maxConcurrentCall: 2, // Max number of calls to execute at a time
    _delayBetweenCalls: 50, // Delay between execution of calls 
    call: function(servicePath, methodName, useGet,
            params, onSuccess, onFailure, userContext, timeout) {
        var queuedCall = new QueuedCall(servicePath, methodName, useGet,
                params, onSuccess, onFailure, userContext, timeout);

        Array.add(GlobalCallQueue._callQueue, queuedCall);
        GlobalCallQueue.run();
    },
    run: function() {
        /// Execute a call from the call queue

        if (0 == GlobalCallQueue._callQueue.length) return;
        if (GlobalCallQueue._callInProgress < GlobalCallQueue._maxConcurrentCall) {
            GlobalCallQueue._callInProgress++;
            // Get the first call queued
            var queuedCall = GlobalCallQueue._callQueue[0];
            Array.removeAt(GlobalCallQueue._callQueue, 0);

            // Call the web method
            queuedCall.execute();
        }
        else {
            // cannot run another call. Maximum concurrent 
            // webservice method call in progress
        }
    },
    callComplete: function() {
        GlobalCallQueue._callInProgress--;
        GlobalCallQueue.run();
    }
};

QueuedCall = function(servicePath, methodName, useGet, params,
        onSuccess, onFailure, userContext, timeout) {
    this._servicePath = servicePath;
    this._methodName = methodName;
    this._useGet = useGet;
    this._params = params;

    this._onSuccess = onSuccess;
    this._onFailure = onFailure;
    this._userContext = userContext;
    this._timeout = timeout;
}

QueuedCall.prototype =
    {
        execute: function() {
            Sys.Net.WebServiceProxy.original_invoke(
                this._servicePath, this._methodName, this._useGet, this._params,
                Function.createDelegate(this, this.onSuccess), // Handle call complete
                Function.createDelegate(this, this.onFailure), // Handle call complete
                this._userContext, this._timeout);
        },
        onSuccess: function(result, userContext, methodName) {
            this._onSuccess(result, userContext, methodName);
            GlobalCallQueue.callComplete();
        },
        onFailure: function(result, userContext, methodName) {
            this._onFailure(result, userContext, methodName);
            GlobalCallQueue.callComplete();
        }
    };

function enableQueuedCall() {
    document.getElementById('enableQueuedCallButton').disabled = true;

    Sys.Net.WebServiceProxy.original_invoke = Sys.Net.WebServiceProxy.invoke;
    Sys.Net.WebServiceProxy.invoke =
            function Sys$Net$WebServiceProxy$invoke(servicePath, methodName,
                useGet, params, onSuccess, onFailure, userContext, timeout) {
                GlobalCallQueue.call(servicePath, methodName, useGet, params,
                onSuccess, onFailure, userContext, timeout);
            }
}


function Trash() {
    GBWebService.GB_wscv.set_defaultFailedCallback(function(result, userContext, methodName) {
        var timedOut = result.get_timedOut();
        if (timedOut)
            debug.trace("Timedout: " + methodName);
        else
            debug.trace("Error: " + methodName);
    });
    GBWebService.GB_wscv.set_defaultSucceededCallback(function(result) {
        debug.trace(result);
    });
}

/* GoogleMap-Funktionen Beginn */
function KarteZugewiesen(bZugewiesen) {
    var cbGoogleZugewiesen1 = FindControl("cbGoogleZugewiesen1");
    if (cbGoogleZugewiesen1 != null) {
        var cbGoogleZugewiesen2 = FindControl("cbGoogleZugewiesen2");
        if (bZugewiesen) {
            cbGoogleZugewiesen1.checked = true;
            cbGoogleZugewiesen2.checked = false;
        }
        else {
            cbGoogleZugewiesen2.checked = true;
            cbGoogleZugewiesen1.checked = false;
        }
    }
    ShowGooglePreView(bZugewiesen);
}
/* blendet das ausgewählte Panel ein */
function ShowGooglePreView(bShow) {
    var panelGoogleMap = document.getElementById("panelGoogleVorschau2");
    if (panelGoogleMap != null) {
        if (bShow) {
            panelGoogleMap.style.display = 'block';
        }
        else {
            panelGoogleMap.style.display = 'none';
        }
    }
    var panVorschauKeine = document.getElementById("panelGoogleVorschau1");
    if (panVorschauKeine != null) {
        if (bShow) {
            panVorschauKeine.style.display = 'none';
        }
        else {
            panVorschauKeine.style.display = 'block';
        }
    }
}
function ResizeGoogleMap() {
    var panelGoogleMap = document.getElementById("divKarte");
    if (panelGoogleMap != null) {
        panelGoogleMap.style.position = '';
        panelGoogleMap.style.zIndex = '';
        panelGoogleMap.style.left = "";
        panelGoogleMap.style.top = "";
    }
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        var panDiv = divs[i];
        var idName = panDiv.id;
        // SprachBlock gefunden
        if (idName.indexOf('panKarte') >= 0) {
            panDiv.style.position = '';
            panDiv.style.zIndex = '';
            panDiv.style.left = "";
            panDiv.style.top = "";
        }
    }
}
/* GoogleMap-Funktionen Ende */
