<?xml version="1.0" encoding="Win-1251"?>
<rmml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="L:\collaborative-nir\program\Cscl3DWS\rmml\RMML.xsd">
	<include src="window.xml"/>
	<composition _name="CLabReportWin" _class="CBaseWindow" visible="false">
		<image _name="imgPhoto" _visible="false">
			<plugin name="dll" src="rmeq:dlls\chem3drnd.dll" uid="mmlabSnapshotPlugin"/>
			<script><![CDATA[
				function onTextRead( aText)
				{
					trace( "----- onTextRead = '" + aText + "'");
					if (aText == ' ')
						Player.globals.xmlReportText = '';
					else
						Player.globals.xmlReportText = aText;
				}
			]]></script>
		</image>
		<browser name="htmlReport" width="730" height="460" x="2" y="25" src="blank.html" userEvents="onHtmlLoad, onSetMode, onSetReportInfo" depth="-100">
			<script><![CDATA[
				function onLoad()
				{
					trace( "onLoad");
					this.fireEvent( "onHtmlLoad");
				}
				function set_mode( ip)
				{
					this.iMode = ip;
					this.fireEvent( "onSetMode");
				}
				function set_report_info( ip, ip2, sp) {
					trace( '#00aaff set_report_info - sp: ' + sp);
					this.bTest = (ip2 > 0);
					this.bTest2 = (ip > 0);
					this.sDate = sp;
					this.fireEvent( "onSetReportInfo");
				}

				function save_report( sp) {
					this.parent.writeReport( sp);
				}
				function get_images( sp)
				{
					this.parent.updateImageGallery( sp);
				}

				// enable photolist for select photo
				function setPhotoModal(flag)
				{
					scene.photoList.modal = flag;
					scene.photoList.list.enableList(flag);
				}
				function tracee(str)
				{
					trace(str);
				}

			]]></script>
		</browser>
		<script event="scene.photoList.list.evItemSelected">
			var photo = eval("scene.photoList.list." + scene.photoList.list.itemSelected + ".content.item.photo");
			var selected = photo.src;
			selected = "file://"+selected.substr(1);
			selected = selected.replace(/\\/g, '/');
			var sJS = "report.insertImage('" + selected + "', " + photo.width + ", " + photo.height + ")";
			this.htmlReport.execScript(sJS);
			htmlReport.setPhotoModal(false);
		</script>
		<script>
			this.htmlBlank = true;
			this.htmlLoaded = false;
		</script>
		<script event="htmlReport.onHtmlLoad"><![CDATA[
			this.htmlLoaded = true;
			if (!this.htmlBlank && this.onAfterHtmlLoad != null)
			{
				this.onAfterHtmlLoad();
				this.onAfterHtmlLoad = null;
			}
		]]></script>
		<script event="htmlReport.onSetMode">
			this.iMode = this.htmlReport.iMode;
			this.setTitle( this.arTitles[ this.iMode - 1]);
		</script>
		<script event="htmlReport.onSetReportInfo">
			this.bTest = this.htmlReport.bTest;
			this.bTest2 = this.htmlReport.bTest2;
			this.sDate = this.htmlReport.sDate;
			this.bReportReady = true;
		</script>
		<script><![CDATA[
			this.reportPath = this.imgPhoto.tempDir + Player.module.identifier.replace( /:/g, '_') + '\\';
			trace( '-------- reportPath = ' + this.reportPath);
			this.arTitles = new Array( "Лабораторный журнал", "Редактор химических уравнений", "Галерея наблюдений");
			this.setTitle( this.arTitles[ 0]);
			//this.resize( 256, 256);
			this.resize( this.htmlReport.width + 6, this.htmlReport.height + 6);
			/*this.htmlReport.x = 2;
			this.htmlReport.y = this.imTop.height;
			*/
			this.x = (800 - this.width) / 2;
			this.y = (535 - this.height) / 2;

			// Парамеры журнала
			this.bLoadOnStart = true;
			this.bReportLoaded = false;
			this.bReportExist = false;
			this.bReportReady = true;
			this.bTest = false;
			this.bTest2 = false;

			this.iTheme = 0;
			this.iLab = 0;

			this.sFileXML = "";
			this.iCurrentExp = 1;
			this.iMode = 1;
			this.iShowMode = 1;
			this.bCloseAnyWay = false;

			this.htmlLoaded = (this.htmlLoaded == null ? false : this.htmlLoaded);
			this.onAfterHtmlLoad = (this.onAfterHtmlLoad == null ? null : this.onAfterHtmlLoad);

			this.journalWrap = (this.journalWrap == null ? null : this.journalWrap);
			this.editorWrap = (this.editorWrap == null ? null : this.editorWrap);

			this.tempLabReportPath = null;

			this.bFirstReport = true;
			this.bReportButton = false;

			function showReportButton()
			{
				this.bReportButton=true;
				this.showReport();
			}
			function showReport()
			{
				trace( '----------- showReport()');
				if (this.loadReport( this.showReport))
				{
					var sJS = "report.showReport( '" + iCurrentExp + "')";
					this.htmlReport.execScript( sJS);
					//this.updateImageGallery( this.iCurrentExp, true);
					this.modal=true;
					this.show();
					this.enabled = true;
					this.iShowMode = 1;

					// show photolist
					if (scene._class.name == "CMolConstBaseScene")
					{
						scene.photoList.visible = true;
					}
				}
				trace("LabReport - " + this.visible + ":" + this.enabled);
			}

			this.bFirstChemEditor = true;
			function showChemEditor()
			{
				trace( '----------- showChemEditor()');
				if (this.loadReport( this.showChemEditor))
				{
					var sJS = "report.showChemEditor( '" + iCurrentExp + "')";
					this.htmlReport.execScript( sJS);
					//this.updateImageGallery( this.iCurrentExp, true);*/
					this.modal=true;
					this.show();
					this.enabled = true;
					this.iShowMode = 2;
				}
			}

			function openReportModule()
			{
				trace( '----------- openReportModule()');
				this.reportModule = null;//new Module();
				if (Player.globals.experiments == null || Player.globals.experiments.length == 0)
				{
					Player.globals.experiments = new Array();
					var exp = new Object();
					exp.title = Player.module.title;
					exp.id = Player.module.identifier;
					exp.alias = '';
					Player.globals.experiments.push( exp);
				}
				return;

				var moduleError = false;
				if (Player.globals.reportModuleIDs[ Player.globals.labModule] == null ||
					!Player.module.linkExists( Player.globals.reportModuleIDs[ Player.globals.labModule]))
				{
					if (!(moduleError = !this.reportModule.create( Player.globals.labModule, 'repModule')))
					{
						Player.globals.reportModuleIDs[ Player.globals.labModule] = this.reportModule.identifier;
					}
				}
				else
					moduleError = !this.reportModule.open( Player.globals.reportModuleIDs[ Player.globals.labModule], 'repModule');
				if (moduleError)
				{
					trace( 'labReport: Can not create or open result module');
					return;
				}
			}

			function correctReport()
			{
				trace( '----------- correctReport()');
				var xmlExpData = new XML();
				var labData = '<?xml version="1.0" encoding="UTF-8"?>\n<data>';
				for (var i = 0;  i < Player.globals.experiments.length;  i++)
				{
					var questions = '';
					var conclusion = '';
					var chemEditor = false;

					if (Player.globals.experiments[ i].id == Player.module.identifier)
						this.iCurrentExp = i + 1;

					var expXmlFile = (Player.globals.experiments[ i].id.length == 0 ? ':' :
						':' + Player.globals.experiments[ i].id + ':') + '\\exp.xml';
					trace( '----------- correctReport:expXmlFile=' + expXmlFile + ' - ' + resourceExists( expXmlFile));

					if (resourceExists( expXmlFile))
					{
						xmlExpData.src = expXmlFile;
						var node = xmlExpData.XMLDocument.firstChild;
						while (node != null && node.nodeName != 'exp'){
							node = node.nextSibling;
						}
						if (node != null)
							node = node.firstChild;
						while (node != null)
						{
							if (node.nodeName == 'data')
							{
								chemEditor = true;
								labData += '<exp id="' + (i+1) + '">';
								var dataNode = node.firstChild;
								var nodeStack = new Array();
								while (dataNode != null || nodeStack.length != 0)
								{
									if (dataNode == null)
									{
										dataNode = nodeStack.pop();
										labData += '</' + dataNode.nodeName + '>';
										dataNode = dataNode.nextSibling;
									}
									else if (dataNode.nodeType == 3)
									{
										labData += dataNode.nodeValue.replace( /\s/g, '');
										dataNode = dataNode.nextSibling;
									}
									else if (dataNode.nodeType == 1)
									{
										labData += '<' + dataNode.nodeName;
										if (dataNode.attributes != null)
											for (var j = 0;  j < dataNode.attributes.length; j++)
											{
												var attr = dataNode.attributes.item( j);
												labData += ' ' + attr.name + '="' + attr.value + '"';
											}
										if (dataNode.hasChildNodes())
										{
											labData += '>';
											nodeStack.push( dataNode);
											dataNode = dataNode.firstChild;
										}
										else
										{
											labData += '/'+'>';
											dataNode = dataNode.nextSibling;
										}
									}
									else
										dataNode = dataNode.nextSibling;
								}
								labData += '</exp>';
							}
							else if (node.nodeName == 'question')
								questions += '<row><question>' + node.textContent + '</question></row>';
							else if (node.nodeName == 'conclusion')
								conclusion += '<conclusion>' + node.textContent + '</conclusion>';
							node = node.nextSibling;
						}
						trace( '#009900' + Player.globals.xmlReportText);
						Player.globals.xmlReportText = Player.globals.xmlReportText.replace(
							'<exp id="' + (i+1) + '" loaded="0"><name>' + Player.globals.experiments[ i].title + '</name></exp>',
							'<exp id="' + (i+1) + '" chemEditor="' + (chemEditor ? '1' : '0') + '"><name>' +
							Player.globals.experiments[ i].title + '</name>' + questions + conclusion + '</exp>');
						trace( '#009900' + Player.globals.xmlReportText);
					}
				}
				labData += '</data>';
				trace( '----------- correctReport:saveText=' + this.reportPath + 'lab.xml|' + labData);
				this.imgPhoto.dll.call( '', 'saveText', this.reportPath + 'lab.xml|' + labData);
				this.writeReport( Player.globals.xmlReportText);
			}

			function initReport()
			{
				trace( '----------- initReport()');
				if (!this.bReportLoaded)
				{
					this.bReportLoaded = true;
					this.bReportReady = false;
					var sJS = Player.globals.xmlReportText.replace( /([\'\\])/g, '\\$1');
					sJS = "report.initReport( '" + sJS + "', '');";
					if (scene._class.name == "CMolConstBaseScene")
					{
						sJS += "report.setImageSize( 220, 131);";
					}
					if (scene.photoList != null)
					{
						scene.photoList.loadPhotos(scene.labReportWindow.imgPhoto.tempDir + Player.module.identifier.replace( /:/g, '_') + '\\', 1);
					}
					if (scene.molList != null)
					{
						scene.molList.loadMolecules(scene.labReportWindow.imgPhoto.tempDir + Player.module.identifier.replace( /:/g, '_') + '\\', 1);
					}
					this.htmlReport.execScript( sJS);
				}
			}

			//
			function loadReport( aHtmlLoadHandler)
			{
				trace( '----------- loadReport() - htmlBlank: ' + this.htmlBlank);
				if (this.htmlBlank)  // Журнал еще не загружен?
				{
					trace( '----------- loadReport: if (this.reportModule == null)');
					this.openReportModule();

					if (Player.globals.xmlReportText == null || Player.globals.xmlReportText.length == 0)
						this.imgPhoto.dll.call( '', 'readText', this.reportPath + 'labreport.xml');
					if (Player.globals.xmlReportText.length == 0)
						this.createReport();

					this.correctReport();

					var repFiles = new Array(
						'pics/select.gif',
						'pics/close.gif', 'pics/del.gif', 'pics/down.gif', 'pics/edit.gif', 'pics/empty.gif', 'pics/new.gif',
						'pics/open.gif', 'pics/remove.gif', 'pics/removeO.gif', 'pics/thumb.gif', 'pics/thumbBottom.gif',
						'pics/thumbTop.gif', 'pics/track.gif', 'pics/up.gif', 'pics/zoomIn.gif', 'pics/zoomInO.gif',
						'pics/zoomOut.gif', 'pics/zoomOutO.gif', 'chemEditor.swf', 'scroll.js', 'xparse.js',
						'labReport.html'
					);
					var repPath;
					for (var i = 0;  i < repFiles.length;  i++)
					{
						repPath = Player.module.extract( getFullPath( 'rmgr:labreport/' + repFiles[ i]));
						//trace(repPath);
					}
					this.htmlBlank = false;
					this.htmlLoaded = false;
					repPath = repPath.replace( /file:\/+/g, '').replace( /\//g, '\\');
					trace( '----------- loadReport:navigate - ' + repPath);
					this.imgPhoto.dll.call( '', 'enableflashplayer8', repPath);
					this.htmlReport.navigate( repPath);
					trace( '----------- loadReport:navigate - ' + repPath);
				}
				trace( '----------- loadReport: after if (this.reportModule == null)');

				trace('----------- loadReport: htmlLoaded=' + this.htmlLoaded);
				if (this.htmlLoaded)
					initReport();
				else if (aHtmlLoadHandler != null)
					this.onAfterHtmlLoad = aHtmlLoadHandler;
				else
					this.onAfterHtmlLoad = this.initReport;
				return this.bReportLoaded;
			}

			function writeReport( aReportXml)
			{
				Player.globals.xmlReportText = aReportXml;
				this.imgPhoto.dll.call( '', 'saveText', this.reportPath + 'labreport.xml|' + aReportXml);
				//this.reportModule.writeText( '/labreport.xml', Player.globals.xmlReportText);
			}

			function createReport()
			{
				trace( '----------- createReport()');
				// Формируем структуру лабораторного журнала
				var sXML = "";
				sXML += '<report>';
				sXML += '<personal>';
				sXML += '<name></name>';
				sXML += '<class></class>';
				sXML += '</personal>';
				sXML += '<lab id="' + (Player.globals.labID == null ? '' : Player.globals.labID) + '">';
				sXML += '<name>' + (Player.globals.labName == null ? '' : Player.globals.labName) + '</name>';
				sXML += "<goal></goal>";
				sXML += "<safety_test></safety_test>";
				sXML += '<data file="' + this.reportPath + 'lab.xml"></data>';

				for (var i = 0;  i < Player.globals.experiments.length;  i++)
				{
					sXML += '<exp id="' + (i+1) + '" loaded="0"><name>' + Player.globals.experiments[ i].title + '</name></exp>';
				}
				sXML += "</lab></report>";
				this.writeReport( sXML);
			}

			function saveReport()
			{
				var sJS = "report.saveReport()";
				this.htmlReport.execScript( sJS);
			}

			function onClose()
			{
				this.saveReport();
				if (!this.bReportButton)
				{
						if (this.iShowMode == 1)
						{
							if (this.journalWrap != null)
							{
								if (this.journalWrap.onClose != null)
									this.journalWrap.onClose();
								this.journalWrap.fireEvent("evClosed");
							}
						}
						else if (this.iShowMode == 2)
						{
							trace("#FF0000LabReport.onClose:"+this.editorWrap.name+"-"+this.editorWrap.onClose);
							if (this.editorWrap != null)
							{
								if (this.editorWrap.onClose != null)
									this.editorWrap.onClose();
								this.editorWrap.fireEvent("evClosed");
							}
						}
				}
				this.bReportButton=false;
				this.modal=false;
				this.htmlReport.setPhotoModal(false);
////				var sJS = "report.insertImage('pics/empty.gif')";
				var sJS = "report.resetLastImage()";
				this.htmlReport.execScript(sJS);

				// hide photolist
				if (scene._class.name == "CMolConstBaseScene")
				{
					scene.photoList.visible = false;
				}
			}

			function getExpImageFile( anExpId, aN)
			{
				return 'exp' + anExpId + aN + ".png";
			}

			function getNewImageFile( anExpId)
			{
				var sImageFile;
				for (var i = 1;  resourceExists( getFullPath( 'repModule:/' + (sImageFile = getExpImageFile( anExpId, i))));  i++);
				return sImageFile;
			}

			function getImageList( anExpId)
			{
				var sImages = "";
				var sImageFile;
				for (var i = 1;  resourceExists( getFullPath( 'repModule:/' + (sImageFile = getExpImageFile( anExpId, i))));  i++)
					sImages = sImages + sImageFile + "|";
				return sImages;
			}

			function updateImageGallery( anExpId, aReset) {
				var sImages = getImageList( anExpId);
				if (sImages.length > 0) {
					var sJS = "";
					if (typeof( aReset) != 'undefined' && aReset)
						sJS = sJS + "report.imageExp=''; ";
					sJS = sJS + "report.setGalleryImages( '" + sImages + "');";
					sJS = sJS.replace( "\\", "/");
					this.htmlReport.execScript( sJS);
				}
			}

			function savePhoto( aImage)
			{
				if (this.reportModule != null)
				{
					var newImageFile = getNewImageFile( this.iCurrentExp);
					this.reportModule.saveMedia( aImage, newImageFile);
					this.tempLabReportPath = Player.module.extract( getFullPath( 'repModule:/'), this.reportModule.identifier + '/');
					//this.updateImageGallery( this.iCurrentExp, true);
				}
			}

			function setTestResults( aResults) {
				sJS = "report.setTestResults( '" + aResults + "')";
				this.htmlReport.execScript( sJS);
				bTest = (aResults != "");
				saveReport();
			}

			function setTest2Results( aResults) {
				sJS = "report.setTest2Results( '" + aResults + "')";
				this.htmlReport.execScript( sJS);
				bTest2 = (aResults != "");
				saveReport();
			}
			this.loadReport();
		]]></script>
	</composition>
</rmml>
