function ToggleForgot() {
	$('forgot', 'login').invoke('toggle');
	$('email').focus();
}
function ToggleLogin() {
	$('login', 'forgot').invoke('toggle');
	$('username').focus();
}
// include required libraries
var libraries = [
	'/js/switchbox.js',
	'/js/date_select.js'
];
IncludeLibraries(libraries);

// submit form
function SubmitForm(obj) {
	// set values
	var id         = $(obj).up('form').id;
	var action_bar = $(obj).up('.action-bar');
	
	// disable actions
	DisableActions(action_bar);
	
	// post form
  $(id).submit();
}

// submit form via Ajax call to controller method
function AjaxSubmit(obj) {
	// set values
	var id         = $(obj).up('form').id;
	var action_bar = $(obj).up('.action-bar');
	
	// disable actions
	DisableActions(action_bar);
	
	// post form
	$(id).request({
		onComplete: function(transport) {
			// get response (expecting JSON)
			var response = transport.responseJSON;
			
			// re-enable actions
			EnableActions(action_bar);
			
			// errors?
			if (response.errors) {
				// clear previous errors
				ClearErrors(id);
				
				// build error message and highlight fields
				var msg    = '';
				var errors = $H(response.errors);
				errors.each(function(pair) {
					msg = msg + '<li>' + pair.value + '</li>';
					if (e = $('f-' + pair.key)) {
						e.addClassName('error');
					}
				});
				
				// display notice
				var notice = '<strong>Please note the following issues:</strong><ul>' + msg + '</ul>';
				SetNotice(escape(notice), 'error');
				
				// scroll to the top of the page
				// so we can see the notice
				$('top').scrollTo();
			} else if (response.notice) {
				// clear errors
				ClearErrors(id);
				
				// reset form
				$(id).reset();
				
				// display notice
				SetNotice(escape(response.notice));
				
				// scroll to the top of the page
				// so we can see the notice
				$('top').scrollTo();
			} else if (response.redirect) {
				// redirect
			  location.href = response.redirect;
			}
		}
	});
}

// disable/enable form actions
function DisableActions(container) {
	// disable/hide
	if ($(container)) {
		var list = $(container).select('button', 'a');
		list.each(function(e) {
			e.blur();
			e.disabled = true;
		});
		list = $(container).select('div');
		list.each(function(e) {
			e.hide();
		});
		
		// add "processing"
		$(container).insert('<div class="processing">processing...</div>');
	}
}
function EnableActions(container) {
	// remove "processing"
	list = $(container).select('div.processing');
	list.each(function(div) {
		div.remove();
	});
	
	// enable/show
	var list = $(container).select('button', 'a');
	list.each(function(e) {
		e.disabled = false;
	});
	list = $(container).select('div');
	list.each(function(e) {
		e.show();
	});
}

// clear error styled fields for a given form
function ClearErrors(id) {
	var list = $$('#' + id + ' fieldset > div[id]');
	list.each(function(row) {
		$(row.id).removeClassName('error');
	});
}

// define behavior to highlight field on focus
var FieldFocus = Behavior.create({
	onfocus:function() {
		this.element.addClassName('field-focus');
	},
	onblur:function() {
		this.element.removeClassName('field-focus');
	}
});

// add behavior to form fields
function AddFieldFocus(input) {
	// init list
	var list = [];
	
	// check input, can be either array of form ids or an element id
	// if neither, default to all forms in document
	if (IsArray(input)) {
		list = input;
	} else if (input != '' && (input = $(input))) {
		list = $A(input.getElementsByTagName('form'));
	} else {
		list = $A(document.getElementsByTagName('form'));
	}
	
	// add behavior
	list.each(function(form) {
		if (form = $(form)) {
			$A(form.getElements()).each(function(e) {
				var types = ['text', 'textarea', 'select-one', 'select-multiple', 'password'];
				if (types.indexOf(e.type) >= 0) {
					FieldFocus.attach(e);
				}
			});
		}
	});
}

// add when loaded
Event.observe(window, 'load', function() {
	AddFieldFocus();
});

