/************************************************************************************
												AJAX FILE BROWSER
												
	This is a demonstration of a useful AJAX capability. It is a simple file browser.
	
	The nice thing about this, as opposed to file browser interfaces on the Web
	server, is that it is completely customizable, and is very "snappy."
	
	The entire file browser is contained in a JavaScript file, so you simply add
	a <script> tag to your code where you want the browser to appear.

	This relies on my AJAX Queue Class. You need to have that class included into
	the <head> of your page for this to work.
	
	All client-side HTML is generated dynamically, using JavaScript DHTML techniques.
	This is a 100% JavaScript solution.
	
	To use this, include this file in your head, then call CreateFileBrowser() with whatever
	subdirectory you are using. The root directory is hardcoded into the server code for
	security purposes.
	
	All HTML is generated dynamically, using JavaScript DHTML techniques. This is a 100%
	JavaScript solution.
	
	Version 1.0
	
	Copyright:	Go ahead and use this as you wish. You can remove this copyright, and you don't
					have to give me credit, but don't be takin' credit yourself, or mis-assign the
					credit.
					
					©2006, Chris Marshall http://www.cmarshall.net/
*/

/* This variable is used to specify a subdirectory of the root that is defined in the server-side file. 
	In order to use this, declare it and set it in a bit of JavaScript before including this file.
*/
var	g_dir_root;

/* This is how we ensure that we can have multiple browsers on the same page. It keeps track of them for us. */
var	g_browser_id_max;

/* This is where the server returns its results as HTML.

	This works by replacing the inner HTML of a designated element (a <div> or <dl>)
	with what the server finds. Each <div> is used for each directory. Nested
	directories result in nested <divs>.
	
	<a> tags are javascript tags that call the callout() function for their <div>.

	in_browse_results contains HTML.
	
	inParam contains the DOM ID of the HTML element to have its HTML replaced.
*/
function receive_browse_results ( in_browse_results, inParam ) {
	var browse_results = in_browse_results;
	document.getElementById(inParam).innerHTML = browse_results;
}

/* This is where the HTTPRequest is actually made. */
function callout ( in_request, in_id ) {
	var url = 'ajax_file_browser_server.php?'+in_request;
	g_ajax_obj.CallXMLHTTPObjectGETParam ( url, receive_browse_results, in_id );
}

/* This function actually creates the file browser XHTML. */
/* dir_root is a subdirectory of the main root (hardcoded in the server file for security purposes). */
function CreateFileBrowser ( dir_root ) {
	/* We test to make sure the browser supports AJAX. We display a warning otherwise. */
	
	if ( !SupportsAjax ( ) ) {
		document.write ( '<h1>Your browser does not support AJAX!</h1>' );
	} else {
		/* What we do here, is to ensure that the <div> containing the browser has a unique ID.
			All contained <div> and <dl> tags will have IDs that are derived from this. */
		/* If it's uninitialized, we set it explicitly to zero. This forces it to be an integer. */
		if ( !g_browser_id_max ) { g_browser_id_max = 0 };
	
		/* We keep incrementing the ID as we find elements with that ID. */
		while ( document.getElementById('ajax_browser_'+g_browser_id_max++) );
		
		/* This is the <div> tag ID. It should be unique. */
		var	div_id = 'ajax_file_browser_'+g_browser_id_max;
		
		/* This is the main <div>. */
		document.writeln ('<div id="'+div_id+'"></div>');
		
		var	query = 'div_id='+div_id;
		
		/* If there was a subdirectory defined, we add it to the query. */
		if ( (dir_root != undefined) && dir_root ) {
			query += '&dir_root='+dir_root;
		}
		
		/* We do an initial callout() to populate the browser. */
		callout ( query, div_id );
	}
}