/**
 * returns an array of detailType structured information for all detailTypes
 *     detailTypes = {"groups" => {"groupIDToIndex":{detailTypeGroupID:index},
 *                                       index:{propName:val,...},...},
 *                    "names":{dtyID:name,...},
 *                    "rectypeUsage" => { dtyID => [rtyID,...], ...},
 *                    'usageCount' => {dtyID:nonzero-count,...},
 *                    "typedefs":{"commonFieldNames":[defDetailTypes Column names],
 *                                "commonNamesToIndex":{"defDetailTypes columnName":index,...},
 *                                dtyID:{"commonFields":[val,....]},
 *                                ...},
 *                    'lookups' => {basetype=>displayName);
 *
 * @global    int $dbID databse ID
 * @param     boolean $useCachedData if true does a lookup for the detailtypes structure in cache
 * @return    object information describing all the detailtypes defined in the database
 * @uses      getDetailTypeColNames()
 * @uses      getColumnNameToIndex()
 * @uses      getRectypeStructureFieldColNames()
 * @uses      getDetailTypeGroups()
 * @uses      getDetailTypeUsageCount()
 * @uses      getDetailTypeDefUsage()
 * @uses      getDtLookups()
 * @uses      getCachedData()
 * @uses      setCachedData()
 */
function getAllDetailTypeStructures($useCachedData = false)
{
    global $dbID;
    $cacheKey = DATABASE . ":AllDetailTypeInfo";
    if ($useCachedData) {
        $dtStructs = getCachedData($cacheKey);
        if ($dtStructs) {
            return $dtStructs;
        }
    }
    $dtG = getDetailTypeGroups();
    /*****DEBUG****/
    //error_log(print_r($dtG,true));
    $dtStructs = array('groups' => $dtG, 'names' => array(), 'rectypeUsage' => getDetailTypeDefUsage(), 'usageCount' => getDetailTypeUsageCount(), 'typedefs' => array('commonFieldNames' => getDetailTypeColNames(), 'fieldNamesToIndex' => getColumnNameToIndex(getDetailTypeColNames())), 'lookups' => getDtLookups());
    $query = "select dtg_ID, dtg_Name, " . join(",", getDetailTypeColNames());
    $query = preg_replace("/dty_ConceptID/", "", $query);
    if ($dbID) {
        //if(trm_OriginatingDBID,concat(cast(trm_OriginatingDBID as char(5)),'-',cast(trm_IDInOriginatingDB as char(5))),'null') as trm_ConceptID
        $query .= " if(dty_OriginatingDBID, concat(cast(dty_OriginatingDBID as char(5)),'-',cast(dty_IDInOriginatingDB as char(5))), concat('{$dbID}-',cast(dty_ID as char(5)))) as dty_ConceptID";
    } else {
        $query .= " if(dty_OriginatingDBID, concat(cast(dty_OriginatingDBID as char(5)),'-',cast(dty_IDInOriginatingDB as char(5))), '') as dty_ConceptID";
    }
    $query .= " from defDetailTypes left join defDetailTypeGroups  on dtg_ID = dty_DetailTypeGroupID" . " order by dtg_Order, dtg_Name, dty_OrderInGroup, dty_Name";
    $res = mysql_query($query);
    $dtStructs['sortedNames'] = mysql__select_assoc('defDetailTypes', 'dty_Name', 'dty_ID', '1 order by dty_Name');
    while ($row = mysql_fetch_row($res)) {
        array_push($dtStructs['groups'][$dtG['groupIDToIndex'][$row[0]]]['allTypes'], $row[2]);
        if ($row[17]) {
            // dty_ShowInLists
            array_push($dtStructs['groups'][$dtG['groupIDToIndex'][$row[0]]]['showTypes'], $row[2]);
        }
        $dtStructs['typedefs'][$row[2]]['commonFields'] = array_slice($row, 2);
        $dtStructs['names'][$row[2]] = $row[3];
    }
    setCachedData($cacheKey, $dtStructs);
    /*****DEBUG****/
    //error_log(print_r($dtStructs['typedefs'],true));
    return $dtStructs;
}
        //[5]
        if (!@$rectypeStructures[$rtStruct["rty_ID"]]) {
            $rectypeStructures[$rtStruct["rty_ID"]] = array($rtsShortRow);
        } else {
            array_push($rectypeStructures[$rtStruct["rty_ID"]], $rtsShortRow);
        }
    }
}
echo "var rectypeStructures = " . json_format($rectypeStructures, true) . ";\n";
echo 'var tempDBName = "' . $tempDBName . '";' . "\n";
echo 'var sourceDBName = "' . $source_db_name . '";' . "\n";
echo 'var URLBase = "' . HEURIST_BASE_URL . '";' . "\n";
echo 'var importTargetDBName = "' . HEURIST_DBNAME . '";' . "\n";
echo 'var importTargetDBFullName = "' . DATABASE . '";' . "\n";
echo "var rectypeGroups = " . json_format($rectypeGroups, true) . ";\n";
echo "var dtlookups = " . json_format(getDtLookups(), true) . ";\n";
?>

            var myDataTable;
            var myDataSource;
            var hideTimer;
            var needHideTip;

            YAHOO.util.Event.addListener(window, "load", function() {
                YAHOO.example.Basic = function() {
                    // Create the columns. Arrow contains the collapse/expand arrow, rtyID is hidden and contains the ID,
                    // rectype contains the name, matches the amount of matches and a tooltip, import a button
                    var myColumnDefs = [
                        { key:"arrow", label:"", formatter:YAHOO.widget.RowExpansionDataTable.formatRowExpansion },
                        { key:"import", label:"Import", sortable:false, resizeable:false, width:30 },
                        { key:"rectype", label:"<span title='Click on row to view information about the record type'><u>Record type</u></span>",