
proc onv3DocDEZLoad {} {

 # ----------------------- put the following in "V3_report_cmd new_report"
 # variable v3DocDirVar
 # if {![info exist v3DocDirVar] || ![file isdirectory $v3DocDirVar] || \
 #     ![file isdirectory [file join $v3DocDirVar images]]} {
 #    set n 0 
 #    while 1 {
 #       set v3DocDirVar [file join [pwd] newReport$n]
 #       if ![file exist $v3DocDirVar] {break}
 #       incr n
 #    }
 #    file mkdir $v3DocDirVar
 #    file mkdir  [file join $v3DocDirVar images]
 # }
 #
 # global env
 # set src [file join $env(VISUAL3P_ROOT) report images *]
 # set target [file join $v3DocDirVar images]
 # foreach f [glob -nocomplain $src] { file copy -force $f $target }

   aie_v3p_doc_nodes restore
}

proc onv3DocDEZCancel {} {
   aie_v3p_doc_nodes clear
}

proc onv3DocDEZOK {} {
   v3DocDEZ apply
   aie_v3p_doc_nodes clear
}

proc onv3DocNewLoad {} {
   global env

   variable v3DocUserVar
   if {![info exist v3DocUserVar] || ($v3DocUserVar == "")} {
      if [info exist env(USERNAME)] {
         set v3DocUserVar $env(USERNAME)
      }
   }
   foreach {n v} {report_paper_size A4 report_cover_page 1} {
      variable $n
      if {![info exist $n] || ([set $n] == "")} {
         set $n $v
      }
   }
}

proc onv3DocInfoLoad {} {
   variable v3DocInfoTitleVar
   if {![info exist v3DocInfoTitleVar] || \
         ($v3DocInfoTitleVar == "")} {
      set v3DocInfoTitleVar "Report Summary Info"
   }
   if ![aie_v3p_doc check_report] {return ""}
}

proc onv3DocCFDLoad {} {
   if ![aie_v3p_doc check_summary] {return ""}
}

proc onv3DocEFDLoad {} {
  if ![aie_v3p_doc check_summary] {return ""}
}

proc onv3DocChapLoad {} {
   if ![aie_v3p_doc check_report] {return ""}
}

proc onv3DocSectLoad {} {
  if ![aie_v3p_doc check_chapter] {return ""}
}

proc onv3DocImgLoad {} {
  if ![aie_v3p_doc check_section] {return ""}
}

proc onv3DocTextLoad {} {
  if ![aie_v3p_doc check_section] {return ""}
}

proc onv3DocTableLoad {} {
  if ![aie_v3p_doc check_section] {return ""}

  variable v3DocTableStyleVar
  if {![info exist v3DocTableStyleVar] || \
         ![string is integer -strict $v3DocTableStyleVar]} {
     set v3DocTableStyleVar 1
  }
  variable v3DocTableBdVar
  if {![info exist v3DocTableBdVar] || \
          ![string is integer -strict $v3DocTableBdVar]} {
     set v3DocTableBdVar 1
  }
}

proc onv3DocSaveXMLLoad {} {
   aie_v3p_doc save xml
}

proc onv3DocSaveHTMLoad {} {
   aie_v3p_doc save html
}

proc onv3DocSavePDFLoad {} {
   aie_v3p_doc save pdf
}


