﻿var in_canada_or_us = true;



function OnCountryChange(parent_div_id, _country_id) {


    var s = TurnIntoIDSelector(parent_div_id, false);

    $(s + " .div_states").hide();
    $(s + " .div_cities").hide();
    $(s + " .city_ddl > option").remove(); // $(s + " .city_ddl").val("");   // 

    if (_country_id != null && _country_id != 0) {


        Fill_States(parent_div_id, _country_id, 0, function(selected_value) {

            var selected_state = selected_value;

            if ($(s + " .state_ddl option").size() == 0) {
                $(s + " .div_states").hide();
                $(s + " .div_cities").show();
                Fill_Cities(parent_div_id, _country_id, 0, 0, function() {
                    $(s + " .div_cities").show();
                });
            }
            else {
                $(s + " .div_states").show();
                if (selected_state != null && selected_state != 0) {

                    Fill_Cities(parent_div_id, _country_id, selected_state, 0, function() {
                        $(s + " .div_cities").show();
                    });
                }
            }

        });

    } else {

        $(s + " .state_ddl > option").remove();
    }
}

function Fill_Countries(parent_div_id, _country_id, _include_north_america, callback) {

    var s = TurnIntoIDSelector(parent_div_id, false);
    var selected_value = 0;
    var ddl_id = $(s + " .country_ddl").attr("id");

  
    if (ddl_id != null && ddl_id != "" && $(s + " .country_ddl option").size() == 0) {

        $.ajax({
            type: "POST",
            url: "/misc/Location.asmx/Countries",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{selected_country_id: '" + _country_id + "', include_north_america: " + _include_north_america + ", north_america_on_top: " + in_canada_or_us + "}",
            success: function(data) {
                if (data.d != null && data.d.length > 0) {
                    selected_value = Fill_DLL(ddl_id, data.d);
                }
                callback(selected_value);
            }
        });

    } else {
         callback(selected_value);
    }

}


function Fill_States(parent_div_id, _country_id, selected_state_id, callback) {
    var s = TurnIntoIDSelector(parent_div_id, false);
    var selected_value = 0;

    $.ajax({
        type: "POST",
        url: "/misc/Location.asmx/States",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{country_id: " + _country_id + ", selected_state_id: " + AlwaysReturnInt(selected_state_id) + "}",
        success: function(data) {

            if (data.d != null && data.d.length > 0) {
                var ddl_id = $(s + " .state_ddl").attr("id");
                selected_value = Fill_DLL(ddl_id, data.d);

            }
            callback(selected_value);

        }
    });
}


function OnStateChange(parent_div_id, _state_id) {

    $(s + " .div_cities").hide();
    $(s + " .city_ddl > option").remove();
    var s = TurnIntoIDSelector(parent_div_id, false);

    if (_state_id != null && _state_id != 0) {

        var _country_id = AlwaysReturnInt($(s + " .country_ddl").val());
        Fill_Cities(parent_div_id, _country_id, _state_id, 0, function() {
            $(s + " .div_cities").show();
        });

    }

}

function Fill_Cities(parent_div_id, _country_id, _state_id, selected_city_id, callback) {

    var s = TurnIntoIDSelector(parent_div_id, false);
    var ddl_id = $(s + " .city_ddl").attr("id");
    var txt_id = $(s + " .city_txt").attr("id");

    if (ddl_id != null && ddl_id != "") {
        $.ajax({
            type: "POST",
            url: "/misc/Location.asmx/Cities",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{country_id: " + _country_id + ", state_id: " + AlwaysReturnInt(_state_id) + ", selected_city_id: " + AlwaysReturnInt(selected_city_id) + "}",
            success: function(data) {
                if (data.d != null && data.d.length > 0) {

                    Fill_DLL(ddl_id, data.d);
                }
                callback();
            },
            error: function(xhr, err) {
            
            }
        });

    } else if (selected_city_id > 0 && txt_id != null && txt_id != "") {
     
        $.ajax({
            type: "POST",
            url: "/misc/Location.asmx/CityName",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: "{city_id: " + AlwaysReturnInt(selected_city_id) + "}",
            success: function(data) {
                if (data.d != null && data.d.length > 0) {
                    $(s + " .city_txt").val(data.d);
                }
                callback();
            }
        });

    } else {
         callback();
    }

}
