var newWindow = {};
var oNewDoc = {};
	
	function OpenReport(strRptName, objParams, blnDebugMode, blnAllInputsFromDocument) {

		/*** window.open specs: ********************************************************************************
		the call: window.open(URL,name,specs,replace)
		
		URL	(Optional):
			Specifies the URL of the page to open. 
			If no URL is specified, a new window with about:blank is opened
		name (Optional):
			Specifies the target attribute or the name of the window. 
			The following values are supported:
				_blank - URL is loaded into a new window. This is default
				_parent - URL is loaded into the parent frame
				_self - URL replaces the current page
				_top - URL replaces any framesets that may be loaded
				name - The name of the window
		specs (Optional):
			A comma-separated list of items. The following values are supported:
				channelmode=yes|no|1|0	Whether or not to display the window in theater mode. Default is no
				directories=yes|no|1|0	Whether or not to add directory buttons. Default is yes
				fullscreen=yes|no|1|0	Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode
				height=pixels	The height of the window. Min. value is 100
				left=pixels	The left position of the window
				location=yes|no|1|0	Whether or not to display the address field. Default is yes
				menubar=yes|no|1|0	Whether or not to display the menu bar. Default is yes
				resizable=yes|no|1|0	Whether or not the window is resizable. Default is yes
				scrollbars=yes|no|1|0	Whether or not to display scroll bars. Default is yes
				status=yes|no|1|0	Whether or not to add a status bar. Default is yes
				titlebar=yes|no|1|0	Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box. Default is yes
				toolbar=yes|no|1|0	Whether or not to display the browser toolbar. Default is yes
				top=pixels	The top position of the window
				width=pixels	The width of the window. Min. value is 100
		replace (Optional):
			Specifies whether the URL creates a new entry or replaces the current entry in the history list. 
			The following values are supported:
				true - URL replaces the current document in the history list
				false - URL creates a new entry in the history list

		*** window.open specs: end ****************************************************************************/
		
		// the function finaklly acts in a little different way comparing a browser to each other:
		// in Firefox, if a rpt is already opened, the window will be destroy and created again.
		// in IE, the window will be "refreshed" changing the content (document).
		
		// extract the filename from the complete report reference, to be used as key in report array
		var m = strRptName.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
		var strWinName = m[2].replace(/./g, '_');
		
		// define the appereance of the report container window.
		var strHtmlWaitMsg = '<div align="center"><table><tr><td align="center"><h2>Apertura del report in corso...<br>Attendere, prego...</h2></td></tr><tr><td align="center"><img src="../images/pleaseWaitRound.gif"></td></tr></table></div>'
		var strWParams = 'channelmode=0,directories=0,fullscreen=0,height=600,left=100,location=0,menubar=0,resizable=1,scollbars=1,status=1,titlebar=0,toolbar=0,top=100,width=800';
		
		// check if reports array exists for the selected array AND if it's not closed
		// the && it's a shortcut, so that it's not evaluating the 2nd condition if the 1st is not passed.
		// this works fine with Firefox, some doubts with IE, but I recover the situation in the next code.
		if (newWindow[strWinName] && !newWindow[strWinName].closed) {
			newWindow[strWinName] = window.open('',strWinName,'');
			newWindow[strWinName].close();
			newWindow[strWinName] = null;
		} 
		// instance a new window. If it was already existing, it would be destroyed previously.
		// if it was existing and it has not been destroyed, the new window will hang the existing one
		// (talking about window, not document!)
		newWindow[strWinName] = window.open('about:blank', strWinName, strWParams);
		try 
		{
			// tries to open the document (window content). This will fail on IE if the window was previously existing.
			oNewDoc[strWinName] = newWindow[strWinName].document.open();
		}
		catch (err)
		{
			// following code will recover both the situations:
			// - previous code have failed due to IE + prev. existing doc.	- or -
			// - link to previous report windows lost due to page refresh!
			newWindow[strWinName] = window.open('',strWinName,'');
			oNewDoc[strWinName] = newWindow[strWinName].document.open();
		}
		
		// just put the report ahead other windows...
		newWindow[strWinName].focus();
		
		// build the page, creating the "wait please", adding params and then forcing a submit.
		oNewDoc[strWinName].write(strHtmlWaitMsg);
		
		// builds the hidden controls used for passing params in post.
		// in debug mode, show passed params instead of calling actual report.
		if (blnDebugMode == true) {
			var strHtml = '<form id="reportGateway" name="reportGateway" method="post" action="/dwwe/tests/seeGetAndPost.php?rptFNameOutOfPostForDebugOnly='+strRptName+'">';
		} else {
			var strHtml = '<form id="reportGateway" name="reportGateway" method="POST" action="' + strRptName + '">';
		}
		// no need to encode values, because they're stuffed into hidden controls. Submit built-in procedure will encode them!
		for(var objParam in objParams){
			var pName = objParam;
			var pValue = objParams[objParam];
			strHtml += '<input id="' + pName + '" name="' + pName + '" type="hidden" value="' + pValue + '">';
		};
		if(blnAllInputsFromDocument == true){
			var objInput = document.getElementsByTagName("input");
			for(var i = 0; i < objInput.length; i++){
				strHtml += '<input id="' + objInput[i].id + '" name="' + objInput[i].name + '" type="hidden" value="' + objInput[i].value + '">';
			}
			objInput = document.getElementsByTagName("select");
			for(var i = 0; i < objInput.length; i++){
				strHtml += '<input id="' + objInput[i].id + '" name="' + objInput[i].name + '" type="hidden" value="' + objInput[i].options[objInput[i].selectedIndex].value + '">';
			
			}
		}
		strHtml += '</form>';
		oNewDoc[strWinName].write(strHtml);

		// close the document (content) and actually send it to the browser.
		oNewDoc[strWinName].close();
		newWindow[strWinName].document.reportGateway.submit();
	}
