<?php
include $_SERVER['DOCUMENT_ROOT'].'/eoDACSBootstrap/eoDACS/SubFW/autoload.php';


$isextern=-1;
if(isset($_GET['isextern']))
{
	$isextern=$_GET['isextern'];
}

?>
<!DOCTYPE html>

<html>

<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>StudyDoseReport</title>
	<!-- <link rel="stylesheet" type="text/css" href="../eoDACS/SubFW/css/sdr.css"> -->
	<link rel="shortcut icon" type="image/x-icon" href="<?php echo $EODACS_ROOT;?>/../eoDACSCommon/pics/eodacs.png" />
	<!-- <link rel="stylesheet" href="css/eoDACS.css"/> -->
	<!-- <script src="js/fingerprint/fingerprint.js"> </script> -->
	<title>Study dose report</title>

	
	<style>
	</style>

	<script language="javascript" id="scriptSDRpage">
	
			var isextern = <?php echo $isextern; ?>;
			if (!String.prototype.replaceAll) 
			{
					String.prototype.replaceAll = function(str, newStr) {
						// If a regex pattern
						if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') 
						{
							return this.replace(str, newStr);
						}

						// If a string
						return this.replace(new RegExp(str, 'g'), newStr);
					};
			}
	
			var myLayout=null;
			var stu_id=-1;
			var pat_id=-1;
			var rep_mode="study";
			var isfirstrequest=true;

			<?php
					if(isset($_GET["stu_id"]))
					{
						$site_study = explode("_",$_GET["stu_id"],2);
	    			if(count($site_study) == 1) // no remote site ID
	    			{
	      			$_GET["stu_id"] = "0_{$_GET["stu_id"]}";;
	    			}
						echo "stu_id='".$_GET["stu_id"]."';";
					}

					if(isset($_GET["pat_id"]))
					{
						echo "pat_id=".$_GET["pat_id"].";";
					}

					if(isset($_GET["mode"]))
					{
							if($_GET["mode"]=="patient")
							{
									echo "rep_mode=\"patient\";";
							}
							if($_GET["mode"]=="detailed")
							{
								echo "rep_mode=\"detailed\";";
							}
					}
			?>


			function tableCreate ( parent, id ) 
			{
		  		var tbl = document.createElement('table');
		  		tbl.style.width = '100%';
		  		tbl.setAttribute('border', '0');
		  		tbl.setAttribute('cellspacing', '0');
		  		tbl.setAttribute('cellpadding', '2');
		  		var tbdy = document.createElement('tbody');
		  		tbl.appendChild(tbdy);
		  		parent.appendChild(tbl)
		  		tbl.id=id;
		  		return tbl;
			}

			function go(c)
			{
					if(!c.data.replace(/\s/g,''))
						c.parentNode.removeChild(c);
			}
			function clean(d)
			{
					var bal=d.getElementsByTagName('*');

					for(i=0;i<bal.length;i++)
					{
							a=bal[i].previousSibling;
							if(a && a.nodeType==3)
								go(a);
							b=bal[i].nextSibling;
							if(b && b.nodeType==3)
								go(b);
					}
					return d;
			}

			function getxhr()
			{
				var xhr = null;

      	if(window.XMLHttpRequest) // Firefox et autres
      		xhr = new XMLHttpRequest();
      	else if(window.ActiveXObject){ // Internet Explorer
      		try {
      			xhr = new ActiveXObject("Msxml2.XMLHTTP");
      		} catch (e) {
      			xhr = new ActiveXObject("Microsoft.XMLHTTP");
      		}
      	}
        else { // XMLHttpRequest non supportï¿½ par le navigateur
        	alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
        	xhr = false;
        }
        return xhr;
    	}

    	function getStuInfos(_stu_id)
    	{
    			console.log ( "getStuInfos ("+_stu_id+")......" );
    			stu_id=_stu_id;
    			getInfos ();
    	}

    	function emptyStudyInformation()
    	{
    		tablestudyinf.innerHTML="";
    	}
    	function partialEmptyTable(table,remaining_lines)
    	{
    		while(1===1)
    		{
    			if(table.rows.length <=remaining_lines)
    				break;
    			table.deleteRow(remaining_lines);
    		}
    	}

    	function getChildbyId(el,id,type)
    	{
 				var children = el.getElementsByTagName(type);// any tag could be used here..

 				for(var i = 0; i< children.length;i++)
 				{
    				if (children[i].getAttribute('id') == id) // any attribute could be used here
    				{
    						return children[i];
     						// do what ever you want with the element..
     						// children[i] holds the element at the moment..
					}
				}
		}

		function overrideField(parent,id,newid,type)
		{
			var overriden=getChildbyId(parent,id,type);
			overriden.id=newid;
		}

			var decodeEntities = (function() 
			{
  			// this prevents any overhead from creating the object each time
  			var element = document.createElement('div');

  			function decodeHTMLEntities (str) 
  			{
    			if(str && typeof str === 'string') 
    			{
      			// strip script/html tags
      			str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
      			str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
      			element.innerHTML = str;
      			str = element.textContent;
      			element.textContent = '';
    			}

    			return str;
  			}

  			return decodeHTMLEntities;
			}) ();

			function getInfos()
			{
				var elements = document.getElementsByClassName("toclean");
				if ( elements != null )
				{
					while(elements.length > 0)
					{
						elements[0].parentNode.removeChild(elements[0]);
					}
				}

				if(stu_id==-1 && pat_id==-1) 
					return;
		
				var xhr=getxhr();
				var param="";
				if(stu_id!=-1)
				{
					param+="stu_id="+stu_id;
				}
				if(pat_id!=-1)
				{
					if(param!="");
						param+="&";
					param+="pat_id="+pat_id;
				}

				if(rep_mode=="patient" && isfirstrequest==true)
				{
					param+="&mode=patient";
				}
	
				if(rep_mode=="detailed" && isfirstrequest==true)
				{
					param+="&mode=detailed";
				}
	
				xhr.onreadystatechange = function f() 
				{
						if(xhr.readyState == 4 && xhr.status == 200) 
						{
							var  response = xhr.responseText;
      
      						reponse = clean(xhr.responseXML.documentElement);

							if(document.getElementById('divsdr') != null)
							{
								if(reponse.getElementsByTagName('error').length > 0)
								{
										dhtmlx.message({
					    				text:reponse.getElementsByTagName('error')[0].firstChild.nodeValue,
					    				expire:2000,
					    				type:"error" // 'customCss' - css class
										});
										document.getElementById('divsdr').style.visibility="hidden";
								}
								else
								{
										document.getElementById('divsdr').style.visibility='visible';
								}
							}
			
							var patients = reponse.getElementsByTagName('patient');
							var lastname="";
							var firstname="";
							var birthdate="";
							var patid="";
							var sex="";
							var sdate="";
							var stime="";
							var groupname = "";

							if(patients.length>0)
							{
								var patient = patients.item(0);
								if(patient.getElementsByTagName("lastname")[0].firstChild)
										lastname=patient.getElementsByTagName("lastname")[0].firstChild.nodeValue;
								if(patient.getElementsByTagName("firstname")[0].firstChild)
										firstname=patient.getElementsByTagName("firstname")[0].firstChild.nodeValue;
								if(patient.getElementsByTagName("birthdate")[0].firstChild)
										birthdate=patient.getElementsByTagName("birthdate")[0].firstChild.nodeValue;
								if(patient.getElementsByTagName("patid")[0].firstChild)
										patid=patient.getElementsByTagName("patid")[0].firstChild.nodeValue;
								if(patient.getElementsByTagName("sex")[0].firstChild)
										sex=patient.getElementsByTagName("sex")[0].firstChild.nodeValue;
							}
			
							sdreport_patname.innerHTML=lastname+" "+firstname;
							sdreport_patsex.innerHTML=sex;
							sdreport_patbd.innerHTML=birthdate;
							sdreport_patid.innerHTML=patid;

							var bodysdr = document.getElementById ('IdcTest123');

							var studyinflist  = reponse.getElementsByTagName('studyinf');
						
							for ( var inflist=0; inflist<studyinflist.length; inflist++ )
							{
							var studyinf = reponse.getElementsByTagName('studyinf').item(inflist);
							var studyinfoid = studyinf.getAttribute("studyid");
							var patientalert = studyinf.getAttribute("patientalert");
							let pai = document.getElementById('patientalerticon');
							pai.style.display="none";

							if(rep_mode == 'detailed' || isextern==1)
							{
								document.getElementById("headerlocation").style.display="none";
							}
							else
							{
								document.getElementById("headerlocation").style.display="inline-block";

								if(patientalert == 'true')
								{
										pai.style.display="inline";
										document.getElementById('patientalerticon').addEventListener('click', function() {
				    							window.postMessage({type:'showalertmsg', payload:{id:studyinfoid}},'*');
										});
								}
							}

							var modtype = studyinf.getElementsByTagName('modtype').item(0).firstChild.nodeValue;
							var dpt = "";
							if ( studyinf.getElementsByTagName('department').item(0).firstChild )
									dpt=studyinf.getElementsByTagName('department').item(0).firstChild.nodeValue;
							var site = "";
							if ( studyinf.getElementsByTagName('institution').item(0).firstChild )
									site=studyinf.getElementsByTagName('institution').item(0).firstChild.nodeValue;


							sdreport_site.innerHTML=site;
							sdreport_service.innerHTML=dpt;

							var divstudyfull=document.getElementById("divstudyfull");

							var sdreport_detailedtable=document.getElementById("sdreport_detailedtable");

							var tablestudyinf=document.getElementById("tablestudyinf");

							if(studyinf.getElementsByTagName("sdate")[0].firstChild)
								sdate=studyinf.getElementsByTagName("sdate")[0].firstChild.nodeValue;
							if(studyinf.getElementsByTagName("stime")[0].firstChild)
								stime=studyinf.getElementsByTagName("stime")[0].firstChild.nodeValue;
							if(studyinf.getElementsByTagName("groupname")[0].firstChild)
								groupname=studyinf.getElementsByTagName("groupname")[0].firstChild.nodeValue;

							var sdreport_div_study_CR=document.getElementById("sdreport_div_study_CR");
							var sdreport_div_study_CT=document.getElementById("sdreport_div_study_CT");
							var sdreport_div_study_MG=document.getElementById("sdreport_div_study_MG");
							var sdreport_div_study_NM=document.getElementById("sdreport_div_study_NM");
								
							var sdreport_anatsumtable_row = document.getElementById("sdreport_anatsumtable_row");

							if ( inflist > 0 )
							{
								bodysdr.innerHTML+="<div class='toclean' style=\"min-width:99%;float:left;margin-top:15px;\"><hr style=\"border-top: 2px dashed;\"> </div>";

								bodysdr.innerHTML+="<div  class='toclean' style=\"min-width:99%;float:left;margin-top:15px;\"> </div>";

								divstudyfull=divstudyfull.cloneNode(true);
								divstudyfull.id="divstudyfull_"+inflist;
								divstudyfull.style.marginBottom="20px";
								divstudyfull.classList.add("toclean");
								bodysdr.appendChild(divstudyfull);

								sdreport_anatsumtable_row 		= sdreport_anatsumtable_row.cloneNode(true);
								sdreport_anatsumtable_row.id  	= "sdreport_anatsumtable_row_"+inflist;

								let tbl = sdreport_anatsumtable_row.querySelector ( 'table');
								tbl.id = "sdreport_anatsumtable_"+inflist;

								sdreport_anatsumtable_row.style.marginBottom="20px";
								sdreport_anatsumtable_row.classList.add("toclean");
								bodysdr.appendChild(sdreport_anatsumtable_row);

								tablestudyinf= getChildbyId(divstudyfull,'tablestudyinf','table'),
								tablestudyinf.id="tablestudyinf"+inflist;

								sdreport_detailedtable_row 		= document.getElementById('sdreport_detailedtable_row').cloneNode(true);
								sdreport_detailedtable_row.id  	= "sdreport_detailedtable_row"+inflist;

								sdreport_detailedtable = sdreport_detailedtable_row.querySelector ( 'table');
								sdreport_detailedtable.id  = "sdreport_detailedtable"+inflist;

								sdreport_detailedtable_row.classList.add("toclean");
								bodysdr.appendChild(sdreport_detailedtable_row);

								if(inflist%2==1)
								{
									var titles=sdreport_anatsumtable.getElementsByClassName("sdrtitle");

									for(var tt=0;tt<titles.length;tt++)
									{
										titles[tt].style.backgroundColor="red !important";//"#546e9a !important";
										titles[tt].className="sdrtitle2";
									}
									titles=divstudyfull.getElementsByClassName("sdrtitle");

									for(var tt=0;tt<titles.length;tt++)
									{
										titles[tt].style.backgroundColor="red !important";//"#546e9a !important";
										titles[tt].className="sdrtitle2";
									}

									titles=sdreport_detailedtable.getElementsByClassName("sdrtitle");

									for(var tt=0;tt<titles.length;tt++)
									{
										titles[tt].style.backgroundColor="red !important";//"#546e9a !important";
										titles[tt].className="sdrtitle2";
									}
								}
								sdreport_sdate=getChildbyId(divstudyfull,'sdreport_sdate','span');
								sdreport_sdate.id="sdreport_sdate"+inflist;
							}

							sdreport_detailedtable.style.display="table";

							sdreport_sdate.innerHTML=sdate+"&nbsp;&nbsp;"+stime;
							if(rep_mode=="study") 
								sdreport_patanatdiv.style.display="none";
								
							if((rep_mode=="patient"||rep_mode=="detailed") )
							{
								sdreport_patanatdiv.style.display="inline-block";

								partialEmptyTable(sdreport_patanatsumtable,2);
								var anatsums = reponse.getElementsByTagName('globalanatsum');
									
								param+="&mode=patient";

								for(var i=0; i<anatsums.length;i++)
								{
									var anatsum = anatsums.item(i);
									var region="";
									var dlp="";
									var dap="";
									var dgm="";
									var ctdivol="";
									var mbq="";

									if(anatsum.getElementsByTagName("region")[0].firstChild)
										region=anatsum.getElementsByTagName("region")[0].firstChild.nodeValue;
									if(anatsum.getElementsByTagName("dap")[0].firstChild)
										dap=anatsum.getElementsByTagName("dap")[0].firstChild.nodeValue;
									if(anatsum.getElementsByTagName("dlp")[0].firstChild)
										dlp=anatsum.getElementsByTagName("dlp")[0].firstChild.nodeValue;
									if(anatsum.getElementsByTagName("ctdivol")[0].firstChild)
										ctdivol=anatsum.getElementsByTagName("ctdivol")[0].firstChild.nodeValue;
									if(anatsum.getElementsByTagName("dgm")[0].firstChild)
										dgm=anatsum.getElementsByTagName("dgm")[0].firstChild.nodeValue;
									if(anatsum.getElementsByTagName("mbq")[0].firstChild)
										mbq=anatsum.getElementsByTagName("mbq")[0].firstChild.nodeValue;

									var newRow   = sdreport_patanatsumtable.insertRow();
									newRow.style.textAlign="center";
									if(i%2==1)
										newRow.classList.add("oddline");
									var regioncell  = newRow.insertCell(0);
									var regiontext  = document.createTextNode(region);
									var dapcell  = newRow.insertCell(1);
									var daptext  = document.createTextNode(dap);
									var dlpcell  = newRow.insertCell(2);
									var dlptext  = document.createTextNode(dlp);
									var ctdivolcell  = newRow.insertCell(3);
									var ctdivoltext  = document.createTextNode(ctdivol);
									var dgmcell  = newRow.insertCell(4);
									var dgmtext  = document.createTextNode(dgm);
									var mbqcell  = newRow.insertCell(5);
									var mbqtext  = document.createTextNode(mbq);

									if(i== (anatsums.length-1))
									{
										regioncell.style.borderBottom="solid 1px";
										dapcell.style.borderBottom="solid 1px";
										dlpcell.style.borderBottom="solid 1px";
										ctdivolcell.style.borderBottom="solid 1px";
										dgmcell.style.borderBottom="solid 1px";
										mbqcell.style.borderBottom="solid 1px";
									}
									regioncell.appendChild(regiontext);
									dapcell.appendChild(daptext);
									dlpcell.appendChild(dlptext);
									ctdivolcell.appendChild(ctdivoltext);
									dgmcell.appendChild(dgmtext);
									mbqcell.appendChild(mbqtext);
								}
								var dap="";var dlp=""; var dgm="";var mbq="";
								var anatsumtot= reponse.getElementsByTagName('globalanatsumtot').item(0);
								if(anatsumtot.getElementsByTagName("dap")[0].firstChild)
									dap=anatsumtot.getElementsByTagName("dap")[0].firstChild.nodeValue;
								if(anatsumtot.getElementsByTagName("dlp")[0].firstChild)
									dlp=anatsumtot.getElementsByTagName("dlp")[0].firstChild.nodeValue;
								if(anatsumtot.getElementsByTagName("ctdivol")[0].firstChild)
									ctdivol=anatsumtot.getElementsByTagName("ctdivol")[0].firstChild.nodeValue;
								if(anatsumtot.getElementsByTagName("dgm")[0].firstChild)
									dgm=anatsumtot.getElementsByTagName("dgm")[0].firstChild.nodeValue;
								if(anatsumtot.getElementsByTagName("mbq")[0].firstChild)
									mbq=anatsumtot.getElementsByTagName("mbq")[0].firstChild.nodeValue;

								var totalRow   = sdreport_patanatsumtable.insertRow();
								totalRow.style.textAlign="center";
								if(anatsums.length%2==1)
									totalRow.classList.add("oddline");
								var regioncell  = totalRow.insertCell(0);
								regioncell.style.fontWeight="bold";
								var regiontext  = document.createTextNode("Total:");
								var dapcell  = totalRow.insertCell(1);
								var daptext  = document.createTextNode(dap);
								var dlpcell  = totalRow.insertCell(2);
								var dlptext  = document.createTextNode(dlp);
								var ctdivolcell  = totalRow.insertCell(3);
								var ctdivoltext  = document.createTextNode(ctdivol);
								var dgmcell  = totalRow.insertCell(4);
								var dgmtext  = document.createTextNode(dgm);
								var mbqcell  = totalRow.insertCell(5);
								var mbqtext  = document.createTextNode(mbq);

								regioncell.appendChild(regiontext);
								dapcell.appendChild(daptext);
								dlpcell.appendChild(dlptext);
								ctdivolcell.appendChild(ctdivoltext);
								dgmcell.appendChild(dgmtext);
								mbqcell.appendChild(mbqtext);
							}

							var anatsums = studyinf.getElementsByTagName('anatsum');
							partialEmptyTable(sdreport_anatsumtable,2);
							partialEmptyTable(sdreport_detailedtable,2);

							for(var i=0; i<anatsums.length;i++)
							{
								var anatsum = anatsums.item(i);
								var region="";
								var dlp="";
								var dap="";
								var ctdivol="";
								var dgm="";
								var mbq="";

								if(anatsum.getElementsByTagName("region")[0].firstChild)
									region=anatsum.getElementsByTagName("region")[0].firstChild.nodeValue;
								if(anatsum.getElementsByTagName("dap")[0].firstChild)
									dap=anatsum.getElementsByTagName("dap")[0].firstChild.nodeValue;
								if(anatsum.getElementsByTagName("dlp")[0].firstChild)
									dlp=anatsum.getElementsByTagName("dlp")[0].firstChild.nodeValue;
								if(anatsum.getElementsByTagName("ctdivol")[0].firstChild)
									ctdivol=anatsum.getElementsByTagName("ctdivol")[0].firstChild.nodeValue;
								if(anatsum.getElementsByTagName("dgm")[0].firstChild)
									dgm=anatsum.getElementsByTagName("dgm")[0].firstChild.nodeValue;
								if(anatsum.getElementsByTagName("mbq")[0].firstChild)
									mbq=anatsum.getElementsByTagName("mbq")[0].firstChild.nodeValue;

								var newRow   = sdreport_anatsumtable.insertRow();
								newRow.style.textAlign="center";
								if(i%2==1)
									newRow.classList.add("oddline");
								var regioncell  = newRow.insertCell(0);
								var regiontext  = document.createTextNode(region);
								var dapcell  = newRow.insertCell(1);
								var daptext  = document.createTextNode(dap);
								var dlpcell  = newRow.insertCell(2);
								var dlptext  = document.createTextNode(dlp);
								var ctdivolcell  = newRow.insertCell(3);
								var ctdivoltext  = document.createTextNode(ctdivol);
								var dgmcell  = newRow.insertCell(4);
								var dgmtext  = document.createTextNode(dgm);
								var mbqcell  = newRow.insertCell(5);
								var mbqtext  = document.createTextNode(mbq);

								if(i== (anatsums.length-1))
								{
									regioncell.style.borderBottom="solid 1px";
									dapcell.style.borderBottom="solid 1px";
									dlpcell.style.borderBottom="solid 1px";
									ctdivolcell.style.borderBottom="solid 1px";
									dgmcell.style.borderBottom="solid 1px";
									mbqcell.style.borderBottom="solid 1px";
								}
								regioncell.appendChild(regiontext);
								dapcell.appendChild(daptext);
								dlpcell.appendChild(dlptext);
								ctdivolcell.appendChild(ctdivoltext);
								dgmcell.appendChild(dgmtext);
								mbqcell.appendChild(mbqtext);
							}
							var dap="";var dlp="";var ctdivol=""; var dgm="";var mbq="";
							var anatsumtot= studyinf.getElementsByTagName('anatsumtot').item(0);
							if(anatsumtot.getElementsByTagName("dap")[0].firstChild)
								dap=anatsumtot.getElementsByTagName("dap")[0].firstChild.nodeValue;
							if(anatsumtot.getElementsByTagName("dlp")[0].firstChild)
								dlp=anatsumtot.getElementsByTagName("dlp")[0].firstChild.nodeValue;
							if(anatsumtot.getElementsByTagName("ctdivol")[0].firstChild)
								ctdivol=anatsumtot.getElementsByTagName("ctdivol")[0].firstChild.nodeValue;
							if(anatsumtot.getElementsByTagName("dgm")[0].firstChild)
								dgm=anatsumtot.getElementsByTagName("dgm")[0].firstChild.nodeValue;
							if(anatsumtot.getElementsByTagName("mbq")[0].firstChild)
								mbq=anatsumtot.getElementsByTagName("mbq")[0].firstChild.nodeValue;

							var totalRow   = sdreport_anatsumtable.insertRow();
							totalRow.style.textAlign="center";
							if(anatsums.length%2==1)
								totalRow.classList.add("oddline");
							var regioncell  = totalRow.insertCell(0);
							regioncell.style.fontWeight="bold";
							var regiontext  = document.createTextNode("Total:");
							var dapcell  = totalRow.insertCell(1);
							var daptext  = document.createTextNode(dap);
							var dlpcell  = totalRow.insertCell(2);
							var dlptext  = document.createTextNode(dlp);
							var ctdivolcell  = totalRow.insertCell(3);
							var ctdivoltext  = document.createTextNode(ctdivol);
							var dgmcell  = totalRow.insertCell(4);
							var dgmtext  = document.createTextNode(dgm);
							var mbqcell  = totalRow.insertCell(5);
							var mbqtext  = document.createTextNode(mbq);

							regioncell.appendChild(regiontext);
							dapcell.appendChild(daptext);
							dlpcell.appendChild(dlptext);
							ctdivolcell.appendChild(ctdivoltext);
							dgmcell.appendChild(dgmtext);
							mbqcell.appendChild(mbqtext);



							var detailed_columns=studyinf.getElementsByTagName('title');
							var nbr_col= detailed_columns.length;
							var row_title=sdreport_detailedtable.rows[1];
							while(row_title.cells.length>0)
							{
								row_title.deleteCell(row_title.cells.length-1);
							}
							sdreport_detailedtable.rows[0].cells[0].colSpan=nbr_col+"";
							for(var i=0; i<nbr_col;i++)
							{
								var titlecell= row_title.insertCell(i);
							}
							for(var i=0; i<nbr_col;i++)
							{
								var colname = detailed_columns.item(i).getAttribute('name');
								var titlecell= row_title.cells[detailed_columns.item(i).getAttribute('order')];
								var titletxt  = document.createTextNode(colname);
								titlecell.appendChild(titletxt);
							}



							partialEmptyTable(tablestudyinf,0);
							var studycolumns=studyinf.getElementsByTagName('column');
							var nbrcol=studycolumns.length;
							var t_row=tablestudyinf.insertRow();
							for(var sc=0;sc<nbrcol;sc++)
							{
								var t_cell=t_row.insertCell(sc);
								var c_table=tableCreate(t_cell,"tablestudy_"+inflist+"_"+sc);
								var column = studycolumns.item(sc);
								var lines = column.getElementsByTagName('line');
								var nbrlines = lines.length;
								for(var sl=0;sl<nbrlines;sl++)
								{
									var line = lines.item(sl);
									var order= line.getAttribute("order")|0;
									var title = line.getAttribute("title");
									var value = "";
									if(line.firstChild != null)
									value = line.firstChild.nodeValue;

									if(typeof value==="undefined") value="";
									while (c_table.rows.length < (order+1))
									{
											var newRow=c_table.insertRow();
											if(order%2==1)
												newRow.classList.add("oddline");
											//newRow
									}
									var row=c_table.rows[order];
									var cell_title=row.insertCell(0);
									var txt_title= document.createTextNode(title.replaceAll(" ",'\xa0'));
									cell_title.appendChild(txt_title);
									cell_title.style.fontWeight="bold";
									cell_title.style.paddingRight="30px";
									var cell_value=row.insertCell(1);
									var txt_value=document.createTextNode(value);
									cell_value.style.width = '100%';
									cell_value.appendChild(txt_value);
								}
							}
							var alertsstudy=studyinf.getElementsByTagName('alert');
							for(var als=0;als<alertsstudy.length ;als++)
							{
								var alert=alertsstudy.item(als);
								var color=alert.getAttribute('color');
							 	var colindex=alert.getAttribute('column');
							 	var linindex=alert.getAttribute('line');
								var alertname=alert.getAttribute('alert');
								var alertnrd=alert.getAttribute('nrdvalue');
								var tab=null;
								if(colindex>=0 && linindex>=0)
								{
									tab=document.getElementById("tablestudy_"+inflist+"_"+colindex);
									if(tab)
									{
										tab.rows[linindex].cells[1].style.color=color;
										tab.rows[linindex].cells[1].innerHTML+="<br><span class=\"detailsview\" onclick=\"window.postMessage({type:'showalertmsg', payload:{id:"+studyinfoid+"}},'*')\">" + alertname + "&nbsp;(" + alertnrd + ")</span>";
									}
								}
							}

							var irradiations = studyinf.getElementsByTagName('irrevent');
							console.log ( "irradiations.length : " + irradiations.length );
							for(var i=0; i< irradiations.length; i++)
							{
								var irrevent=irradiations.item(i);
								var newRow = sdreport_detailedtable.insertRow();
								newRow.style.textAlign="center";

								if(i%2==1)
									newRow.classList.add("oddline");
								for(var j=0;j<nbr_col;j++)
								{
									var colvalue="";
									if(irrevent.getElementsByTagName("_"+j).length>0)
										if(irrevent.getElementsByTagName("_"+j)[0].firstChild)
											colvalue=irrevent.getElementsByTagName("_"+j)[0].firstChild.nodeValue;
									var cell = newRow.insertCell(j);
									var txt = document.createTextNode(decodeEntities(colvalue));
									cell.appendChild(txt);
								}
								if(irrevent.getElementsByTagName('alert').length>0)
								{
									var alert=irrevent.getElementsByTagName("alert")[0];
									var color=alert.getAttribute('color');
								 	var colindex=alert.getAttribute('column');
									var alertname=alert.getAttribute('alert');
									var alertnrd=alert.getAttribute('nrdvalue');
									if(colindex>=0)
									{
										newRow.cells[colindex].innerHTML+="<br><span class=\"detailsview\" onclick=\"window.postMessage({type:'showalertmsg', payload:{id:"+studyinfoid+"}},'*')\">"+alertname+"&nbsp;(>"+alertnrd+")</span>";
									}
									newRow.style.color=color;
								}
							}

							if(rep_mode=='detailed')
								isfirstrequest=false;
							}
							document.body.style.display="block";
							isfirstrequest=false;
							if(myLayout != null && myLayout.cells('b') != null)
							{
								myLayout.cells('b').progressOff();
							}
						}
				}
					
				console.log ( "Send request : getsdr.php" );
				xhr.open("GET","./SubFW/database/report/getsdr.php?"+param);
    			//xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    			xhr.send();

    			console.log ( "getInfos end.");
			}


