// Function to generate a handler for a dialog
function dialog_click(return_url, width) {
    return function() {
    // Pull out the "data-nwid" field from the <a...> tag and store
    // it locally...
    dialog_url =  $(this).attr('data-url');
    dialog_title =  $(this).attr('data-title');
    $("<div>").dialog({
	title: dialog_title,
	modal: true,
	resizable: false,
	width: width,
	position: [250,50],
	open: function() {
	    that$ = $(this);
	    data = $.get(dialog_url, null, function(data, status, x) {
	            that$.html(data);
		});
	},
	buttons: {
	    'Ok': function() {
		that=this;
		$.ajax({type: 'POST',
		       url: dialog_url,
		       data: $('form',$(this)).serializeArray(),
		       error:function(response, status) {
			   if (status == 'error')
			       location.replace('/http/' + response.status + '/');
			   else if (status == 'timeout')
			       location.replace('/http/timeout');
		       },

		       success:function(response, status, t) {
			   if (t.status == 205) {
			       $(that).dialog('close');
			       if (return_url != null) {
			           location.replace(return_url);
			           location.reload(true);
			       }
			   }
			   else {
			       $(that).html(response);
			   }
		       },
		       dataType:'html'});
			},
		'Cancel' : function() {
		    $(this).dialog('close')
		    }
		}
    });
  }
}