proc onv3DocDEZApply {} {
   set type [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName v3DocEXG]

   if {$type == "v3DocNew"} {
      aie_v3p_doc start_report
      v3DocDEZ set v3DocEXGVal 1
      return
   } else {
      if ![aie_v3p_doc check_report] {return}
   }

   switch -- $type {
      v3DocInfo {
         aie_v3p_doc start_summary
         v3DocDEZ set v3DocEXGVal 2
      }
      v3DocCFD {
         set s [V3_report_cmd dataset -type cfd \
                  -model [v3DocDEZ get v3DocCModelVar] \
                  -code [v3DocDEZ get v3DocCCodeVar] \
                  -turbulence_model [v3DocDEZ get v3DocCTurbVar] \
                  -grid_type [v3DocDEZ get v3DocCGridVar] \
                  -date [v3DocDEZ get v3DocCDateVar]]
         v3DocDEZ set v3DocEXGVal -1

         aie_v3p_doc_nodes cfd $s
      }
      v3DocEFD {
         set s [V3_report_cmd dataset -type efd \
                    -instrument [v3DocDEZ get v3DocEDeviceVar] \
                    -sub_type [v3DocDEZ get v3DocESubVar] \
                    -date [v3DocDEZ get v3DocEDateVar] \
                    -auxilary [v3DocDEZ get v3DocEAuxiVar]]
         v3DocDEZ set v3DocEXGVal -1
         aie_v3p_doc_nodes efd $s
      }
      v3DocChap {
         aie_v3p_doc new_chapter
         foreach n {Title Intro Outro} { v3DocDEZ set v3DocChap${n}Var "" }
         v3DocDEZ set v3DocEXGVal 5
      }
      v3DocSect {
         aie_v3p_doc new_section
         foreach n {Title Intro Outro} { v3DocDEZ set v3DocSect${n}Var "" }
         v3DocDEZ set v3DocEXGVal 6
      }
      v3DocImg {
         global tdv_current_viewer

         set wins ""
         set which [v3DocDEZ get v3DocImgWinVar]
         switch -- $which {
            current {lappend wins $tdv_current_viewer}
            dataset {
               global dset_info
               foreach {s n} [array get dset_info *,v3dset] {
                  if {[info exist dset_info($n,gwin)] && \
                      [winfo exist $dset_info($n,gwin)]} {
                     if {[lsearch $wins $dset_info($n,gwin)] < 0} {
                        lappend wins $dset_info($n,gwin)
                     }
                  }
               }
            }
            windows {
               global tdv_workspace toplevel
               set mm $toplevel(windows_menu)
               set n [$mm index end]
               set i [$mm cget -tearoff]
               while {$i <= $n} {
                  set w [lindex [$mm entrycget $i -command] 1]
                  if [winfo exist $w] { lappend wins $w }
                  incr i
               }
            }
            histogram {
               global stat_info
               if {![info exist stat_info(hist_fm)] || \
                   ![winfo exist $stat_info(hist_fm)] || \
                   ![winfo ismapped $stat_info(hist_fm)]} {
                  return ""
               }
               if {![info exist stat_info(hist_gm)] || \
                   ![info exist stat_info(hist_anno)]} {
                  return ""
               }
               lappend wins $stat_info(hist_fm) 
            } 
         }

         set width [v3DocDEZ get v3DocImgWidthVar]
         if ![string is integer -strict $width] {set width ""}
         set height [v3DocDEZ get v3DocImgHeightVar]
         if ![string is integer -strict $height] {set height ""}

         set img_list ""
         foreach w $wins {
            set rr [list window $w]
            if {($width != "") && ($height != "")} {
               lappend rr width ${width}px height ${height}px
            }
            lappend img_list -window $rr
         }

         lappend img_list -format [v3DocDEZ get v3DocImgTypeVar]
         lappend img_list -caption [v3DocDEZ get v3DocImgTextVar]

         set s [eval V3_report_cmd graphics $img_list]

         aie_v3p_doc_nodes graphics $s

         v3DocDEZ set v3DocImgTextVar ""
      }
      v3DocText {
         set val [v3DocDEZ get v3DocTextVar]
         if {$val == ""} {return ""}

         set s [V3_report_cmd text $val]
         aie_v3p_doc_nodes text $s
         v3DocDEZ set v3DocTextVar ""
      }
      v3DocTable {
         set tab_list [list -caption [v3DocDEZ get v3DocTableTextVar]]

         lappend tab_list -header [list Name Mean Median \
                     Minimum Maximum "Mid Mean" \
                    "Stardard Deviation" "Avarage Absolute Deviation" \
                     Skewness Kuttosis "Mid Range"]
         set irow 0
         while 1 {
            set obj [V3_compare_get obj_by_index $irow]
            if {$obj == ""} { break }

            global stat_info
            if [info exist stat_info($obj)] {
               set rr ""
               lappend rr $stat_info($obj,title)
               eval lappend rr [lrange [lindex $stat_info($obj) 0] 0 4]

               set dd [lrange [lindex $stat_info($obj) 1] 0 4]
               set dd [lreplace $dd 2 3 \
                         [lindex [lindex $stat_info($obj) 2] 0] \
                         [lindex [lindex $stat_info($obj) 2] 1]]
               eval lappend rr $dd
               
               lappend tab_list -row $rr
            }
            incr irow 
         }

         variable v3DocTableStyleVar
         if {$v3DocTableStyleVar} {
            lappend tab_list -tablestyle "grid"
         } else {
            lappend tab_list -tablestyle "nogrid"
         }

         variable v3DocTableBdVar
         if [string is integer -strict $v3DocTableBdVar] {
            lappend tab_list -borderstyle $v3DocTableBdVar
         }

         set s [eval V3_report_cmd table $tab_list]

         aie_v3p_doc_nodes table $s
         v3DocDEZ set v3DocTableTextVar ""
      }
   }
}


