var formId;
var firstSelectId;
var secondSelectId;
var firstSelect;
var secondSelect;
var secondaryOptions;

function disable_select_options( selectElement, emptyLabel, emptyValue ) {
	clear_select_options( selectElement );
	append_select_option( selectElement, emptyLabel, emptyValue );
	Form.Element.disable(selectElement);
}


function append_select_option( selectElement, label, value ){
	var option = document.createElement("option");
	option.value = value;
	option.appendChild(document.createTextNode(label));
	selectElement.appendChild( option );
}


function clear_select_options( selectElement ) {
	while (selectElement.childNodes.length > 0) {
		selectElement.removeChild(selectElement.childNodes[0]);
	}
}


function init_strada_sweeps_select() {
	
	formId = 'form_id_1';
	firstSelectId = 'c_2993_question_10';
	secondSelectId = 'c_2993_question_11';
	secondSelectEmptyLabel = '[Select an Occupation]';
	secondSelectEmptyValue = '';
	firstSelect = $(firstSelectId);
	secondSelect = $(secondSelectId);

	secondaryOptions = $H( {
		
		"option_1": $H( { 
			"option_4": "Less than 100",
			"option_5": "100 - 249",
			"option_6": "250 - 499",
			"option_7": "500 - 749",
			"option_8": "750 - 999",
			"option_9": "1000+"
		} ), 

		"option_2": $H( { 
			"option_10": "Less than 1000",
			"option_11": "1000 - 3999",
			"option_12": "4000 - 7999",
			"option_13": "8000 - 11999",
			"option_14": "12000 - 15999",
			"option_15": "16000+"
		} ),

		"option_3": $H( { 
			"option_43": "Not Applicable"
		} ),
		
		"[Select]": $H( { 
			secondSelectEmptyLabel: secondSelectEmptyValue
		} ),
		
		"Select one...": $H( { 
			secondSelectEmptyLabel: secondSelectEmptyValue
		} ),
		
		"": $H( { 
			secondSelectEmptyLabel: secondSelectEmptyValue
		} )
		
	} );
	
	
	/* Check to make sure the form this javascript is relevant to
	* actually exists on this page. */
	if( $(formId) != null ) {
		
		Event.observe( firstSelect, 'change', update_select_options );
		
		/* Clear out all the options */
		clear_select_options( secondSelect );
		
		/*
		 * If we already have an occupation selected we need to population
		 * the rice acres select.
		*/
		if ( $F(firstSelect) != "" ) {
			
			/* Pick the correct set of options from secontary options array */
			var secondaryOptionSet = secondaryOptions.find( function ( value, index ) {
				
				if( value[0] == $F(firstSelect) ) {
					return true;
				}
				return false;
			} );
			
			/* Add the options to the second select */
			secondaryOptionSet[1].each( function( option ) {
				append_select_option( secondSelect, option[1], option[0] );
			} );
			
			Form.Element.enable(secondSelect);
		}
		else {
			disable_select_options( secondSelect, secondSelectEmptyLabel, secondSelectEmptyValue );
		}
	}
};


function update_select_options( e ) {
	
	/* Clear out all the options */
	clear_select_options( secondSelect );
	
	/* Pick the correct set of options from secontary options array */
	var secondaryOptionSet = secondaryOptions.find( function ( value, index ) {
		
		if( value[0] == Event.element(e).value ) {
			return true;
		}
		return false;
	} );
	
	/* Add the options to the second select */
	secondaryOptionSet[1].each( function( option ) {
		append_select_option( secondSelect, option[1], option[0] );
	} );
	
	
	Form.Element.enable(secondSelect);
};


Event.observe(window, 'load', init_strada_sweeps_select, false);
