| 
					
				 | 
			
			
				@@ -73,54 +73,6 @@ function generateDebugSection(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-function populateBasicData(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let debug=false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		print(config,"crfRef matched entries: "+data.rows.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (data.rows.length!=1) { return;} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	config.document.getElementById('eudraCTNumber').innerHTML=data.rows[0].EudraCTNumber; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	config.document.getElementById('studyCoordinator').innerHTML=data.rows[0].StudyCoordinator; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	config.document.getElementById('studySponsor').innerHTML=data.rows[0].StudySponsor; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let qconfig=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig.queryName='site'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig.filterArray=[LABKEY.Filter.create('siteNumber', data.rows[0].Site)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig.success=function(data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		setHTML(config,data,'siteName','siteName'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		setHTML(config,data,'sitePhone','sitePhone'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	LABKEY.Query.selectRows(qconfig); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		print(config,"set user: "+data.rows[0].UserId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let qconfig1=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig1.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig1.schemaName='core'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig1.queryName='Users'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig1.filterArray= [LABKEY.Filter.create('UserId',data.rows[0].UserId)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig1.success= function(data){setHTML(config,data,'investigatorName','DisplayName')}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	qconfig1.failure=onFailure; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		print(config,"USER select rows"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	LABKEY.Query.selectRows(qconfig1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-function setHTML(config,data,elementId,varName){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	print(config,"setHTML "+data.rows.length+" elementId: "+elementId+" varName:"+varName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (data.rows.length>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let entry=data.rows[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let el=config.document.getElementById(elementId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		print(config,"setHTML: Entry[" + varName + "]: " + entry[varName]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		print(config,"setHTML: Element[" + elementId + "]: " + el); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		el.innerHTML=entry[varName]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 function setAdditionalData(config,additionalData,entry){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	let debug=true; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -163,84 +115,6 @@ function readonlySetup(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return setup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-function generateForm(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let debug=true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) print(config,"generateForm ["+data.rows.length+"]"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	for (let i=0;i<data.rows.length;i++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if (debug) print(config,"generateForm ["+i+"/"+data.rows.length+"]"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let entry=data.rows[i]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		//this is actually a pointer into another list (==lookup) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		//another selectRows is needed to actually get the name for the queryName 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let fields=data.metaData.fields; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if (debug) print(config,"generateForm ["+i+"]: fields: "+fields); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let field="NONE"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		//if (debug) printTableSetup(config,data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		for (f in fields){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			if (fields[f]['name']!='queryName') continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			field=fields[f]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if (debug) print(config,"generateForm ["+i+"]: field: "+field); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let lookup=field.lookup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		if (debug) print(config,"generateForm ["+i+"]: lookup: "+lookup); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		let qconfig=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		qconfig.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		qconfig.schemaName=lookup.schemaName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		qconfig.queryName=lookup.queryName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		qconfig.filterArray=[LABKEY.Filter.create(lookup.keyColumn,entry['queryName'])]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		qconfig.success=function(data){generateFormLookup(config,data,entry)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		LABKEY.Query.selectRows(qconfig); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-function generateFormLookup(config,data,entry){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//id : entry[queryName] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//name: dentry[queryName] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let debug=true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let queryId=entry['queryName']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (data.rows.length==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		alert("No rows found for listId: "+entry['queryName']); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let dentry=data.rows[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		for (f in dentry) print(config,"generateFormLookup field: "+f+" value: "+dentry[f]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let queryName=dentry["queryName"]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) print(config,"generateFormLookup: ID: "+queryId+" name: "+queryName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//update fields for review 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (!(queryName in config.fields)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		config.fields[queryName]=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (!(queryId in config.queryMap)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		config.queryMap[queryId]=queryName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let field=config.fields[queryName]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	field.title=entry["title"]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	field.queryId=queryId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (debug) print(config,"entry[showFlag]: "+entry["showFlag"]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let additionalData=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	setAdditionalData(config,additionalData,entry); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	generateSection(config,queryName,entry["title"],queryName, additionalData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 function generateSection(config, sectionName, sectionTitle, listName, additionalData){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	let formName=config.masterForm; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1207,6 +1081,7 @@ function setFlag(config,data,flag){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 function completeWithFlag(config,data,flag){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	let debug=true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if (debug){ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1476,38 +1351,135 @@ function redirect(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+var blob; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+var blobInterval; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function checkBlob(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Blob: "+blob); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if (blob) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		clearInterval(blobInterval); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		config.a.href = config.window.URL.createObjectURL(blob); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		print(config,"HREF: "+config.a.href); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		config.a.download = 'test.pdf'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		config.a.click(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		config.window.URL.revokeObjectURL(config.a.href); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.count=config.count+1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Eval: "+config.count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if (config.count>100){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		clearInterval(blobInterval); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function printForm(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let doc=new PDFDocument(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	doc.fontSize(25).text("Some text with standard font Andrej!", 100, 100); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	doc.end(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let stream = doc.pipe(blobStream()).on("finish",function(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		blob=stream.toBlob("application/pdf");}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"BLob: "+blob); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.a = config.document.createElement("a"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.body.appendChild(config.a); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.a.innerHTML="Download PDF"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.a.style = "display: none"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.count=0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	blobInterval=setInterval(checkBlob,1000,config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//pick data from crfForm list 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        print(config,"Printing form"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (let i=0;i<config.formConfig.formSetup.rows.length;i++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let entry=config.formConfig.formSetup.rows[i]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let qName=config.formConfig.queryMap[entry['queryName']]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		print(config,'Adding data for '+qName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		selectRows.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		selectRows.queryName=qName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		selectRows.filterArray=[LABKEY.Filter.create('crfRef',config.formConfig.crfEntry['entryId'])]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		selectRows.success=function(data){collectFormData(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		//deal with additional data 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function collectFormData(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.fields[data.queryName].data=data; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (f in config.formConfig.fields){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let field=config.formConfig.fields[f]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if ("data" in field) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"All done"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	formatFormData(config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function formatFormData(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (queryName in config.formConfig.fields){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		formatDataset(config,queryName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function formatDataset(config,queryName){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let data=config.formConfig.fields[queryName].data; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let fields=data.metaData.fields; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (let i=0;i<data.rows.length;i++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		for (f in fields){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			let field=fields[f]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if (field.hidden) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			let vName=field.name; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			let vType=field.type; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			print(config,'['+vName+'/'+vType+']: '+data.rows[i][vName]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 function generateMasterForm(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	generateDebugSection(config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	setFormConfig(config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function afterConfig(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	let debug=true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		print(config,"generateMasterForm");	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	selectRows.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	selectRows.queryName='crfEntry'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	selectRows.filterArray=[LABKEY.Filter.create('entryId',getCRFref(config))]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	selectRows.success=function(data){populateBasicData(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//requires populateBasicData 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	config.fields=new Object();	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	config.queryMap=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	let configSelectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.queryName=config.setupQueryName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//this is new 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.queryName="FormSetup"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.filterArray=[LABKEY.Filter.create("formName",config.formId)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.getElementById('investigatorName').innerHTML=config.formConfig.user['DisplayName']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.getElementById('eudraCTNumber').innerHTML=config.formConfig.crfEntry['EudraCTNumber']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.getElementById('studyCoordinator').innerHTML=config.formConfig.crfEntry['StudyCoordinator']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.getElementById('studySponsor').innerHTML=config.formConfig.crfEntry['StudySponsor']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.getElementById('siteName').innerHTML=config.formConfig.site['siteName']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.document.getElementById('sitePhone').innerHTML=config.formConfig.site['sitePhone']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (let i=0;i<config.formConfig.formSetup.rows.length;i++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let entry=config.formConfig.formSetup.rows[i]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let queryName=config.formConfig.queryMap[entry['queryName']]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let additionalData=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		setAdditionalData(config,additionalData,entry); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		generateSection(config,queryName,entry['title'],queryName,additionalData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		print(config,'Running this'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//config.fields=new Object();	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//config.queryMap=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.fields=config.formConfig.fields; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.queryMap=config.formConfig.queryMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.success=function(data){generateForm(config,data);}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	configSelectRows.failure=onFailure;//function(errorTxt){print(config,"generateForm fail" + errorTxt)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	LABKEY.Query.selectRows(configSelectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		print(config,"Generating buttons"); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -1529,7 +1501,125 @@ function generateMasterForm(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		generateButton(config,"submitDiv","Complete submission","Submit",onSubmit,config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		generateButton(config,"submitDiv","Remove submission","Remove CRF form",onRemoveCRF,config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	generateButton(config,"submitDiv","Print Form (Experimental)","Print",printForm,config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	generateButton(config,"submitDiv","Done","Exit",redirect,config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function setFormConfig(config){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//generateDebugSection(config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let debug=true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if (debug) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		print(config,"generateMasterForm1");	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.queryName='crfEntry'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.filterArray=[LABKEY.Filter.create('entryId',getCRFref(config))]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//store form related data to this object 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.success=function(data){afterCRFEntry(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	//requires populateBasicData 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function afterCRFEntry(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.crfEntry=data.rows[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Setting crfEntry (x) to "+config.formConfig.crfEntry["entryId"]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.queryName='site'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.filterArray=[LABKEY.Filter.create('siteNumber',config.formConfig.crfEntry.Site)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.success=function(data){afterSite(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function afterSite(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"afterSite"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.site=data.rows[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Setting site name to "+config.formConfig.site["siteName"]+" phone: "+config.formConfig.site["sitePhone"]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.schemaName='core'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.queryName='Users'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.filterArray=[LABKEY.Filter.create('siteNumber',config.formConfig.crfEntry.UserId)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.success=function(data){afterUser(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function afterUser(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.user=data.rows[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Setting user to "+config.formConfig.user["DisplayName"]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.schemaName='lists'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.queryName='FormSetup'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.filterArray=[LABKEY.Filter.create('formName',config.formId)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.success=function(data){afterFormSetup(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function afterFormSetup(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.formSetup=data; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Number of datasets for form ["+config.formId+"]: "+config.formConfig.formSetup.rows.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let fields=config.formConfig.formSetup.metaData.fields; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let formQueryName='queryName'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let field="NONE"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (f in fields){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if (fields[f]['name']!=formQueryName) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		field=fields[f]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let lookup=field.lookup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"Getting dataset names from :"+lookup.queryName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	let selectRows=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.containerPath=config.containerPath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.schemaName=lookup.schemaName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.queryName=lookup.queryName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	selectRows.success=function(data){afterFormDatasets(config,data)}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	LABKEY.Query.selectRows(selectRows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+function afterFormDatasets(config,data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.formDatasets=data; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.fields=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	config.formConfig.queryMap=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (let i=0;i<config.formConfig.formSetup.rows.length;i++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let entry=config.formConfig.formSetup.rows[i]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let queryId=entry['queryName']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		for (let j=0;j<config.formConfig.formDatasets.rows.length;j++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if (queryId!=config.formConfig.formDatasets.rows[j]['Key']) continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			let qName=config.formConfig.formDatasets.rows[j]['queryName']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			config.formConfig.fields[qName]=new Object(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			config.formConfig.queryMap[queryId]=qName; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			let field=config.formConfig.fields[qName]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			field.title=entry['title']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			field.queryId=queryId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			//print(config,"     "+config.formConfig.formDatasets.rows[j]['queryName']); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	print(config,"List of datasets in form : "); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for (f in config.formConfig.fields){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		let field=config.formConfig.fields[f]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		print(config,"\t"+f+" ID: "+field.queryId+' title '+field.title); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	afterConfig(config); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 |