/********************************************************************
*                                                                   *
*   AJAX ROTATING MESSAGE DISPLAY                                   *
*     Version:  0.1                                                 *
*     Released: 03/10/2008                                          *
*     Author:   David Morrison                                      *
*                                                                   *
*   Description: A simple XML file is imported, parsed and a        *
*    random parent item's child elements are output to specified    *
*    Id'ed locations on the page.  The XML child elements and       *
*    the specified tag IDs are indicated in seperate arrays         *
*    in the script.                                                 *
*                                                                   *
*   Revision History:                                               *   
*                                                                   *
*   Future Plans:                                                   *
*   -In order to make this more extendable/reusable, place the      *
*    external XML filename, XML parent item tag name, XML tag       *
*    name array and HTML tag ID array as arguments in the           *
*    function call.                                                 *
*   -Pair up XML tag names and HTML tag IDs in a dynamic            *
*    associative array.                                             *
*   -Generate dynamic array variable names for each of the          *
*    individual HTML tag ID arrays using eval() or a script library *
*   -Provide the TimeOut option for rotating content on timed (ms)  *
*    rotation schedule, without requiring a page load               *
*                                                                   *
*********************************************************************/

// These two functions provide true/false existence/validity checks for functions and objects without crashing the script inline on fail.
function isFunction(o) { return 'function' == typeof o; }
function isObject(o) { return (o && 'object' == typeof o) || isFunction(o); }

function parseXML(xmlFile, xmlItemTagName, xmlTagNames_arr, htmlTagIDs_arr) {
	/*
	 // The XML filename (to be moved to the function call in the future)
	var xmlFile = "messages.xml";
	 // The XML item parent container tag name (to be moved to the function call in the future)
	var xmlItemTagName  = "message"; 
	 // The collection of child/data elements tag names in the XML doc  (to be moved to the function call in the future)
	var xmlTagNames_arr = Array("header","body"); 
	*/
	 // Determine the number of child elements to be read (this is mainly for future use where the tag names will be supplied from the function call instead of in-script)
	var dataCount  = xmlTagNames_arr.length;
	/*
	 // Create an array of the HTML tags IDs where the xml data will be written  (to be moved to the function call in the future)
	var htmlTagIDs_arr  = Array("messageSubject","messageBody");
	*/
	 // Determine the number of of child elements to be read (this is mainly for future use where the tag names will be supplied from the function call instead of in-script)
	var htmlTagIDCount  = htmlTagIDs_arr.length;  // Should be the same number as dataCount
	
	 // Note: It would be ideal to load the XML tag names array and their corresponding HTML tag ID's array into a hash, or associative array, but due 
	 //   to JavaScript's unreliable handling of these it will have to wait for later (maybe through the Prototype API library or an Object() hash)
	
	 // Create array variables for each of the above HTML tags IDs
	var messageSubject_arr = Array();	
	var messageBody_arr = Array();	
	
	 // Create an array of the above HTML tags ID array vars to simulate a 2D array
	var var_arrays_arr = Array (messageSubject_arr, messageBody_arr);

	 // Load and parse the XML file
	try { xmlDoc=new ActiveXObject("Microsoft.XMLDOM");	}
	catch(e) {
		try { xmlDoc=document.implementation.createDocument("","",null); }
		catch(e) { alert(e.message); return; }
	}	  
	xmlDoc.async=false;
	xmlDoc.load(xmlFile);
	
	 // Get the count of XML parent items
	var arrayCount = xmlDoc.getElementsByTagName(xmlItemTagName).length;  
	
	 // Get a random number to select one of the array items
	var randomEl=Math.floor(Math.random()*(arrayCount))	
	
	 // Do the whammy-jammy, i.e load the HTML Tag ID value array up with the child data from the selected XML parent item
	for (i=0; i<arrayCount; i++) for (x=0; x<dataCount; x++) ( isObject(xmlDoc.getElementsByTagName(xmlTagNames_arr[x])[i].childNodes[0]) ) ? var_arrays_arr[x][i]=xmlDoc.getElementsByTagName(xmlTagNames_arr[x])[i].childNodes[0].nodeValue : var_arrays_arr[x][i]="";
	
	 // Load the parsed XML data into the corresponding HTML elements
	for (i=0; i<dataCount; i++) document.getElementById(htmlTagIDs_arr[i]).innerHTML = var_arrays_arr[i][randomEl]; 
}
