#------------------------------------------------------------#
# Include source files that contain some of the procedure    #
# definition we are going to use in this file                #
#------------------------------------------------------------#

source combo.tcl
source output.tcl
source alarm.tcl
source check_graph_type.tcl
source submenu/surface_submenu.tcl
source submenu/vehicle_submenu.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 xpos 0
set ypos 0
set vga 1

set max_auto_loop_var 0
set min_auto_loop_var 0
set  io_elem_timer 0
set base .bpm

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 popup {surf_vehicle dig_ana elem_number } {

   global base

   # Create a new window to be poped up

   if [winfo exists $base] {destroy $base}


   global env
   set env(DISPLAY) :0
   toplevel $base  -width 20c -height 16c
   wm title $base "Element Properties"
   focus $base



   #--------------------------------------------------------------------------------#
   # Need to declare these 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 xpos
   global ypos
   global dig_value
   global ana_value
   global ana_raw
   global io_elem_timer


   #-----------------------------------------------------#
   # 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 $base.output_label \
        -text Output
    listbox $base.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_info


   ##################
   # Element name   #
   ##################

   label $base.elem_name_label \
        -text {Element Name}
   entry $base.elem_name_entry -textvariable elem_name \
        -background white -width 213
   focus $base.elem_name_entry

   # clean up the element name entry field
  # $base.elem_name_entry delete 0 100

   place $base.elem_name_label \
        -x 20 -y 15 -anchor nw -bordermode ignore
   place $base.elem_name_entry \
        -x 125 -y 10 -width 213 -height 25 -anchor nw -bordermode ignore


    #############
    # Graphic   #
    #############

    label $base.graph_info_label \
        -text "Graphic Information"
    frame $base.graph_info \
        -borderwidth 2 -height 110 -relief groove -width 225
    label $base.graph_info.type_label \
        -text Type
    label $base.graph_info.xlabel \
    	-text X(0-800)
    label $base.graph_info.ylabel \
    	-text Y(0-600)

    entry  $base.graph_info.xentry \
    	-bg white -width 13 -textvariable xpos
    entry  $base.graph_info.yentry \
    	-bg white -width 13 -textvariable ypos


    #----------------------------------------------------------
    # This will create a combobox for the graphic type
    # -> procedure defined in 'combo.tcl'
    #----------------------------------------------------------

    graphic_type_combobox $surveh $diganal $elem_num

    ############################
    # Place them in window     #
    ############################

    place $base.graph_info \
        -x 20 -y 70 -width 225 -height 110 -anchor nw -bordermode ignore
    place $base.graph_info_label \
        -x 20 -y 45 -anchor nw -bordermode ignore  
    place $base.graph_info.type_label \
        -x 10 -y 20 -anchor nw -bordermode ignore
    

    place $base.graph_info.xlabel \
    	-x 10 -y 45  -anchor nw -bordermode ignore
    place $base.graph_info.ylabel \
    	-x 10 -y 70 -anchor nw -bordermode ignore	
    place  $base.graph_info.xentry \
    	-x 80 -y 45
    place  $base.graph_info.yentry \
    	-x 80 -y 70


###############################################################################   
    #############
    # Enable    #
    #############

    label $base.enable_label \
        -height 0 -text Enable -width 50
    frame $base.enable \
        -borderwidth 2 -height 75 -relief groove -width 195
    
    checkbutton $base.enable.enable_channel \
         -text "Enable Channel" \
        -variable enable_channel

    bind $base.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 $xpos $ypos $vga			
        # 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_output]
			
    }

    checkbutton $base.enable.enable_auto \
        -text "Enable Autocalibrate" \
        -variable enable_auto

    bind $base.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 $xpos $ypos $vga			
        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output]
			
    }



    if {$diganal != 0 } {
    	global enable_auto
    	set enable_auto 0
    	$base.enable.enable_auto configure -state disabled
    }
	
    	

	
    place $base.enable \
        -x 20 -y 210 -width 225 -height 75 -anchor nw -bordermode ignore
    place $base.enable.enable_channel \
        -x 20 -y 15 -anchor nw -bordermode ignore
    place $base.enable.enable_auto \
        -x 20 -y 35 -anchor nw -bordermode ignore
    place $base.enable_label \
        -x 20 -y 185 -width 50 -height 20 -anchor nw -bordermode ignore


     #####################
     # Autocalibrate Control   #
     #####################

     label $base.ac_label \
        -text "Autocalibrate Setup"
     frame $base.ac_frame \
        -borderwidth 2 -height 200 -relief groove -width 195
     label $base.ac_frame.maxlabel \
        -text Max
     label $base.ac_frame.minlabel \
        -text Min
     entry $base.ac_frame.maxauto \
        -background white -textvariable maxautocalibrate -width 13
     entry $base.ac_frame.minauto \
        -background white -textvariable minautocalibrate -width 13
     button $base.ac_frame.auto_button \
        -command autocalibrate -height 0 -text Autocalibrate -width 0

     place $base.ac_label \
        -x 255 -y 45 -anchor nw -bordermode ignore
     place $base.ac_frame \
        -x 260 -y 70 -width 195 -height 110 -anchor nw -bordermode ignore
     place $base.ac_frame.maxlabel \
        -x 12 -y 20 -anchor nw -bordermode ignore
     place $base.ac_frame.minlabel \
        -x 12 -y 45 -anchor nw -bordermode ignore
     place $base.ac_frame.maxauto \
        -x 70 -y 20  -anchor nw -bordermode ignore
     place $base.ac_frame.minauto \
        -x 70 -y 45  -anchor nw -bordermode ignore
     place $base.ac_frame.auto_button \
        -x 70 -y 75 -anchor nw -bordermode ignore

  ###########################
    # Analog Setup		          #
    ###########################


    label $base.as_label \
        -text "Analog Setup"
    frame $base.as_frame \
        -borderwidth 2 -relief groove -width 195 -height 75
    label $base.as_frame.gainlabel \
        -text Gain
    label $base.as_frame.offsetlabel \
        -text Offset
    entry $base.as_frame.gainentry \
        -background white -textvariable gainvalue -width 13
    entry $base.as_frame.offsetentry \
        -background white -textvariable offset -width 13

    bind $base.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 $xpos $ypos $vga			
        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output]
	
    }

    bind $base.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 $xpos $ypos $vga			
        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output]
	
    }

     place $base.as_label \
        -x 255 -y 185 -anchor nw -bordermode ignore
     place $base.as_frame \
        -x 260 -y 210  -anchor nw -bordermode ignore
     place $base.as_frame.gainlabel \
        -x 12 -y 10 -anchor nw -bordermode ignore
     place $base.as_frame.offsetlabel \
        -x 12 -y 35 -anchor nw -bordermode ignore
     place $base.as_frame.gainentry \
        -x 70 -y 10  -anchor nw -bordermode ignore
     place $base.as_frame.offsetentry\
        -x 70 -y 35  -anchor nw -bordermode ignore



    ################
    # Value        #
    ################

    label $base.value_label \
        -text Value
    frame $base.value\
        -borderwidth 2 -height 130 -relief groove -width 225

    place $base.value\
        -x 20 -y 315 -width 225 -height 130 -anchor nw -bordermode ignore
    place $base.value_label \
        -x 20 -y 290 -anchor nw -bordermode ignore

    ##########
    # Analog #
    ##########

    entry $base.value.analog_entry -state disabled \
        -background white -textvariable ana_value -width 108
    entry $base.value.analog_raw_entry -state disabled \
        -background white -textvariable ana_raw -width 108

    label $base.value.analog_label \
        -text Analog -width 60
    label $base.value.ana_raw_label \
        -text {Analog Raw} -width 91


    place $base.value.analog_entry \
        -x 105 -y 45 -width 108 -height 22 -anchor nw -bordermode ignore
    place $base.value.analog_raw_entry \
        -x 105 -y 70 -width 108 -height 22 -anchor nw -bordermode ignore

    place $base.value.analog_label \
        -x 10 -y 45 -width 60 -height 20 -anchor nw -bordermode ignore
    place $base.value.ana_raw_label \
        -x 10 -y 70 -width 91 -height 20 -anchor nw -bordermode ignore

    ###########
    # Digital #
    ###########

    checkbutton $base.value.digital -text "Digital Value"  -variable dig_value
    place $base.value.digital -x 10 -y 10 -anchor nw -bordermode ignore



    ##############
    # DEFAULT    #
    ##############

    label $base.default_label \
        -height 0 -text Default -width 0
    place $base.default_label \
        -x 255 -y 290 -anchor nw -bordermode ignore 
    frame $base.default_frame \
        -borderwidth 2 -height 130 -relief groove -width 225
    place $base.default_frame\
        -x 260 -y 315 -width 195 -height 130 -anchor nw -bordermode ignore

    label $base.default_frame.default1label \
        -text "Power Up"
    label $base.default_frame.default2label \
        -text "On Deck"
    label $base.default_frame.default3label \
        -text "10-100 M"
    label $base.default_frame.default4label \
        -text ">100 M"

    entry $base.default_frame.default1 \
        -background white -textvariable default1value
    entry $base.default_frame.default2 \
        -background white -textvariable default2value
    entry $base.default_frame.default3 \
        -background white -textvariable default3value
    entry $base.default_frame.default4 \
        -background white -textvariable default4value
 

    place $base.default_frame.default1label \
        -x 5 -y 20 -width 80 -anchor nw -bordermode ignore
    place $base.default_frame.default2label \
        -x 5 -y 45 -width 80 -anchor nw -bordermode ignore
    place $base.default_frame.default3label \
        -x 5 -y 70 -width 80  -anchor nw -bordermode ignore
    place $base.default_frame.default4label \
        -x 5 -y 95 -width 80  -anchor nw -bordermode ignore

    place $base.default_frame.default1 \
        -x 80 -y 20 -width 90  -anchor nw -bordermode ignore
    place $base.default_frame.default2 \
        -x 80 -y 45 -width 90  -anchor nw -bordermode ignore
    place $base.default_frame.default3 \
        -x 80 -y 70 -width 90 -anchor nw -bordermode ignore
    place $base.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 $base.output_text <ButtonRelease-1> {
    	set selected  [.bpm.output_text curselection]
    	puts "$selected"
    	
    }




    #--------------------------------------------------------------#
    # Create "Add" and "Delete" buttons                            #
    # The command "add_output" and delete_output are called from   #
    # "output.tcl"                                                 #
    #--------------------------------------------------------------#

    button $base.output_add  -text Add -command {add_output $surveh}
    button $base.output_delete -text Delete \
    	-command {
    			global selected
    			if {$selected == ""} { set selected end }
    			delete_output $selected
    		 }

    place $base.output_label \
        -x 470 -y 45 -anchor nw -bordermode ignore
    place $base.output_text \
        -x 470 -y 70 -width 100  -anchor nw -bordermode ignore
    place $base.output_add \
        -x 470 -y 215 -width 50
    place $base.output_delete \
         -x 520 -y 215  -width 50


