#------------------------------------------------------------#
# Include source files that contain some of the procedure    #
# definition we are going to use in this file                #
#------------------------------------------------------------#

source combo2.tcl
source output2.tcl
source check_graph_type2.tcl
source submenu/surface_submenu2.tcl
source submenu/vehicle_submenu2.tcl

#-----------------------------------------------------#
# Initialize all the variables that are going to be   #
# sent down to C++ database                           #
#-----------------------------------------------------#



set surveh ""
set diganal ""
set elem_num ""
set elem_name ""
set calc "0:0:0:0"
set default1value 0
set default2value 0
set default3value 0
set default4value 0
set enable_channel 1
set enable_auto 1
set enable_graphic 1
set dig_value 1
set ana_value ""
set ana_raw ""
set offset ""
set gainvalue ""
set maxautocalibrate ""
set minautocalibrate ""
set graph_type ""
set color ""
set xpos2 0
set ypos2 0
set vga2 2
set max_auto_loop_var 0
set min_auto_loop_var 0
set io_elem_timer2 0
set selected ""



#-----------------------------------------------------------------------------#
# This procedure will popup a Big Popup Menu where the user can enter         #
# information and set or define their configuration file                      #
# It takes 3 parameter passed from the "vehicle" and "surface" submenu        #
# files when user click on the element                                        #
#                                                                             #
# "surf_vehicle" = 1 / 0 ----> Surface / Vehicle                              #
# "dig_ana" = 1 / 0 ---> Digital / Analog                                     #
# "elem_number" = #any_number ---> The element number                              #
#-----------------------------------------------------------------------------#

