function record_original_index( id ){
	
	var dropdown_field="toggleselecttext_values_dropdown"+id;
	var var_name=string_replace( "|", "_", dropdown_field+"_index" );
	var_name=string_replace( ">", "_", var_name );
	var_name=string_replace( ",", "_", var_name );
	if( eval("typeof "+var_name+"=='undefined'") ){
		eval(var_name+"="+get_element_value( dropdown_field, "selectedIndex" ));
	}
 
}
 
function toggle_child_dropdown_type( id, field_to_show, activator ){
	
	var dropdown_field="toggleselecttext_values_dropdown"+id;
	var text_field="toggleselecttext_custom_value"+id;
 
	//Don't continue if the dropdown was used to execute this function and "Add..." wasn't selected
	if( activator=="dropdown" && document.getElementById(dropdown_field)[get_element_value( dropdown_field, "selectedIndex" )].text!="Add..." ){
			return false;
		}
 
	//Determine which field to hide/show
	if( field_to_show=="auto" ){
	
		if( document.getElementById(dropdown_field+"_container").className!="hide" ){
			
			var field_to_show=text_field;
			
		}else{
		
			var field_to_show=dropdown_field;
		
		}
		
	}else if( field_to_show=="dropdown_field" ){
		field_to_show = dropdown_field;
	}else if( field_to_show=="text_field" ){
		field_to_show = text_field;
	}
	
	//Determine the opposite
	if( field_to_show==text_field ){
			
		var field_to_hide=dropdown_field;
		
	}else{
					
		var field_to_hide=text_field;
		
	}
 
		//If the dropdown is going to be shown, preselect the second option so as to not show the "Add..." option
	if( field_to_show==dropdown_field ){

		/**
		//Make sure that there's an option apart from the "Add..."
		var option_to_select=0;
		if( get_element_value( dropdown_field, "length" )>1 ){
			var option_to_select=1;
		}
		document.getElementById(dropdown_field)[option_to_select].selected=true;
		**/
 
	}
	
	//Show and enable
	toggle_visibility( field_to_show+"_container", "show" );
	toggle_disability( field_to_show, false );
 
	//Hide and disable
	toggle_visibility( field_to_hide+"_container", "hide" );
	toggle_disability( field_to_hide, true );
	
	if( field_to_show==dropdown_field ){	

		//If the "Add..." option is selected, select the original
		if( document.getElementById(dropdown_field)[get_element_value( dropdown_field, "selectedIndex" )].text=="Add..." ){
 
			var var_name=string_replace( "|", "_", dropdown_field )+"_index";
			var_name=string_replace( ">", "_", var_name );
			var_name=string_replace( ",", "_", var_name );
			eval("var original_index="+var_name);
				document.getElementById(dropdown_field)[original_index].selected=true;
 
			}
 
			change_element_value( "toggleselecttext_toggle_link"+id, "innerHTML", "Edit" );
	
	}else if( field_to_show==text_field ){
		
		//Show the toggle link
		document.getElementById("toggleselecttext_toggle_link"+id).innerHTML="Select a value";
			
			if( field_to_show==text_field  ){
 
				if( document.getElementById(dropdown_field)[get_element_value( dropdown_field, "selectedIndex" )].text!="Add..." ){
 
					var selected_index=document.getElementById(dropdown_field).selectedIndex;
					var selected_text=document.getElementById(dropdown_field)[selected_index].text;
					var selected_value=document.getElementById(dropdown_field)[selected_index].value;
					change_element_value( text_field, "value", selected_value );
 
					//Enable the dropdown field so that once value has been edited, the interview topic's id can be changed
					toggle_disability( dropdown_field, false );
 
				}else{
 
					change_element_value( text_field, "value", "" );
 
				}
 
			}
			
		}
					
	}

/*
 * Sets every other select or text field to the same value and state of this one
 * 
 */
function cascade_select_dropdown_values( id, select_id_pattern ){
	
	// Ids of each field
	var dropdown_field_prefix = "toggleselecttext_values_dropdown";
	var dropdown_field = dropdown_field_prefix+id;
	var text_field_prefix = "toggleselecttext_custom_value";
	var text_field = text_field_prefix+id;
 
	//Determine which field type is being shown
	if( document.getElementById(dropdown_field+"_container").className!="hide" ){
		
		var field_being_shown=dropdown_field;
		
		// Get the selected index of the current select dropdown
		var selected_index = document.getElementById(field_being_shown).selectedIndex;
		
	}else{
	
		var field_being_shown=text_field;
		
		// Get the selected index of the current select dropdown
		var current_value = document.getElementById(field_being_shown).value;
	
	}
	
	// If the field is a select dropdown
	if( field_being_shown==dropdown_field ){
		var all_fields = document.getElementsByTagName("select");
	}
	// If the field is a text field
	else if( field_being_shown==text_field ){
		var all_fields = document.getElementsByTagName("input");
	}

	// This pattern will determine whether a field is changed
	var select_id_pattern = select_id_pattern.split("...");
	
	// For each select or text field
	for( var a=0; a<all_fields.length; a++ ){
		
		var this_id = all_fields[a].id;
		
		// Determine whether the field's id matches the pattern
		if( 
		string_replace( select_id_pattern[0], "", this_id )!=this_id 
		&&
		string_replace( select_id_pattern[1], "", this_id )!=this_id
		){
		
			// If the field is a select dropdown
			if( field_being_shown==dropdown_field ){				
				// Toggle the field type
				toggle_child_dropdown_type( string_replace( dropdown_field_prefix, "", this_id ) ,'dropdown_field','link');
				document.getElementById(this_id).selectedIndex = selected_index;
			}
			// If the field is a text field
			else if( field_being_shown==text_field ){				
				// Toggle the field type
				toggle_child_dropdown_type( string_replace( text_field_prefix, "", this_id ) ,'text_field','link');
				document.getElementById(this_id).value = current_value;
			}
			
		}
		
	}
	
}