###############################################################################
	

	
     ####################
     # OK Button        #
     ####################

     button $base.ok\
        -height 0 -text "OK"  \
        -command {	set io_elem_timer  0
 			do_ok_command     }

		
     place $base.ok \
        -x 470 -y 380 -anchor nw -bordermode ignore  -width 100



     ####################
     # Cancel Button    #
     ####################

      button $base.cancel \
         -command {set io_elem_timer 0
		destroy .bpm ; destroy .add_output} -text "Cancel" -width 0

      place $base.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 .ventana <ButtonRelease-1> {
    	global xpos ypos
    	#puts "%x %y"
     	set xpos %x
     	set ypos %y
    }	

    # Loop to update the analog and digital value
    set io_elem_timer 1
    loop_ret_io_elem_value

# end of procedure "popup"
}



#-----------------------------------------------------------------------------


proc loop_ret_io_elem_value { } {

	global base surveh diganal elem_num	
	global io_elem_timer dig_value ana_value ana_raw
 	global enable_channel
	
 	if {![winfo exists $base] || $io_elem_timer == 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]
      	#puts "elem_prop.tcl :enable_channel is : $enable_channel"

 	
      if {$enable_channel == 1} {

        #puts "elem_prop.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_prop.tcl : enable_channel is off"
      	set dig_value 0
      	
      }

      after 500 [list  loop_ret_io_elem_value ]
	

}