function OnCloseModalStudyReport ()
{
	console.log ( "OnCloseModalStudyReport..." );

	$('#IdcStudyDoseReportCaption').show ();
	var element = $('#IdcTestDetach').detach();
	$('#IdcPDDosimetryDiv').append(element);
}

function OpenModalStudyReport ( stu_id )
{
		console.log ( "OpenModalStudyReport ( "+stu_id+" )..." );

		$('#IdcStudyDoseReportCaption').hide ();
		
		var element = $('#IdcTestDetach').detach();

		DocumentViewerShow ( Translate ("Patient dosimetric summary"), element, null, OnCloseModalStudyReport );

		console.log ( "call getStuInfos...");
		getStuInfos ( stu_id );
		console.log ( "after getStuInfos.");

		switchLanguage ( eoDACS.CurrentLanguage );

		console.log ( "after switchLanguage.");
}





function OnCloseModalPatientReport ()
{
	console.log ( "OnCloseModalPatientReport..." );

	$('#IdcStudyDoseReportCaption').show ();
	var element = $('#IdcPatientDosimetrySplitter1_2').detach();
	$('#IdcDivPatientDosimetry').append(element);

	$('#IdcPDPatientDiv').hide ();
	$('#IdcPDMask').hide ();
	$('#IdcPDDosimetryDiv').hide ();
	$('#IdcPDPatientCaption').text ( Translate ('No patient selected') );
	$('#IdcPDPatientCaption').data ( 'id', -1 );
	IdcPatientDosimetryDT.Refresh ();

	$('#IdcPDSearchDiv').show ();
}

