/*
 * html_data.js
 *	Uses IFRAMEs as a method for dynamic retrieval of data from the server.
 *
 * It is preferred that you always use the xml_data.js XML DOM model first but only if that
 * fails should you, where possible, revert to the HTML IFRAME model.
 *
 * It is intended that this HTML IFRAME model will be removed as soon as possible to make way for XML
 * DOM.  Unfortunately we need to support older browsers for now.
 */

/*
 * Global used to generate unique IFRAME ID's
 */
htmlDataObjectNextIFRAMEId = 0;
htmlDataObjectInstanceArray = new Array();

/*
 * htmlDataObject_getElementData()
 *
 * Use the tag_name as the id of an element.
 */
function htmlDataObject_getElementData(tag_name)
{
	var frame = null;
	if (document.frames) {
		frame = document.frames[this.iFrameName];
	}
	if ((frame == null) && document.getElementById) {
		frame = document.getElementById(this.iFrameName);
	}
	if ((frame == null) && document.all) {
		frame = document.all[this.iFrameName];
	}
	if (frame == null) {
		return null;
	}

	var doc = null;
	if (frame.contentDocument) {
		doc = frame.contentDocument;
	}
	if ((doc == null) && frame.contentWindow && frame.contentWindow.document) {
		doc = frame.contentWindow.document;
	}
	if ((doc == null) && frame.document) {
		doc = frame.document;
	}
	if (doc == null) {
		return null;
	}

	var elem = null;
	if (doc.getElementById) {
		elem = doc.getElementById(tag_name);
	}
	if ((elem == null) && doc.all) {
		elem = doc.all[tag_name];
	}
	if (elem == null) {
		return null;
	}

	var text = null;
	try {
		text = elem.innerHTML;
	} catch (e) {
		try {
			text = elem.innerText;
		} catch (ee) {
		}
	}
	if ((text == null) || (text == "")) {
		return null;
	}
	return text;
}

/*
 * htmlDataObject_createHTMLDocument()
 *	Creates the object used to retrieve the HTML resource
 */
function htmlDataObject_createHTMLDocument()
{
	/*
	 * Create a unique name for this retrieval instance document
	 */
	htmlDataObjectNextIFRAMEId++;
	this.iFrameName = "HTML_DATA" + htmlDataObjectNextIFRAMEId;

	/*
	 * Create a hidden DIVision into which we place the iframe
	 */
	var hiddenDiv = document.createElement('DIV');
	hiddenDiv.setAttribute("id", this.iFrameName + "_DIV");
	hiddenDiv.setAttribute("name", this.iFrameName + "_DIV");
	hiddenDiv.style.visibility = 'hidden';
	hiddenDiv.style.position = 'absolute';
	hiddenDiv.style.top = '0px';
	hiddenDiv.style.left = '0px';

	/*
	 * Write an IFRAME declaration into the hidden iframe - this is used to retrieve the content
	 */
	hiddenDiv.innerHTML = '<iframe name="' + this.iFrameName + '" id="' + this.iFrameName + '" src="' + this.dataURL + '"></iframe>';

	/*
	 * Add the hidden division (including the iframe) into the document
	 */
	document.body.appendChild(hiddenDiv);
}

/*
 * htmlDataObject_destroyHTMLDocument()
 *	Destroys the iframe
 */
function htmlDataObject_destroyHTMLDocument()
{
	if (this.iFrameName == null) {
		return;
	}

	/*
	 * Destroy the DIV that contains the IFRAME
	 */
	var divName = this.iFrameName + "_DIV";
	var div = document.getElementById(divName);
	try {
		if (div) {
			div.removeNode(true);
		}
	} catch (e) {
	}

	this.iFrameName = null;
}

/*
 * htmlDataObject_retrieveData()
 *	Begin retrieval.
 */
function htmlDataObject_retrieveData()
{
	/*
	 * Destroy any existing document
	 */
	this.destroyHTMLDocument();

	/*
	 * Create the document object that will retrieve the requested doc
	 */
	this.createHTMLDocument();

	/*
	 * Start a timeout timer.
	 */
	this.timeoutTimerId = window.setTimeout("htmlDataObjectInstanceArray[" + this.objectArrayPosition + "].timerTimedout();", this.timeoutMillis);

	/*
	 * Start a timer to periodically check for the document being loaded
	 */
	this.loaderTimerId = window.setTimeout("htmlDataObjectInstanceArray[" + this.objectArrayPosition + "].timerComplete();", 250);
}

function htmlDataObject_timerTimedout()
{
	/*
	 * Cancel the load checker timer
	 */
	window.clearTimeout(this.loaderTimerId);
	this.loaderTimerId = 0;

	/*
	 * Destroy the document
	 */
	this.destroyHTMLDocument();

	/*
	 * Invoke the timeout callback function
	 */
	this.dataTimeoutFunc(this);
}

function htmlDataObject_timerComplete()
{
	/*
	 * There must be an element that has an attribute of id="loaded".
	 * Once we detect that it is present then we know the document has loaded
	 */
	var loaded = false;
	if (this.getElementData("loaded") != null) {
		loaded = true;
	}
	if (!loaded) {
		this.loaderTimerId = window.setTimeout("htmlDataObjectInstanceArray[" + this.objectArrayPosition + "].timerComplete();", 100);
		return;
	}

	window.clearTimeout(this.timeoutTimerId);
	this.dataReadyFunc(this);
}

/*
 * htmlDataObject()
 *	Used to retrieve data from the server using HTML file formats
 */
function htmlDataObject(dataReadyFunc, dataTimeoutFunc, timeoutMillis, dataURL)
{
	/*
	 * Initialise properties.
	 */
	this.dataReadyFunc = dataReadyFunc;
	this.dataTimeoutFunc = dataTimeoutFunc;
	this.dataURL = dataURL;
	this.timeoutMillis = timeoutMillis;
	this.timeoutTimerId = 0;
	this.loaderTimerId = 0;
	this.iFrameName = "";			/* The id and name of the iframe, it's outer DIV has a postfixed '_DIV' */

	/*
	 * Initialize the public methods
	 */
	this.retrieveData = htmlDataObject_retrieveData;
	this.getElementData = htmlDataObject_getElementData;

	/*
	 * Initialise private methods
	 */
	this.createHTMLDocument = htmlDataObject_createHTMLDocument;
	this.destroyHTMLDocument = htmlDataObject_destroyHTMLDocument;
	this.timerComplete = htmlDataObject_timerComplete;
	this.timerTimedout = htmlDataObject_timerTimedout;

	/*
	 * Insert this instance into the global instance array.
	 * Unfortunately we cannot do this any other way (normally using anonymous functions)
	 * as MAC IE and Safari don't work with them (especially when it comes to timers).
	 */
	this.objectArrayPosition = htmlDataObjectInstanceArray.length;
	htmlDataObjectInstanceArray[htmlDataObjectInstanceArray.length] = this;
}

function html_data_js_loaded() { return true; }
