﻿// AddressSearchScript.js

document.onclick = function(e) {
    if (e == null || !e) {
        e = window.event;
    }

    var obj = e.target ? e.target : e.srcElement ? e.srcElement : null;

    var searchString = "ButtonOpenSelection";

    if (obj == null) {
        //	    alert('close');
        CloseAllSelectionPanels();
        return;
    }
    else {
        if (obj.id.length < searchString) {
            //    	    alert('close' + " obj.id: " + obj.id);
            CloseAllSelectionPanels();
            return;
        }
        else {
            if (obj.id.substring(0, searchString.length) != searchString) {
                CloseAllSelectionPanels();
                return;
            }
        }
    }

    //    alert('leave');
}

function CloseAllAndSearch() {
    CloseAllSelectionPanels();

    var fieldWithId = document.getElementById("PortalButtonToBeClicked");

    document.getElementById(fieldWithId.value).click();
}

function Tastendruck(Ereignis) {
    if (Ereignis == null || !Ereignis) {
        Ereignis = window.event;
    }
    if (Ereignis.keyCode == 13) {
        CloseAllAndSearch(); return false;
    }
}

document.onkeydown = Tastendruck;

function SearchButtonClicking() {
    CloseAllSelectionPanels();
}

function CloseAllSelectionPanels() {
    var searchString = "close";
    var allDivs = document.getElementsByTagName('div');

    for (var counter = 0; counter < allDivs.length; counter++) {
        if (allDivs[counter].id.substring(0, searchString.length) == searchString) {
            allDivs[counter].style.display = 'none';
            allDivs[counter].style.visibility = 'hidden';
        }
    }
}

function SetRouteButtonsVisible(routeButtons) {
    var theButtons = JSON.parse(routeButtons);

    var departureBtn = document.getElementById(theButtons.btnDeparture);
    var arrivalBtn = document.getElementById(theButtons.btnArrival);

    //    departureBtn.src = baseIconUrl + "/addressSearch/departure.png"
    //    arrivalBtn.src = baseIconUrl + "/addressSearch/arrival.png"
    if (departureBtn != null) {
        departureBtn.className = "AddressSearchRouteButtonVisible";
    }
    if (arrivalBtn != null) {
        arrivalBtn.className = "AddressSearchRouteButtonVisible";
    }
}

function SetRouteButtonsDisabled(routeButtons) {
    var theButtons = JSON.parse(routeButtons);

    var departureBtn = document.getElementById(theButtons.btnDeparture);
    var arrivalBtn = document.getElementById(theButtons.btnArrival);

    //    departureBtn.src = baseIconUrl + "/addressSearch/departure_disabled.png"
    //    arrivalBtn.src = baseIconUrl + "/addressSearch/arrival_disabled.png"
    if (departureBtn != null) {
        departureBtn.className = "AddressSearchRouteButtonHidden";
    }
    if (departureBtn != null) {
        arrivalBtn.className = "AddressSearchRouteButtonHidden";
    }
}

function SetChosenAdr(address, controlsId, elementsToCloseId, routeButtons) {

    //Set routing Buttons in correct mode
    SetRouteButtonsVisible(routeButtons);

    //Close or hide all elements that we dont want to see anymore
    CloseElements(elementsToCloseId);

    //elements where the address gets displayed (textboxes)
    var controls = document.getElementById(controlsId);
    var displayingControls = JSON.parse(controls.value);

    var countryControl = document.getElementById(displayingControls.Country);
    var zipControl = document.getElementById(displayingControls.Zip);
    var cityControl = document.getElementById(displayingControls.City);
    var streetControl = document.getElementById(displayingControls.Street);

    var hidCountryControl = document.getElementById(displayingControls.HidCountry);
    var hidStateControl = document.getElementById(displayingControls.HidState);
    var hidZipControl = document.getElementById(displayingControls.HidZip);
    var hidCityControl = document.getElementById(displayingControls.HidCity);
    var hdStreetControl = document.getElementById(displayingControls.HidStreet);
    var hdHouseNumberControl = document.getElementById(displayingControls.HidHouseNumber);

    var hidIdsControl = document.getElementById(displayingControls.HidIds);
    var hidSearchPrecisionControl = document.getElementById(displayingControls.HidSearchPrecision);

    var hidLong = document.getElementById(displayingControls.HidLong);
    var hidLat = document.getElementById(displayingControls.HidLat);

    var addressToDisplay = JSON.parse(address);

    //focussing and bluring is needed so that the watermark gets the right css style
    if (addressToDisplay.Zip != null) {
        hidZipControl.value = addressToDisplay.Zip;
        zipControl.value = addressToDisplay.Zip;
        zipControl.focus();
        zipControl.blur();
    }
    if (addressToDisplay.City != null) {
        hidCityControl.value = addressToDisplay.City;
        cityControl.value = addressToDisplay.City;
        cityControl.focus();
        cityControl.blur();
    }
    if (addressToDisplay.Street != null) {
        hdStreetControl.value = addressToDisplay.Street;
        streetControl.value = addressToDisplay.Street;
        if (addressToDisplay.HouseNumber != null) {
            // hdStreetControl.value = hdStreetControl.value + " " + addressToDisplay.HouseNumber;
            streetControl.value = streetControl.value + " " + addressToDisplay.HouseNumber;
        }
        streetControl.focus();
        streetControl.blur();
    }
    if (addressToDisplay.Country != null) {
        countryControl.value = addressToDisplay.Country;
        hidCountryControl.value = addressToDisplay.Country;
    }
    if (addressToDisplay.State != null) {
        hidStateControl.value = addressToDisplay.State;
    }
    if (addressToDisplay.HouseNumber != null) {
        hdHouseNumberControl.value = addressToDisplay.HouseNumber;
    }
    if (addressToDisplay.Ids != null) {
        hidIdsControl.value = addressToDisplay.Ids;
    }
    if (addressToDisplay.SearchPrecision != null) {
        hidSearchPrecisionControl.value = addressToDisplay.SearchPrecision;
    }
    if (addressToDisplay.Position != null) {
        if (addressToDisplay.Position.Longitude != null) {
            hidLong.value = addressToDisplay.Position.Longitude;
        }
        if (addressToDisplay.Position.Latitude != null) {
            hidLat.value = addressToDisplay.Position.Latitude;
        }
    }

    try {
        MoaAddressSearchDisplayAddress(address);
    }
    catch (e) {
    }
}