proc aie_v3p_doc_cmd {what args} {

   switch -- $what {
      start_report {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 0
      }
      start_summary {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 1
      }
      add_cfd {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 2
      }
      add_efd {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 3
      }
      start_chapter {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 4
      }
      start_section {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 5
      }
      add_image {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 6
      }
      add_text {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 7
      }
      add_table {
         v3DocDEZ load
         v3DocDEZ set v3DocEXGVal 8
      }
      save {
         eval aie_v3p_doc save $args
      }
   }
}



proc aie_v3p_doc {what args} {
   global v3doc_info

   set top_tree $::AIEnv::GUIManagement::Tree::aieTree

   switch -- $what {
      check_report {
         set nn [V3_report_get size]
         if {($nn == "") || ($nn == 0)} {
            mess "Need to create a report first\n"
            v3DocDEZ set v3DocEXGVal 0
            return 0
          }
          return 1
      }
      start_report {
         if {[V3_report_get size] > 0} {
            if [confirm_diag "Empty enxisting report"] {return ""}
          # cleanup old report root
            aie_v3p_doc_nodes clear
         }

         set opts ""
         lappend opts title [v3DocDEZ get v3DocTitleVar]
         lappend opts userName [v3DocDEZ get v3DocUserVar]
         lappend opts paper [v3DocDEZ get report_paper_size]

         set cover yes
         if ![v3DocDEZ get report_cover_page] {set cover no}

         lappend opts coverpage $cover

         if [v3DocDEZ exist v3DocDirVar] {
            set dir [v3DocDEZ get v3DocDirVar]
            if {($dir != "") && [file isdirectory [file dirname $dir]]} {
               lappend opts report_dir $dir 
            }
         }
 
         eval V3_report_cmd new_report $opts

       # create a report root in the tree
         aie_v3p_doc_nodes root

         return 1
      }
      check_summary {
         if {[V3_report_get summary] == ""} {
            mess "Need to create a summary chapter first\n"
            v3DocDEZ set v3DocEXGVal 1
            return 0
         }
         return 1 
      }
      start_summary {
         V3_report_cmd summary -title [v3DocDEZ get v3DocInfoTitleVar] \
                          -introduction [v3DocDEZ get v3DocInfoIntroVar] \
                          -finale [v3DocDEZ get v3DocInfoOutroVar]
         aie_v3p_doc_nodes summary
      }
      check_chapter {
         if {[V3_report_get chapter] == ""} {
            mess "Need to create a chapter first\n"
            v3DocDEZ set v3DocEXGVal 4
            return 0
         }
         return 1
      }
      new_chapter {
         V3_report_cmd new_chapter -title [v3DocDEZ get v3DocChapTitleVar] \
                         -introduction [v3DocDEZ get v3DocChapIntroVar] \
                         -finale [v3DocDEZ get v3DocChapOutroVar]
         aie_v3p_doc_nodes chapter
      }
      check_section {
         if {[V3_report_get section] == ""} {
            mess "Need to create a section first\n"
            v3DocDEZ set v3DocEXGVal 5
            return 0
         }
         return 1
      }
      new_section {
         V3_report_cmd new_section -title [v3DocDEZ get v3DocSectTitleVar] \
                         -introduction [v3DocDEZ get v3DocSectIntroVar] \
                         -finale [v3DocDEZ get v3DocSectOutroVar]
         aie_v3p_doc_nodes section
      }
      save {
         set dir [v3DocDEZ get v3DocDirVar]

         set ftype [lindex $args 0]
         set file [tk_getSaveFile -defaultextension $ftype -initialdir $dir \
                        -title "Select a file to save report"]
         if {$file == ""} {return ""}

         if {[file dirname $file] == $dir} {
            set file  [file tail $file]
         }

         set type [file extension $file]
         if [string match -nocase .pdf $type] {
            set type pdf
         } elseif [string match -nocase .htm* $type] {
            set type html
         } else {
            set type xml
         }

         set xml_file [file rootname $file].xml
         V3_report_cmd to_xml $xml_file

         global is_win env
         if !$is_win {return $xml_file}

         set report_dir [file join $env(MED_ROOT) report]
         set DIR [file nativename $env(MED_ROOT)]

         set xmlto [file join $report_dir xml2${type}.bat]
         if ![file executable $xmlto] { return ""}

         if {$type == "html"} {
            V3_report_cmd xml2html $xml_file $file
         } elseif {$type == "pdf"} {

            global hardcopy
            set ofile [file dirname $hardcopy(outfile)]
            if {[file pathtype $ofile] == "relative"} {
               set ofile [file rootname [file join [pwd] $ofile]]
            }

            V3_report_cmd xml2pdf $xml_file $file $ofile
         }
         return $file
      }
   }
}

