function pausecomp(millis) {
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); } 
	while(curDate-date < millis);
	}

// The first of 2 functions for interactive multi-menus
function add_drop_request( main_container, add_or_drop, associated_value ) {
	// First we're going to find a hidden input element containing a comma-delineated list of hard-wired
	// data field names to send along.
	hard_wired_fields_array = $(main_container).children(".hard_wired_field_names").val().split(",");  // The classnames of the hard wired fields
	
	// Instantiate a new javascript object that will hold our data
	data_to_send = {}
	
	// Loop through the hard-wired field names and grab values. Insert values into our data object.
	for( i=0; i < hard_wired_fields_array.length; i++ ) {
		object_key = hard_wired_fields_array[i];
		data_to_send[object_key] = $(main_container).children("."+hard_wired_fields_array[i]).val();
		}
	
	// "data_to_send" now contains all the hard wired data. We need to note whether this is an add
	// or drop, and what the associated value is.
	data_to_send["add_or_drop"] = add_or_drop;
	data_to_send["associated_value"] = associated_value;
	data_to_send["fields_list"] = hard_wired_fields_array.join(",")+",add_or_drop,associated_value";
	
	// If we use the "success" handler, "htmlresponse" is a json object w/ the following indices: 
	// 		status, summary_html, value_id
	$.ajax({
		type : "POST",
		url : "/elements/php/dynamic_forms.php",
		dataType : "json",
		data : data_to_send
		});
	}

// The second of 2 functions for interactive multi-menus
function addListenerForRemoveButton( obj ) {
	obj.children(".remove_button").click(function(){
		// It's ugly when buttons remember that they've been pushed
		$(this).blur();
		
		// Memoize some important things
		main_container = $(this).parents(".interactive_menu_items_container");
		value_to_drop = $(this).siblings(".drop_associated_value").val();
		name_to_drop = $(this).siblings(".plaintext_name").text();
		
		// Do the AJAX calls to save on server
		add_drop_request( main_container, 'drop', value_to_drop );
		
		if ( $(this).parent().siblings(".real_item").size() > 0 ) {
			// The item we're deleting is not the only one. No need to re-introduce dummy item.
			}
		else {
			// The item we're deleting is the last one. Re-add the "no items" dummy text
			$(this).parent().siblings(".no_items_dummy_item").show();
			}
		
		// We can re-add the item to the pull-down menu
		$(this).parents(".interactive_menu_items_container").children(".interactive_menu_potential_items_pulldown_container").children("select.interactive_multi_menu_pulldown_menu").addOption( value_to_drop, name_to_drop );

		
		// Nuke the item we're deleting.
		$(this).parent().remove();
		
				
		return false;
		});
	}

$(document).ready(function() {
	$(".rounded").corner();
	$(".rounded_bottom").corner("bottom");
	$(".rounded_top").corner("top");
	$(".striped tr:even").addClass("alt");
	$(".striped tr tr").removeClass("alt");
	$(".summary_row:even").addClass("alt");
	/// Preload success / failure images
	green_check = new Image();
	green_check.src = "/elements/images/icon_green_checkmark.png";
	red_x = new Image();
	red_x.src = "/elements/images/icon_red_x.png";
	
	// Links w/ class "nibot_vis_toggle" handle show/hide automagically
	$("a.nibot_vis_toggle").click(function() {
		id_to_toggle = $(this).children(".to_toggle_id").val();
		prev_selected = $(this).children(".selected");
		prev_unselected = prev_selected.siblings("img.switch");
		prev_selected.removeClass("selected");
		prev_unselected.addClass("selected");
		$("#"+id_to_toggle).toggle();
		return false;
		});
	
	// Begin centralized smart checkboxes code
	$(".nibot_smart_checkbox_link").click(function(){
		id_correlating_form_element = $(this).siblings(".smart_checkbox_field_linker").val();
		changing_to = "true";
		if ( $("#"+id_correlating_form_element).val() == "true" )
			{
			changing_to = "false";
			}
		$("#"+id_correlating_form_element).val( changing_to );

		// Change the clicked image to checked/unchecked box
		if ( changing_to == "true" )
			{
			$("img." + id_correlating_form_element + "_image").attr( "src", "/elements/templates/txtblaster/images/button_checkbox_checked_16x16.png" );
			}
		else
			{
			$("img." + id_correlating_form_element + "_image").attr( "src", "/elements/templates/txtblaster/images/button_checkbox_16x16.png" );
			}
		return false;
		});
	$(".nibot_smart_checkbox_leader_link").click(function(){
		id_correlating_form_element = $(this).siblings(".smart_checkbox_field_linker").val();
		changed_to = $("#"+id_correlating_form_element).val();
		lead_on = $(this).siblings(".smart_checkbox_lead_on").val();
		// Parent of $(this) is li. Parent of li is ul
		if ( changed_to == lead_on )
			{
			// We need to turn sibling checkboxes from natural to overridden
			$(this).parent().parent().children("li.follower").children(".natural").addClass("hidden");
			$(this).parent().parent().children("li.follower").children(".overridden").removeClass("hidden");
			}
		else
			{
			// We need to turn sibling checkboxes from overridden to natural
			$(this).parent().parent().children("li.follower").children(".natural").removeClass("hidden");
			$(this).parent().parent().children("li.follower").children(".overridden").addClass("hidden");
			}
		return false;
		});
	// End centralized smart checkboxes code
	
	
	// Begin centralized interactive multi-menu code
	$(".interactive_menu_item").each(function (i) {
		addListenerForRemoveButton( $(this) );
		});
	
	$(".interactive_multi_menu_add_button").click(function(){
		// It's ugly when buttons remember that they've been pushed
		$(this).blur();
		
		// Memoize some important things
		main_container = $(this).parents(".interactive_menu_items_container");
		menu = $(this).siblings(".interactive_multi_menu_pulldown_menu");
		ul_current_items = main_container.children("ul.interactive_menu_items_list");
		
		if ( menu.val() == '' )
			{
			// They might have clicked "add" w/ the dummy text. We want to ignore it.
			return false;
			}
		
		// Do the AJAX calls to save on server
		add_drop_request( main_container, 'add', menu.val() );
		
		// Hide the dummy text (if it hasn't been done already
		ul_current_items.children("li.no_items_dummy_item").hide();
		
		// Construct a new list item for the item
		ul_current_items.append(
			'<li class="interactive_menu_item real_item">'+
				  '<span class="plaintext_name">'+menu.children("option:selected").text()+'</span>'+
				  '<input type="hidden" class="drop_associated_value" value="'+menu.val()+'" />'+
				  '<input type="button" class="remove_button" value="-" title="Remove" />'+
			'</li>' );
		addListenerForRemoveButton( ul_current_items.children(".interactive_menu_item:last") );
		$(this).siblings(".interactive_multi_menu_pulldown_menu").removeOption( menu.val() );
		return false;
		});
	// End centralized interactive multi-menu code
	});
