$ta_result = db_query("SELECT DISTINCT ACTARMCD, ACTARM from _hcvt2_dm");
if ($ta_result) {
	$seq = 1;
	while ($ta_row = db_fetch_assoc($ta_result)) {
		if ($ta_row['ACTARMCD'] != '' && !in_array($ta_row['ACTARMCD'], array('NOTTRT', 'SCRNFAIL'))) {
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($ta_row['ACTARMCD']) . ',' .
				fix_null($ta_row['ACTARM']) . ',' .
				fix_null($seq) . ',' .
				fix_null('TRT') . ',' .
				fix_null('TREATMENT') . ',' .
				fix_null('') . ',' .
				fix_null('') . ',' .
				fix_null('TREATMENT') .
				')';
			$seq++;
		}
	}
}
/**
 * end Main Loop
 */
$timer['end_main'] = microtime(true);
/**
 * if not exists, create your domain data table
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'DM',
		//show_var($subj_val_array, 'SUBJ VAL', 'blue');
	}
	$seq = 1;
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		$query[] = '(' .
			fix_null($constants['STUDYID']) . ',' .
			fix_null($constants['DOMAIN']) . ',' .
			fix_null($constants['USUBJID']) . ',' .
			fix_null($seq) . ',' .
			fix_null($subj_array['vstestcd']) . ',' .
			fix_null($subj_array['vstest']) . ',' .
			fix_null($subj_array['vsorres']) . ',' . // vsorres
			fix_null($subj_array['vsorresu']) . ',' .
			fix_null($subj_array['vsorres']) . ',' . // vsstresn
			fix_null($subj_array['vsdtc']) .
			')';
		$seq++;
	}
}
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` VARCHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `VSSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `VSTESTCD` VARCHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `VSTEST` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `VSORRES` VARCHAR(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `VSORRESU` VARCHAR(12) COLLATE utf8_unicode_ci DEFAULT NULL,
  `VSSTRESN` VARCHAR(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `VSDTC` VARCHAR(16) COLLATE utf8_unicode_ci DEFAULT NULL,
	}
	$seq = 1;
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		$query[] = '(' .
			fix_null($constants['STUDYID']) . ',' .
			fix_null($constants['DOMAIN']) . ',' .
			fix_null($constants['USUBJID']) . ',' .
			fix_null($seq) . ',' .
			fix_null($subj_array['sutrt']) . ',' . // sutrt
			fix_null($subj_array['sutrt']) . ',' . // sucat
			fix_null($subj_array['sutrt']) . ',' . // sudecod
			fix_null($subj_array['sudosfrq']) . ',' .
			fix_null($subj_array['suoccur']) . ',' .
			fix_null($subj_array['sustdtc']) . ',' . // sustdtc
			fix_null($subj_array['sustdtc']) .
			')';
		$seq++;
	}
}
/**
 * end Main Loop
 */
/**
 * if not exists, create your domain data table
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` CHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` CHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` CHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `SUSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
foreach ($vals_array as $subj_usubjid => $subj_val_array) {
	//d($subj_val_array);
	$seq = 1;
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		if ($subj_array['dsterm'] != '') {
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['dsterm']) . ',' .
				fix_null($subj_array['dsdecod']). ',' .
				fix_null($subj_array['dsstdtc']) . ',' .
				fix_null($subj_array['dsstdy']) . ',' .
				fix_null($subj_array['dscat']) .
				')';
			$seq++;
		}
	}
}
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` VARCHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `DSSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DSTERM` CHAR(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DSDECOD` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DSSTDTC` VARCHAR(16) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DSSTDY` VARCHAR(4) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DSCAT` VARCHAR(40) COLLATE utf8_unicode_ci DEFAULT NULL,
 * Main loop
 */
foreach ($vals_array as $subj_usubjid => $subj_val_array) {
	//d($subj_val_array);
	$seq = 1;
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		if ($subj_array['coval'] != '') {
			$query[] = '(' . 
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null(substr($subj_array['coval'], 0, 100)) . ',' .
				fix_null($subj_array['coeval']) . ',' .
				fix_null($subj_array['codtc']) .
				')';
			$seq++;
		}
	}
}
$timer['end_main'] = microtime(true);
/**
 * end Main Loop
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` VARCHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `COSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `COVAL` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
			$llt = $subj_array['mhmodify'] != '' ? $subj_array['mhmodify'] : get_llt($subj_array['mhterm']);
			$pt = get_pt($llt);
			$soc = get_bodsys($pt);
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['mhterm']) . ',' .
				fix_null($llt) . ',' .
				fix_null($pt) . ',' .
				fix_null($soc) . ',' .
				fix_null($subj_array['mhoccur']) . ',' .
				fix_null($subj_array['mhpresp']) . ',' .
				fix_null($subj_array['mhstdtc']) . ',' .
				fix_null($subj_array['mhstdtc']) .
				')';
			$seq++;
		}
	}
}
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` CHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` CHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` CHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `MHSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MHTERM` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MHMODIFY` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MHDECOD` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MHBODSYS` VARCHAR(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MHOCCUR` CHAR(1) COLLATE utf8_unicode_ci DEFAULT NULL,
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		if ($subj_array['faorres'] != '') {
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['fatestcd']) . ',' .
				fix_null($subj_array['fatest']) . ',' .
				fix_null($subj_array['faorres']) . ',' .
				fix_null($subj_array['fastresc']) . ',' .
				fix_null($subj_array['fastresn']) . ',' .
				fix_null(fix_case($subj_array['facat'])) . ',' .
				fix_null($subj_array['faobj']) . ',' .
				fix_null($subj_array['fadtc']) .
				')';
			$seq++;
		}
	}
}
$timer['end_main'] = microtime(true);
/**
 * end Main Loop
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` VARCHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `FASEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `FATESTCD` VARCHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
		}
	} else {
		error_log(db_error() . "\n");
		d($other_drug, db_error());
	}
}
/**
 * process all drugs in $drugs_out to populate table
 */
