set log .logging
set check_log_name .check_log_name
set log_channel -1
set log_entry ""
set log_filename "disk_logging"
set log_time 1
set log_interval 1
set disk_status 0

proc logging { } {
	
    global log log_filename log_time log_interval
    global disk_status

    if [winfo exists $log] {destroy $log}
		
    global env
    set env(DISPLAY) :0
    toplevel $log -width 22c -height 16c
    wm title $log "Logging Field"
	


    label $log.lab43 \
        -foreground #0000d0 -relief groove -text {Add Logging Field}
    label $log.lab44 \
        -foreground #0000d0 -relief groove \
        -text {Existing Logging Field}

    label $log.lab58 \
        -text {Logging Fields}

    listbox $log.lis57 -font {Halvetica 10} \
        -background white -yscroll "$log.vscr set" -xscroll "$log.hscr set"
    scrollbar $log.vscr \
        -command {$log.lis57 yview}
    scrollbar $log.hscr \
        -command {$log.lis57 xview} -orient horizontal

    # Type
    frame $log.fra65 \
        -borderwidth 2 -relief groove
    entry $log.fra65.ent78 \
        -background white -textvariable log_entry
    label $log.lab71 \
        -text {Select Type}
    radiobutton $log.fra65.rad66 \
        -text "Sub Analog" -variable log_type  -value 0  -command {select_log_sub_analog; set log_type 0}
    radiobutton $log.fra65.rad67 \
        -text "Surface Analog" -value 1 -command {select_log_surf_analog;}
    radiobutton $log.fra65.rad75 \
        -text "Telemetry Error" -variable log_type  -value 2  -command {set log_entry "Telemetry Error"; set log_type 2}
    radiobutton $log.fra65.rad76 \
        -text "Teleos Input" -variable log_type  -value 3  -command {set log_entry "Teleos Input"; set log_type 3}

    radiobutton $log.fra65.rad70 \
        -text "Heading" -variable log_type  -value 4  -command {set log_entry "Heading"; set log_type 4}
    radiobutton $log.fra65.rad71 \
        -text "Pitch" -variable log_type  -value 5  -command {set log_entry "Pitch"; set log_type 5}
    radiobutton $log.fra65.rad72 \
        -text "Roll" -variable log_type  -value 6  -command {set log_entry "Roll"; set log_type 6}
    radiobutton $log.fra65.rad73 \
        -text "Depth (Meter)" -variable log_type  -value 7  -command {set log_entry "Depth"; set log_type 7}
    radiobutton $log.fra65.rad74 \
        -text "Depth (Milibar)" -variable log_type  -value 8  -command {set log_entry "Milibar"; set log_type 8}

    radiobutton $log.fra65.rad77 \
        -text "Speed 1" -variable log_type  -value 9  -command {set log_entry "Speed 1"; set log_type 9}
    radiobutton $log.fra65.rad78 \
        -text "Speed 2" -variable log_type  -value 10  -command {set log_entry "Speed 2"; set log_type 10}
    radiobutton $log.fra65.rad79 \
        -text "Speed 3" -variable log_type  -value 11  -command {set log_entry "Speed 3"; set log_type 11}
    radiobutton $log.fra65.rad80 \
        -text "Water Alarm" -variable log_type  -value 12  -command {set log_entry "Water Alarm"; set log_type 12}
    radiobutton $log.fra65.rad81 \
        -text "GF Board 1" -variable log_type  -value 13  -command {set log_entry "GF Board 1"; set log_type 13}
    radiobutton $log.fra65.rad82 \
        -text "GF Board 2" -variable log_type  -value 14  -command {set log_entry "GF Board 2"; set log_type 14}


    # OK, Add, Remove, and Close button
    button $log.ok -width 5 -height 2 -text OK -command check_log_name
    button $log.but88 \
        -command {add_log $log_entry  } \
        -width 5 -height 2 -text Add
    button $log.but89 \
        -command {delete_log $log_entry} \
        -width 5 -height 2 -text Remove
    button $log.but90 \
        -command {destroy $log} \
        -width 5 -height 2 -text Close

    	


    ###################
    # SETTING GEOMETRY
    ###################
    place $log.lab43 \
        -x 20 -y 15 -width 350 -height 20
    place $log.lab44 \
        -x 400 -y 15 -width 200 -height 20
    place $log.lab58 \
        -x 400 -y 50

    # Listbox for the logging field (and scrollbar)
    place $log.lis57 \
        -x 400 -y 75 -width 200 -height 226
    place $log.vscr \
        -x 600 -y 75 -width 16 -height 225
    place $log.hscr \
        -x 400 -y 300 -width 200 -height 16

    # Type
    place $log.fra65 \
        -x 20 -y 75 -width 350 -height 300
    place $log.fra65.ent78 \
        -x 15 -y 15
    place $log.lab71 \
        -x 20 -y 50

    place $log.fra65.rad66 \
        -x 10 -y 50
    place $log.fra65.rad67 \
        -x 10 -y 75
    place $log.fra65.rad75 \
        -x 10 -y 100
    place $log.fra65.rad76 \
        -x 10 -y 125
    place $log.fra65.rad70 \
        -x 10 -y 150
    place $log.fra65.rad71 \
        -x 10 -y 175
    place $log.fra65.rad72 \
        -x 10 -y 200
    place $log.fra65.rad73 \
        -x 10 -y 225
    place $log.fra65.rad74 \
        -x 10 -y 250
    place $log.fra65.rad77 \
        -x 200 -y 50
    place $log.fra65.rad78 \
        -x 200 -y 75
    place $log.fra65.rad79 \
        -x 200 -y 100
    place $log.fra65.rad80 \
        -x 200 -y 125
    place $log.fra65.rad81 \
        -x 200 -y 150
    place $log.fra65.rad82 \
        -x 200 -y 175

    # Filename
    label $log.filename_label -text "Filename"
    entry $log.filename_entry -textvariable log_filename \
    	-bg white -width 15

    place $log.filename_label -x 400 -y 325
    place $log.filename_entry -x 490 -y 325

    # Length of Time to Log
    label $log.time_label -text "Logging Time "
    entry $log.time_entry -textvariable log_time \
    	-bg white -width 10
    label $log.time_limit -text "min"	

    place $log.time_label -x 400 -y 350
    place $log.time_entry -x 490 -y 350
    place $log.time_limit -x 570 -y 350


    # Sampling Interval
    label $log.sampling_label -text "Sampling Int"
    entry $log.sampling_entry -textvariable log_interval \
    	-bg white -width 10
    label $log.sampling_int -text "100 ms"	
    

    place $log.sampling_label -x 400 -y 375
    place $log.sampling_entry -x 490 -y 375
    place $log.sampling_int -x 570 -y 375



    bind $log.filename_entry <Return> { check_log_name }
    bind $log.sampling_entry <Return> { check_log_name }
    bind $log.time_entry <Return> { check_log_name }

    # OK, Add, Remove, Close buttons
    place $log.ok \
        -x 330 -y 400
    place $log.but88 \
        -x 400 -y 400
    place $log.but89 \
        -x 470 -y 400
    place $log.but90 \
        -x 540 -y 400

     # Add a  disk enable button
     # Calls c++ function to log data to disk and also print out data
     checkbutton $log.disk_enable -text "Disk Enable" -variable disk_status -command check_log_name

     place $log.disk_enable  -x 30 -y 415

	#---------------------------------------#      		
 	# Display the previous selection     	#
 	# Inserting data into the listbox	#	
  	#---------------------------------------#
      		
   	set logging_size [get_log_count]
   	#puts "logging new size: logging_size $logging_size"

        for {set i 0} {$i < $logging_size} {incr i} {
        	
		set temp [split [get_log_info $i] |]
                set log_filename [lindex $temp 0]
		set log_time [lindex $temp 1]
		set log_interval [lindex $temp 2]
      	       	set temp_type [lindex $temp 3]
        	set temp_channel [lindex $temp 4]
        	
        	#-----------------------------------------------------------------------#
        	# We need this comparison so that when the user rename the		#
        	# element, then the name in the list also get updated          		#
        	# Here, we avoid using outdated name stored in logging database       	#
        	# However, if the name is empty, then we defaulted to        		#     	
        	# "Sub Analog ###" or "Surface Analog ###" where "###" is the          	#
        	# channel number                                                       	#
        	#-----------------------------------------------------------------------#
        	
        	if {$temp_type == 0} {
        		set name [get_element_name 0 0 $temp_channel]
        		if {$name == ""} {set name [lindex $temp 5]}         		
        	} elseif {$temp_type == 1} {
        		set name [get_element_name 1 0 $temp_channel]
        		if {$name == ""} {set name [lindex $temp 5]}
        	} elseif {$temp_type == 15} {
        		set name [get_element_name 1 3 $temp_channel]
			if {$name == ""} {set name [lindex $temp 5]}
		} else {
        	      	set name [lindex $temp 5]
        	}
        	
        	
      		$log.lis57 insert end "$name"
               	
        }
         	
}


