/*
======================================================================
 Liquid Nails: Product Information page JavaScript.
 Requires productCatalog.jsp.
======================================================================
*/

function my_init() {

	Event.observe( $( 'my_sort_code' ), 'click', my_updateForm );
	Event.observe( $( 'my_sort_name' ), 'click', my_updateForm );
	Event.observe( $( 'my_showCategories' ), 'click', my_updateForm );

	my_updateForm();

} // End my_init().

function my_updateForm() {

	var my_formOptions = $( 'my_formOptions' );
	var my_sort_code = $( 'my_sort_code' );
	var my_showCategories = $( 'my_showCategories' );

	// Display the form options.
	my_formOptions.style.visibility = 'visible';

	// Set the sort method.
	var sortBy = ( my_sort_code.checked ) ? 'number' : 'name';
	productCatalog.setSortBy( sortBy );

	// Set if categories should be displayed.
	productCatalog.setShowCategories( my_showCategories.checked );

	// Set the list options.
	var arr = productCatalog.getSelectOptions();
	my_setProductOptions( arr );

} // End my_updateForm().

function my_setProductOptions( options_arr ) {

	var my_productId = $( 'my_productId' );

	clearOptions( my_productId );

	options_arr.each(
		function( opt ) {
			addOption( my_productId, opt.text, opt.value );
		} // End function.
	);

} // End my_setProductOptions().

function my_validateForm() {

	var form = $('my_productInfoForm');
	var typeInputs = form.getInputs( 'radio', 'type' );
	var typeChecked = '';
	var my_productId = $( 'my_productId' );
	var selectedIndex = my_productId.selectedIndex;

	// Check if the user selected an info type. 
	typeInputs.each(
		function( input ) {
			if ( input.checked ) {
				typeChecked = input.value;
			} // End if.
		} // End function().
	);

	// Set the form's target based on info type.
	// If no type is selected, notify the user.
	switch ( typeChecked ) {

		case '':
			alert( 'Please select what information you would like to retrieve.' );
			return false;

		case 'msdsSheets':
		case 'techData':
			form.target = '_blank';
			break;

		default:
			form.target = '_self';

	} // End switch.

	if ( ( selectedIndex == -1 ) || ( my_productId.options[ selectedIndex ].value == '' ) ) {
		alert( 'Please select a product.' );
		my_productId.focus();
		return false;
	} // End if.

	// Track the MSDS/Tech Data downloads with Google Analytics.
	switch ( typeChecked ) {
		case 'msdsSheets':
		case 'techData':
			var trackUrl = 'x.productinfo?productId=' + my_productId.options[ selectedIndex ].value + '&type=' + typeChecked;
			pageTracker._trackPageview( trackUrl );
			break;
	} // End switch.

	return true;

} // End my_validateForm().

// Initialize the page.
//
Event.observe( window, 'load', my_init );