proc popup2 {surf_vehicle dig_ana elem_number } {

   global base2
   set base2 .bpm2

   # Create a new window to be poped up
   if [winfo exists $base2] {destroy $base2}

   global env
   set env(DISPLAY) :0.1
   toplevel $base2  -width 20c -height 16c
   wm title $base2 "Element Properties"

   focus $base2


   #--------------------------------------------------------------------------------#
   # Need to declare these two variables as global, otherwise when we               #
   # set them to different values it won't keep the new value and still             #
   # using the old values which is the value we set when we initialize them above   #
   #--------------------------------------------------------------------------------#
   global diganal
   global surveh
   global elem_num
   global elem_name
   global xpos2
   global ypos2
   global dig_value
   global ana_value
   global ana_raw
   global io_elem_timer2

   #-----------------------------------------------------#
   # copy the parameter to the global variable so that   #
   # it can be used by any procedure inside 'popup'      #
   #-----------------------------------------------------#
   set surveh $surf_vehicle
   set diganal $dig_ana
   set elem_num $elem_number

    ##############
    # Output     #
    ##############

    label $base2.output_label \
        -text Output
    listbox $base2.output_text \
        -background white -height 8



   #--------------------------------------------------------#
   # Now , get all the values 				    #
   # for this element from default C++ configuration files. #
   # This one call "ret_all_info" which returns a string    #         #
   # containing configuration values, and this procedure    #
   # will parse it  and will set the each of the            #
   # value to the global one so that we can use it          #
   # anywhere here                                          #
   #--------------------------------------------------------#
   my_ret_all_info2

   ##################
   # Element name   #
   ##################

   label $base2.elem_name_label \
        -text {Element Name}
   entry $base2.elem_name_entry -textvariable elem_name \
        -background white -width 213
   focus $base2.elem_name_entry

   place $base2.elem_name_label \
        -x 20 -y 15 -anchor nw -bordermode ignore
   place $base2.elem_name_entry \
        -x 125 -y 10 -width 213 -height 25 -anchor nw -bordermode ignore


    #############
    # Graphic   #
    #############

    label $base2.graph_info_label \
        -text "Graphic Information"
    frame $base2.graph_info \
        -borderwidth 2 -height 110 -relief groove -width 225
    label $base2.graph_info.type_label \
        -text Type
    label $base2.graph_info.xlabel \
        -text X(0-800)
    label $base2.graph_info.ylabel \
        -text Y(0-600)
    entry $base2.graph_info.xentry \
    	-bg white -width 13 -textvariable xpos2
    entry $base2.graph_info.yentry \
    	-bg white -width 13 -textvariable ypos2


    #----------------------------------------------------------
    # This will create a combobox for the graphic type
    # -> procedure defined in 'combo.tcl'
    #----------------------------------------------------------

    graphic_type_combobox2 $surveh $diganal $elem_num


    ############################
    # Place them in window     #
    ############################

    place $base2.graph_info \
        -x 20 -y 70 -width 225 -height 110 -anchor nw -bordermode ignore
    place $base2.graph_info_label \
        -x 20 -y 45 -anchor nw -bordermode ignore
    place $base2.graph_info.type_label \
        -x 10 -y 20 -anchor nw -bordermode ignore

    place $base2.graph_info.xlabel \
        -x 10 -y 45 -anchor nw -bordermode ignore
    place $base2.graph_info.ylabel \
        -x 10 -y 70 -anchor nw -bordermode ignore
    place $base2.graph_info.xentry \
        -x 80 -y 45
    place $base2.graph_info.yentry \
        -x 80 -y 70


    #############
    # Enable    #
    #############

    label $base2.enable_label \
        -height 0 -text Enable -width 50
    frame $base2.enable \
        -borderwidth 2 -height 75 -relief groove -width 195

    checkbutton $base2.enable.enable_channel \
         -text "Enable Channel" \
        -variable enable_channel

    checkbutton $base2.enable.enable_auto \
        -text "Enable Autocalibrate" \
        -variable enable_auto

    bind $base2.enable.enable_channel <ButtonRelease-1> {
    	global default1value default2value default3value default4value

    	# Pack the 4 default values to a single string
        set calc "$default1value:$default2value:$default3value:$default4value"

    	# Send Input to IO database -> this will update the element in database
    	send_io_elem_info $surveh $diganal $elem_num $elem_name $calc $default_type $enable_channel $enable_auto $enable_graphic $dig_value $ana_value $ana_raw $offset $gainvalue $maxautocalibrate $minautocalibrate $graph_type $color $xpos2 $ypos2 $vga2			
        # Set the digital value to be 0 if disabled
        enabling_channel $surveh $diganal $elem_num $enable_channel

        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output2]
					
    }
    bind $base2.enable.enable_auto <ButtonRelease-1> {
    	global default1value default2value default3value default4value

    	# Pack the 4 default values to a single string
        set calc "$default1value:$default2value:$default3value:$default4value"

    	# Send Input to IO database -> this will update the element in database
    	send_io_elem_info $surveh $diganal $elem_num $elem_name $calc $default_type $enable_channel $enable_auto $enable_graphic $dig_value $ana_value $ana_raw $offset $gainvalue $maxautocalibrate $minautocalibrate $graph_type $color $xpos2 $ypos2 $vga2			
        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output2]
			
    }



    if {$diganal != 0 } {
    	global enable_auto
    	set enable_auto 0
    	$base2.enable.enable_auto configure -state disabled
    }
	

    place $base2.enable \
        -x 20 -y 210 -width 225 -height 75 -anchor nw -bordermode ignore
    place $base2.enable.enable_channel \
        -x 20 -y 15 -anchor nw -bordermode ignore
    place $base2.enable.enable_auto \
        -x 20 -y 35 -anchor nw -bordermode ignore
    place $base2.enable_label \
        -x 20 -y 185 -width 50 -height 20 -anchor nw -bordermode ignore





     #####################
     # Autocalibrate Control   #
     #####################

     label $base2.ac_label \
        -text "Autocalibrate Setup"
     frame $base2.ac_frame \
        -borderwidth 2 -height 200 -relief groove -width 195
     label $base2.ac_frame.maxlabel \
        -text Max
     label $base2.ac_frame.minlabel \
        -text Min
     entry $base2.ac_frame.maxauto \
        -background white -textvariable maxautocalibrate -width 13
     entry $base2.ac_frame.minauto \
        -background white -textvariable minautocalibrate -width 13
     button $base2.ac_frame.auto_button \
        -command autocalibrate2 -height 0 -text Autocalibrate -width 0

     place $base2.ac_label \
        -x 255 -y 45 -anchor nw -bordermode ignore
     place $base2.ac_frame \
        -x 260 -y 70 -width 195 -height 110 -anchor nw -bordermode ignore
     place $base2.ac_frame.maxlabel \
        -x 12 -y 20 -anchor nw -bordermode ignore
     place $base2.ac_frame.minlabel \
        -x 12 -y 45 -anchor nw -bordermode ignore
     place $base2.ac_frame.maxauto \
        -x 70 -y 20  -anchor nw -bordermode ignore
     place $base2.ac_frame.minauto \
        -x 70 -y 45  -anchor nw -bordermode ignore
     place $base2.ac_frame.auto_button \
        -x 70 -y 75 -anchor nw -bordermode ignore

  ###########################
    # Analog Setup		          #
    ###########################


    label $base2.as_label \
        -text "Analog Setup"
    frame $base2.as_frame \
        -borderwidth 2 -relief groove -width 195 -height 75
    label $base2.as_frame.gainlabel \
        -text Gain
    label $base2.as_frame.offsetlabel \
        -text Offset
    entry $base2.as_frame.gainentry \
        -background white -textvariable gainvalue -width 13
    entry $base2.as_frame.offsetentry \
        -background white -textvariable offset -width 13

    bind $base2.as_frame.gainentry <Return> {
    	global default1value default2value default3value default4value

    	# Pack the 4 default values to a single string
        set calc "$default1value:$default2value:$default3value:$default4value"

    	# Send Input to IO database -> this will update the element in database
    	send_io_elem_info $surveh $diganal $elem_num $elem_name $calc $default_type $enable_channel $enable_auto $enable_graphic $dig_value $ana_value $ana_raw $offset $gainvalue $maxautocalibrate $minautocalibrate $graph_type $color $xpos2 $ypos2 $vga2			
        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output2]
			
    }

    bind $base2.as_frame.offsetentry <Return> {
    	global default1value default2value default3value default4value

    	# Pack the 4 default values to a single string
        set calc "$default1value:$default2value:$default3value:$default4value"

    	# Send Input to IO database -> this will update the element in database
        send_io_elem_info $surveh $diganal $elem_num $elem_name $calc $default_type $enable_channel $enable_auto $enable_graphic $dig_value $ana_value $ana_raw $offset $gainvalue $maxautocalibrate $minautocalibrate $graph_type $color $xpos2 $ypos2 $vga2			
        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output2]
	
    }

     place $base2.as_label \
        -x 255 -y 185 -anchor nw -bordermode ignore
     place $base2.as_frame \
        -x 260 -y 210  -anchor nw -bordermode ignore
     place $base2.as_frame.gainlabel \
        -x 12 -y 10 -anchor nw -bordermode ignore
     place $base2.as_frame.offsetlabel \
        -x 12 -y 35 -anchor nw -bordermode ignore
     place $base2.as_frame.gainentry \
        -x 70 -y 10  -anchor nw -bordermode ignore
     place $base2.as_frame.offsetentry\
        -x 70 -y 35  -anchor nw -bordermode ignore







    ################
    # Value        #
    ################

    label $base2.value_label \
        -text Value
    frame $base2.value\
        -borderwidth 2 -height 130 -relief groove -width 225

    place $base2.value\
        -x 20 -y 315 -width 225 -height 130 -anchor nw -bordermode ignore
    place $base2.value_label \
        -x 20 -y 290 -anchor nw -bordermode ignore

    ##########
    # Analog #
    ##########

    entry $base2.value.analog_entry -state disabled \
        -background white -textvariable ana_value -width 108
    entry $base2.value.analog_raw_entry -state disabled \
        -background white -textvariable ana_raw -width 108

    label $base2.value.analog_label \
        -text Analog -width 60
    label $base2.value.ana_raw_label \
        -text {Analog Raw} -width 91

    place $base2.value.analog_entry \
        -x 105 -y 45 -width 108 -height 22 -anchor nw -bordermode ignore
    place $base2.value.analog_raw_entry \
        -x 105 -y 70 -width 108 -height 22 -anchor nw -bordermode ignore

    place $base2.value.analog_label \
        -x 10 -y 45 -width 60 -height 20 -anchor nw -bordermode ignore
    place $base2.value.ana_raw_label \
        -x 10 -y 70 -width 91 -height 20 -anchor nw -bordermode ignore

    ###########
    # Digital #
    ###########

    checkbutton $base2.value.digital -text "Digital Value"  -variable dig_value
    place $base2.value.digital -x 10 -y 10 -anchor nw -bordermode ignore


    ##############
    # DEFAULT    #
    ##############

    label $base2.default_label \
        -text Default
    place $base2.default_label \
        -x 255 -y 290 -anchor nw -bordermode ignore
    frame $base2.default_frame \
        -borderwidth 2 -height 130 -relief groove -width 225
    place $base2.default_frame\
        -x 260 -y 315 -width 195 -height 130 -anchor nw -bordermode ignore

    label $base2.default_frame.default1label \
        -text "Power Up"
    label $base2.default_frame.default2label \
        -text "On Deck"
    label $base2.default_frame.default3label \
        -text "10-100 M"
    label $base2.default_frame.default4label \
        -text ">100 M"

    entry $base2.default_frame.default1 \
        -background white -textvariable default1value
    entry $base2.default_frame.default2 \
        -background white -textvariable default2value
    entry $base2.default_frame.default3 \
        -background white -textvariable default3value
    entry $base2.default_frame.default4 \
        -background white -textvariable default4value

    place $base2.default_frame.default1label \
        -x 5 -y 20 -width 80  -anchor nw -bordermode ignore
    place $base2.default_frame.default2label \
        -x 5 -y 45 -width 80  -anchor nw -bordermode ignore
    place $base2.default_frame.default3label \
        -x 5 -y 70 -width 80  -anchor nw -bordermode ignore
    place $base2.default_frame.default4label \
        -x 5 -y 95 -width 80  -anchor nw -bordermode ignore

    place $base2.default_frame.default1 \
        -x 80 -y 20 -width 90  -anchor nw -bordermode ignore
    place $base2.default_frame.default2 \
        -x 80 -y 45 -width 90  -anchor nw -bordermode ignore
    place $base2.default_frame.default3 \
        -x 80 -y 70 -width 90  -anchor nw -bordermode ignore
    place $base2.default_frame.default4 \
        -x 80 -y 95 -width 90 -anchor nw -bordermode ignore







    #---------------------------------------------------------
    # When we click on the output, we bind it to a variable, so that when we click on
    # 'delete' button, it will remove the correct selected line
    #---------------------------------------------------------

    global selected
    bind $base2.output_text <ButtonRelease-1> {
    	set selected  [.bpm2.output_text curselection]
    	puts "$selected"
    	
    }

    #-----------------------------------------------------------------------------




    #--------------------------------------------------------------#
    # Create "Add" and "Delete" buttons                            #
    # The command "add_output2" and delete_output2 are called from   #
    # "output2.tcl"                                                 #
    #--------------------------------------------------------------#

    button $base2.output_add  -text Add -command {add_output2 $surveh}
    button $base2.output_delete -text Delete \
    	-command {
    			global selected
    			if {$selected == ""} {set selected end}
    			delete_output2 $selected
    		 }


    place $base2.output_label \
        -x 470 -y 45 -anchor nw -bordermode ignore
    place $base2.output_text \
        -x 470 -y 70 -width 100  -anchor nw -bordermode ignore
    place $base2.output_add \
        -x 470 -y 215 -width 50
    place $base2.output_delete \
         -x 520 -y 215  -width 50


