     return createWordReport(filename, data)
 
 # ANIMAL BIOASSAY REPORT -------------------------------------------------------
-animalWordReport = (tbl_id, filename) ->
-
-    getData = () ->
-        tbl = Tables.findOne(tbl_id)
-        studies = AnimalEvidence
-            .find({tbl_id: tbl_id}, {sort: {sortIdx: 1}})
-            .fetch()
-
-        for study in studies
-            study.reference = Reference.findOne(_id: study.referenceID)
-            share.setAnimalWordFields(study)
-
-        d =
-            "monographAgent": tbl.monographAgent
-            "volumeNumber": tbl.volumeNumber
-            "hasTable": true
-            "table": tbl
-            "studies": studies
-
-        return d
-
-    data = getData()
-    path = share.getWordTemplatePath(filename)
-    docx = new DocxGen().loadFromFile(path, {async: false, parser: angularParser})
-    docx.setTags(data)
-    docx.applyTags()
-    docx.output({type: "string"})
-
+animalWordReport = (tbl_id) ->
+    tbl = Tables.findOne(tbl_id)
+    studies = AnimalEvidence
+        .find({tbl_id: tbl_id}, {sort: {sortIdx: 1}})
+        .fetch()
+
+    for study in studies
+        study.reference = Reference.findOne(_id: study.referenceID)
+        share.setAnimalWordFields(study)
+
+    d =
+        "monographAgent": tbl.monographAgent
+        "volumeNumber": tbl.volumeNumber
+        "hasTable": true
+        "table": tbl
+        "studies": studies
+
+    return d
 
 # MECHANISTIC REPORT -----------------------------------------------------------
-mechanisticWordReport = (tbl_id, filename) ->
+mechanisticWordReport = (tbl_id) ->
 
     getReferences = (obj) ->
         refs = Reference.find({_id: {$in: obj.references}}).fetch()

            getReferences(child)
            getChildren(child)

    path = share.getWordTemplatePath(filename)
    docx = new DocxGen().loadFromFile(path, {async: false, parser: angularParser})
    docx.setTags(data)
    docx.applyTags()
    docx.output({type: "string"})

# GENOTOXICITY REPORT ----------------------------------------------------------
genotoxWordReport = (tbl_id, filename) ->

    getData = () ->
        tbl = Tables.findOne(tbl_id)
        d =
            "monographAgent": tbl.monographAgent
            "volumeNumber": tbl.volumeNumber
            "hasTable": true
            "table": tbl

        vals = GenotoxEvidence
            .find({tbl_id: tbl_id}, {sort: {sortIdx: 1}})
            .fetch()

        for val in vals
            val.reference = Reference.findOne(_id: val.referenceID)
            share.setGenotoxWordFields(val)

        d.nonMammalianInVitro = _.filter(vals, (v) -> return v.dataClass == "Non-mammalian in vitro")
        d.mammalianInVitro = _.filter(vals, (v) -> return v.dataClass == "Mammalian and human in vitro")
        d.animalInVivo = _.filter(vals, (v) -> return v.dataClass == "Animal in vivo")
        d.humanInVivo = _.filter(vals, (v) -> return v.dataClass == "Human in vivo")
        return d

    data = getData()
    path = share.getWordTemplatePath(filename)
    docx = new DocxGen().loadFromFile(path, {async: false, parser: angularParser})
    docx.setTags(data)
    docx.applyTags()
    docx.output({type: "string"})


Meteor.methods
    downloadWordReport: (tbl_id, filename) ->
        tbl = Tables.findOne(tbl_id)
        switch tbl.tblType
            when "Exposure Evidence"
                fn = exposureWordReport
            when "Epidemiology Evidence"
                fn = epiWordReport
            when "Animal Bioassay Evidence"
                fn = animalWordReport
            when "Genetic and Related Effects"
                fn = genotoxWordReport
            when "Mechanistic Evidence Summary"
                fn = mechanisticWordReport
            else
                return console.error("unknown table type: " + tbl.tblType)
        fn(tbl_id, filename)

    monographAgentEpiReport: (d) ->
        epiWordReportMultiTable(d.templateFN, d.monographagent, d.volumenumber)

    docx.applyTags()
    return docx.output({type: "string"})

epiWordReport = (tbl_id, epiSortOrder) ->
    switch epiSortOrder
        when "Reference"
            return getEpiDataByReference(tbl_id)
        when "Organ-site"
            return getEpiDataByOrganSite(tbl_id)
        else
            console.error("unknown epiSortOrder")

epiWordReportMultiTable = (filename, monographAgent, volumeNumber) ->
    epiSortOrder = ReportTemplate.findOne({filename: filename}).epiSortOrder
