
function combo_dropdown( evt, combo_base_name, gateway )
{	
	text_element = document.getElementById( combo_base_name + "_text" )
	
	if ( text_element == null )
	{
		alert( combo_base_name + "_text element not found." );
	}	
	
	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	if ( !evt )
	{
		evt = window.event;
	}
	
	switch( evt.type )
	{
		case "keyup":
			
			if ( evt.keyCode == "9" ) // tab
			{
				combo_dropdown_hide( combo_base_name );
				return;
			}

			if ( evt.keyCode == "13" ) // enter
			{
				return false;
			}

			if ( evt.keyCode == "38" ) // up arrow
			{
				return;
			}

			if ( evt.keyCode == "40" ) // down arrow
			{
				return;
			}

		break;
	}

	combo_ajax_query( combo_base_name, gateway );
}

function combo_select_previous( combo_base_name )
{
	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	if ( select_element.options.length == 0 
			|| select_element.options.length == false )
	{
		return;
	}

	var index = select_element.options.selectedIndex;
	
	index--;
		
	if ( index < 0 )
	{
		index = 0;
	}
	
	select_element.options.selectedIndex = index;
}

function combo_select_next( combo_base_name )
{
	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	if ( select_element.options.length == 0 
			|| select_element.options.length == false )
	{
		return;
	}

	var index = select_element.options.selectedIndex;
	
	index++;
	
	if ( index > select_element.options.length - 1 )
	{
		index = select_element.options.length - 1;
	}
	
	select_element.options.selectedIndex = index;	
}

function combo_dropdown_toggle( evt, combo_base_name, gateway )
{
	text_element = document.getElementById( combo_base_name + "_text" )
	
	if ( text_element == null )
	{
		alert( combo_base_name + "_text element not found." );
	}	
	
	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	if ( select_element.style.visibility == 'visible' )
	{
		select_element.style.visibility = "hidden";
		select_element.style.display = "none";
	}
	else
	{
		combo_dropdown( evt, combo_base_name, gateway );
	}
	
	text_element.focus();
	focused_form_control = select_element;	
}

function combo_dropdown_show( combo_base_name )
{
	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	select_element.options.selectedIndex = 0;
	select_element.style.visibility = "visible";
	select_element.style.display = "block";
}

function combo_dropdown_hide( combo_base_name )
{
	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	select_element.style.visibility = "hidden";
	select_element.style.display = "none";
}

function combo_textbox_blur( combo_base_name )
{
	func = function()
	{
		if ( focused_form_control.id != combo_base_name + "_select" )
		{
			combo_dropdown_hide( combo_base_name );
		}
	}
	
	var combo_textbox_blur_timer = setTimeout( func, 
		1000 );
}

function combo_ajax_query( combo_base_name, gateway )
{
	if ( ajax_request_timeout_timer != undefined )
	{
		clearTimeout( ajax_request_timeout_timer );		
	}

	func = function()
	{
		combo_ajax_query_keyboard_delay( combo_base_name, gateway );
	}

	ajax_request_timeout_timer = setTimeout( func, ajax_request_typing_delay_in_ms );
}

function combo_ajax_query_keyboard_delay( combo_base_name, gateway )
{	

//	alert( gateway );

	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:
	
						window.clearTimeout( ajax_request_timeout_timer );
	
						if ( xmlHttp.status == 200 )
						{
							//alert( xmlHttp.responseText );
							//alert( xmlHttp.responseXML );
						
							var txt = xmlHttp.responseText;
							//alert( split_res[1] );
							
							var el = document.getElementById( combo_base_name + "_select" )
						
							if ( el == null )
							{
								alert( "select element not found" );
								return false;
							}
													
							el.options.length = 0;
						
							var split_res = xmlHttp.responseText.split( "|" );
						
							for ( si = 0; si < split_res.length - 1; si++ )
							{
								key = split_res[si+1];
								value = split_res[si+1];
								el.options[si] = new Option( value, key );
							}												
						
							el.options.selectedIndex = 0;
							el.style.visibility = "visible";
							el.style.display = "block";
	
						}
						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;
			}
			
		}
	
		xmlHttp.send( null );	
		ajax_request_timeout_timer = window.setTimeout( "ajax_request_timeout()", ajax_request_timeout_in_ms );
		
	}
	catch( e )
	{
		alert( e );
		
		alert( "I could not open the AJAX gateway.\n\n" + gateway );
		
		return false;
	}
	
	return true;
}

function combo_set_text( combo_base_name )
{
	text_element = document.getElementById( combo_base_name + "_text" )
	
	if ( text_element == null )
	{
		alert( combo_text_text + " element not found." );
	}	

	select_element = document.getElementById( combo_base_name + "_select" );
	
	if ( select_element == null )
	{
		alert( combo_base_name + "_select element not found." );
	}

	if ( select_element.options.selectedIndex >= 0 )
	{
		text_element.value = select_element.options[select_element.options.selectedIndex].value;
	}
		
	select_element.style.visibility = "hidden";
	select_element.style.display = "none";

	text_element.focus();
	focused_form_control = select_element;
}

function check_for_tab( evt, combo_base_name )
{
	if ( !evt )
	{
		evt = window.event;
	}
	
	switch( evt.type )
	{
		case "keydown":
			
			if ( evt.keyCode == "9" ) // tab
			{
				combo_dropdown_hide( combo_base_name );
				return;
			}

			if ( evt.keyCode == "13" ) // enter
			{
				combo_set_text( combo_base_name );
				return false;
			}

			if ( evt.keyCode == "38" ) // up arrow
			{
				combo_select_previous( combo_base_name );
			}

			if ( evt.keyCode == "40" ) // down arrow
			{
				combo_select_next( combo_base_name );
			}

			break;
	}
}
	