d($drugs_out);
if (!empty($drugs_out)) {
	foreach ($drugs_out AS $drug_out => $other_drug) {
		$query[] = '(' .
			fix_null('vpk') . ',' .
			fix_null($drug_out) . ',' .
			fix_null($other_drug) .
			')';
	}
	$sql .= implode(",\n", $query);
}
!d($sql);
if (!$debug) {
	if (db_query($sql)) {
		d('INSERT successful');
	} else {
		error_log("SQL INSERT FAILED: " . db_error() . "\n");
		d(db_error());
	}
}
$timer['main_end'] = microtime(true);
$init_time = benchmark_timing($timer);
	$seq = 1;
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		if (isset($subj_array['svstdtc'])) {
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['visit']) . ',' .
				fix_null($subj_array['visitdy']) . ',' .
				fix_null($subj_array['svstdtc']) . ',' .
				fix_null($subj_array['svstdtc']) . ',' .
				fix_null($subj_array['svstdy']) . ',' .
				fix_null($subj_array['svstdy']) . ',' .
				fix_null($subj_array['svupdes']) .
				')';
			$seq++;
		}
	}
}
if ($subjects != '') {
	d($query);
}
/**
 * end Main Loop
 */
$timer['end_main'] = microtime(true);
/**
 * if not exists, create your domain data table
 */
				fix_null($meddra['aebdsycd']) . ',' .
				fix_null(fix_case($subj_array['aesev'])) . ',' .
				fix_null($subj_array['aeser']) . ',' .
				fix_null(fix_case($subj_array['aeacn'])) . ',' .
				fix_null(fix_case($subj_array['aerel'])) . ',' .
				fix_null(fix_case($subj_array['aeout'])) . ',' .
				fix_null($subj_array['aescong']) . ',' .
				fix_null($subj_array['aesdisab']) . ',' .
				fix_null($subj_array['aesdth']) . ',' .
				fix_null($subj_array['aeshosp']) . ',' .
				fix_null($subj_array['aeslife']) . ',' .
				fix_null($subj_array['aesmie']) . ',' .
				fix_null($subj_array['aecontrt']) . ',' .
				fix_null($subj_array['aetoxgr']) . ',' .
				fix_null($subj_array['aestdtc']) . ',' .
				fix_null($subj_array['aeendtc']) .
				')';
			$seq++;
		}
	}
}
/**
 * end Main Loop
 */
$timer['end_main'] = microtime(true);
/**
 * if not exists, create your domain data table
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` CHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` CHAR(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'AE',
	$seq = 1;
	$constants['USUBJID'] = $constants['STUDYID'] . '-' . $subj_usubjid;
	foreach ($subj_val_array AS $subj_array) {
		if ($subj_array['exstdtc'] != '') {
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['extrt']) . ',' . // extrt
				fix_null($subj_array['extrt']) . ',' . // exdecod
				fix_null($subj_array['exdose']) . ',' .
				fix_null($subj_array['exdosu']) . ',' .
				fix_null($subj_array['exdosfrq']) . ',' .
				fix_null($subj_array['exstdtc']) . ',' .
				fix_null($subj_array['exendtc']) .
				')';
			$seq++;
		}
	}
}
$timer['end_fields'] = microtime(true);
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` varchar(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'EX',
  `USUBJID` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  `EXSEQ` int(8) DEFAULT NULL,
  `EXTRT` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `EXDECOD` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `EXDOSE` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `EXDOSU` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
		 */
		$subj_values_query = $values_query . "AND a.record = '{$main_row['subjid']}'";
		$subj_values_result = db_query($subj_values_query);
		if ($subj_values_result) {
			while ($vals = db_fetch_assoc($subj_values_result)) {
				if ($vals['IETESTCD'] != '') {
					$query[] = '(' .
						fix_null($constants['STUDYID']) . ',' .
						fix_null($constants['DOMAIN']) . ',' .
						fix_null($constants['USUBJID']) . ',' .
						fix_null($seq) . ',' .
						fix_null($vals['IETESTCD']) . ',' .
						fix_null($vals['IETEST']) . ',' .
						fix_null($vals['IECAT']) . ',' .
						fix_null($vals['IEORRES']) . ',' .
						fix_null($vals['IESTRESC']) .
						')';
					$seq++;
				}
			}
		}
	}
	db_free_result($main_result);
}
/**
 * end Main Loop
 */
