﻿function ChangeCountry(addressHolderId, countryDropDownListId, citiesAvailableForCountriesTextBoxId, countriesWithStreetFilterTextBoxId, addressAutoCompleteTextBoxId) {
    $().ready(function() {
        var countryDropDownList = document.getElementById(countryDropDownListId);
        var citiesAvailableForCountriesTextBox = document.getElementById(citiesAvailableForCountriesTextBoxId);
        var countriesWithStreetFilterTextBox = document.getElementById(countriesWithStreetFilterTextBoxId);
        var addressAutoCompleteTextBox = document.getElementById(addressAutoCompleteTextBoxId);

        var selectedCountryId = GetSelectedvalueFromDropDownList(countryDropDownList);

        var countryRequiresStreetFilter = CheckCountryStreetFilter(selectedCountryId, countriesWithStreetFilterTextBox);
        var citiesAvailableForCountry = CheckCitiesAvailableForCountry(selectedCountryId, citiesAvailableForCountriesTextBox);

        if (citiesAvailableForCountry) {
            $("#" + addressHolderId + " .CityAutoCompleteTitle").attr('style', 'display: normal;');
            $("#" + addressHolderId + " .AddressAutoCompleteTextBox").attr('style', 'display: normal;');
            $("#" + addressHolderId + " .StreetInformationRow").attr('style', 'display: normal;');

            if (countryRequiresStreetFilter) {
                $("#" + addressHolderId + " .StreetTitleRow").attr('style', 'display: none;');
                $("#" + addressHolderId + " .StreetValueRow").attr('style', 'display: none;');
                $("#" + addressHolderId + " .AddressAutoCompleteTextBox").attr('style', 'width: 300px;');
            }
            else {
                $("#" + addressHolderId + " .StreetTitleRow").attr('style', 'display: normal;');
                $("#" + addressHolderId + " .StreetValueRow").attr('style', 'display: normal;');
                $("#" + addressHolderId + " .AddressAutoCompleteTextBox").attr('style', 'width: 200px;');
            }
            $("#" + addressHolderId + " .FreeInputTextBox").val("");
            $("#" + addressHolderId + " .FreeInputRow").attr('style', 'display: none;');
        }
        else {
            $("#" + addressHolderId + " .CityAutoCompleteTitle").attr('style', 'display: none;');
            $("#" + addressHolderId + " .AddressAutoCompleteTextBox").attr('style', 'display: none;');
            $("#" + addressHolderId + " .StreetInformationRow").attr('style', 'display: none;');
            $("#" + addressHolderId + " .FreeInputRow").attr('style', 'display: normal;');
            $("#" + addressHolderId + " .AddressAutoCompleteTextBox").val("");
        }

        SetupAddressAutoComplete(addressAutoCompleteTextBox, countryDropDownListId, countryRequiresStreetFilter);
    });
}

function SetupAddressAutoComplete(autoCompleteTextBox, countryDropDownListId, countryRequiresStreetFilter) {
    var countryDropDownList = document.getElementById(countryDropDownListId);
    var selectedCountryId = GetSelectedvalueFromDropDownList(countryDropDownList);

    var originalyDisabled = autoCompleteTextBox.disabled;

    autoCompleteTextBox.disabled = true;

    $(autoCompleteTextBox).flushCache();
    var fullpath = GetBaseUrl() + '/Infomat/Pages/CitiesAutoComplete.aspx?countryId=' + selectedCountryId;

    $.get(fullpath, function(response) {
        var dataArray = response.split("||");

        if (countryRequiresStreetFilter) {
            $(autoCompleteTextBox).autocomplete(dataArray, {
                matchContains: true,
                minChars: 2,
                mustMatch: false,
                width: 300,
                max: 20
            });
        }
        else {
            $(autoCompleteTextBox).autocomplete(dataArray, {
                matchContains: true,
                minChars: 2,
                mustMatch: false,
                width: 200,
                max: 20
            });
        }

        if (originalyDisabled == false) {
            autoCompleteTextBox.disabled = false;
        }

        autoCompleteTextBox.focus();
    });
}

function CheckCitiesAvailableForCountry(countryId, citiesAvailableForCountriesTextBox) {
    if ($(citiesAvailableForCountriesTextBox).val().indexOf(countryId) != -1) {
        return true;
    }
    else {
        return false;
    }
}

function CheckCountryStreetFilter(countryId, countriesWithStreetFilterTextBox) {
    if ($(countriesWithStreetFilterTextBox).val().indexOf(countryId) != -1) {
        return true;
    }
    else {
        return false;
    }
}
