var xmlHttp = null;

var baseaddr = "http://latinlexicon.org/";
var base_AJAX_address = "http://latinlexicon.org/ajax/";

// in milliseconds
var ajax_request_timeout_in_ms = 10000;
var ajax_request_typing_delay_in_ms = 250;
var ajax_request_timeout_timer = undefined;
var ajax_request_delay_timer = undefined;
var ajax_request_delay_timer_2 = undefined;

try
{
	xmlHttp = create_cross_browser_xmlHttp_object();
}
catch( e )
{
	if ( e instanceof String )
	{
		alert( e );
	}

	alert( "Can't create xmlHttp transport object." );
}	

function ajax_request_timeout()
{
	alert( "I can't fulfill the AJAX request because it timed out. \n\n" + 
		"Wait 10 seconds and try again.");
	
	xmlHttp.abort();
}

function create_cross_browser_xmlHttp_object()
{
	var xmlHttp;
	
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				throw "Your browser does not support AJAX!" 
			}
		}
	}
	
	return xmlHttp;
}

function urlencode(str) 
{
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) 
{
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

//var baseaddr = ".";

/*
if( document.getElementsByTagName ) 
{
	var elems = document.getElementsByTagName( 'base' );

	if( elems.length ) 
	{
		baseaddr = elems[ 0 ].href;
	}
}
*/

function entry_feedback( p1, p2, p3, p4 )
{
	if( p1 == 4 || p1 == 5 )
	{
		// the user pressed cancel
		if( p3 == null )
		{
			return;
		}
		
		p3 = p3.replace( /^\s+|\s+$/ig, "" );
		
		if( p3 == "null" || p3 == "undefined" || p3 == "''" || p3 == "" )
		{
			alert( "Sorry, I can't submit a blank answer!" );
			return;
		}
	}

	p1 = urlencode( p1 );
	p2 = urlencode( p2 );
	p3 = urlencode( p3 );
	p4 = urlencode( p4 );

	var gateway = baseaddr + "/ajax/entry_feedback.php?p1=" 
		+ p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4;

	try
	{
		oldAjaxImage = document.getElementById( ajaxImage ).src;
				
		document.getElementById( ajaxImage ).src = imgRefresh;

		xmlHttp.open( "GET", gateway, true );
		xmlHttp.onreadystatechange = function()
		{
			if( xmlHttp.readyState == 4 )
			{
				//document.myForm.time.value = xmlHttp.responseText;
		
				if ( xmlHttp.status == 200 )
				{
					var tmp_str = xmlHttp.responseText.replace( /^\s+|\s+$/g, '' ); 

					if ( tmp_str.length > 0 )
					{
						alert( tmp_str );
					}
				}
				else
				{
					if ( xmlHttp.status == 0 ) 
					{
						// This just means we aborted the connection
					}
					else
					{
						alert( "I encountered error " + 
							xmlHttp.status + " (" + xmlHttp.statusText + ") " + 
							"while trying to communicate with the AJAX gateway." );							
					}
				}
		
				document.getElementById( ajaxImage ).src = oldAjaxImage;
			}			
		}
		xmlHttp.send( null );	
		//alert( ajaxImage );

	}
	catch( e )
	{
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		document.getElementById( ajaxImage ).src = oldAjaxImage;
	}

}

/**
	*
	*
	*/
function ajax_latin_and_english_lexicon_lookup_event( evt, language )
{
	var lookup_input_element_id = "lookup_input_element";
	var lookup_select_element_id = "lookup_select_element";

	// for Internet Explorer compatibility
	if ( !evt )
	{
		evt = window.event;
	}

	var input_element = document.getElementById( lookup_input_element_id )

	if ( input_element == null )
	{
			alert( "input element not found" );
			return false;
	}		

	var select_element = document.getElementById( lookup_select_element_id )

	if ( select_element == null )
	{
			alert( "select element not found" );
			return false;
	}		

	switch( evt.type )
	{
			case "keydown":
			
				if ( evt.keyCode == "38" ) // up arrow
				{
					if ( select_element.options.selectedIndex >= 0
							&& select_element.selectedIndex > 0 )
					{
						select_element.selectedIndex--;						
						lookup_entry( select_element.options[select_element.options.selectedIndex].value );						
					}
				}
			
				else if ( evt.keyCode == "40" ) // down arrow
				{
					if ( select_element.options.selectedIndex >= 0
							&& select_element.selectedIndex < ( select_element.options.length - 1 ) )
					{
						select_element.selectedIndex++;
						lookup_entry( select_element.options[select_element.options.selectedIndex].value );
					}
					
				}
			
				break;
				
			case "keyup":			
			
				keychar = String.fromCharCode( evt.keyCode );
				
				if ( ( keychar >= 'A' && keychar <= 'Z' )				// A-Z
					|| ( keychar >= 'a' && keychar <= 'z' ) 			// a-z (probably unnecessary but just in case)
					|| ( evt.keyCode == 8 )  											// backspace
					|| ( evt.keyCode == 46 ) )										// delete
				{
					if ( language == 'english' )
					{
						ajax_english_lexicon_lookup( input_element.value );
					}
					else
					{
						ajax_latin_lexicon_lookup( input_element.value );
					}
				}
				
				else if ( evt.keyCode == "13" ) // enter key
				{
					if ( select_element.options.selectedIndex >= 0 )
					{					
						lookup_entry( select_element.options[select_element.options.selectedIndex].value );
					}
				}

				else if ( evt.keyCode == "27" ) // escape key
				{
					input_element.focus();
					input_element.value = '';
				}
				
				break;

			case "keypress":							
				break;
			
			case "mousedown":
				break;
				
			case "mouseup":
				break;
				
			default:
				alert( evt.type );
	}
}

/**
	*
	*
	*/
function ajax_latin_lexicon_lookup( word )
{
	if ( ajax_request_delay_timer != undefined )
	{
		clearTimeout( ajax_request_delay_timer );		
	}

	word = word.replace( /^\s+|\s+$/g, '' ); 
	word = word.replace( /'/g, '' );

	ajax_request_delay_timer = setTimeout( "ajax_latin_lexicon_lookup_keyboard_delay( '" + word + "' )", 
		ajax_request_typing_delay_in_ms );
}

/**
	*
	*
	*/
function ajax_latin_lexicon_lookup_keyboard_delay( word )
{
	var lookup_select_element_id = "lookup_select_element";

	p1 = urlencode( word );

	p1 = p1.replace( /^\s+|\s+$/g, '' ); 

	var el = document.getElementById( lookup_select_element_id )

	if ( el == null )
	{
			alert( "select element not found" );
			return false;
	}		

	if ( p1.length == 0 )
	{		
		el.options.length = 0;
		return;
	}

	var gateway = baseaddr + "/ajax/lookup.php?p1=" + p1;
	
	try
	{		
		el.disabled = true;
		
		xmlHttp.open( "GET", gateway, true );
		xmlHttp.onreadystatechange = 	xmlHttp.onreadystatechange = function()
		{
			switch( xmlHttp.readyState )
			{
				/*
				
				0 = open has not yet been called
				1 = send has not yet been called but open has been called
				2 = send has been called but no response from server
				3 = data is in the process of being received from the server
				4 = response from server has arrived				
				
				*/
			
				case 4:
	
					var el = document.getElementById( lookup_select_element_id )

					if ( el == null )
					{
							alert( "select element not found" );
							return false;
					}

					el.disabled = false;
	

					if ( xmlHttp.status == 200 )
					{
						var split_res = xmlHttp.responseText.split( "|" );
											
						el.options.length = 0;
						
						for ( si = 0; si < split_res.length - 2; si++ )
						{
							var pair = split_res[si+1].split( "=" );
							
							key = pair[1];
							value = pair[0];
							
							// Select the first word in the list automatically
							if ( si == 0 )
							{
								el.options[si] = new Option( value, key, true, true );
							}
							else
							{
								el.options[si] = new Option( value, key );
							}
						}
					}
					else
					{
						if ( xmlHttp.status == 0 ) 
						{
							// This just means we aborted the connection
						}
						else
						{
							alert( "I encountered error " + 
								xmlHttp.status + " (" + xmlHttp.statusText + ") " + 
								"while trying to communicate with the AJAX gateway." );							
						}
					}
				
					break;
					
				default:
					// ignore everything else 
			}
			
		}
		xmlHttp.send( null );	
	}
	catch( e )
	{
		alert( e );
		
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		
		return false;
	}
	
	return true;
}

function ajax_latin_lexicon_lookup_iphone( word )
{
	if ( ajax_request_delay_timer != undefined )
	{
		clearTimeout( ajax_request_delay_timer );		
	}

	word = word.replace( /^\s+|\s+$/g, '' ); 
	word = word.replace( /'/g, '' );

	ajax_request_delay_timer = setTimeout( "ajax_latin_lexicon_lookup_iphone_keyboard_delay( '" + word + "' )", 
		ajax_request_typing_delay_in_ms );
}

function ajax_latin_lexicon_lookup_iphone_keyboard_delay( word )
{
	var iphone_search_list_element_id = "iphone_search_list_element";

	p1 = urlencode( word );

	p1 = p1.replace( /^\s+|\s+$/g, "" ); 
	
	if ( p1.length == 0 )
	{
		var el = document.getElementById( iphone_search_list_element_id )
		el.innerHTML = null;
		return;
	}

	var gateway = baseaddr + "/../ajax/lookup_iphone.php?p1=" + p1;
	
	try
	{
		xmlHttp.open( "GET", gateway, true );
		xmlHttp.onreadystatechange = 	xmlHttp.onreadystatechange = function()
		{
			switch( xmlHttp.readyState )
			{
				/*
				
				0 = open has not yet been called
				1 = send has not yet been called but open has been called
				2 = send has been called but no response from server
				3 = data is in the process of being received from the server
				4 = response from server has arrived				
				
				*/
			
				case 4:
	
					if ( xmlHttp.status == 200 )
					{
						var split_res = xmlHttp.responseText.split( "|" );
						
						var el = document.getElementById( iphone_search_list_element_id )
	
						if ( el == null )
						{
								alert( "select element not found" );
								return false;
						}
												
						el.innerHTML = null;
				
						var output = "";
								
						for ( si = 0; si < split_res.length - 2; si++ )
						{
							var pair = split_res[si+1].split( "=" );
							key = pair[1];
							value = pair[0];
							
							output += "<li onclick=\"lookup_entry_iphone( this );\">" +
								"<div>" + value +
								"<span>" +
								"<input value=\"" + key + "\" type=\"hidden\">" +
								"</span>" +
								"</div>" +
								"</li>";
						}
						
						el.innerHTML = output;
					}
					else
					{
						if ( xmlHttp.status == 0 ) 
						{
							// This just means we aborted the connection
						}
						else
						{
							alert( "I encountered error " + 
								xmlHttp.status + " (" + xmlHttp.statusText + ") " + 
								"while trying to communicate with the AJAX gateway." );							
						}
					}
				
					break;
					
				default:
					// ignore everything else 
			}
			
		}
		xmlHttp.send( null );	
	}
	catch( e )
	{
		alert( e );
		
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		
		return false;
	}
	
	return true;
}

/**
	*
	*
	*/
function ajax_english_lexicon_lookup( word )
{
	if ( ajax_request_delay_timer != undefined )
	{
		clearTimeout( ajax_request_delay_timer );		
	}

	word = word.replace( /^\s+|\s+$/g, '' ); 
	word = word.replace( /'/g, '' );

	ajax_request_delay_timer = setTimeout( "ajax_english_lexicon_lookup_keyboard_delay( '" + word + "' )", 
		ajax_request_typing_delay_in_ms );
}

function ajax_english_lexicon_lookup_keyboard_delay( word )
{
	var lookup_select_element_id = "lookup_select_element";

	p1 = urlencode( word );

	p1 = p1.replace(/^\s+|\s+$/g, ''); 

	var el = document.getElementById( lookup_select_element_id )

	if ( el == null )
	{
			alert( "select element not found" );
			return false;
	}		

	if ( p1.length == 0 )
	{
		var el = document.getElementById( lookup_select_element_id )
		el.options.length = 0;
		return;
	}

	var gateway = baseaddr + "/ajax/lookup_english.php?p1=" + p1;

	try
	{
		el.disabled = true;

		xmlHttp.open( "GET", gateway, true );
		xmlHttp.onreadystatechange = function()
		{
			switch( xmlHttp.readyState )
			{
				/*
				
				0 = open has not yet been called
				1 = send has not yet been called but open has been called
				2 = send has been called but no response from server
				3 = data is in the process of being received from the server
				4 = response from server has arrived				
				
				*/
			
				case 4:
		
					if ( xmlHttp.status == 200 )
					{
						var split_res = xmlHttp.responseText.split( "|" );
						
						var el = document.getElementById( lookup_select_element_id )
		
						if ( el == null )
						{
								alert( "select element not found" );
								return false;
						}

						el.disabled = false;

						el.options.length = 0;
						
						for ( si = 0; si < split_res.length - 2; si++ )
						{
							var pair = split_res[si+1].split( "=" );
							key = pair[1];
							value = pair[0];
							
							if ( si == 0 )
							{
								el.options[si] = new Option( value, key, true, true );
							}
							else
							{
								el.options[si] = new Option( value, key );
							}
						}
		
					}
					else
					{
						if ( xmlHttp.status == 0 ) 
						{
							// This just means we aborted the connection
						}
						else
						{
							alert( "I encountered error " + 
								xmlHttp.status + " (" + xmlHttp.statusText + ") " + 
								"while trying to communicate with the AJAX gateway." );							
						}
					}
				
					break;
					
				default:
					// not used
			}
		}
		xmlHttp.send( null );	
	}
	catch( e )
	{
		alert( e );
		
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		
		return false;
	}
	
	return true;
}

function lookup_input_focus() 
{
	var temp_function = function() 
	{
		var lookup_input_element_id = "lookup_input_element";
	
		var input_element = document.getElementById( lookup_input_element_id )
	
		if ( input_element == null )
		{
				alert( "input element not found" );
				return false;
		}		
		
		input_element.focus();
	} 
	
	var generic_timer = setTimeout(	temp_function, ajax_request_typing_delay_in_ms );
}

var old_lexicon_id;

function lookup_entry( lexicon_id )
{	
	if ( ajax_request_delay_timer_2 != undefined )
	{
		clearTimeout( ajax_request_delay_timer_2 );		
	}
							
	lexicon_id = lexicon_id.replace( /^\s+|\s+$/g, '' ); 
	lexicon_id = lexicon_id.replace( /'/g, '' );

	if ( lexicon_id != old_lexicon_id )
	{
		ajax_request_delay_timer_2 = setTimeout( "lookup_entry_keyboard_delay( '" + lexicon_id + "' )", 
			ajax_request_typing_delay_in_ms );

		old_lexicon_id = lexicon_id;
	}

	lookup_input_focus();
}

function lookup_entry_keyboard_delay( lexicon_id )
{	
	var lookup_div_element_id = "lookup_div_element";

	p1 = urlencode( lexicon_id );

	p1 = p1.replace(/^\s+|\s+$/g, ''); 

	if ( p1.length == 0 )
	{
		return;
	}

	var gateway = baseaddr + "/ajax/lookup_entry.php?p1=" + p1.toString();

	try
	{
		xmlHttp.open( "GET", gateway, true );
		xmlHttp.onreadystatechange = function()
		{
			switch( xmlHttp.readyState )
			{
				/*
					
					0 = open has not yet been called
					1 = send has not yet been called but open has been called
					2 = send has been called but no response from server
					3 = data is in the process of being received from the server
					4 = response from server has arrived				
					
					*/
				
					case 4:
				
						if ( xmlHttp.status == 200 )
						{
							//alert( xmlHttp.responseText );
							//alert( xmlHttp.responseXML );
	
							var txt = xmlHttp.responseText;
							//alert( split_res[1] );
							
							var el = document.getElementById( lookup_div_element_id )
				
							if ( el == null )
							{
									alert( "div element not found" );
									return false;
							}
																									
							el.innerHTML = txt;
							initChecks();
				
						}
						else
						{
							if ( xmlHttp.status == 0 ) 
							{
								// This just means we aborted the connection
							}
							else
							{
								alert( "I encountered error " + 
									xmlHttp.status + " (" + xmlHttp.statusText + ") " + 
									"while trying to communicate with the AJAX gateway." );							
							}
						}
					
						break;
						
					default:
						// Not used
			}		
		}		
		xmlHttp.send( null );	
	}
	catch( e )
	{
		alert( e );
		
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		
		return false;
	}
	
	return true;
}

function lookup_entry_iphone( el )
{	
	var elements = el.getElementsByTagName( "input" );
	
	if ( elements.length == 0 )
	{
		alert( "There are not enough input elements in this list element." );
		return;
	}
	
	if ( elements.length > 1 )
	{
		alert( "There are too many input elements in this list element." );
		return;
	}

	var input_element = elements[0];	
	var lexicon_id = input_element.value;	

	lexicon_id = lexicon_id.replace( /^\s+|\s+$/g, "" ); 

	if ( ajax_request_delay_timer != undefined )
	{
		clearTimeout( ajax_request_delay_timer );		
	}

	lexicon_id = lexicon_id.replace( /^\s+|\s+$/g, '' ); 
	lexicon_id = lexicon_id.replace( /'/g, '' );

	ajax_request_delay_timer = setTimeout( "lookup_entry_iphone_keyboard_delay( '" + lexicon_id + "' )", 
		ajax_request_typing_delay_in_ms );
}

function lookup_entry_iphone_keyboard_delay( lexicon_id )
{	
	var lookup_div_element_id = "lookup_div_element";
	var iphone_search_list_element_id = "iphone_search_list_element";
	var lookup_container_element_id = "iphone_search_container";
	var lookup_back_button_element_id = "lookup_back_button";

	p1 = urlencode( lexicon_id );

	p1 = p1.replace( /^\s+|\s+$/g, "" ); 

	if ( p1.length == 0 )
	{
		alert( "Javascript error: lookup_entry_iphone_keyboard_delay" );
		return;
	}

	var gateway = baseaddr + "/../ajax/lookup_entry_iphone.php?p1=" + p1.toString();

	var el = document.getElementById( lookup_container_element_id )

	if ( el == null )
	{
		alert( "list element not found" );
		return false;
	}
	
	el.style.visibility = "hidden";
	el.style.display = "none";

	var el = document.getElementById( lookup_div_element_id )

	if ( el == null )
	{
		alert( "div element not found" );
		return false;
	}
	
	el.innerHTML = "";

	el.style.visibility = "visible";
	el.style.display = "block";

	var el = document.getElementById( lookup_back_button_element_id )

	if ( el == null )
	{
		alert( "div element not found" );
		return false;
	}

	el.style.visibility = "visible";
	el.style.display = "block";
	
	try
	{
		xmlHttp.open( "GET", gateway, true );
		xmlHttp.onreadystatechange = function()
		{
			switch( xmlHttp.readyState )
			{
				/*
					
					0 = open has not yet been called
					1 = send has not yet been called but open has been called
					2 = send has been called but no response from server
					3 = data is in the process of being received from the server
					4 = response from server has arrived				
					
					*/
				
					case 4:
				
						if ( xmlHttp.status == 200 )
						{
							//alert( xmlHttp.responseText );
							//alert( xmlHttp.responseXML );
	
							var txt = xmlHttp.responseText;
							//alert( split_res[1] );
							
							var el = document.getElementById( lookup_div_element_id )
				
							if ( el == null )
							{
									alert( "div element not found" );
									return false;
							}
												
							el.innerHTML = txt;
							initChecks();
				
						}
						else
						{
							if ( xmlHttp.status == 0 ) 
							{
								// This just means we aborted the connection
							}
							else
							{
								alert( "I encountered error " + 
									xmlHttp.status + " (" + xmlHttp.statusText + ") " + 
									"while trying to communicate with the AJAX gateway." );							
							}
						}
					
						break;
						
					default:
						// Not used
			}		
		}		
		xmlHttp.send( null );	
	}
	catch( e )
	{
		alert( e );
		
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		
		return false;
	}
	
	return true;
}

function back_to_search()
{
	var lookup_div_element_id = "lookup_div_element";
	var iphone_search_list_element_id = "iphone_search_list_element";
	var lookup_container_element_id = "iphone_search_container";
	var lookup_back_button_element_id = "lookup_back_button";

	var el = document.getElementById( lookup_container_element_id )

	if ( el == null )
	{
		alert( "list element not found" );
		return false;
	}
	
	el.style.visibility = "visible";
	el.style.display = "block";

	var el = document.getElementById( lookup_div_element_id )

	if ( el == null )
	{
		alert( "div element not found" );
		return false;
	}

	el.style.visibility = "hidden";
	el.style.display = "none";

	var el = document.getElementById( lookup_back_button_element_id )

	if ( el == null )
	{
		alert( "div element not found" );
		return false;
	}

	el.style.visibility = "hidden";
	el.style.display = "none";
	
	setTimeout
	( 
		function()
		{
			window.scrollTo(0, 1) 
		}, 0 
	);
}

var saved_tags = new Array();
var saved_section_tags = new Array();

function clear_text( id )
{
	var element = document.getElementById( id );
	
	if ( element == null )
	{
		alert( "input element not found" );
		return false;
	}
	
	element.value = "";
	element.focus();
}

function clear_select( id )
{
	var element = document.getElementById( id );
	
	if ( element == null )
	{
		alert( "select element not found" );
		return false;
	}
	
	element.options.length = 0;
}

function clear_search( id )
{
	var element = document.getElementById( id );
	
	if ( element == null )
	{
		alert( "text element not found" );
		return false;
	}
	
	link_search( element );
}

function link_search( text_box )
{
	// only fill out the saved_tags once
	if ( saved_tags.length == 0 )
	{
		// get all the div tags in the document
		var div_tags = document.getElementsByTagName( "div" );
		
		// iterate the div tags
		for ( si = 0; si < div_tags.length; si++ )
		{
			var tag = div_tags[si];
	
			// find the div tags named "link_container"
			if ( tag.className == "link_container" )
			{
				// grab all the "a" tags
				var sub_tags = tag.getElementsByTagName( "a" );

				// get the link description
				var keyword_string = sub_tags[0].innerHTML;
	
				// grab all the "div" tags
				var sub_tags = tag.getElementsByTagName( "div" );
				
				// get the full description, convert to lower case
				keyword_string += " " + sub_tags[0].innerHTML;
				keyword_string = keyword_string.toLowerCase();
	
				// set a variable inside the "div" tag
				tag.keyword_string = keyword_string;
	
				// save the "div" tag (as a pointer!)
				saved_tags[saved_tags.length] = tag;
			}

			// find the div tags named "link_section_container"
			if ( tag.className == "link_section_container" )
			{
				// save the "div" tag (as a pointer!)
				saved_section_tags[saved_section_tags.length] = tag;
			}

		}
	}

	// grab the value of the text box ("what the user typed")
	var search_string = text_box.value;
	search_string = search_string.toLowerCase();
	
	// split it up into an array
	var search_array = search_string.split( " " );
	
	// make a list of tags to show (these are all pointers!)
	var show_list = new Array();
	
	// iterate through the list of saved tags to determine
	// if this link matches or not
	for ( si = 0; si < saved_tags.length; si++ )
	{
		var tag_item = saved_tags[si];
		
		show_list[si] = 0; 
		
		for ( ssi = 0; ssi < search_array.length; ssi++ )
		{
			var search_item = search_array[ssi];

			if( tag_item.keyword_string.indexOf( search_item ) != -1 )
			{
				show_list[si]++;
			}
		}
		
		if( show_list[si] == search_array.length )
		{
			tag_item.style.display = "block";
		}
		else
		{
			tag_item.style.display = "none";			
		}
		
	}
	
	/* ONLY SHOW LINK SECTIONS WHICH HAVE MORE THAN ONE VISIBLE LINK_CONTAINER */
		
	// iterate the section tags
	for ( si = 0; si < saved_section_tags.length; si++ )
	{
		var tag = saved_section_tags[si];

		// grab all the "div" tags
		var sub_tags = tag.getElementsByTagName( "div" );
	
		var show_section = 0;
		
		for ( ti = 0; ti < sub_tags.length; ti++ )
		{
			if ( sub_tags[ti].style.display == "block" )
			{
				show_section++;
			}
		}
		
		if ( show_section > 0 )
		{
			tag.style.display = "block";
		}
		else
		{
			tag.style.display = "none";
		}
		
	}


}