// This file is automatically included by javascript_include_tag :defaults

function animate( ids ) {
	if( !(ids instanceof Array) ) ids = $A(arguments);
	ids.each( function(id) { 
		Effect.Appear( id ); 
	});
}

function AppearingElement(id) {
	this.id = id;
	Event.observe( document, 'dom:loaded', this.hide.bindAsEventListener( this ) );
	Event.observe( window, 'load', this.show.bindAsEventListener( this ) );
}

AppearingElement.prototype.show = function( /* event */ ) {
	Effect.Appear( this.id );
}

AppearingElement.prototype.hide = function( /* event */ ) {
	Element.hide( this.id );
}

AppearingElement.make = function( id ) {
	new AppearingElement( id );
}


/*
	A focused element is one which receives the focus as soon as it is instantiated.
*/

function FocusedElement(id) {
	this.id = id;
	Event.observe( window, 'load', this.setFocus.bindAsEventListener( this ) );
}

FocusedElement.prototype.setFocus = function ( /* event */ ) {
	$(this.id).focus();
}

FocusedElement.make = function( id ) {
	new FocusedElement( id );
}

/*
	Toggles display of detail element depending on state of check box
*/
function CheckBoxToggledDetail( check_box_id, detail_id ) {
	this.detail_id = detail_id;
	this.check_box_id = check_box_id;
	var handler = this.toggle.bindAsEventListener( this );
	Event.observe( window, 'load', handler );
	Event.observe( document, 'dom:loaded', function() {
		Event.observe( $(check_box_id), 'change', handler );
	})
}

CheckBoxToggledDetail.prototype.toggle = function( /* event */ ) {
	if( $(this.check_box_id).checked ) {
		// alert('Use registered office as service address');
		Element.hide( this.detail_id );
	} else {
		// alert('Specify service address');
		Element.show( this.detail_id );
	}
}

/*
	Toggles display of detail element depending on which option is selected from drop down list
*/
function SelectDetail( control_id, display_options ) {
	this.control_id = control_id;
	this.display_options = display_options;
	var handler = this.select.bindAsEventListener( this );
	Event.observe( window, 'load', handler );
	Event.observe( document, 'dom:loaded', function() {
		Event.observe( $(control_id), 'change', handler );
	})
}

SelectDetail.prototype.select = function( /* event */ ) {
	var select = $(this.control_id);
	if( !select ) {
		throw new Error( 'Select control could not be found. The id specified was ' + this.control_id );
	}
	var selected = select.options[ select.selectedIndex ];
	if( selected ) {
		var display_option = this.display_options[ selected.value ];
		display_option[1].each( Element.hide );
		display_option[0].each( Element.show );
	}
}

/*
	Toggles display of detail element depending on which option is selected from drop down list
*/
function FieldsetSelector( control_id, css_selector ) {
	this.control_id = control_id;
	this.display_options = {};

	var handler = this.select.bindAsEventListener( this );
	var this_selector = this;

	Event.observe( window, 'load', handler );
	Event.observe( document, 'dom:loaded', function() {
		Event.observe( $(control_id), 'change', handler );
		this_selector.all_elements = $$(css_selector || 'fieldset.dynamic');
	});
}

FieldsetSelector.prototype.select = function( /* event */ ) {
	var select = $(this.control_id);
	if( !select ) {
		throw new Error( 'Select control could not be found. The id specified was ' + this.control_id );
	}
	var selected = select.options[ select.selectedIndex ];
	if( selected ) {
		this.all_elements.each( Element.hide );
		this.display_options[ selected.value ].each( Element.show );
	}
}


/* Functions to help with Ajax searches */
function clearAllChildren (parent) {
	while( parent.hasChildNodes() ) {
		parent.removeChild( parent.firstChild );
	}
}

function searchInProgress( response, button, caption ) {
	var button = $(button);
	button.disabled = true;
	button.value = caption;

	var div = document.createElement( 'div' );
	div.className = 'searching';

	var response = $(response);
	clearAllChildren( response );
	response.appendChild( div );
}

function searchComplete( button, caption ) {
	var button = $(button);
	button.disabled = false;
	button.value = caption;
}