###############################################################################
    	

     ####################
     # OK Button        #
     ####################

     button $base2.ok\
        -height 0 -text "OK" -width 6 \
        -command { 	set io_elem_timer2  0
        			do_ok_command2
        		}

		
     place $base2.ok \
        -x 470 -y 380 -anchor nw -bordermode ignore -width 100


     ####################
     # Cancel Button    #
     ####################

      button $base2.cancel \
         -command { set io_elem_timer2  0
         	 destroy .bpm2 ; destroy .add_output2} -text "Cancel" -width 100

      place $base2.cancel \
         -x 470 -y 415 -anchor nw -bordermode ignore -width 100


     #---------------------------------------------------------------
    # Bind the motion of the mouse to display the mouse
    # coordinate at current time
    #---------------------------------------------------------------

    bind .ventana2 <ButtonRelease-1> {
    	global xpos2 ypos2
    	#puts "%x %y"
     	set xpos2 %x
     	set ypos2 %y
    }	



    # Loop to update the analog and digital value
    set io_elem_timer2 1
    loop_ret_io_elem_value2




# end of procedure "popup"
}



#-----------------------------------------------------------------------------


proc loop_ret_io_elem_value2 { } {

	global base2 surveh diganal elem_num 	
	global io_elem_timer2 dig_value ana_value ana_raw
 	global enable_channel
	
 	if {![winfo exists $base2] || $io_elem_timer2 == 0 } { return }

 	#------------------------------------------------------------------#
      	# Update the channel enable                                        #
      	#------------------------------------------------------------------#
      	set all_values [ret_all_info $surveh $diganal $elem_num 1]
  	set enable_channel [lindex [split $all_values | ] 6]
      	
    if {$enable_channel == 1} {

	#puts "elem_prop2.tcl : Updating digital /analog value"
 	
        # "ret_io_elem_info"
      	set values [ret_io_elem_value $surveh $diganal $elem_num]

      	# Now parse the string "values" and set it to "values_list"
     	set values_list [ split $values | ]
 	
 	set dig_value [lindex $values_list 0]
   	set ana_value [lindex $values_list 1]
      	set ana_raw [lindex $values_list 2]

    } else {
        #puts "elem_prop2.tcl : enable_channel is off"
 	set dig_value 0
   	
    }
      		
    after 500 [list  loop_ret_io_elem_value2 ]
	

}