#-----------------------------------------------------------------------------




#-----------------------------------------------------------------------------

   ###############################################################################

   proc my_ret_io_elem_value { } {

      global surveh
      global diganal
      global elem_num
      global enable_channel
      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_value"
   }


#-----------------------------------------------------------------------------


proc my_ret_all_info { } {

      # 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 xpos
      global ypos
      global vga
      global default1value default2value default3value default4value



      # set the string returned to be "all_info"
      # ret_all_info $surveh $diganal $elem_num $vga
      set all_info [ret_all_info $surveh $diganal $elem_num 1]


      # now parse it and set it to "all_info_list"
      set all_info_list [ split $all_info | ]

      # 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 xpos 0
      	set ypos 0
      	set vga 1
      	puts "Element does not exist, set everything to 0 or Null "

        # Pack the 4 default values to a single string
        #set calc "$default1value:$default2value:$default3value:$default4value"
        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 \
                          $xpos $ypos 1

        my_ret_io_elem_value

      } else {

      #-------------------------------------------------------------------
      # Now use the temporary variable to keep the value returned for
      # surface/vehicle, digital/analog, and element number
      #-------------------------------------------------------------------

      	      	#puts "elem_prop.tcl : all : $all_info_list"
      		
      		# 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]
      		
      		
      		
      		
      		
      		set default_type [ lindex $all_info_list 5 ]
      		#puts "elem_prop.tcl : default type here : $default_type"
      		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 xpos [ lindex $all_info_list 18 ]
      		set ypos [ lindex $all_info_list 19 ]
      		set vga 1
        	
      		# we have to get the output nodes from db and display it back in GUI
      		display_output_in_listbox

      		# This will update the raw, calculated analog, and digital value.
      		my_ret_io_elem_value
		
        # end of else
        }