function ClearTextFields(controlsId) {
    var controls = document.getElementById(controlsId);
    var displayingControls = JSON.parse(controls.value);

    var zipControl = document.getElementById(displayingControls.Zip);
    var cityControl = document.getElementById(displayingControls.City);
    var streetControl = document.getElementById(displayingControls.Street);

    ClearTextField(zipControl);
    ClearTextField(cityControl);
    ClearTextField(streetControl);
}

function ClearTextField(field) {
    if (field != null) {
        field.value = "";
        field.text = "";
        field.focus();
        field.blur();
    }
}

function SearchIsPossible(controlsId, values, errorMsg) {
    var controls = document.getElementById(controlsId);
    var displayingControls = JSON.parse(controls.value);
    var watermarks = JSON.parse(values);

    var zipControl = document.getElementById(displayingControls.Zip);
    var cityControl = document.getElementById(displayingControls.City);

    var zipWatermark = watermarks.Zip;
    var cityWatermark = watermarks.City;

    if (zipControl.value == zipWatermark && cityControl.value == cityWatermark) {
        alert(errorMsg);
        return false;
    }
}


function DisableElement(elementId) {
    var elem = document.getElementById(elementId);
    if (elem != null) {
        elem.disabled = true;
    }
}


function HideSingleElement(elementToHideID) {
    var elem = document.getElementById(elementToHideID);
    if (elem) {
        elem.style.visibility = 'hidden';
        elem.style.left = 0 + "px";
        elem.style.height = 0 + "px";
        elem.style.width = 0 + "px";
    }
}


function ClickSearchButton(elementID) {
    document.getElementById(elementID).click();
}

function HideSelectionList(idOfSelectionPanel) {
    var pnl = document.getElementById(idOfSelectionPanel);
    pnl.className = "AddressSearchTotalListHidden";
}

function CloseElements(elementsToCloseId) {

    var elementsToClose = document.getElementById(elementsToCloseId);

    var elems = JSON.parse(elementsToClose.value);

    var countryControl = document.getElementById(elems.CountrySelectionButton);
    var cityControl = document.getElementById(elems.CitySelectionButton);
    var zipControl = document.getElementById(elems.ZipSelectionButton);
    var streetControl = document.getElementById(elems.StreetSelectionButton);
    var housenumberControl = document.getElementById(elems.HouseNumberSelectionButton);
    //var allResultsControl = document.getElementById(elems.PanelAllResults);
    var descriptionControl = document.getElementById(elems.PanelDescription);

    if (countryControl != null) {
        HideSingleElement(countryControl.id);
    }
    if (cityControl != null) {
        HideSingleElement(cityControl.id);
    }
    if (zipControl != null) {
        HideSingleElement(zipControl.id);
    }
    if (streetControl != null) {
        HideSingleElement(streetControl.id);
    }
    if (housenumberControl != null) {
        HideSingleElement(housenumberControl.id);
    }
    //    if (allResultsControl != null) {
    //        HideSingleElement(allResultsControl.id);
    //    }
    if (descriptionControl != null) {
        HideSingleElement(descriptionControl.id);
    }
}