proc aie_v3p_doc_delete {Tree Node Type args} {
   global v3doc_info

   foreach sub [$Tree nodes $Node] {
      set rr [$Tree itemcget $sub -data]
      aie_v3p_doc_delete $Tree $sub [lindex $rr 0]
   }

   set nd [lindex [$Tree itemcget $Node -data] 1]
   set type [string range $Type 6 end]

   switch $type {
      root {
      }
      cfd - efd {
         V3_report_cmd delete $nd $type
      }
      chapter - section - summary {
         if {[info exist v3doc_info($type)] && \
             ($v3doc_info($type) == $Node)} { 
            unset v3doc_info($type)
         } 
         V3_report_cmd delete $nd $type
      } 
      graphics - text - table {
         V3_report_cmd delete $nd $type
      }
   }
   $Tree delete $Node
}

proc aie_v3p_doc_select {Tree Node Type re args} {
   global v3doc_info

   set type [string range $Type 6 end]
   switch $type {
      root { }
      summary { }
      cfd { }
      efd { }
      chapter {
         if [info exist v3doc_info(chapter)] {
            if {$v3doc_info(chapter) == $Node} { return "" }
            aie_v3p_doc_nodes unselect_chapter $re
         }
         eval $Tree itemconfigure $Node $v3doc_info(selecting)

         set v3doc_info(chapter) $Node
         set rr [$Tree itemcget $v3doc_info(chapter) -data]
         V3_report_cmd go_chapter [lindex $rr 1]

         if {$re} {
            set s [lindex [$Tree nodes $Node] 0]
            if {$s != ""} { aie_v3p_doc_select $Tree $s v3doc_section 0 } 
         }
      }
      section {
         if [info exist v3doc_info(section)] {
            if {$v3doc_info(section) == $Node} { return "" }
            aie_v3p_doc_nodes unselect_section
         }

         eval $Tree itemconfigure $Node $v3doc_info(selecting)

         set v3doc_info(section) $Node
         set rr [$Tree itemcget $v3doc_info(section) -data]
         V3_report_cmd go_section [lindex $rr 1]

         if {$re} {
            set p [$Tree parent $Node]
            while {[lindex [$Tree itemcget $p -data] 0] \
                                       != "v3doc_chapter"} {
               set p [$Tree parent $Node]
            }
            aie_v3p_doc_select $Tree $p v3doc_chapter 0
         }
      }
      graphics - text - table {
         set p [$Tree parent $Node]
         while {[lindex [$Tree itemcget $p -data] 0] \
                                    != "v3doc_section"} {
            set p [$Tree parent $Node]
         }
         aie_v3p_doc_select $Tree $p v3doc_section 1
      }
   }
}