function OpenModalPatientReport ( pat_id, pname )
{
		console.log ( "OpenModalPatientReport ( "+pat_id+" )..." );

		InitPatientDosimetryPage ();

		// $('#IdcStudyDoseReportCaption').hide ();

		$('#IdcPDPatientCaption').data ( 'id', pat_id );

		var element = $('#IdcPatientDosimetrySplitter1_2').detach();

		

		DocumentViewerShow ( Translate ("Dosimetric folder") + " - " + pname, element, null, OnCloseModalPatientReport );

		$('#IdcPDSearchDiv').hide ();
		$('#IdcPDPatientCaption').text ( Translate ('Studies list') );
		$('#IdcPDPatientDiv').hide ();
		$('#IdcPDMask').hide ();
		$('#IdcPDDosimetryDiv').show ();
		
		console.log ( "Refresh DT..." );

		IdcPatientDosimetryDT.Refresh ();

		// switchLanguage ( eoDACS.CurrentLanguage );
}

function detailedreport(trueorfalse)
{
		if ( trueorfalse )
		{
				rep_mode="detailed";
				isfirstrequest=true;
		}
		else
		{
				rep_mode="study";
				isfirstrequest=true;
		}
		getInfos();
}

function openBrief ()
{
		var page="SubFW/html/briefdosereport.php";
		page+="?pat_id="+pat_id;
		if(pat_id==-1)
				page+="&stu_id="+stu_id;
							
		DocumentViewerShow ( Translate ("Patient dosimetric summary"), null, page );

		// document.getElementById ( 'IdcDocumentViewerFrame' ).src = page;
}