#-----------------------------------------------------------------------------


   ###############################################################################

   proc my_ret_io_elem_value2 { } {

      global surveh
      global diganal
      global elem_num

      global dig_value
      global ana_value
      global ana_raw

      global gainvalue offset maxautocalibrate minautocalibrate
  	
      # set variable "values" to a string returned from C++ function
      # "ret_io_elem_info"
      set values [ret_io_elem_value $surveh $diganal $elem_num]

      # Now parse the string "values" and set it to "values_list"
      set values_list [ split $values | ]

      # Use the variable 'temp' to get the first value passed and check if it is an
      # VALID value or not ( Invalid = -1 )

      	set dig_value [lindex $values_list 0]
   	set ana_value [lindex $values_list 1]
      	set ana_raw [lindex $values_list 2]
	set gainvalue [lindex $values_list 3]
	set offset [lindex $values_list 4]
	set maxautocalibrate [lindex $values_list 5]
	set minautocalibrate [lindex $values_list 6]
 


   # end of procedure "my_ret_io_elem_value2"
   }


#-----------------------------------------------------------------------------


proc my_ret_all_info2 { } {

      # get the value returned for comparison with the old values
      global surveh_ret
      global diganal_ret
      global elem_num_ret

      global surveh
      global diganal
      global elem_num
      global elem_name
      global calc
      global default_type
      global enable_channel
      global enable_auto
      global enable_graphic
      global dig_value
      global ana_value
      global ana_raw
      global offset
      global gainvalue
      global maxautocalibrate
      global minautocalibrate
      global graph_type
      global color
      global xpos2
      global ypos2
      global vga2
      global default1value default2value default3value default4value



      # set the string returned to be "all_info2"
      # ret_all_info $surveh $diganal $elem_num $vga2
      set all_info2 [ret_all_info $surveh $diganal $elem_num 2]


      # now parse it and set it to "all_info_list"
      set all_info_list [ split $all_info2 | ]

      # Set a checking variable "temp"
      set temp [lindex $all_info_list 0]

      if { $temp == -1 } {
	
      	set elem_name ""
      	
      	set default_type 0
      	set enable_channel 1
      	set enable_auto 1
     	set enable_graphic 1
      	set dig_value ""
      	set ana_value ""
      	set ana_raw ""
      	set offset 0
      	set gainvalue 0
      	set maxautocalibrate 0
      	set minautocalibrate 0
      	set graph_type None
      	set color ""
      	set xpos2 0
      	set ypos2 0
      	set vga2 2
      	puts "Element does not exist, set everything to 0 or Null "

    	# Pack the 4 default values to a single string
        set calc "0:0:0:0"

        send_io_elem_info $surveh $diganal $elem_num $elem_name \
                          $calc $default_type $enable_channel \
                          $enable_auto $enable_graphic $dig_value \
                          $ana_value $ana_raw $offset $gainvalue \
                          $maxautocalibrate $minautocalibrate $graph_type $color \
                          $xpos2 $ypos2 2

        my_ret_io_elem_value2

      } else {

      #-------------------------------------------------------------------
      # Now use the temporary variable to keep the value returned for
      # surface/vehicle, digital/analog, and element number
      #-------------------------------------------------------------------

      	      	
      		# Now set it to global variables available
      		set surveh [ lindex $all_info_list 0 ]
      		set diganal [ lindex $all_info_list 1 ]
      		set elem_num [ lindex $all_info_list 2 ]
      		set elem_name [ lindex $all_info_list 3 ]
		
      		set calc [ lindex $all_info_list 4 ]
      		#puts "elem_prop.tcl : calc here : $calc"
      	
      		
      		set def [split $calc :]
      		set default1value [lindex $def 0]
      		set default2value [lindex $def 1]
      		set default3value [lindex $def 2]
      		set default4value [lindex $def 3]
      		
      		if {$default1value == "" || $default2value == "" || $default3value == "" || $default4value == ""  } {
      			set default1value 0
      			set default2value 0
      			set default3value 0
      			set default4value 0
      		
      		}
      			
      		set default_type [ lindex $all_info_list 5 ]
      		set enable_channel [ lindex $all_info_list 6 ]
      		set enable_auto [ lindex $all_info_list 7 ]
     		set enable_graphic [ lindex $all_info_list 8 ]
      		set dig_value [ lindex $all_info_list 9 ]
      		set ana_value [ lindex $all_info_list 10 ]
      		set ana_raw [ lindex $all_info_list 11 ]
      		set offset [ lindex $all_info_list 12 ]
      		set gainvalue [ lindex $all_info_list 13 ]
      		set maxautocalibrate [ lindex $all_info_list 14 ]
      		set minautocalibrate  [ lindex $all_info_list 15 ]
      		set graph_type  [ lindex $all_info_list 16 ]
      		set color [ lindex $all_info_list 17 ]
      		set xpos2 [ lindex $all_info_list 18 ]
      		set ypos2 [ lindex $all_info_list 19 ]
      		set vga2 2
        	
      		# we have to get the output nodes from db and display it back in GUI
      		display_output_in_listbox2

	        	my_ret_io_elem_value2
		
        # end of else
        }



# end of procedure "my_ret_all_info2"
}

