var openerRef = null;
if (window.attachEvent)
{
	window.attachEvent("onload", initDialog);
}
else if (window.addEventListener)
{
	window.addEventListener("load", initDialog, false);
}

function initDialog()
{
	var applet = document.getElementById('applet');
	var clientParameters = getParameters();
	openerRef = window.opener ? window.opener : window.top;
	
	// Mathml content
	if (clientParameters._wrs_temporalImage)
	{
		var mathml = openerRef.wrs_mathmlDecode(clientParameters._wrs_temporalImage.getAttribute(openerRef._wrs_conf_imageMathmlAttribute));

		function setAppletMathml()
		{
			// Internet explorer fails on "applet.isActive". It only supports "applet.isActive()"
			try
			{
				if (applet.isActive && applet.isActive())
				{
					applet.setContent(mathml);
				}
				else
				{
					setTimeout(setAppletMathml, 50);
				}
			}
			catch (e)
			{
				if (applet.isActive())
				{
					applet.setContent(mathml);
				}
				else
				{
					setTimeout(setAppletMathml, 50);
				}
			}
		}

		setAppletMathml();
	}

	// Button events
	$addHandler($get("submit"), "click", function()
	{
		var mathml = '';

		if (!applet.isFormulaEmpty())
		{
			mathml += applet.getContent(); 				// if isn't empty, get mathml code to mathml variable
			mathml = openerRef.wrs_mathmlEntities(mathml); 	// Apply a parse
		}

		var args = { mathml: mathml };
		closeWindow(args);
	});

	$addHandler($get("cancel"), "click", function() { closeWindow(); });
};

function getParameters()
{
	var rwindow = getRadWindow();
	if (rwindow)
	{
		return rwindow.ClientParameters;
	}
	else
	{
		return window.dialogArguments;
	}
}

function getRadWindow() //mandatory for the RadWindow dialogs functionality
{
	if (window.radWindow)
	{
		return window.radWindow;
	}
	if (window.frameElement && window.frameElement.radWindow)
	{
		return window.frameElement.radWindow;
	}
	return null;
}

function closeWindow(returnVal)
{
	var rwindow = getRadWindow();
	if (rwindow)
	{
		rwindow.close(returnVal);
	}
	else
	{
		window.returnValue = returnVal;
		window.close();
	}
}