﻿//function SetCityNameByZipCode(formId, code) {
//    SetCityNameByZipCodeWithId(formId, "#City", code);
//}

function SetCityNameByZipCode(formId, cityId, code) {
    $(formId + " " + cityId + " option").each(function () {
        var option = $(this);
        var array = $(this).val().split(",");
        if (array.length > 0) {
            for (var i = 0; i < array.length; i++) {
                if (array[i] == code) {
                    option.attr("selected", true);
                    break;
                }
                else { option.removeAttr('selected'); }
            }
        }
        else {
            option.removeAttr('selected');
        }
    });
}

//function SetCityName(formId, name) {
//    SetCityNameWithId(formId, "#City", name);
//}

function SetCityName(formId, cityId, name) {
    $(formId + " " + cityId + " option").each(function () {
        if (name == $(this).text()) {
            $(this).attr("selected", true);
        }
        else {
            $(this).attr("selected", false);
        }
    });
}

//function ResetCity(formId, defaultZipCodeValue, defaultCityValue) {
//    ResetCityWithId(formId, "#City", "#ZipCode", defaultZipCodeValue, defaultCityValue)
//}

function ResetCity(formId, cityId, zipcodeId, defaultZipCodeValue, defaultCityValue) {
    $(formId + " " + cityId + " option").each(function () {
        $(this).removeAttr('selected');
    });
    $(formId + " " + zipcodeId).val(defaultZipCodeValue);
    $(formId + " " + cityId + " option[value=\"" + empty + "\"]").remove();
    $("<option value=\"" + empty + "\" selected=\"selected\">" + defaultCityValue + "</option>").insertBefore(formId + " " + cityId + " option:first");
}

//function CheckCities(formId, defaultZipCodeValue) {
//    CheckCitiesWithId(formId, "#City", "#ZipCode", defaultZipCodeValue);
//}

function CheckCities(formId, cityId, zipcodeId, defaultZipCodeValue) {
    $(formId + " " + cityId + " option:selected").each(function () {
        var zipcode = $(this).val().split(",");
        var length = zipcode.length;
        if (length == 1 && zipcode[0] == empty) {
            $(formId + " " + zipcodeId).val(defaultZipCodeValue);
        }
        else if (length == 1) {
            $(formId + " " + zipcodeId).val(zipcode[0]);
        }
        else {
            var zc = $(formId + " " + zipcodeId).val();
            var isValid = false;
            for (var i = 0; i < zipcode.length; i++) {
                if (zipcode[i] == zc) {
                    isValid = true;
                    break;
                }
            }
            if (!isValid) $(formId + " " + zipcodeId).val(zipcode[0]);
        }
    });
}

//function SetRegionName(formId, regionidvalue) {
//    SetRegionNameWithId(formId, "#Region", regionidvalue);
//}

function SetRegionName(formId, regionId, regionidvalue) {
    $(formId + " " + regionId + " option").each(function () {
        if (regionidvalue == $(this).val()) {
            $(this).attr("selected", true);
        }
        else {
            $(this).attr("selected", false);
        }
    });
}

//function SetDistrictName(formId, regionidvalue) {
//    SetDistrictNameWithId(formId, "#District", regionidvalue);
//}

function SetDistrictName(formId, districtId, districtidvalue) {
    $(formId + " " + districtId + " option").each(function () {
        if (districtidvalue == $(this).val()) {
            $(this).attr("selected", true);
        }
        else {
            $(this).attr("selected", false);
        }
    });
}

function SetZipCodeChangedEvent(formId, cityId, zipcodeId, defaultZipCodeValue, defaultCityValue) {
    $(formId + " " + zipcodeId).change(function () {
        var zipcode = $(this).val();
        if (zipcode.length == 4) {
            var validZip = false;
            $(formId + " " + cityId + " option").each(function() {
                var option = $(this);
                var array = option.val().split(",");
                for (var i = 0; i < array.length; i++) {
                    if (array[i] == zipcode) {
                        validZip = true;
                        option.attr("selected", true);
                        break;
                    }
                    else { option.removeAttr('selected'); }
                }
            });
            if (!validZip) {
                ResetCity(formId, cityId, zipcodeId, defaultZipCodeValue, defaultCityValue);
            }
            else {
                $(formId + " .submitbuttoncity").click();
            }
        } else if (zipcode.length != 0 && zipcode != defaultZipCodeValue) {
            ResetCity(formId, cityId, zipcodeId, defaultZipCodeValue, defaultCityValue);
        }
        else {
            ResetCity(formId, cityId, zipcodeId, defaultZipCodeValue, defaultCityValue);
            $(formId + " .submitbuttoncity").click();
        }
    });
}

function SetCityChangedEvent(formId, cityId, zipcodeId, defaultZipCodeValue) {
    $(formId + " " + cityId).change(function () {
        CheckCities(formId, cityId, zipcodeId, defaultZipCodeValue);
    });
}

function SetRegionChangedEvent(formId, regionId) {
    $(formId + " " + regionId).change(function () {
        $(formId + " .submitbuttonregion").click();
    });
}