proc display_output_in_listbox2 { } {
	
        global surveh
        global diganal
        global elem_num

        set all_output_info2 [display_output $surveh $diganal $elem_num]
        set all_output2 [split $all_output_info2 |] 		
	#puts "all output-----------$all_output_info2"
			
	if {$surveh == 1} {
	      	  	  		
		set temp_count [llength $all_output2]
		set count [incr temp_count -1]
		
		for {set i 0 } {$i <= $count } {incr i 2} {
			
			# Dig Gesout
			if { [lindex $all_output2 $i] == 0 } {
				set e_num [lindex $all_output2 [expr {$i + 1}]]
				.bpm2.output_text insert end "Dig Gesout $e_num"
			
			}
			
			# Dig MPL
			if { [lindex $all_output2 $i] == 1 } {
				set e_num [lindex $all_output2 [expr {$i + 1}]]
				.bpm2.output_text insert end "Dig MPL $e_num"
			
			} 	
		
			# Ana Gesdac
			if { [lindex $all_output2 $i] == 2 } {
				set e_num [lindex $all_output2 [expr {$i + 1}]]
				.bpm2.output_text insert end "Ana Gesdac $e_num"
			
			}
			
		# end for
		}	 	
		
	    	
	} else {
	
		# Analog Output
		if { [lindex $all_output2 0] == 3 } {
			set e_num [lindex $all_output2 1]
			.bpm2.output_text insert end "[get_element_name 1 3 $e_num]"
			
		}	
		
	}
	
}