#-----------------------------------------------------------------------------#
#
# This procedure is to create menu for the sub analog channels (48 of them)
#
#-----------------------------------------------------------------------------#

set log_sub_ana  .logging_sub_analog

proc select_log_sub_analog { } {

   global log_entry log_channel
   global log_sub_ana



   if [winfo exists $log_sub_ana] { destroy $log_sub_ana }

   global env
   set env(DISPLAY) :0
   toplevel $log_sub_ana


   label $log_sub_ana.title -text "Analog Input"  -relief groove -fg blue
   pack $log_sub_ana.title -side top  -fill x -padx 3 -pady 3
   label $log_sub_ana.title2 -text "Analog Output"  -relief groove -fg blue

   menubutton $log_sub_ana.b1 -text "1-8" -menu $log_sub_ana.b1.menu
   menubutton $log_sub_ana.b2 -text "9-16" -menu $log_sub_ana.b2.menu
   menubutton $log_sub_ana.b3 -text "17-24" -menu $log_sub_ana.b3.menu
   menubutton $log_sub_ana.b4 -text "25-32" -menu $log_sub_ana.b4.menu
   menubutton $log_sub_ana.b5 -text "33-40" -menu $log_sub_ana.b5.menu
   menubutton $log_sub_ana.b6 -text "41-48" -menu $log_sub_ana.b6.menu
   menubutton $log_sub_ana.b7 -text "1-8" -menu $log_sub_ana.b7.menu
   menubutton $log_sub_ana.b8 -text "9-16" -menu $log_sub_ana.b8.menu


   menu $log_sub_ana.b1.menu
   $log_sub_ana.b1.menu add command -label "1 [get_element_name 0 0 1]" \
	-command {destroy $log_sub_ana ; set log_channel 1; set log_entry [get_element_name 0 0 1]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "2 [get_element_name 0 0 2]" \
	-command {destroy $log_sub_ana ; set log_channel 2; set log_entry  [get_element_name 0 0 2]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "3 [get_element_name 0 0 3]" \
	-command {destroy $log_sub_ana ; set log_channel 3; set log_entry  [get_element_name 0 0 3]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "4 [get_element_name 0 0 4]" \
	-command {destroy $log_sub_ana ; set log_channel 4; set log_entry  [get_element_name 0 0 4]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "5 [get_element_name 0 0 5]" \
	-command {destroy $log_sub_ana ; set log_channel 5; set log_entry  [get_element_name 0 0 5]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "6 [get_element_name 0 0 6]" \
	-command {destroy $log_sub_ana ; set log_channel 6; set log_entry  [get_element_name 0 0 6]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "7 [get_element_name 0 0 7]" \
	-command {destroy $log_sub_ana ; set log_channel 7; set log_entry  [get_element_name 0 0 7]; subs_name $log_entry }
   $log_sub_ana.b1.menu add command -label "8 [get_element_name 0 0 8]" \
	-command {destroy $log_sub_ana ; set log_channel 8; set log_entry  [get_element_name 0 0 8]; subs_name $log_entry }
	                                                                                         	
	


   menu $log_sub_ana.b2.menu
   $log_sub_ana.b2.menu add command -label "9 [get_element_name 0 0 9]" \
	-command {destroy $log_sub_ana ; set log_channel 9; set log_entry  [get_element_name 0 0 9]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "10 [get_element_name 0 0 10]" \
	-command {destroy $log_sub_ana ; set log_channel 10; set log_entry  [get_element_name 0 0 10]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "11 [get_element_name 0 0 11]" \
	-command {destroy $log_sub_ana ; set log_channel 11; set log_entry  [get_element_name 0 0 11]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "12 [get_element_name 0 0 12]" \
	-command {destroy $log_sub_ana ; set log_channel 12; set log_entry  [get_element_name 0 0 12]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "13 [get_element_name 0 0 13]" \
	-command {destroy $log_sub_ana ; set log_channel 13; set log_entry  [get_element_name 0 0 13]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "14 [get_element_name 0 0 14]" \
	-command {destroy $log_sub_ana ; set log_channel 14; set log_entry  [get_element_name 0 0 14]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "15 [get_element_name 0 0 15]" \
	-command {destroy $log_sub_ana ; set log_channel 15; set log_entry  [get_element_name 0 0 15]; subs_name $log_entry }
   $log_sub_ana.b2.menu add command -label "16 [get_element_name 0 0 16]" \
	-command {destroy $log_sub_ana ; set log_channel 16; set log_entry  [get_element_name 0 0 16]; subs_name $log_entry }

	

   menu $log_sub_ana.b3.menu
   $log_sub_ana.b3.menu add command -label "17 [get_element_name 0 0 17]" \
	-command {destroy $log_sub_ana ; set log_channel 17; set log_entry  [get_element_name 0 0 17]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "18 [get_element_name 0 0 18]" \
	-command {destroy $log_sub_ana ; set log_channel 18; set log_entry  [get_element_name 0 0 18]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "19 [get_element_name 0 0 19]" \
	-command {destroy $log_sub_ana ; set log_channel 19; set log_entry  [get_element_name 0 0 19]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "20 [get_element_name 0 0 20]" \
	-command {destroy $log_sub_ana ; set log_channel 20; set log_entry  [get_element_name 0 0 20]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "21 [get_element_name 0 0 21]" \
	-command {destroy $log_sub_ana ; set log_channel 21; set log_entry  [get_element_name 0 0 21]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "22 [get_element_name 0 0 22]" \
	-command {destroy $log_sub_ana ; set log_channel 22; set log_entry  [get_element_name 0 0 22]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "23 [get_element_name 0 0 23]" \
	-command {destroy $log_sub_ana ; set log_channel 23; set log_entry  [get_element_name 0 0 23]; subs_name $log_entry }
   $log_sub_ana.b3.menu add command -label "24 [get_element_name 0 0 24]" \
	-command {destroy $log_sub_ana ; set log_channel 24; set log_entry  [get_element_name 0 0 24]; subs_name $log_entry }

	


   menu $log_sub_ana.b4.menu
   $log_sub_ana.b4.menu add command -label "25 [get_element_name 0 0 25]" \
	-command {destroy $log_sub_ana ; set log_channel 25; set log_entry  [get_element_name 0 0 25]; subs_name $log_entry }
   $log_sub_ana.b4.menu add command -label "26 [get_element_name 0 0 26]" \
	-command {destroy $log_sub_ana ; set log_channel 26; set log_entry  [get_element_name 0 0 26]; subs_name $log_entry }
   $log_sub_ana.b4.menu add command -label "27 [get_element_name 0 0 27]" \
	-command {destroy $log_sub_ana ; set log_channel 27; set log_entry  [get_element_name 0 0 27]; subs_name $log_entry }
   $log_sub_ana.b4.menu add command -label "28 [get_element_name 0 0 28]" \
	-command {destroy $log_sub_ana ; set log_channel 28; set log_entry  [get_element_name 0 0 28]; subs_name $log_entry }   	
   $log_sub_ana.b4.menu add command -label "29 [get_element_name 0 0 29]" \
	-command {destroy $log_sub_ana ; set log_channel 29; set log_entry  [get_element_name 0 0 29]; subs_name $log_entry }
   $log_sub_ana.b4.menu add command -label "30 [get_element_name 0 0 30]" \
	-command {destroy $log_sub_ana ; set log_channel 30; set log_entry  [get_element_name 0 0 30]; subs_name $log_entry }
   $log_sub_ana.b4.menu add command -label "31 [get_element_name 0 0 31]" \
	-command {destroy $log_sub_ana ; set log_channel 31; set log_entry  [get_element_name 0 0 31]; subs_name $log_entry}
   $log_sub_ana.b4.menu add command -label "32 [get_element_name 0 0 32]" \
	-command {destroy $log_sub_ana ; set log_channel 32; set log_entry  [get_element_name 0 0 32]; subs_name $log_entry }


   menu $log_sub_ana.b5.menu	
   $log_sub_ana.b5.menu add command -label "33 [get_element_name 0 0 33]" \
	-command {destroy $log_sub_ana ; set log_channel 33; set log_entry  [get_element_name 0 0 33]; subs_name $log_entry }
   $log_sub_ana.b5.menu add command -label "34 [get_element_name 0 0 34]" \
	-command {destroy $log_sub_ana ; set log_channel 34; set log_entry  [get_element_name 0 0 34]; subs_name $log_entry }
   $log_sub_ana.b5.menu add command -label "35 [get_element_name 0 0 35]" \
	-command {destroy $log_sub_ana ; set log_channel 35; set log_entry  [get_element_name 0 0 35]; subs_name $log_entry }
   $log_sub_ana.b5.menu add command -label "36 [get_element_name 0 0 36]" \
	-command {destroy $log_sub_ana ; set log_channel 36; set log_entry  [get_element_name 0 0 36]; subs_name $log_entry }   	
   $log_sub_ana.b5.menu add command -label "37 [get_element_name 0 0 37]" \
	-command {destroy $log_sub_ana ; set log_channel 37; set log_entry  [get_element_name 0 0 37]; subs_name $log_entry }
   $log_sub_ana.b5.menu add command -label "38 [get_element_name 0 0 38]" \
	-command {destroy $log_sub_ana ; set log_channel 38; set log_entry  [get_element_name 0 0 38]; subs_name $log_entry }
   $log_sub_ana.b5.menu add command -label "39 [get_element_name 0 0 39]" \
	-command {destroy $log_sub_ana ; set log_channel 39; set log_entry  [get_element_name 0 0 39]; subs_name $log_entry }
   $log_sub_ana.b5.menu add command -label "40 [get_element_name 0 0 40]" \
	-command {destroy $log_sub_ana ; set log_channel 40; set log_entry  [get_element_name 0 0 40]; subs_name $log_entry }

   menu $log_sub_ana.b6.menu	
   $log_sub_ana.b6.menu add command -label "41 [get_element_name 0 0 41]" \
	-command {destroy $log_sub_ana ; set log_channel 41; set log_entry  [get_element_name 0 0 41]; subs_name $log_entry }
   $log_sub_ana.b6.menu add command -label "42 [get_element_name 0 0 42]" \
	-command {destroy $log_sub_ana ; set log_channel 42; set log_entry  [get_element_name 0 0 42]; subs_name $log_entry }
   $log_sub_ana.b6.menu add command -label "43 [get_element_name 0 0 43]" \
	-command {destroy $log_sub_ana ; set log_channel 43; set log_entry  [get_element_name 0 0 43]; subs_name $log_entry }
   $log_sub_ana.b6.menu add command -label "44 [get_element_name 0 0 44]" \
	-command {destroy $log_sub_ana ; set log_channel 44; set log_entry  [get_element_name 0 0 44]; subs_name $log_entry }   	
   $log_sub_ana.b6.menu add command -label "45 [get_element_name 0 0 45]" \
	-command {destroy $log_sub_ana ; set log_channel 45; set log_entry  [get_element_name 0 0 45]; subs_name $log_entry }
   $log_sub_ana.b6.menu add command -label "46 [get_element_name 0 0 46]" \
	-command {destroy $log_sub_ana ; set log_channel 46; set log_entry  [get_element_name 0 0 46]; subs_name $log_entry }
   $log_sub_ana.b6.menu add command -label "47 [get_element_name 0 0 47]" \
	-command {destroy $log_sub_ana ; set log_channel 47; set log_entry  [get_element_name 0 0 47]; subs_name $log_entry }
   $log_sub_ana.b6.menu add command -label "48 [get_element_name 0 0 48]" \
	-command {destroy $log_sub_ana ; set log_channel 48; set log_entry  [get_element_name 0 0 48]; subs_name $log_entry }

   menu $log_sub_ana.b7.menu
   $log_sub_ana.b7.menu add command -label "1 [get_element_name 0 0 49]" \
	-command {destroy $log_sub_ana ; set log_channel 49; set log_entry [get_element_name 0 0 49]; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "2 [get_element_name 0 0 50]" \
	-command {destroy $log_sub_ana ; set log_channel 50; set log_entry  [get_element_name 0 0 50]; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "3 [get_element_name 0 0 51]" \
	-command {destroy $log_sub_ana ; set log_channel 51; set log_entry  [get_element_name 0 0 51]; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "4 [get_element_name 0 0 52]" \
	-command {destroy $log_sub_ana ; set log_channel 52; set log_entry  [get_element_name 0 0 52]; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "5 [get_element_name 0 0 53]" \
	-command {destroy $log_sub_ana ; set log_channel 53; set log_entry  [get_element_name 0 0 53]; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "6 [get_element_name 0 0 54]" \
	-command {destroy $log_sub_ana ; set log_channel 54; set log_entry  [get_element_name 0 0 54; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "7 [get_element_name 0 0 55]" \
	-command {destroy $log_sub_ana ; set log_channel 55; set log_entry  [get_element_name 0 0 55]; subs_name $log_entry }
   $log_sub_ana.b7.menu add command -label "8 [get_element_name 0 0 56]" \
	-command {destroy $log_sub_ana ; set log_channel 56; set log_entry  [get_element_name 0 0 56]; subs_name $log_entry }
	                                                                                         	
   menu $log_sub_ana.b8.menu
   $log_sub_ana.b8.menu add command -label "9 [get_element_name 0 0 57]" \
	-command {destroy $log_sub_ana ; set log_channel 57; set log_entry  [get_element_name 0 0 57]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "10 [get_element_name 0 0 58]" \
	-command {destroy $log_sub_ana ; set log_channel 58; set log_entry  [get_element_name 0 0 58]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "11 [get_element_name 0 0 59]" \
	-command {destroy $log_sub_ana ; set log_channel 59; set log_entry  [get_element_name 0 0 59]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "12 [get_element_name 0 0 60]" \
	-command {destroy $log_sub_ana ; set log_channel 60; set log_entry  [get_element_name 0 0 60]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "13 [get_element_name 0 0 61]" \
	-command {destroy $log_sub_ana ; set log_channel 61; set log_entry  [get_element_name 0 0 61]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "14 [get_element_name 0 0 62]" \
	-command {destroy $log_sub_ana ; set log_channel 62; set log_entry  [get_element_name 0 0 62]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "15 [get_element_name 0 0 63]" \
	-command {destroy $log_sub_ana ; set log_channel 63; set log_entry  [get_element_name 0 0 63]; subs_name $log_entry }
   $log_sub_ana.b8.menu add command -label "16 [get_element_name 0 0 64]" \
	-command {destroy $log_sub_ana ; set log_channel 64; set log_entry  [get_element_name 0 0 64]; subs_name $log_entry }


   # add a Close button
   button $log_sub_ana.close -text Close -command {destroy $log_sub_ana}
	

   # Pack things up
   pack $log_sub_ana.b1 $log_sub_ana.b2 $log_sub_ana.b3 $log_sub_ana.b4 \
    	$log_sub_ana.b5 $log_sub_ana.b6 -side top
   pack $log_sub_ana.title2 -side top  -fill x -padx 3 -pady 3
   pack $log_sub_ana.b7 $log_sub_ana.b8
   pack $log_sub_ana.close -pady 2m 	

   	
	
}


#-----------------------------------------------------------------------------#
#
# This procedure is to create menu for the surface analog channels
# (16 of them)
#
#-----------------------------------------------------------------------------#



set log_surf_ana  .logging_surf_analog

proc select_log_surf_analog { } {

   global log_entry log_type log_channel
   global log_surf_ana



   if [winfo exists $log_surf_ana] { destroy $log_surf_ana }

   global env
   set env(DISPLAY) :0
   toplevel $log_surf_ana


   label $log_surf_ana.title -text "Analog Input"  -relief groove -fg blue
   pack $log_surf_ana.title -side top  -fill x -padx 3 -pady 3

   label $log_surf_ana.title2 -text "Analog Output"  -relief groove -fg blue


   menubutton $log_surf_ana.b1 -text "1-8" -menu $log_surf_ana.b1.menu
   menubutton $log_surf_ana.b2 -text "9-16" -menu $log_surf_ana.b2.menu
   menubutton $log_surf_ana.b3 -text "1-4" -menu $log_surf_ana.b3.menu

   menu $log_surf_ana.b1.menu
   $log_surf_ana.b1.menu add command -label "1 [get_element_name 1 0 1]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 1; set log_entry  [get_element_name 1 0 1]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "2 [get_element_name 1 0 2]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 2; set log_entry  [get_element_name 1 0 2]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "3 [get_element_name 1 0 3]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 3; set log_entry  [get_element_name 1 0 3]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "4 [get_element_name 1 0 4]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 4; set log_entry  [get_element_name 1 0 4]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "5 [get_element_name 1 0 5]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 5; set log_entry  [get_element_name 1 0 5]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "6 [get_element_name 1 0 6]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 6; set log_entry  [get_element_name 1 0 6]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "7 [get_element_name 1 0 7]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 7; set log_entry  [get_element_name 1 0 7]; subs_name $log_entry }
   $log_surf_ana.b1.menu add command -label "8 [get_element_name 1 0 8]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 8; set log_entry  [get_element_name 1 0 8]; subs_name $log_entry }
	                                                                                         	
	


   menu $log_surf_ana.b2.menu
   $log_surf_ana.b2.menu add command -label "9 [get_element_name 1 0 9]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 9; set log_entry  [get_element_name 1 0 9]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "10 [get_element_name 1 0 10]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 10; set log_entry  [get_element_name 1 0 10]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "11 [get_element_name 1 0 11]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 11; set log_entry  [get_element_name 1 0 11]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "12 [get_element_name 1 0 12]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 12; set log_entry  [get_element_name 1 0 12]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "13 [get_element_name 1 0 13]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 13; set log_entry  [get_element_name 1 0 13]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "14 [get_element_name 1 0 14]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 14; set log_entry  [get_element_name 1 0 14]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "15 [get_element_name 1 0 15]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 15; set log_entry  [get_element_name 1 0 15]; subs_name $log_entry }
   $log_surf_ana.b2.menu add command -label "16 [get_element_name 1 0 16]" \
	-command {destroy $log_surf_ana ; set log_type 1; set log_channel 16; set log_entry  [get_element_name 1 0 16]; subs_name $log_entry }

   menu $log_surf_ana.b3.menu
   $log_surf_ana.b3.menu add command -label "1 [get_element_name 1 3 1]" \
	-command {destroy $log_surf_ana ; set log_type 15; set log_channel 1; set log_entry  [get_element_name 1 3 1]; subs_name $log_entry }
   $log_surf_ana.b3.menu add command -label "2 [get_element_name 1 3 2]" \
	-command {destroy $log_surf_ana ; set log_type 15; set log_channel 2; set log_entry  [get_element_name 1 3 2]; subs_name $log_entry }
   $log_surf_ana.b3.menu add command -label "3 [get_element_name 1 3 3]" \
	-command {destroy $log_surf_ana ; set log_type 15; set log_channel 3; set log_entry  [get_element_name 1 3 3]; subs_name $log_entry }
   $log_surf_ana.b3.menu add command -label "4 [get_element_name 1 3 4]" \
	-command {destroy $log_surf_ana ; set log_type 15; set log_channel 4; set log_entry  [get_element_name 1 3 4]; subs_name $log_entry }

   # add a Close button
   button $log_surf_ana.close -text Close -command {destroy $log_surf_ana}
	
   # Pack things up
   pack $log_surf_ana.b1 $log_surf_ana.b2 -side top
   pack $log_surf_ana.title2 -side top  -fill x -padx 3 -pady 3
   pack $log_surf_ana.b3 -side top
   pack $log_surf_ana.close -pady 2m  	
}


proc subs_name { entry_name } {

	#---------------------------------------#
	# Check the entry name (empty or not)	#
	#---------------------------------------#
	global log_entry log_type log_channel
	
	
	if {$entry_name == ""} {
		if {$log_type == 0} {
			set log_entry "Sub Analog $log_channel"		
		} elseif {$log_type == 1} {
		   	set log_entry "Surface Analog $log_channel"
		} elseif {$log_type == 15} {
			set log_entry "Surface Analog Out $log_channel"
		}
		
	}
	
}





proc add_log { type_ent } {

	global log log_type log_channel log_filename log_time log_interval
	  	
	#---------------------------------------#
	# Check the item existence		#
	#---------------------------------------#
	if { [check_log_db $type_ent ] > 0 } { return }	
	if { $log_type == -1 || $type_ent == "" } { return }	
	
	
	#---------------------------------------#
	# Display it to the db and listbox	#
	#---------------------------------------#
	
	
	add_log_item $type_ent $log_type $log_channel $log_filename $log_time $log_interval
	$log.lis57 insert end "$type_ent"
	
	#---------------------------------------#
	# Reset type and channel		#
	#---------------------------------------#
	
	set log_type -1
	set log_channel -1
	
	

}


proc delete_log  { type } {
 	
	global log log_type log_channel
	
	#---------------------------------------#
	# Get the index of line selected        #
	# ( start from "0" to "end" )		#
	#---------------------------------------#
	
	set selected [$log.lis57 curselection]     	
	if { $selected == "" } {set selected end}
	set deleted_item [$log.lis57 get $selected]
	
	
	#------------------------------#
	# Delete it from the listbox   #
	#------------------------------#
	
	#puts "---------- deleting ----------"
	#puts "log_type : $log_type"
	#puts "log_channel : $log_channel"
	
	if {$selected == "end"} {set selected [expr {[$log.lis57 index end] - 1} ] }
	
	
	delete_log_item $selected
	$log.lis57 delete $selected
	
	
}

proc check_log_name { } {
	global disk_status
	global log_filename log_time log_interval
	global env
	set env(DISPLAY) :0
	
	global check_log_name
	global disk_status_inhibit
	set disk_status_inhibit 0
	
	if { $log_filename != "" && $log_time != "" && $log_interval != ""} {	
		if { [file exists ../logging/$log_filename]  && $disk_status } {
			
			if [winfo exists $check_log_name] {destroy $check_log_name}	
			
			toplevel $check_log_name
			wm title $check_log_name "Warning Logging Name Conflict"
			label $check_log_name.lable -text "Logging Name Exists.Overwrite Existing File?"
			
			button $check_log_name.ok -text "Ok" \
			-command { set disk_status_inhibit 1; \
						update_log_variables $log_filename $log_time $log_interval; \
						set_disk_enable $disk_status; \
			destroy $check_log_name	} -width 5 -height 2
			
			button $check_log_name.cancel -text "Cancel" \
				-command {set disk_status_inhibit 1; set disk_status 0; destroy $check_log_name } \
				-width 5 -height 2	
			
			pack $check_log_name.lable -side top -pady 2m -padx 2m
			pack $check_log_name.ok -side left  -pady 2m -padx 2m
			pack $check_log_name.cancel -side left  -pady 2m -padx 2m	
			after 10000 set disk_status_inhibit 1
		
		} else {
   			# Call c++ function to update the new variables
    			update_log_variables $log_filename $log_time $log_interval
  			set_disk_enable $disk_status
  			set disk_status_inhibit 1
  			}
		}	

}



