// include required libraries
var libraries = [
	'/js/switchbox.js',
	'/js/date_select.js'
];
IncludeLibraries(libraries);

// submit form via Ajax call to controller method
function AjaxSubmit(button) {

	// set values
	var id          = button.form.id;
	var button_text = $(button).innerHTML;

	// disable button
	DisableButton(button);
	
	// post form
	$(id).request({


		onComplete: function(transport) {

	// get response (expecting JSON)
			var response = transport.responseJSON;
			
			// re-enable button
			EnableButton(button, button_text);
			
			// 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);
				
				//$(id).insert({'before':'<div style="height:' + $(id).getHeight() + 'px;"></div>'});
        $(id).hide();
				var parent = $(id).getOffsetParent();
        parent.hide();
				$$('.indicator').each(function(indicator) {
          indicator.hide();
        });
				
				// 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;
			}
		}
	});
}

// enable/disable form buttons
function DisableButton(e) {

	e.disabled = true;
	$(e).update('Processing...');
  $(e).addClassName('processing');
}
function EnableButton(e, value) {
	e.disabled = false;
	$(e).update(value);
  $(e).removeClassName('processing');
}

// clear error styled rows for a given form
function ClearErrors(id) {
	var list = $$('#' + id + ' table tbody > tr[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();
});
