jQuery.fn.disable = function() {
    this.children().remove().end().append('<option selected value="">Not Available</option>').fadeTo('slow', .7);
};
jQuery.fn.clear = function() {
    if (this.id == 'id_city') {
    this.children().remove().end().append('<option selected value="">Choose a State</option>').fadeTo('slow', .7);
}
};

function show_locations() {
      if ($('#id_city').val() != '') { $('#id_city').fadeTo('slow', 1); }
    if ($('#id_county').val() != '') { $('#id_county').fadeTo('slow', 1); }
    if ($('#id_cong_district').val() != '') { $('#id_cong_district').fadeTo('slow', 1); }
}

function hide_locations() {
    $('#city-wrapper').fadeOut('slow');
    $('#county-wrapper').fadeOut('slow');
    $('#cong-district-wrapper').fadeOut('slow');
}

function clearSelect(el) {
    el.options.length = 0;
}


function populateSelect(el, items, text) {
    clearSelect(el);
    if (items.length > 0)
        el.options[0] = new Option(text, '');
    $.each(items, function () {
        el.options[el.options.length] = new Option(this.name, this.value);
    });
}


function populateCongDistrict(el, items) {
    clearSelect(el);
    if (items.length > 0)
        el.options[0] = new Option('Cong. District', '');
    $.each(items, function () {
        el.options[el.options.length] = new Option("District " + this.name + " (" + this.value + ")" , this.name);
    });
}


function fill_cities(state) {
    $.ajax({
        url: '/cow/get-cities/' + state + '.json/', 
        dataType: 'json',
        success: function(data) {
            populateSelect($('#id_city').get(0), data, "City");
            $('#id_city').attr('disabled',false);
            
        },
        error: function() {
            $('#id_city').disable();
        }
       });
}

function fill_counties(state) {
    $.ajax( {
            url: '/cow/get-counties/' + state + '.json/',
            dataType: 'json',
            success: function(data) {
                populateSelect($('#id_county').get(0), data, "County");
                $('#county-wrapper').show();
                $('#id_county').attr('disabled',false);
            },
            error: function() {
                    //clearSelect('#id_county');
                    $('#id_county').disable();
            }
        });
}

function fill_cong_districts(state) {
    $.ajax( {
            url: '/cow/get-cong-districts/' + state + '.json/',
            dataType: 'json',
            success: function(data) {
                populateCongDistrict($('#id_cong_district').get(0), data);
                $('#id_cong_district').attr('disabled',false);
                $('#cong-district-wrapper').show();
            },
            error: function() {
                    //clearSelect('#id_county');
                    $('#id_cong_district').disable();
            }
        });
}


function cow_ready() {
    if ($('#id_state').val() == 'US') {
        $('#id_city').hide();
        $('#city-wrapper > .help-text').hide();
        $('#id_county').hide();
         $('#county-wrapper > .help-text').hide();
        $('#id_cong_district').hide();
         $('#cong-district-wrapper > .help-text').hide();
    }
        


    $('#id_state').change(function() {
          clearSelect($('#id_city').get(0));
          clearSelect($('#id_county').get(0));
          clearSelect($('#id_cong_district').get(0));
          $('#costofwar-form').submit();
    });  


    $('#id_city').change(function() {
            clearSelect($('#id_county').get(0));
            clearSelect($('#id_cong_district').get(0));
            $('#costofwar-form').submit();
            // $('#id_county').val('').fadeTo('slow', .7);  
            //             $('#id_cong_district').val('').fadeTo('slow', .7);
            //             $(this).fadeTo('slow', 1);
    });

    $('#id_county').change(function() {
        if (this.value != '') {
            clearSelect($('#id_city').get(0));
            clearSelect($('#id_cong_district').get(0));
            $('#costofwar-form').submit();
            // $('#id_city').val('').fadeTo('slow', .7);  
            //             $('#id_cong_district').val('').fadeTo('slow', .7);
            //             $(this).fadeTo('slow', 1);
        }
    });

    $('#id_cong_district').change(function() {
        if (this.value != '') {
            clearSelect($('#id_city').get(0));
            clearSelect($('#id_county').get(0));
            $('#costofwar-form').submit();
            // $('#id_county').val('').fadeTo('slow', .7);  
            // $('#id_city').val('').fadeTo('slow', .7);
            // $(this).fadeTo('slow', 1);
        }
    });
    
}