function printDiv(data) 
{
		var mywindow = window.open('', 'new div', 'height=700,width=1000');
		mywindow.document.write('<html><head><title></title>');
		mywindow.document.write('<link rel="stylesheet" type="text/css" href="css/sdr.css">');
		mywindow.document.write('</head><body>');
		if(rep_mode=="detailed")
				mywindow.document.write('<div style="width: 100%;text-align:center"><h2>Historique dosimétrique de '+sdreport_patname.innerHTML+'</h2></div><br>');
							else
									mywindow.document.write('<div style="width: 100%;text-align:center"><h2>Rapport dosimétrique: '+sdreport_patname.innerHTML+'</h2></div><br>');
							mywindow.document.write(data);
							mywindow.document.write('</body></html>');
							mywindow.document.close();
							mywindow.focus();

	  					setTimeout(function(){mywindow.print();mywindow.close();},500);

      				return true;
  				}

  				window.printDiv=printDiv;
  				window.detailedreport=detailedreport;
  				window.openBrief=openBrief;
					window.getInfos = getInfos;
			</script>
	</head>

	<body onload="getInfos();" id="bodysdr" <?php if($isextern==1) { echo 'style="font-size:small;"';} ?> >
			<?php include 'div_alertack.php' ?>

			<?php error_log("EODACS_ROOT".$GLOBALS['EODACS_ROOT']); ?>
			
			<div id="fulldiv" class="d-flex flex-column w-100 h-100 m-0 p-2 bg-white">

				<div id="IdcStudyDoseReportCaption" class="row m-0 p-0 pt-1 pb-1 ps-1 pe-1 w-100 ximeo_app-color text-white rounded-top">
					<div class="col-md-auto m-0 p-0 pe-3 pt-0 ps-1">
						<img id="patientalerticon" class="imgclickable ximeo_bmp m-0 p-0" data-langkey_title="Patient alert" src='../eoDACS/SubFW/bmp/alert_red_filled.png' style="display: none;"/>
						<!-- <i id="patientalerticon" class="fas fa-2x fa-exclamation-triangle fs-4" style="display:none;color: rgb(235, 108, 99);cursor:pointer!important;"> </i> -->
					</div>
					<div class="col m-0 p-0 pt-1">
						<span id ="IdcSDRCaption" class="fw-bold" data-langkey="Dosimetric report"></span>
					</div>
					<div class="col-md-auto m-0 p-0">
						<div class="d-flex flex-row-reverse w-100 h-100 m-0 p-0">
							<div class="col-md-auto h-100 m-0 p-0">
								<select id="IdcPDVisitReportSelect" class="form-select form-select-sm m-0 p-0 ps-2 pe-5 mb-1 mt-1">
            						<option value="1">Option 1</option>
            						<option value="2">Option 2</option>
        						</select>
        					</div>

							<div class="col-md-auto h-100 m-0 p-0">
								<span class="no-print" style="float:right;color:lightblue;font-weight:500;letter-spacing:2px;">
									<!--
									<img class="imgclickable ximeo_bmp m-0 p-0" data-langkey_title="Patient dosimetric summary" src='../eoDACS/SubFW/bmp/briefunipage.png' onclick="window.openBrief();"/>
									<img class="imgclickable ximeo_bmp m-0 p-0" data-langkey_title="Examination report" src='../eoDACS/SubFW/bmp/unipage.png' onclick="window.detailedreport(false);"/>
									<img class="imgclickable ximeo_bmp m-0 p-0" data-langkey_title="Complete dosimetric history" src='../eoDACS/SubFW/bmp/multipage_wh.png' onclick="window.detailedreport(true);"/>
									-->
									<img class="imgclickable ximeo_bmp m-0 p-0 ms-4 me-2" data-langkey_title="Print" src='../eoDACS/SubFW/bmp/printer_1 - Copie.png' onclick="window.printDiv(fulldiv.innerHTML);"/>
								</span>
							</div>
						</div>
					</div>
				</div>

				<div class="row flex-grow-1 m-0 p-0 ps-1 pe-1 pb-1 w-100 border-start border-end border-bottom border-top" style="min-height: 0px;">

					<div id="IdcSDRMainDiv1" class="m-0 p-0 w-100 h-100">

						<div class="d-flex flex-column w-100 h-100 m-0 p-0">
							<!-- Patient / Exam location -->
							<div class="row w-100 m-0 p-0">
								<div class="col-md-auto m-0 p-0 pt-2">
									<div class="row m-0 p-0 ps-2 pb-2 ximeo_bg-dark-3 text-white" data-langkey="Patient">
									</div>
									<div class="row flex-grow-1 m-0 p-0 ps-1 pe-1 border-start border-end border-bottom" style="min-height: 0px;">
																		
										<div class="col-md-auto m-0 p-0">
											<div class="row m-0 p-0">
												<span class="fw-bold" data-langkey="Name :"></span>
											</div>
											<div class="row m-0 p-0">
												<span class="fw-bold" data-langkey="Sex :"></span>
											</div>
											<div class="row m-0 p-0">
												<span class="fw-bold" data-langkey="Birthdate :"></span>
											</div>
											<div class="row m-0 p-0">
												<span class="fw-bold" data-langkey="ID :"></span>
											</div>
											<div class="row m-0 p-0">
												<span class="fw-bold" data-langkey="NISS :"></span>
											</div>
										</div>

										<div class="col-md-auto m-0 p-0">
											<div class="row m-0 p-0">
												<span class="fw-bold" id="sdreport_patname"></span>
											</div>
											<div class="row m-0 p-0">
												<span id="sdreport_patsex"></span>
											</div>
											<div class="row m-0 p-0">
												<span id="sdreport_patbd"></span>
											</div>
											<div class="row m-0 p-0">
												<span id="sdreport_patid"></span>
											</div>
											<div class="row m-0 p-0">
												<span id="sdreport_patniss"></span>
											</div>
										</div>
									</div>
								</div>
													
								<div id="headerlocation" class="col m-0 p-0 pt-2 ps-2">
									<div class="d-flex flex-column w-100 h-100 m-0 p-0">
										<div class="row m-0 p-0 ps-2 pb-2 ximeo_bg-dark-3 text-white" data-langkey="Exam location">
										</div>
										<div class="row flex-grow-1 m-0 p-0 ps-2 pe-2 border-start border-end border-bottom" style="min-height: 0px;">
											<div class="col-md-auto m-0 p-0">
												<div class="row m-0 p-0">
													<span class="fw-bold" data-langkey="Site :"></span>
												</div>
												<div class="row m-0 p-0">
													<span class="fw-bold" data-langkey="Service :"></span>
												</div>
											</div>
											<div class="col-md-auto m-0 p-0">
												<div class="row m-0 p-0">
													<span id="sdreport_site"></span>
												</div>
												<div class="row m-0 p-0">
													<span id="sdreport_service"></span>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>

							<!-- Patient Anatomical region summary -->
							<div class="row w-100 m-0 p-0">
								<div class="col m-0 p-0 pt-2 w-100">
									<div id="sdreport_patanatdiv" class="d-flex flex-column w-100 h-100 m-0 p-0">
										<div class="row w-100 m-0 p-0 ps-2 pb-2 ximeo_bg-dark-3 text-white" data-langkey="Patient Anatomical region summary">
										</div>
										<div class="row flex-grow-1 m-0 p-0 ps-2 pe-2 border-start border-end border-bottom" style="min-height: 0px;">
											<table style="width: 100%;" cellspacing="0" cellpadding="2" border="0" id="sdreport_patanatsumtable">
												<tbody>
													<tr class="oddline" style="font-weight:bold; text-align:center;">
														<td>Region</td>
														<td>PDS(mGy.cm²)</td>
														<td>PDL(mGy.cm)</td>
														<td>CTDIvol(mGy)</td>
														<td>DGM(mGy)</td>
														<td>Act.(MBq)</td>
													</tr>
												</tbody>
											</table>
										</div>
									</div>
								</div>
							</div>

							<div id="IdcTest123" class="row flex-grow-1 w-100 m-0 p-0" style="min-height: 0px; overflow-y: auto; !important">
								<!-- Study information -->
								<div id="divstudyfull" class="row w-100 m-0 p-0">
									<div class="col m-0 p-0 pt-2 w-100">
										<div class="d-flex flex-column w-100 h-100 m-0 p-0">
											<div class="row w-100 m-0 p-0 ps-2 pb-2 ximeo_bg-dark-3 text-white">
												<div class="col-md-auto m-0 p-0 pe-3" data-langkey="Study information">
												</div>
												<div class="col-md-auto m-0 p-0">
													<span id="sdreport_sdate"></span>
												</div>
											</div>
											<div class="row m-0 p-0 ps-2 pe-2 border-start border-end border-bottom" style="min-height: 0px;">
												<table style="width: 100%;" cellspacing="0" cellpadding="2" border="0" id="tablestudyinf" >
												</table>
											</div>
										</div>
									</div>
								</div>
								<!-- Study Anatomical region summary -->
								<div id="sdreport_anatsumtable_row" class="row w-100 m-0 p-0">
									<div class="col m-0 p-0 pt-2 w-100">
										<div class="d-flex flex-column w-100 h-100 m-0 p-0">
											<div class="row flex-grow-1 m-0 p-0 border-start border-end border-bottom" style="min-height: 0px;">
												<table style="width: 100%;" cellspacing="0" cellpadding="2" border="0" id="sdreport_anatsumtable">
													<tbody>
														<tr>
															<td class="ximeo_bg-dark-3 text-white" colspan="6" data-langkey="Study Anatomical region summary">Study Anatomical region summary</td>
														</tr>
														<tr class="oddline" style="font-weight:bold; text-align:center;">
															<td>Region</td>
															<td>PDS(mGy.cm²)</td>
															<td>PDL(mGy.cm)</td>
															<td>CTDIvol(mGy)</td>
															<td>DGM(mGy)</td>
															<td>Act.(MBq)</td>
														</tr>
													</tbody>
												</table>
											</div>
										</div>
									</div>
								</div>

								<!-- Detail information -->
								<div id="sdreport_detailedtable_row" class="row w-100 m-0 p-0">
									<div class="col m-0 p-0 pt-2 w-100">
										<div class="d-flex flex-column w-100 h-100 m-0 p-0">
											<!--
											<div class="row w-100 m-0 p-0 ps-2 pb-2 ximeo_bg-dark-3 text-white">
												<div class="col-md-auto m-0 p-0 pe-3 sdrtitle" data-langkey="Detail information">
												</div>
											</div>
											-->
											<div class="row flex-grow-1 m-0 p-0 border-start border-end border-bottom" style="min-height: 0px;">
												<table style="width: 100%;display:none;" cellspacing="0" cellpadding="2" border="0" id="sdreport_detailedtable">
													<tbody>
														<tr>
															<td class="ximeo_bg-dark-3 text-white" colspan="1" data-langkey="Detail information">Detailed information</td>
														</tr>
																	
														<tr class="oddline" style="font-weight:bold; text-align:center;">
														</tr>
													</tbody>
												</table>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>

					</div>


					<div id="IdcSDRMainDiv2" class="m-0 p-0 w-100 h-100 overflow-auto" style="display: none;">
					</div>

					<div id="IdcSDRMainDiv3" class="w-100 h-100 m-0 p-0 pt-2 ps-2 pe-2 pb-2 bg-light" style="display: none;">
  						
  						<div class="w-100 h-100 m-0 p-0 d-flex flex-column">
							<div class="flex-grow-1 overflow-auto bg-white border m-0 p-0">
								<div id="IdcPDJasperContainer" class="w-100 h-100 m-0 p-0 ps-2 pe-2 pt-2 pb-2 bg-light d-flex overflow-auto">
								</div>
							</div>

							<div class="d-flex align-items-center">
  								<input type="checkbox" id="IdcPDJasperPDF" />
  								<label for="IdcPDJasperPDF" class="ms-2">Affichage PDF</label>
							</div>
						</div>

					</div>

				</div>
			
			</div>

	</body>
	


	<script>
		<?php
			if($isextern==1)
			{
				?>
				headerpatient.style.display='none';
				
				//divstudyfull.style.display='none';
				headerlocation.style.display='none';
				print_icon.style.display='none';
				if(isset($_GET['mode']) && $_GET['mode']=="detailed")
				{
					detailedreport(true);
				}
				<?php
			}
		?>
	</script>

</html>