proc aie_v3p_doc_nodes {what args} {
   global v3doc_info
   set top_tree $::AIEnv::GUIManagement::Tree::aieTree

   switch -- $what {
      clear {
         if ![info exist v3doc_info(root)] {return ""}
         $top_tree delete $v3doc_info(root)

         foreach n {root summary chapter section nd_count} {
            if [info exist v3doc_info($n)] {unset v3doc_info($n)}
         }
      }
      restore {
         set nn [V3_report_get size]
         if {($nn == "") || ($nn == 0)} { return "" }
         if [info exist v3doc_info(root)] {return ""}

         V3_report_get wonder_start
         
         aie_v3p_doc_nodes root 

         while 1 {
            set rr [V3_report_get wonder_next]
            if {$rr == ""} {return ""}
            set key [lindex $rr 0]
            set type ""
            switch -- $key {
               Chapter - Section {
                  set type [string tolower $key 0]
                  V3_report_cmd go_$type [lindex $rr 1]
               }
               Graphics - Text - Table - Summary {
                  set type [string tolower $key 0]
               }
               "CFD Data Info" {set type cfd }
               "EFD Data Info" {set type efd }
            }
            if {$type != ""} {
               aie_v3p_doc_nodes $type [lindex $rr 1]
            }
         }
      } 
      root {
         set op [Bitmap::get open]
         foreach {n c im} [list selecting blue $op unselecting black ""] {
            if ![info exist v3doc_info($n)] {
               set v3doc_info($n) [list -fill $c -image $im]
            }
         }

         set v3doc_info(nd_count) 0
         set node v3doc${v3doc_info(nd_count)}
         $top_tree insert end root $node -text "Report" \
                  -open 1 -data "v3doc_$what root *"

         set v3doc_info(root) $node
         incr v3doc_info(nd_count)
      }
      summary {
         set v3doc_info(summary) v3doc${v3doc_info(nd_count)}
         incr v3doc_info(nd_count)

         set s [V3_report_get summary]
         $top_tree insert end $v3doc_info(root) $v3doc_info(summary) \
                  -text "Summary" -open 1 -data "v3doc_$what $s *"
      }
      cfd - efd {
         set node v3doc${v3doc_info(nd_count)}
         incr v3doc_info(nd_count)

         set s [lindex $args 0]
         $top_tree insert end $v3doc_info(summary) $node \
                     -text [string toupper $what] \
                     -open 1 -data "v3doc_$what $s *"
      }
      unselect_chapter {
         if [info exist v3doc_info(chapter)] {
            eval $top_tree itemconfigure \
                   $v3doc_info(chapter) $v3doc_info(unselecting)
            if [lindex $args 0] {
               aie_v3p_doc_nodes unselect_section
            }
            unset v3doc_info(chapter)
         }
      }
      unselect_section {
         if [info exist v3doc_info(section)] {
            eval $top_tree itemconfigure \
                      $v3doc_info(section) $v3doc_info(unselecting)
            unset v3doc_info(section)
         }
      }
      chapter {
         aie_v3p_doc_nodes unselect_chapter 1

         set v3doc_info(chapter) v3doc${v3doc_info(nd_count)}
         incr v3doc_info(nd_count)

         set rr [V3_report_get chapter]
         $top_tree insert end $v3doc_info(root) $v3doc_info(chapter) \
                       -text [lindex $rr 0] \
                       -open 1 -data "v3doc_$what  [lindex $rr 1] *"  

         eval $top_tree itemconfigure \
                   $v3doc_info(chapter) $v3doc_info(selecting)
      }
      section {
         aie_v3p_doc_nodes unselect_section

         set v3doc_info(section) v3doc${v3doc_info(nd_count)}
         incr v3doc_info(nd_count)

         set rr [V3_report_get section]  
         $top_tree insert end $v3doc_info(chapter) $v3doc_info(section) \
                       -text [lindex $rr 0] \
                       -open 1 -data "v3doc_$what [lindex $rr 1] *"

         eval $top_tree itemconfigure \
                   $v3doc_info(section) $v3doc_info(selecting)
      }
      text - graphics - table {
         set node v3doc${v3doc_info(nd_count)}
         incr v3doc_info(nd_count)

         $top_tree insert end $v3doc_info(section) $node \
                      -text [string toupper $what] \
                      -open 1 -data "v3doc_$what [lindex $args 0] *"
      }
   }
}

# ------ the following will be replaced

proc onv3ReportNewLoad {} {
   aie_v3p_report new
}

proc onv3ReportSaveXMLLoad {} {
   aie_v3p_report save xml
}

proc onv3ReportSaveHTMLoad {} {
   aie_v3p_report save html
}

proc onv3ReportSavePDFLoad {} {
   aie_v3p_report save pdf
}



proc onv3ReportDEZLoad {} {
   variable v3VarsEXGVal
   if [info exist v3VarsEXGVal] {
      v3ReportDEZ set v3VarsEXGVal -1
   }
}