/**
 * if not exists, create your domain data table
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['lbtestcd']) . ',' .
				fix_null($subj_array['lbtest']) . ',' .
				fix_null($subj_array['lborres']) . ',' .
				fix_null($subj_array['lborresu']) . ',' .
				fix_null($subj_array['lbstresc']) . ',' .
				fix_null($subj_array['lbstresn']) . ',' .
				fix_null($subj_array['lbstresu']) . ',' .
				fix_null($subj_array['lbstnrlo']) . ',' .
				fix_null($subj_array['lbstnrhi']) . ',' .
				fix_null($subj_array['lbnrind']) . ',' .
				fix_null($subj_array['lbdtc']) . ',' .
				fix_null(get_visit_num($sv_table_name, $constants['USUBJID'], $subj_array['lbdtc'])) . ',' .
				fix_null($subj_array['lbblfl']) .
				')';
			$seq++;
		}
	}
}
$timer['have_sql_array'] = microtime(true);
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL,
  `USUBJID` VARCHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `LBSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `LBTESTCD` VARCHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `LBTEST` VARCHAR(40) COLLATE utf8_unicode_ci DEFAULT NULL,
  `LBORRES` VARCHAR(40) COLLATE utf8_unicode_ci DEFAULT NULL,
  `LBORRESU` VARCHAR(40) COLLATE utf8_unicode_ci DEFAULT NULL,
			if ($meddra_result) {
				$meddra = db_fetch_assoc($meddra_result);
				db_free_result($meddra_result);
			}
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['ceterm']) . ',' .
				fix_null($meddra['aedecod']) . ',' .
				fix_null($meddra['aesoc']) . ',' .
				fix_null($subj_array['ceoccur']) . ',' .
				fix_null($subj_array['cestdtc']) . ',' .
				fix_null($subj_array['ceendtc']) . ',' .
				fix_null('Y') .
				')';
			$seq++;
		}
	}
}
/**
 * end Main Loop
 */
$timer['end_main'] = microtime(true);
/**
 * if not exists, create your domain data table
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` CHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` CHAR(2) COLLATE utf8_unicode_ci NOT NULL,
	foreach ($subj_val_array AS $subj_array) {
		if ($subj_array['cmtrt'] != '') {
			$query[] = '(' .
				fix_null($constants['STUDYID']) . ',' .
				fix_null($constants['DOMAIN']) . ',' .
				fix_null($constants['USUBJID']) . ',' .
				fix_null($seq) . ',' .
				fix_null($subj_array['cmtrt']) . ',' .
				fix_null($subj_array['cmcat']) . ',' .
				fix_null($subj_array['cmdecod']) . ',' .
				fix_null($subj_array['cmindc']) . ',' .
				fix_null($subj_array['cmdose']) . ',' .
				fix_null($subj_array['cmdosu']) . ',' .
				fix_null($subj_array['cmdosfrq']) . ',' .
				fix_null($subj_array['cmstdtc']) . ',' .
				fix_null($subj_array['cmendtc']) .
				')';
			$seq++;
		}
	}
}
$timer['end_fields'] = microtime(true);
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` VARCHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'CM',
  `USUBJID` VARCHAR(16) COLLATE utf8_unicode_ci NOT NULL,
  `CMSEQ` CHAR(8) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CMTRT` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CMCAT` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CMDECOD` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CMINDC` VARCHAR(100) COLLATE utf8_unicode_ci DEFAULT NULL,
			fix_null($constants['DOMAIN']) . ',' .
			fix_null($constants['USUBJID']) . ',' .
			fix_null($subj_array['SUBJID']) . ',' .
			fix_null($subj_array['RFSTDTC']) . ',' .
			fix_null($subj_array['RFENDTC']) . ',' .
			fix_null($constants['SITEID']) . ',' .
			fix_null($subj_array['BRTHDTC']) . ',' .
			fix_null($subj_array['AGE']) . ',' .
			fix_null($subj_array['SEX']) . ',' .
			fix_null(fix_case($subj_array['RACE'])) . ',' .
			fix_null(fix_case($subj_array['ETHNIC'])) . ',' .
			fix_null($subj_array['armcd']) . ',' .
			fix_null($subj_array['arm']) . ',' .
			fix_null($subj_array['actarmcd']) . ',' .
			fix_null($subj_array['actarm']) . ',' .
			fix_null($constants['COUNTRY']) .
			')';
		$seq++;
	}
}
/**
 * end Main Loop
 */
$timer['end_main'] = microtime(true);
/**
 * if not exists, create your domain data table
 */
$table_create_query = "CREATE TABLE IF NOT EXISTS `$table_name` (
  `STUDYID` CHAR(8) COLLATE utf8_unicode_ci NOT NULL,
  `DOMAIN` CHAR(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'DM',
  `USUBJID` CHAR(16) COLLATE utf8_unicode_ci NOT NULL,