proc do_ok_command2 { } {

	global surveh diganal elem_num elem_name calc default_type enable_channel enable_auto enable_graphic dig_value ana_value ana_raw offset gainvalue maxautocalibrate minautocalibrate graph_type color xpos2 ypos2 vga2			
      	global default1value default2value default3value default4value
  				

      	# Ask the user to enter element name before adding to database      	
      	if {$elem_name == "" } {
				 	
		# Element name can't be empty
		set status [tk_dialog  .warning { } {Please insert name in element name field. } warning 0 {Close } ]
       		switch $status {0 {puts "Close"; return} }             	
             	               							
	}
	
	
	
				
			# If from SUB, send alarm info (diganal, elem_num, name) to Alarm database
			if {$surveh == 0} {
				
				# This will be used in "alarm.tcl" to popup the alarm
				#set_alarm_name $elem_name
				
				# Add it to alarm database
				add_alarm_to_db $diganal $elem_num $elem_name
			}
			
        		
			
		    	# Pack the 4 default values to a single string
		        set calc "$default1value:$default2value:$default3value:$default4value"

        		# Send Input to IO database -> this will update the element in database
        		# And also send down the value for gain and offset to the sub
        		send_io_elem_info $surveh $diganal $elem_num $elem_name $calc $default_type $enable_channel $enable_auto $enable_graphic $dig_value $ana_value $ana_raw $offset $gainvalue $maxautocalibrate $minautocalibrate $graph_type $color $xpos2 $ypos2 $vga2			
        		
        		# Send the Output list to IO database
        		send_out_list_info $surveh $diganal $elem_num [get_output2]
			
        		# Check the graph type to be displayed			
        		check_graph_type2 $surveh $diganal $graph_type $xpos2 $ypos2 $elem_name $elem_num
			
        		# Display the new name to the submenu list
        		display_new_name2
        		
			destroy .bpm2
			destroy .add_output2	
			
}

