﻿        function CargarLocalidades(urlController, idTxtCp, idComboLocalidades) {
            var cp = $("#" + idTxtCp).val();
            if (cp == undefined || cp == null || cp.length < 5 || cp == "99999"){
                if (cp.length > 0)
                    alert('Código postal incorrecto');
                return;
            }

            $.ajax({
                type: "POST",
                url: urlController,
                async: false,
                data: "cp=" + cp,
                success:
                    function(result) {
                            $("#" + idComboLocalidades).html(result);
                            //Si sólo hay una opción, seleccionarla directamente
                            if ($("#" + idComboLocalidades).children().length == 2) {
                                $("#" + idComboLocalidades + " option:eq(1)").attr("selected", "selected");
                            }
                            else {
                                if ($("#" + idComboLocalidades).children().length == 1){
                                    alert('No se han encontrado localidades para el código postal ' + cp);
                                }
                        }
                    },
                error:
                    function(XMLHttpRequest, textStatus, errorThrown) {
                        ShowError(XMLHttpRequest, textStatus, errorThrown);
                    }
            });
        }