# end of procedure "my_ret_all_info"
}

proc display_output_in_listbox { } {
	
        global surveh
        global diganal
        global elem_num

       #####################################################################
       # CHANGES HERE
       #####################################################################
	
        set all_output_info [display_output $surveh $diganal $elem_num]
        set all_output [split $all_output_info |] 		
        #puts "all output : $all_output_info"
	
	if {$surveh == 1} {
	
		set temp_count [llength $all_output]
		set count [incr temp_count -1]
		
		for {set i 0 } {$i <= $count } {incr i 2} {
			
			# Dig Gesout
			if { [lindex $all_output $i] == 0 } {
				set e_num [lindex $all_output [expr {$i + 1}]]
				.bpm.output_text insert end "Dig Gesout $e_num"
			
			}
			
			# Dig MPL
			if { [lindex $all_output $i] == 1 } {
				set e_num [lindex $all_output [expr {$i + 1}]]
				.bpm.output_text insert end "Dig MPL $e_num"
			
			} 	
		
			# Ana Gesdac
			if { [lindex $all_output $i] == 2 } {
				set e_num [lindex $all_output [expr {$i + 1}]]
				.bpm.output_text insert end "Ana Gesdac $e_num"
			
			}
			
		# end for
		}	 	
		
	    	
	} else {
	
	#####################################################################
	# CHANGES HERE
	#####################################################################
	
	
		# Analog Output
		if { [lindex $all_output 0] == 3 } {
			set e_num [lindex $all_output 1]
			.bpm.output_text insert end "[get_element_name 1 3 $e_num]"
			
		}
		
	}

}



proc do_ok_command { } {

    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 xpos ypos vga			
    global default1value default2value default3value default4value
       	
       	
       	# Ask user to enter a name for the element
       	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 $xpos $ypos $vga			
        		

        # Send the Output list to IO database
        send_out_list_info $surveh $diganal $elem_num [get_output]
						
        # Check graph type to be displayed
        check_graph_type $surveh $diganal $graph_type $xpos $ypos $elem_name $elem_num
			
        # Display the new name to the submenu list
        display_new_name
		
	destroy .bpm
	destroy .add_output		

}



#----------------------------------------------------------#
# 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_name { } {

        global surveh diganal elem_num elem_name

          if ![winfo exists .ventana2] {
             	puts "elem_prop.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 2	#
             #------------#
        	
             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 autocalibrate { } {
	
	global surveh diganal elem_num maxautocalibrate minautocalibrate max_auto_loop_var
	global enable_auto
	
	# Type has to be surface analog
	if {$diganal != 0 } {
		puts "elem_prop.tcl : It is not the appropriate type"
		return
	}
	
	# Enable autocalibrate button has to be enabled
	if {$enable_auto == 0} {
		return
	
	}
	
	
	set max_auto_loop_var 1
	
	button .bpm.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 .bpm.auto -x 200 -y 200
 	grab .bpm.auto
 	
	getting_max_auto_loop
	
	
}
 	


proc getting_max_auto_loop { } {

	global surveh diganal elem_num maxautocalibrate
	global max_auto_loop_var  min_auto_loop_var
	global gainvalue offset
	
	if {![winfo exists .bpm.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 .bpm.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 .bpm.automin -x 200 -y 200
 	             grab .bpm.automin
 	
		getting_min_auto_loop
		
		destroy .bpm.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_prop.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_prop.tcl: Waiting for MAX value........."		
            after 250 [list getting_max_auto_loop]
}

proc getting_min_auto_loop { } {

	global surveh diganal elem_num minautocalibrate
	global min_auto_loop_var
	
         	if {![winfo exists .bpm.automin] || $min_auto_loop_var == 0 } {
		destroy .bpm.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_prop.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_prop.tcl: Waiting for MIN value........."
            	after 250 [list getting_min_auto_loop]	
}

