$(document).ready(function(){
    $(".location").location_chooser("");
    $(".admin_location").location_chooser("admin/");
});

jQuery.fn.location_chooser = function(admin){
    $(this).children(".country_select").change(function(){
        jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),
            country:$(this).val(), field_set:$(this).parent().attr("id") }), dataType:'script', type:'post', url: "/"+admin+'location'});
    });
    $(this).children(".state_select").change(function(){
        jQuery.ajax({data:({authenticity_token : encodeURIComponent('<%= form_authenticity_token %>'),
            country:$(this).siblings(".country_select").val(), field_set:$(this).parent().attr("id"), state: $(this).val() }), dataType:'script', type:'post', url:"/"+admin+'location'});
    });
}