function update_region_field(src, dest, clear) {
    if($(src).val().length == 0){ return; }
    $.getJSON('/countries/ajax/region-codes/'+$(src).val()+'/', function(json) {
       var parent = $(dest).parent();
       var value = $(dest).val()
       var attributes = $(dest)[0].attributes;
       var stored = new Array(attributes.length);
       for (var index in attributes) {
	 var attribute = attributes[index];
	 if (attribute.nodeName == 'type') {
	      continue;
	 }
	 if (index == 'length') {
	      break; //weird bug?
	 }
	 var arr = new Array(2);
	 arr[0] = attribute.nodeName;
	 arr[1] = attribute.nodeValue;
	 stored[index] = arr;
       }
       var child_text = '';
       if (json.type == 'text') {
	 child_text = '<input type="text" id="'+dest.substring(1)+'" name="'+dest.substring(4)+'"/>';
       } else {
	 child_text = '<select id="'+dest.substring(1)+'" name="'+dest.substring(4)+'">'+json.html+'</select>';
       }
       parent.html(child_text);
       ndest = parent.children();
       for (var index in stored) {
	 var attribute = stored[index];
	 ndest.attr(attribute[0], attribute[1]);
       }
       if (!clear) {
	 ndest.val(value);
       }
    });
}
