var m_bMainFrameLoaded = false;
window.onload = SetMainFrameLoaded;

function SetMainFrameLoaded()
{
	m_bMainFrameLoaded = true;
}


/*
* IFrame produce tag
*/
function RenderIFrameTag(sUrl, nWidth, nHeight, sScrolling, bAutoScaleHeight)
{
	var sID = GetRandomString();
	var sTag = '<iframe id="iframe_{id}" src="{url}" width="{width}" height="{height}" scrolling="{scrolling}" frameBorder="0" marginHeight="0" marginWidth="0" {onload}></iframe>';

	if (!nWidth)
	{
		nWidth = 100;
	}

	if (bAutoScaleHeight)
	{
		nHeight = 1;
	}
	else if (!nHeight)
	{
		nHeight = 100;
	}

	// Unieke ID toewijzen
	sTag = sTag.replace("{id}", sID);
	sTag = sTag.replace("{url}", sUrl);
	sTag = sTag.replace("{width}", nWidth);
	sTag = sTag.replace("{height}", nHeight);
	sTag = sTag.replace("{scrolling}", sScrolling);

	if (bAutoScaleHeight)
	{
		sTag = sTag.replace("{onload}", 'onload="IFrameAutoScaleHeight(this.id, 1)"');
	}
	else
	{
		sTag = sTag.replace("{onload}", "");
	}

	document.write(sTag);
}


/*
* Get a random string
*/
function GetRandomString()
{
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';

	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}

	return randomstring;
}


/*
* IFrame automaticaly scale height according to the body
*/
function IFrameAutoScaleHeight(sID, nAttempt)
{
	if(m_bMainFrameLoaded || (nAttempt > 100))
	{
		var oIFrame = document.getElementById(sID);

		var nHeight = GetIFrameDocumentHeight(oIFrame);
		oIFrame.style.height = "auto";

		if (nHeight)
		{
			oIFrame.style.height = nHeight + "px";
		}
	}
	else
	{
		//alert('MainFrame not loaded!');
		window.setTimeout("IFrameAutoScaleHeight('" + sID + "', " + (nAttempt + 1) + ");", 100);
	}
}


/*
* IFrame bepaal de hoogte van het document element
*/
function GetIFrameDocumentHeight(oIFrame)
{
	var nDocumentHeight = 0, nScrollHeight, nOffsetHeight;

	// Bepaal het document element
	if (oIFrame.contentWindow.document)
	{
		oIFrameDocument = oIFrame.contentWindow.document;
	}
	else if (oIFrame.document)
	{
		oIFrameDocument = oIFrame.document;
	}
	else
	{
		oIFrameDocument = null;
	}

	// bepaal de height
	if (oIFrameDocument)
	{
		if (oIFrameDocument.height)
		{
			nDocumentHeight = oIFrameDocument.height;
		}
		else if (oIFrameDocument.body)
		{
			if (oIFrameDocument.body.scrollHeight)
			{
				nDocumentHeight = nScrollHeight = oIFrameDocument.body.scrollHeight;
			}

			if (oIFrameDocument.body.offsetHeight)
			{
				nDocumentHeight = nOffsetHeight = oIFrameDocument.body.offsetHeight;
			}

			if (nScrollHeight && nOffsetHeight)
			{
				nDocumentHeight = Math.max(nScrollHeight, nOffsetHeight);
			}
		}
	}

	return nDocumentHeight;
}