var opener = window.top;

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

function initDialog()
{
	setTimeout(function()
	{
	    var applet = document.getElementById('applet');
	}, 1000);
	var clientParameters = getRadWindow().ClientParameters;
	// Mathml content
	if (clientParameters._wrs_temporalImage) {
		var mathml = opener.wrs_mathmlDecode(clientParameters._wrs_temporalImage.getAttribute(opener._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 {
					setAppletMathml();
				}
			}
			catch (e) {
				if (applet.isActive()) {
					applet.setContent(mathml);
				}
				else {
					setTimeout(setAppletMathml, 50);
				}
			}
		}
        setTimeout(function()
        {
 		    setAppletMathml(); 
 		}, 0);
	}
	
	// Button events
	$addHandler($get("submitButton"), "click", function()
	{
		var mathml = '';
		
		if (!applet.isFormulaEmpty()) {
			mathml += applet.getContent();					// if isn't empty, get mathml code to mathml variable
			mathml = opener.wrs_mathmlEntities(mathml);		// Apply a parse
		}

		//if (opener.wrs_int_updateFormula)
		//{
		//	opener.wrs_int_updateFormula(mathml);
		//}

		//window.close();
		//debugger;
		var args = { mathml: mathml };
		getRadWindow().close(args);
		return false;
	});
	
	$addHandler($get("cancel"), "click", function() { getRadWindow().close() });
};

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;
}
