function showErrors(xhr, selector) {
    try {
        jQuery(selector).html('The following errors were encountered:');
        var ul = jQuery('<ul class="errors"></ul>').appendTo(selector);
        jQuery(xhr.responseXML).find('error').each(function() {
            var message = jQuery(this).find('message').text();
            ul.append('<li>' + message + '</li>');
        });
    }
    catch (exception) {
        jQuery(selector).html('An unexpected error was encountered.');
    }
    jQuery(selector).addClass('errors').fadeIn();
}
function hideErrors(selector) {
    jQuery(selector).fadeOut();
}
function moveSelected(fromSelector, toSelector) {
    jQuery(fromSelector + " option:selected").each(function () {
        jQuery(toSelector)[0].add(this, null);
        jQuery(fromSelector)[0].remove(this.index);
    });
}
jQuery(document).ready(function() {
    jQuery(".datepicker").datepicker({
      dateFormat: 'yy-mm-dd', // year actually shown with 4 digits (yyyy-MM-dd)
      changeMonth: true,      // pull-down menu for month (saves scrolling through months)
      changeYear: true,       // pull-down menu for year (saves scrolling through months)
      showOn: 'both',         // show datepicker on either field focus or button click
      buttonImageOnly: true,  // prevent image being shown on (ugly-looking) button
      buttonImage: 'http://jqueryui.com/demos/datepicker/images/calendar.gif'
    });
    jQuery(".ckeditor").ckeditor({
    	language: 'en-au',
        toolbar: [
		    {name: 'clipboard', items : ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo']},
			{name: 'basicstyles', items : ['Bold','Italic','Underline','Subscript','Superscript','-','RemoveFormat']},
			{name: 'paragraph', items : ['NumberedList','BulletedList','-','Outdent','Indent','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','Table','HorizontalRule','SpecialChar']},
			'/',
			{name: 'styles', items : ['Styles','Format','Font','FontSize']},
			{name: 'colors', items : ['TextColor','BGColor']},
			{name: 'insert', items : ['Link','Unlink','Image']},
			{name: 'document', items : ['Source']}
    	]
	});
    jQuery('.tabbed').tabs({
    	show: function(event, ui) {
    		// Fix issue with text overflowing right border of panel when tabs first rendered.
    		// The width of elements with the panel were being assigned the panel's full width;
    		// this code subtracts double the padding value (for left + right) to avoid overflow.
    		// Note that when tabs are selected by clicking them, there is no overflow issue;
    		// however, that just means this code effectively does nothing so it causes no harm.
    		var padding = 5;
    		jQuery(ui.panel).css('padding', '' + padding + 'px');
    		jQuery(ui.panel).children().css('max-width', '' + (jQuery(ui.panel).innerWidth() - (padding * 2)) + 'px');
    	},
		ajaxOptions: {
			error: function(xhr, status, index, anchor) {
				jQuery(anchor.hash).html('Error loading tab.');
			}
		}
	});
});
