
/*--------------------------------------------------
 common.js

 Date: 6/17/2008
 Purpose: JavaScript functionality for all pages
/--------------------------------------------------*/




// Flags ------------------------------------------

// Global updating status flag for signaling mouse event functions
var _isPageUpdating = false;




/*******************************************************************
 initializePage()

 Date: 2/12/2009
 Purpose: Perform page initialization
********************************************************************/

function initializePage() {

	
	// Enable highlighting and activation of controls
	initializeElementHighlighting();

}



/*******************************************************************
 initializeElementHighlighting()

 Date: 5/22/2009
 Purpose: Enable highlighting and activation of controls
********************************************************************/

function initializeElementHighlighting() {
	var x = 0;
	var controls = $$(
		".bar-form-field input", 
		".bar-form-field select" 
	);
	controls.each( function(e){
			// Check whether highlighting has already been enabled
			var highlightingEnabled = ( $(e).readAttribute( "highlightingEnabled" ) != null );
			if( highlightingEnabled ) {
				// Already enabled, so skip this one
				return;
			}
			if( e.id == null ) {
				x++;
				e.id = "form_table__element_" + x;
			}
			$(e).observe( "focus", function(){ 
					$(e.id).addClassName("ControlHighlighted");
				} );
			$(e).observe( "blur", function(){ 
					$(e.id).removeClassName("ControlHighlighted");
				} );
			$(e).writeAttribute( "highlightingEnabled", "true" );
		} );
}



/***************************************************
 countChars( textboxID, counterID )

 Date: 10/18/2006
 Purpose: Count and limit the chars in a textbox. 
 Update a counter element with the remaining chars
***************************************************/

function countChars( textboxID, counterID ) {
	if( t = $( textboxID ) ) {
		var count = t.value.length;
		var max = t.getAttribute( "maxlength" ) * 1;
		if( count > max ) {
			t.value = t.value.substring( 0, max );
		} else {
			$( counterID ).innerHTML = '<br />characters left: ' + ( max - count );
		}
	}
}



/*******************************************************************
 highlightElement()

 Date: 6/17/2008
 Purpose: Add or remove highlighting from an element
********************************************************************/

function highlightElement( element, state ) {
	// If page is updating, then don't do anything
	if( _isPageUpdating ) return;
	var className = "Highlighted";
	state ? $(element).addClassName( className ) : $(element).removeClassName( className )
}



/*******************************************************************
 _h()

 Date: 3/10/2009
 Purpose: Alias of highlightElement()
********************************************************************/

function _h( element, state ) {
	highlightElement( element, state );
}



/***************************************************
 toggleContainer()

 Date: 12/12/2007
 Purpose: Toggle container display based on condition.
 containers parameter can be a string or an array
***************************************************/

function toggleContainer( containers, condition ) {
	var c;
	// Convert containers to an array, if not one already
	containers = Object.isArray( containers ) ? containers : [ containers ];
	// To display or not to display
	containers.each( function( e ){ if( c = $( e ) ) { condition ? c.show() : c.hide(); } } );
}



/*******************************************************************
 getSelectBoxIndexByValue()

 Date: 1/19/2007
 Purpose: Get an select box value's index
********************************************************************/	

function getSelectBoxIndexByValue( element, value ) {
	if( elem = $(element) ) {
		for( i = 0; i < elem.options.length; i++ ) {
			if( elem.options[i].value == value ) {
				return i;	
			}
		}
	}
}



/*******************************************************************
 clearSelectBox( elem )

 Date: 4/13/2007
 Purpose: Clear a select box's selected values
********************************************************************/	

function clearSelectBox( elementID ) {
	var e;
	if( e = $(elementID) ) {
		e.selectedIndex = 0;
		//for( i = 0; i < e.options.length; i++ ) {
		//	e.options[i].selected = false;
		//}
	}
}



/*******************************************************************
 getSelectedIndexArray( selectElement )

 Date: 4/25/2007
 Purpose: Returns the selected indexes as an array
********************************************************************/

function getSelectedIndexArray( selectElement ) {
	var i, indexArray = new Array();
	if( element = $( selectElement ) ) {
		for( i = 0; i < element.options.length; i++ ) {
			if( element.options[i].selected ) {
				indexArray.push(i);
			}
		}
	}
	return indexArray;
}




/*******************************************************************
 selectAllOptions( selectElement )

 Date: 4/25/2007
 Purpose: Selects all options 
********************************************************************/

function selectAllOptions( selectElement ) {
	var i, option;
	if( element = $( selectElement ) ) {
		for( i = 0; i < element.options.length; i++ ) {
			option = element.options[i];
			if( typeof( option ) == "object" ) {
				option.selected = true;
			}
		}
	}
}



/***************************************************
 attachRadioEventHandler()

 Date: 7/26/2007
 Purpose: Attach a handler to all buttons in a radio group
***************************************************/

function attachRadioEventHandler( baseName, eventName, handler ) {
	var i = 0;
	var element = new Object();
	while( element = $(baseName + "_" + i) ){
		Event.observe( element.id, eventName, handler );
		i++;
	}
}



/*******************************************************************
 getRadioValue( element )

 Date: 7/17/2007
 Purpose:  Get a radio field's value
***********************************************************/	

function getRadioValue( radio ) {
	var value = "";
	if( typeof( radio ) != "object" ) {
		if( ! ( radio = document.forms[0].elements[radio] ) ) return;
	}
	if( radio.length ){
		for( i = 0; i < radio.length; i++ ) {
			if( radio[i].checked ) {
				value = radio[i].value;
				break;
			}
		}
	}
	return value;
}



/*******************************************************************
 getSelectedOptionValue()

 Date: 4/9/2009
 Purpose:  Get the text value of the selected option(s)
***********************************************************/	

function getSelectedOptionValue( selectBox ) {
	// Get object
	var selectBox = $(selectBox);
	// Get selected index(s)
	var selectedValues = $F(selectBox);
	if( !Object.isArray( selectedValues ) ) {
		selectedValues = [selectedValues];
	}
	// Iterate over options and get selected text
	var currentValue = "";
	var output = new Array();
	for( var k = 0; k < selectedValues.length; k++ ) {
		currentValue = selectedValues[k];
		for( var i = 0; i < selectBox.options.length; i++ ) {
			if( selectBox.options[i].value == currentValue ) {
				output.push( selectBox.options[i].text );
			}
		}
	}
	// Return value(s)
	return output.reduce();
}


/*******************************************************************
 selectOptionsMatchingPattern()

 Date: 4/15/2009
 Purpose: Selects options matching pattern
********************************************************************/

function selectOptionsMatchingPattern( selectElement, pattern ) {
	var i, option;
	var searchPattern = new RegExp(pattern, "i");
	if( element = $( selectElement ) ) {
		for( i = 0; i < element.options.length; i++ ) {
			option = element.options[i];
			if( typeof( option ) == "object" ) {
				if( option.text.match( searchPattern ) ) {
					option.selected = true;
				}
			}
		}
	}
}



/*******************************************************************
 deselectOptionsMatchingPattern()

 Date: 4/15/2009
 Purpose: Deselects options matching pattern
********************************************************************/

function deselectOptionsMatchingPattern( selectElement, pattern ) {
	var i, option;
	var searchPattern = new RegExp(pattern, "i");
	if( element = $( selectElement ) ) {
		for( i = 0; i < element.options.length; i++ ) {
			option = element.options[i];
			if( typeof( option ) == "object" ) {
				if( option.text.match( searchPattern ) ) {
					option.selected = false;
				}
			}
		}
	}
}







