jQuery(document).ready(function () {    

    $('.form-field-city').hide();
    $('.form-field-store').hide();

    loadFirst();

    $("#drpCountry").change(function () {
        fillCity();
        $('.form-field-store').hide();
    });
    $("#drpCity").change(function () {
        fillStore();
    });

    //Load Countries
    function loadFirst() {

        
        //dummy first option
        $('#drpCountry').append($('<option></option>').val("0").html("Please select country"));
        $.ajax({
            type: "GET",
            url: "vilaclubxml",
            dataType: "xml",
            success: function (xml) {
                $(xml).find('country').each(function () {
                    $('#drpCountry').append($('<option></option>').attr("code",$(this).attr('cCode')).val($(this).attr('cId')).html($(this).attr('cName')));
                });
            }
        });
    }

    // Load city
    function fillCity() {
        
        $('.form-field-city').show();
        $('#drpCity option').remove();
        $('#drpCity').attr('disabled', true);
        //dummy first option
        $('#drpCity').append($('<option></option>').val("0").html("Please select city"));

        if ($('#drpCountry').val() != "0") {
            var currentId =  $('#drpCountry').val();
            $.ajax({
                type: "GET",
                url: "vilaclubxml",
                dataType: "xml",
                success: function (xml) {
                    // foreach city on country id
                    $(xml).find("country[cId="+currentId+"]").find("city").each(function () {                    
                        $('#drpCity').append($('<option></option>').val($(this).attr('ciId')).html($(this).attr('cityName')));

                    });
                }
            });
            $('#drpCity').removeAttr('disabled');
        }
    }

    // Load store
    function fillStore() {
        $('.form-field-store').show();
        $('#drpStore option').remove();
        $('#drpStore').attr('disabled', true);

        //dummy first option
        $('#drpStore').append($('<option></option>').val("0").html("Please select store"));

        $('#btnGo').attr('disabled', true);

        if ($('#drpCity').val() != "0") {
            var currentId = $('#drpCity').val();
            $.ajax({
                type: "GET",
                url: "vilaclubxml",
                dataType: "xml",
                success: function (xml) {
                    // foreach store on city id
                    $(xml).find("city[ciId=" + currentId + "]").find("store").each(function () {
                        $('#drpStore').append($('<option></option>').attr("po",$(this).attr('sPO')).val($(this).attr('sId')).html($(this).attr('sName')));

                    });
                }
            });
            $('#drpStore').removeAttr('disabled');
        }
    }
});
