$(document).ready(function () {
    $('#popupSubInfoError').hide();

    if ($(':radio[name="rblAddress"]').length > 0) {
        $(':radio[name="rblAddress"]:first', this).get(0).checked = true;
    }

    $("#m_ddlCountry").change(function () {
        var url = "GetStatesInCountry";
        var country = $('#m_ddlCountry :selected').text();
        $.ajax({
            datatype: "json",
            method: "get",
            data: { 'countryID': $(this).val() },
            cache: false,
            url: url
        }).done(
            function (states) {
                $('#hdn_Country').val(country);
                $("#m_ddlState").html("");
                $.each(states, function (key, state) {
                    $("#m_ddlState").append("<option value='" + state.ID + "'>" + state.Name + "</option>");
                });
            }
        );
    });

    $("#m_ddlState").change(function () {
        var state = $('#m_ddlState :selected').text();
        $('#hdn_State').val(state);
    });

    $(".rbl").change(function () {
        // Get the total number of addresses returned by validation
        var totalNumberOfAddresses = $(':radio[name="rblAddress"]').length;

        // Get the index of the radio button clicked
        var indexSelectedAddress = $(':radio[name="rblAddress"]:checked').index(':radio[name="rblAddress"]');

        var error = 'This address is NOT validated. Press "Cancel" and edit the address or "Continue" to use the selected address.';

        // If the address selected is the same as the one entered
        if (indexSelectedAddress == (totalNumberOfAddresses - 1)) {
            // Display the warning message
            $('#popupSubInfoError').text(error);
            $('#popupSubInfoError').show();
        } else {
            // Hide the warning message
            $('#popupSubInfoError').text('');
            $('#popupSubInfoError').hide();
        }
    });

    $("#m_btnSelectMatchingAddress").click(function () {
        // Create a JSON address
        var objSubscriberInfo = new Object();

        // Get the various field values and set object properties
        objSubscriberInfo.FirstName = $('#m_txtFirstName').val();
        objSubscriberInfo.LastName = $('#m_txtLastName').val();

        // Get the total number of addresses returned by validation
        var totalNumberOfAddresses = $(':radio[name="rblAddress"]').length;

        // Get the index of the radio button clicked
        var indexSelectedAddress = $(':radio[name="rblAddress"]:checked').index(':radio[name="rblAddress"]');

        // If the address selected is the same as the one entered, mark the adress as NOT VALIDATED
        if (indexSelectedAddress == (totalNumberOfAddresses - 1)) {
            objSubscriberInfo.AddressIsValidated = 0;
        } else
            objSubscriberInfo.AddressIsValidated = 1;

        var arr = $('input[name="rblAddress"]:checked').val().split('+');

        objSubscriberInfo.Address1 = arr[0];
        objSubscriberInfo.Address2 = arr[1];
        objSubscriberInfo.City = arr[2];
        objSubscriberInfo.Country = arr[3];
        objSubscriberInfo.CountryID = $('#m_ddlCountry :selected').val();
        objSubscriberInfo.State = arr[4];
        objSubscriberInfo.StateID = $('#m_ddlState :selected').val();
        objSubscriberInfo.PostalCode = arr[5];

        objSubscriberInfo.Email = $('#m_txtEmail').val();

        objSubscriberInfo.phone = '';

        // Store the stringified JSON object into a hidden field
        $("#hdn_SelectedAddress").val(JSON.stringify(objSubscriberInfo));
    });
});

function closePopup() {
    $('#popupSubInfo').hide();
}