#----------------------------------------------------------#
# This function will update the element name on the pull down menu         		#
# coming from :                                                                                               	#
#                                                                                                                   	#
# 	surface -> analog in -> ...                                                            	#
# 	surface -> digital in  -> ...                                                         	#
# 	surface -> analog out -> ...                                                           	#
# 	vehicle ->  digital in -> ...                                                             	#
# 	vehicle ->  analog in -> ...                                                          	#
#                                                                                                            	#
# Note : Update needs to be done for BOTH screen                                      	#
#                                                                                                            	#
#----------------------------------------------------------#

proc display_new_name2 { } {

        global surveh diganal elem_num elem_name

        if ![winfo exists .ventana] {
             	puts "elem_prop2.tcl : Can Not Find First Screen "
             	return;
        }

        # Surface Digital
        if {$surveh == 1 && $diganal == 1} {

        	#------------#
        	# Screen 1	#
             #------------#

        	set win .ventana.bar.maintenance.menu.element.surface.digin
        	
        	if { $elem_num <= 16 } {
        		$win.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win.part2 entryconfigure [expr {$elem_num - 16}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 32 && $elem_num <= 48 } {
        		$win.part3 entryconfigure [expr {$elem_num - 32}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 48 && $elem_num <= 64 } {
        		$win.part4 entryconfigure [expr {$elem_num - 48}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 64 && $elem_num <= 80 } {
        		$win.part5 entryconfigure [expr {$elem_num - 64}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 80 && $elem_num <= 96 } {
        		$win.part6 entryconfigure [expr {$elem_num - 80 }] -label "$elem_num $elem_name"
        	}	


             #------------#
        	# Screen 2	#
             #------------#
        	
        	set win2 .ventana2.bar.maintenance.menu.element.surface.digin
        	
        	if { $elem_num <= 16 } {
        		$win2.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win2.part2 entryconfigure [expr {$elem_num - 16}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 32 && $elem_num <= 48 } {
        		$win2.part3 entryconfigure [expr {$elem_num - 32}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 48 && $elem_num <= 64 } {
        		$win2.part4 entryconfigure [expr {$elem_num - 48}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 64 && $elem_num <= 80 } {
        		$win2.part5 entryconfigure [expr {$elem_num - 64}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 80 && $elem_num <= 96 } {
        		$win2.part6 entryconfigure [expr {$elem_num - 80}] -label "$elem_num $elem_name"
        	}	
        	                              	
        }
 	
        # Surface Analog
        if {$surveh == 1 && $diganal == 0} {


        	#------------#
        	# Screen 1	#
             #------------#
        	
        	set win .ventana.bar.maintenance.menu.element.surface.anain
        	if { $elem_num <= 16 } {
        		$win.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win.part2 entryconfigure [expr {$elem_num - 16} ] -label "$elem_num $elem_name"
        	}
        	
        	if { $elem_num > 32 && $elem_num <= 48 } {
        		$win.part3 entryconfigure [expr {$elem_num - 32} ] -label "$elem_num $elem_name"
        	}
        	
        	if { $elem_num > 48 && $elem_num <= 64 } {
        		$win.part4 entryconfigure [expr {$elem_num - 48} ] -label "$elem_num $elem_name"
        	}
        	
        	
        	#------------#
        	# Screen 2	#
             #------------#
        	
        	set win2 .ventana2.bar.maintenance.menu.element.surface.anain
        	
        	if { $elem_num <= 16 } {
        		$win2.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win2.part2 entryconfigure [expr {$elem_num - 16} ] -label "$elem_num $elem_name"
        	}	
        	
        	if { $elem_num > 32 && $elem_num <= 48 } {
        		$win2.part3 entryconfigure [expr {$elem_num - 32} ] -label "$elem_num $elem_name"
        	}
        	
        	if { $elem_num > 48 && $elem_num <= 64 } {
        		$win2.part4 entryconfigure [expr {$elem_num - 48} ] -label "$elem_num $elem_name"
        	}
        }

        # Vehicle Digital
        if {$surveh == 0 && $diganal == 1} {
        	
        	
        	#------------#
        	# Screen 1	#
             #------------#
        	
        	set win .ventana.bar.maintenance.menu.element.vehicle.digin
        	if { $elem_num <= 16 } {
        		$win.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win.part2 entryconfigure [expr {$elem_num - 16} ] -label "$elem_num $elem_name"
        	}	
        	
        	
        	#------------#
        	# Screen 1	#
             #------------#
        	
        	set win2 .ventana2.bar.maintenance.menu.element.vehicle.digin
        	if { $elem_num <= 16 } {
        		$win2.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win2.part2 entryconfigure [expr {$elem_num -16}] -label "$elem_num $elem_name"
        	}	
        }
	
        # Vehicle Analog
        if {$surveh == 0 && $diganal == 0} {


             #------------#
        	# Screen 1	#
             #------------#
        	
        	set win .ventana.bar.maintenance.menu.element.vehicle.anain
        	if { $elem_num <= 16 } {
        		$win.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win.part2 entryconfigure [expr { $elem_num - 16}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 32 && $elem_num <= 48 } {
        		$win.part3 entryconfigure [expr {$elem_num - 32}] -label "$elem_num $elem_name"
        	}	
        	
        	#------------#
        	# Screen 2	#
             #------------#
        	
        	set win2 .ventana2.bar.maintenance.menu.element.vehicle.anain
        	if { $elem_num <= 16 } {
        		$win2.part1 entryconfigure $elem_num -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 16 && $elem_num <= 32 } {
        		$win2.part2 entryconfigure [expr {$elem_num - 16}] -label "$elem_num $elem_name"
        	}	
        	if { $elem_num > 32 && $elem_num <= 48 } {
        		$win2.part3 entryconfigure [expr {$elem_num - 32}] -label "$elem_num $elem_name"
        	}	
        	                      	
        }

}

#------------------------ end of display_submenu proc ----------------------------#


proc autocalibrate2 { } {
	global surveh diganal elem_num maxautocalibrate minautocalibrate max_auto_loop_var
	global enable_auto

	set max_auto_loop_var 1
	
	# Type has to be surface analog
	if {$diganal != 0 } {
		puts "elem_prop2.tcl : It is not the appropriate type"
		return
	}
	
	# Enable autocalibrate button has to be enabled
	if {$enable_auto == 0} {
		return
	
	}
	
	
	button .bpm2.auto -text  "Please Move The Analog Raw To MAXIMUM. Press Here When Finished. " \
 		-wraplength 100 -bg red -fg white -command {set max_auto_loop_var 0}
 	place .bpm2.auto -x 200 -y 200
 	#grab .bpm2.auto
 	
	getting_max_auto_loop2
 	
}
 	
proc getting_max_auto_loop2 { } {

	global surveh diganal elem_num maxautocalibrate
	global max_auto_loop_var  min_auto_loop_var
	
	if {![winfo exists .bpm2.auto] || $max_auto_loop_var == 0 } {
		
		# Getting ready to display the Button that asks user to get min value
		set min_auto_loop_var 1
		
		button .bpm2.automin -text  "Please Move The Analog Raw To MINIMUM. Press Here When Finished. " \
 			-wraplength 100 -bg blue -fg white \
 			-command {	set min_auto_loop_var 0
 					set values [split [ret_io_elem_value $surveh $diganal $elem_num] | ]
 					set gainvalue [lindex $values 3]
 					set offset [lindex $values 4] }
 		place .bpm2.automin -x 200 -y 200
 	             #grab .bpm2.automin
 	
		getting_min_auto_loop2
		
		destroy .bpm2.auto
		return
	} 	

 	set max_auto_value [ auto_calibrate_max $surveh $diganal $elem_num $maxautocalibrate ]
 	#set max_auto_value 1
 	
 	if {  $max_auto_value == -1 } {
 		puts "elem_prop2.tcl : Error in reading maximum autocalibrate value" 	
 	}
 	
 	if {  $max_auto_value == -2 } {
 	
 		set status [tk_dialog  .elem_warning2 { } {The Autocalibrate is not enabled.} warning 0 {Close } ]
       		switch $status {   0 { return } }
 	
 	}
 	
 	puts "elem_prop2.tcl: Waiting for MAX value........."		
            after 250 [list getting_max_auto_loop2]
}

proc getting_min_auto_loop2 { } {

	global surveh diganal elem_num minautocalibrate
	global min_auto_loop_var
	
         	if {![winfo exists .bpm2.automin] || $min_auto_loop_var == 0 } {
		destroy .bpm2.automin
		return
	}
	
	
 	set min_auto_value [ auto_calibrate_min $surveh $diganal $elem_num $minautocalibrate ]
 	#set min_auto_value 1
 	
 	
 	if {  $min_auto_value == -1 } {
 		puts "elem_prop2.tcl : Error in reading minimum autocalibrate value" 	
 	}
 	
 	if {  $min_auto_value == -2 } {
 	
 		set status [tk_dialog  .elem_warning2 { } {The Autocalibrate is not enabled.} warning 0 {Close } ]
       		switch $status {   0 { return } }
 	
 	}
 	
  	puts "elem_prop2.tcl: Waiting for MIN value........."
            	after 250 [list getting_min_auto_loop2]	
}
