/***************************************************************************************************
* FSX Ajax Library 1.0                                                                             *
*                                                                                                  *
* Copyright (C) 2010 Flemming Soervin. All Rights Reserved.                                        *
*                                                                                                  *
* This Library is released as Open Source and may freely be used and distributed in any private    *
* as well as commercial projects free of charge.                                                   *
***************************************************************************************************/

function getXMLHTTPRequest()
// Initialize the HTML Retrieval Object
{
	var request = false;
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject)
		{
			try
			{
				request = new ActiveXObject("Msml2.XMLHTTP");
			}
			catch(error1)
			{
				try
				{
					request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(error2)
				{
					request = false;
				}
			}
		}
	}
	return request;
}

function isComplete(HTTPObj)
// Check if Call to page is completed
{
	var request = false;
	if (HTTPObj.readyState == 4)
	{
		request = true;
	}
	return request;
}

function isOk(HTTPObj)
// Check if call to page completed and returned ok
{
	var request = false;

	if (isComplete(HTTPObj) == true)
	{
		if (HTTPObj.status == 200)
		{
			request = true;
		}
	}
	return request;
}

function getFramePage(URL, ID, ClassFrame)
{
	// Load an external page into a frage (since external pages cannot be loaded into elements for security reasons)
	myDiv = '<DIV CLASS="' + ClassFrame + '"><IFRAME WIDTH="100%" HEIGHT="100%" STYLE="border:0px;" SRC="' + URL + '"></IFRAME></DIV>';
	document.getElementById(ID).innerHTML = myDiv;
}

function getPage(URL, ID)
{
	// Load an internal page into an element
	var result = false;
	globalAjaxFSXID = ID;
	if (myAjaxXMLHTTPRequest != false)
	{
		if (myAjaxLoading == true)
		{
			/* Show Progress Indicator */
		}
		myAjaxXMLHTTPRequest.open('GET', URL, true);
		myAjaxXMLHTTPRequest.onreadystatechange = getPageDone;
		myAjaxXMLHTTPRequest.send(null);
		result = true;
	}
	if (myAjaxLoading == true)
	{
		/* Hide Progress Indicator */
	}
	return result;
}

function getPageDone()
{
	// Page finished loading. Check status and show either the contents or a possible error
	if (isComplete(myAjaxXMLHTTPRequest) == true)
	{
		if (isOk(myAjaxXMLHTTPRequest) == true)
		{
			document.getElementById(globalAjaxFSXID).innerHTML = myAjaxXMLHTTPRequest.responseText;
		} else {
			alert('Error in page: ' + myAjaxXMLHTTPRequest.responseText);
		}
	}
}

function ProgressIndicator(ID, HTMLCode)
{
	// Set the Progress Indicator
	myAjaxProgressID = ID;
	myAjaxProgressContent = HTMLCode;
}

function ProgressIndicatorVisibility(mode)
{
	// Set visibility of the Progress Indicator
}

var globalAjaxFSXID = ''; // Placeholder for Element to contain pages
var myAjaxXMLHTTPRequest = getXMLHTTPRequest(); // Ajax HTML Loader Object

// Progress Indicator
var myAjaxLoading = false; // Progress Indicator Mode
var myAjaxProgressID = ''; // Progress Indicator Field
var myAjaxProgressContent = ''; // Content of the Progress Indicator

