// JavaScript Document

var application_id = null;
var request_app = null;

function applications_request( app_id )
{
	application_id = app_id;
	
	request_app = create_xml_http_request();
	
	var category_id = document.getElementById( 'header_category_id' ).options[ document.getElementById( 'header_category_id' ).selectedIndex ].value;
	var url = "ajax/applications.php?category_id=" + category_id; // creates the XML document
	
	applications_loading();
	
	request_app.onreadystatechange=applications_response;
	request_app.open("GET",url,true);
	request_app.send(null);		
}

function applications_response()
{
	if( request_app.readyState == 4 )
	{
		applications_clear();
		var xml_doc = request_app.responseXML.documentElement;		
		var objs = xml_doc.getElementsByTagName( 'application' );
		var count = objs.length;
		
		if( count > 0 )
		{
			var div = document.createElement( 'div' );
			div.className = "header_select";
			
			var select_obj = document.createElement( 'select' );
			select_obj.name = "application_id";
			
			for( var i = 0; i < count; i++ )
			{
				var id = objs[i].getAttribute( "id" );
				var obj = new Option( get_xml_node( xml_doc, 'application', i ), id );
				
				if( id == application_id )
				{
					obj.selected = true;	
				}
				select_obj.options[ i ] = obj;
				
			}
			div.appendChild( select_obj );
			document.getElementById( 'applications_header' ).appendChild( div );
			document.getElementById( 'applications_header' ).style.display = "block";
		}
	}
}

function applications_loading()
{
	applications_clear();
	var div = document.createElement( 'div' );
	div.style.padding = "4px";
	
	var img = document.createElement( 'img' );
	img.src = "images/loading-bar.gif";
	img.alt = "Loading...";
	
	div.appendChild( img );
	document.getElementById( 'applications_header' ).appendChild( div );
}

function applications_clear()
{
	var parent = document.getElementById( 'applications_header' );
	if( typeof( parent ) !== "undefined" && parent )
	{
		while( parent.hasChildNodes() )
			parent.removeChild( parent.firstChild );
	}	
	document.getElementById( 'applications_header' ).style.display = "none";
}