proc onv3ReportDEZApply {} {
   set type [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName v3ReportType]

   switch -- $type {
      v3ReportText {
         set val [set v3ReportTextVar]
         if {$val != ""} { V3_report text $val }

         set v3ReportTextVar ""
      }
      v3ReportImg {
         global hardcopy tdv_current_viewer has_fromppm
 
         set has_fromppm [expr {[glob -nocomplain $bindir/fromppm*] != ""}]
         set hardcopy(startcmd_current) ""
         set hardcopy(endcmd_current) ""

         set hardcopy(format) [set v3ReportImgTypeVar]
         set hardcopy(outfile) screen.$hardcopy(format)

         set wins ""
         set which [set v3ReportImgWinVar]
         switch -- $which {
            current {lappend wins $tdv_current_viewer}
            dataset {
               global dset_info 
               foreach {s n} [array get dset_info *,v3dset] {
                  if {[info exist dset_info($n,gwin)] && \
                      [winfo exist $dset_info($n,gwin)]} {
                     if {[lsearch $wins $dset_info($n,gwin)] < 0} {
                        lappend wins $dset_info($n,gwin)
                     }
                  }
               }
            }
            windows {
               global tdv_workspace toplevel
               set mm $toplevel(windows_menu)
               set n [$mm index end]
               set i [$mm cget -tearoff]
               while {$i <= $n} {
                  set w [lindex [$mm entrycget $i -command] 1]
                  if [winfo exist $w] { lappend wins $w }
                  incr i
               }
            }
         }

         set img_list ""
         foreach w $wins {
            top_menuselect $w
            update idletasks

            set hardcopy(sub_current) $w
            set hardcopy(sub_current) $w
            set hardcopy(top_current) .[lindex [split $w \.] 1]

            set file [hardcopy_create]
            if [file exist $file] { lappend img_list -image $file }
         }

         lappend img_list -caption [set v3ReportImgVar]
         eval V3_report graphics $img_list

         set v3ReportImgVar ""
      }
      v3ReportCFD {
         V3_report dataset -type cfd \
                 -model [set v3ReportCModelVar] \
                 -code [set v3ReportCCodeVar] \
                 -turbulence_model [set v3ReportCTurbVar] \
                 -grid_type [set v3ReportCGridVar] \
                 -date [set v3ReportCDateVar]
       # v3ReportDEZ cancel
      }
      v3ReportEFD {
         V3_report dataset -type efd \
                 -instrument [set v3ReportEDeviceVar] \
                 -sub_type [set v3ReportESubVar] \
                 -date [set v3ReportEDateVar] \
                 -auxilary [set v3ReportEAuxiVar]
       # v3ReportDEZ cancel
      }
      v3ReportSaveXML - v3ReportSaveHTM - v3ReportSavePDF {
      }
   }
}


proc aie_v3p_report {what args} {

   switch -- $what {
      add {
         set which 0
         switch -- [lindex $args 0] {
            image {set which 1}
            cfd   {set which 2}
            efd   {set which 3}
         }

         if [v3ReportDEZ exist V3ReporTypeVar] {
            v3ReportDEZ set V3ReporTypeVar $which
         }
         v3ReportDEZ show 
      }
      new {
         if {[V3_report_get size] > 0} {
            if [confirm_diag "Empty enxisting report"] {return}
         }
         V3_report new
      }
      save {
         set ftype [lindex $args 0]
         set file [tk_getSaveFile -defaultextension $ftype -initialdir . \
                        -title "Select a file to save report"]
         if {$file == ""} {return ""}

         set type [file extension $file]
         if [string match -nocase .pdf $type] {
            set type pdf
         } elseif [string match -nocase .htm* $type] {
            set type html
         } else {
            set type xml
         }

         set xml_file [file rootname $file].xml
         V3_report to_xml $xml_file

         global is_win env
         if !$is_win {return $xml_file}

         set report_dir [file join $env(MED_ROOT) report]
         set DIR [file nativename $report_dir]

         set xmlto [file join $report_dir xml2${type}.bat]
         if [file executable $xmlto] {
            mess "exec $xmlto $DIR $xml_file $file\n"
            exec $xmlto $DIR $xml_file $file
            return $file
         }
      }
   }
}
