global num_of_items
set num_of_items 0
set surface .ventana.bar.maintenance.menu.element.surface
set surface2 .ventana2.bar.maintenance.menu.element.surface

proc add_output { sv } {

   global elem_num surface surface2
   global num_of_items

   # if it is output for vehicle
   if { $sv == 0 } {
   	
   set addout .add_output	
   if [winfo exists $addout] {destroy $addout}

   global env
   set env(DISPLAY) :0
   toplevel $addout -width 10c -height 5c
   wm title $addout "Add Output"


   label $addout.title -text "Adding Output"  -relief groove -fg blue

   menubutton $addout.anaout -text "Analog Out" -menu $addout.anaout.menu

   #------------------------------------------------------------------------------------#
   # Create the Analog Out submenu
   #------------------------------------------------------------------------------------#
   	
   menu $addout.anaout.menu

   $addout.anaout.menu add command -label "1 [get_element_name 1 3 1]" \
	-command { create_anaout_item 1 }
		
   $addout.anaout.menu add command -label "2 [get_element_name 1 3 2]" \
	-command { create_anaout_item 2 }	
	
   $addout.anaout.menu add command -label "3 [get_element_name 1 3 3]" \
	-command { create_anaout_item 3 }
		
   $addout.anaout.menu add command -label "4 [get_element_name 1 3 4]" \
	-command { create_anaout_item 4 }
	
	
   #------------------------------------------------------------------------------------#
   # Add a Close button
   #------------------------------------------------------------------------------------#

   button $addout.close -text Close -command {destroy .add_output}

   #------------------------------------------------------------------------------------#
   # Pack things up
   #------------------------------------------------------------------------------------#

   pack $addout.title -side top  -fill x -padx 3 -pady 3
   pack  $addout.anaout -side top	
   pack $addout.close -side bottom -pady 3m

	
   #------------------------------------------------------------------------------------#
   # else , it is output for SURFACE (sv == 1)
   #------------------------------------------------------------------------------------#
	
   } else {


   # initially the number of items in the list is 0

   set addout .add_output


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

   global env
   set env(DISPLAY) :0
   toplevel $addout -width 10c -height 5c
   wm title $addout "Add Output"


   label $addout.title -text "Adding Output"  -relief groove -fg blue

   menubutton $addout.dig -text Digital -menu $addout.dig.menu
   menubutton $addout.ana -text Analog -menu $addout.ana.menu

   #------------------------------------------------------------------------------------#
   # Create the Digital submenu
   #------------------------------------------------------------------------------------#
   
   menu $addout.dig.menu
   $addout.dig.menu add cascade -label GESOUT \
	-menu $addout.dig.menu.gesout
   $addout.dig.menu add cascade -label MPL \
	-menu $addout.dig.menu.mpl

   #------------------------------------------------------------------------------------#
   # Create submenu for "GESOUT"	
   #------------------------------------------------------------------------------------#
   
   menu $addout.dig.menu.gesout
   $addout.dig.menu.gesout add cascade -label "1-8" \
   	-menu $addout.dig.menu.gesout.b1
   $addout.dig.menu.gesout add cascade -label "9-16" \
   	-menu $addout.dig.menu.gesout.b2
   $addout.dig.menu.gesout add cascade -label "17-24" \
   	-menu $addout.dig.menu.gesout.b3
   $addout.dig.menu.gesout add cascade -label "25-32" \
   	-menu $addout.dig.menu.gesout.b4
   $addout.dig.menu.gesout add cascade -label "33-40" \
   	-menu $addout.dig.menu.gesout.b5
   $addout.dig.menu.gesout add cascade -label "41-48" \
   	-menu $addout.dig.menu.gesout.b6
   $addout.dig.menu.gesout add cascade -label "49-56" \
   	-menu $addout.dig.menu.gesout.b7
   $addout.dig.menu.gesout add cascade -label "57-64" \
   	-menu $addout.dig.menu.gesout.b8

   	
   menu $addout.dig.menu.gesout.b1
   $addout.dig.menu.gesout.b1 add command -label "1" \
	-command {list_output "Dig Gesout 1" }
   $addout.dig.menu.gesout.b1 add command -label "2" \
	-command {list_output "Dig Gesout 2" }
   $addout.dig.menu.gesout.b1 add command -label "3" \
	-command {list_output "Dig Gesout 3" }
   $addout.dig.menu.gesout.b1 add command -label "4" \
	-command {list_output "Dig Gesout 4" }
   $addout.dig.menu.gesout.b1 add command -label "5" \
	-command {list_output "Dig Gesout 5" }
   $addout.dig.menu.gesout.b1 add command -label "6" \
	-command {list_output "Dig Gesout 6" }
   $addout.dig.menu.gesout.b1 add command -label "7" \
	-command {list_output "Dig Gesout 7" }
   $addout.dig.menu.gesout.b1 add command -label "8" \
	-command {list_output "Dig Gesout 8" }


   menu $addout.dig.menu.gesout.b2
   $addout.dig.menu.gesout.b2 add command -label "9" \
	-command {list_output "Dig Gesout 9"}
   $addout.dig.menu.gesout.b2 add command -label "10" \
	-command {list_output "Dig Gesout 10"}
   $addout.dig.menu.gesout.b2 add command -label "11" \
	-command {list_output "Dig Gesout 11"}
   $addout.dig.menu.gesout.b2 add command -label "12" \
	-command {list_output "Dig Gesout 12"}
   $addout.dig.menu.gesout.b2 add command -label "13" \
	-command {list_output "Dig Gesout 13"}
   $addout.dig.menu.gesout.b2 add command -label "14" \
	-command {list_output "Dig Gesout 14"}
   $addout.dig.menu.gesout.b2 add command -label "15" \
	-command {list_output "Dig Gesout 15"}
   $addout.dig.menu.gesout.b2 add command -label "16" \
	-command {list_output "Dig Gesout 16"}


   menu $addout.dig.menu.gesout.b3
   $addout.dig.menu.gesout.b3 add command -label "17" \
	-command {list_output "Dig Gesout 17"}
   $addout.dig.menu.gesout.b3 add command -label "18" \
	-command {list_output "Dig Gesout 18"}
   $addout.dig.menu.gesout.b3 add command -label "19" \
	-command {list_output "Dig Gesout 19"}
   $addout.dig.menu.gesout.b3 add command -label "20" \
	-command {list_output "Dig Gesout 20"}
   $addout.dig.menu.gesout.b3 add command -label "21" \
	-command {list_output "Dig Gesout 21"}
   $addout.dig.menu.gesout.b3 add command -label "22" \
	-command {list_output "Dig Gesout 22"}
   $addout.dig.menu.gesout.b3 add command -label "23" \
	-command {list_output "Dig Gesout 23"}
   $addout.dig.menu.gesout.b3 add command -label "24" \
	-command {list_output "Dig Gesout 24"}


   menu $addout.dig.menu.gesout.b4
   $addout.dig.menu.gesout.b4 add command -label "25" \
	-command {list_output "Dig Gesout 25"}
   $addout.dig.menu.gesout.b4 add command -label "26" \
	-command {list_output "Dig Gesout 26"}
   $addout.dig.menu.gesout.b4 add command -label "27" \
	-command {list_output "Dig Gesout 27"}
   $addout.dig.menu.gesout.b4 add command -label "28" \
	-command {list_output "Dig Gesout 28"}
   $addout.dig.menu.gesout.b4 add command -label "29" \
	-command {list_output "Dig Gesout 29"}
   $addout.dig.menu.gesout.b4 add command -label "30" \
	-command {list_output "Dig Gesout 30"}
   $addout.dig.menu.gesout.b4 add command -label "31" \
	-command {list_output "Dig Gesout 31"}
   $addout.dig.menu.gesout.b4 add command -label "32" \
	-command {list_output "Dig Gesout 32"}
	

   menu $addout.dig.menu.gesout.b5
   $addout.dig.menu.gesout.b5 add command -label "33" \
	-command {list_output "Dig Gesout 33"}
   $addout.dig.menu.gesout.b5 add command -label "34" \
	-command {list_output "Dig Gesout 34"}
   $addout.dig.menu.gesout.b5 add command -label "35" \
	-command {list_output "Dig Gesout 35"}
   $addout.dig.menu.gesout.b5 add command -label "36" \
	-command {list_output "Dig Gesout 36"}
   $addout.dig.menu.gesout.b5 add command -label "37" \
	-command {list_output "Dig Gesout 37"}
   $addout.dig.menu.gesout.b5 add command -label "38" \
	-command {list_output "Dig Gesout 38"}
   $addout.dig.menu.gesout.b5 add command -label "39" \
	-command {list_output "Dig Gesout 39"}
   $addout.dig.menu.gesout.b5 add command -label "40" \
	-command {list_output "Dig Gesout 40"}
	
   menu $addout.dig.menu.gesout.b6
   $addout.dig.menu.gesout.b6 add command -label "41" \
	-command {list_output "Dig Gesout 41"}
   $addout.dig.menu.gesout.b6 add command -label "42" \
	-command {list_output "Dig Gesout 42"}
   $addout.dig.menu.gesout.b6 add command -label "43" \
	-command {list_output "Dig Gesout 43"}
   $addout.dig.menu.gesout.b6 add command -label "44" \
	-command {list_output "Dig Gesout 44"}
   $addout.dig.menu.gesout.b6 add command -label "45" \
	-command {list_output "Dig Gesout 45"}
   $addout.dig.menu.gesout.b6 add command -label "46" \
	-command {list_output "Dig Gesout 46"}
   $addout.dig.menu.gesout.b6 add command -label "47" \
	-command {list_output "Dig Gesout 47"}
   $addout.dig.menu.gesout.b6 add command -label "48" \
	-command {list_output "Dig Gesout 48"}

   menu $addout.dig.menu.gesout.b7
   $addout.dig.menu.gesout.b7 add command -label "49" \
	-command {list_output "Dig Gesout 49"}
   $addout.dig.menu.gesout.b7 add command -label "50" \
	-command {list_output "Dig Gesout 50"}
   $addout.dig.menu.gesout.b7 add command -label "51" \
	-command {list_output "Dig Gesout 51"}
   $addout.dig.menu.gesout.b7 add command -label "52" \
	-command {list_output "Dig Gesout 52"}
   $addout.dig.menu.gesout.b7 add command -label "53" \
	-command {list_output "Dig Gesout 53"}
   $addout.dig.menu.gesout.b7 add command -label "54" \
	-command {list_output "Dig Gesout 54"}
   $addout.dig.menu.gesout.b7 add command -label "55" \
	-command {list_output "Dig Gesout 55"}
   $addout.dig.menu.gesout.b7 add command -label "56" \
	-command {list_output "Dig Gesout 56"}
	
   menu $addout.dig.menu.gesout.b8
   $addout.dig.menu.gesout.b8 add command -label "57" \
	-command {list_output "Dig Gesout 57"}
   $addout.dig.menu.gesout.b8 add command -label "58" \
	-command {list_output "Dig Gesout 58"}
   $addout.dig.menu.gesout.b8 add command -label "59" \
	-command {list_output "Dig Gesout 59"}
   $addout.dig.menu.gesout.b8 add command -label "60" \
	-command {list_output "Dig Gesout 60"}
   $addout.dig.menu.gesout.b8 add command -label "61" \
	-command {list_output "Dig Gesout 61"}
   $addout.dig.menu.gesout.b8 add command -label "62" \
	-command {list_output "Dig Gesout 62"}
   $addout.dig.menu.gesout.b8 add command -label "63" \
	-command {list_output "Dig Gesout 63"}
   $addout.dig.menu.gesout.b8 add command -label "64" \
	-command {list_output "Dig Gesout 64"}

  	
  		
   #------------------------------------------------------------------------------------#
   # Create submenu for "MPL"    
   #------------------------------------------------------------------------------------#

   menu $addout.dig.menu.mpl
   $addout.dig.menu.mpl add cascade -label "1-8" \
   	-menu $addout.dig.menu.mpl.b1
   $addout.dig.menu.mpl add cascade -label "9-16" \
   	-menu $addout.dig.menu.mpl.b2
   $addout.dig.menu.mpl add cascade -label "17-24" \
   	-menu $addout.dig.menu.mpl.b3
   $addout.dig.menu.mpl add cascade -label "25-32" \
   	-menu $addout.dig.menu.mpl.b4
   $addout.dig.menu.mpl add cascade -label "33-40" \
   	-menu $addout.dig.menu.mpl.b5
   $addout.dig.menu.mpl add cascade -label "41-48" \
   	-menu $addout.dig.menu.mpl.b6
   	

   menu $addout.dig.menu.mpl.b1
   $addout.dig.menu.mpl.b1 add command -label "1" \
	-command {list_output "Dig MPL 1"}
   $addout.dig.menu.mpl.b1 add command -label "2" \
	-command {list_output "Dig MPL 2"}
   $addout.dig.menu.mpl.b1 add command -label "3" \
	-command {list_output "Dig MPL 3"}
   $addout.dig.menu.mpl.b1 add command -label "4" \
	-command {list_output "Dig MPL 4"}
   $addout.dig.menu.mpl.b1 add command -label "5" \
	-command {list_output "Dig MPL 5"}
   $addout.dig.menu.mpl.b1 add command -label "6" \
	-command {list_output "Dig MPL 6"}
   $addout.dig.menu.mpl.b1 add command -label "7" \
	-command {list_output "Dig MPL 7"}
   $addout.dig.menu.mpl.b1 add command -label "8" \
	-command {list_output "Dig MPL 8"}


   menu $addout.dig.menu.mpl.b2	
   $addout.dig.menu.mpl.b2 add command -label "9" \
	-command {list_output "Dig MPL 9"}
   $addout.dig.menu.mpl.b2 add command -label "10" \
	-command {list_output "Dig MPL 10"}
   $addout.dig.menu.mpl.b2 add command -label "11" \
	-command {list_output "Dig MPL 11"}
   $addout.dig.menu.mpl.b2 add command -label "12" \
	-command {list_output "Dig MPL 12"}
   $addout.dig.menu.mpl.b2 add command -label "13" \
	-command {list_output "Dig MPL 13"}
   $addout.dig.menu.mpl.b2 add command -label "14" \
	-command {list_output "Dig MPL 14"}
   $addout.dig.menu.mpl.b2 add command -label "15" \
	-command {list_output "Dig MPL 15"}
   $addout.dig.menu.mpl.b2 add command -label "16" \
	-command {list_output "Dig MPL 16"}

   menu $addout.dig.menu.mpl.b3	
   $addout.dig.menu.mpl.b3 add command -label "17" \
	-command {list_output "Dig MPL 17"}
   $addout.dig.menu.mpl.b3 add command -label "18" \
	-command {list_output "Dig MPL 18"}
   $addout.dig.menu.mpl.b3 add command -label "19" \
	-command {list_output "Dig MPL 19"}
   $addout.dig.menu.mpl.b3 add command -label "20" \
	-command {list_output "Dig MPL 20"}
   $addout.dig.menu.mpl.b3 add command -label "21" \
	-command {list_output "Dig MPL 21"}
   $addout.dig.menu.mpl.b3 add command -label "22" \
	-command {list_output "Dig MPL 22"}
   $addout.dig.menu.mpl.b3 add command -label "23" \
	-command {list_output "Dig MPL 23"}
   $addout.dig.menu.mpl.b3 add command -label "24" \
	-command {list_output "Dig MPL 24"}

   menu $addout.dig.menu.mpl.b4	
   $addout.dig.menu.mpl.b4 add command -label "25" \
	-command {list_output "Dig MPL 25"}
   $addout.dig.menu.mpl.b4 add command -label "26" \
	-command {list_output "Dig MPL 26"}
   $addout.dig.menu.mpl.b4 add command -label "27" \
	-command {list_output "Dig MPL 27"}
   $addout.dig.menu.mpl.b4 add command -label "28" \
	-command {list_output "Dig MPL 28"}
   $addout.dig.menu.mpl.b4 add command -label "29" \
	-command {list_output "Dig MPL 29"}
   $addout.dig.menu.mpl.b4 add command -label "30" \
	-command {list_output "Dig MPL 30"}
   $addout.dig.menu.mpl.b4 add command -label "31" \
	-command {list_output "Dig MPL 31"}
   $addout.dig.menu.mpl.b4 add command -label "32" \
	-command {list_output "Dig MPL 32"}
	

   #------------------------------------------------------------------------------------#
   # Create the Analog submenu
   #------------------------------------------------------------------------------------#

   menu $addout.ana.menu
   $addout.ana.menu add cascade -label GESDAC \
	-menu $addout.ana.menu.gesdac

   #------------------------------------------------------------------------------------#
   # Create the "GESDAC" submenu
   #------------------------------------------------------------------------------------#
   
   menu $addout.ana.menu.gesdac
   $addout.ana.menu.gesdac add cascade -label "1-8" \
	-menu $addout.ana.menu.gesdac.b1
   $addout.ana.menu.gesdac add cascade -label "9-16" \
	-menu $addout.ana.menu.gesdac.b2
   $addout.ana.menu.gesdac add cascade -label "17-24" \
	-menu $addout.ana.menu.gesdac.b3
   $addout.ana.menu.gesdac add cascade -label "25-32" \
	-menu $addout.ana.menu.gesdac.b4

   #------------------------------------------------------------------------------------#
   # adding element to the "GESDAC"	
   #------------------------------------------------------------------------------------#

   menu $addout.ana.menu.gesdac.b1
   $addout.ana.menu.gesdac.b1 add command -label "1" \
	-command {list_output "Ana Gesdac 1"}
   $addout.ana.menu.gesdac.b1 add command -label "2" \
	-command {list_output "Ana Gesdac 2"}
   $addout.ana.menu.gesdac.b1 add command -label "3" \
	-command {list_output "Ana Gesdac 3"}
   $addout.ana.menu.gesdac.b1 add command -label "4" \
	-command {list_output "Ana Gesdac 4"}
   $addout.ana.menu.gesdac.b1 add command -label "5" \
	-command {list_output "Ana Gesdac 5"}
   $addout.ana.menu.gesdac.b1 add command -label "6" \
	-command {list_output "Ana Gesdac 6"}
   $addout.ana.menu.gesdac.b1 add command -label "7" \
	-command {list_output "Ana Gesdac 7"}
   $addout.ana.menu.gesdac.b1 add command -label "8" \
	-command {list_output "Ana Gesdac 8"}


   menu $addout.ana.menu.gesdac.b2
   $addout.ana.menu.gesdac.b2 add command -label "9" \
	-command {list_output "Ana Gesdac 9"}
   $addout.ana.menu.gesdac.b2 add command -label "10" \
	-command {list_output "Ana Gesdac 10"}
   $addout.ana.menu.gesdac.b2 add command -label "11" \
	-command {list_output "Ana Gesdac 11"}
   $addout.ana.menu.gesdac.b2 add command -label "12" \
	-command {list_output "Ana Gesdac 12"}
   $addout.ana.menu.gesdac.b2 add command -label "13" \
	-command {list_output "Ana Gesdac 13"}
   $addout.ana.menu.gesdac.b2 add command -label "14" \
	-command {list_output "Ana Gesdac 14"}
   $addout.ana.menu.gesdac.b2 add command -label "15" \
	-command {list_output "Ana Gesdac 15"}
   $addout.ana.menu.gesdac.b2 add command -label "16" \
	-command {list_output "Ana Gesdac 16"}


   menu $addout.ana.menu.gesdac.b3
   $addout.ana.menu.gesdac.b3 add command -label "17" \
	-command {list_output "Ana Gesdac 17"}
   $addout.ana.menu.gesdac.b3 add command -label "18" \
	-command {list_output "Ana Gesdac 18"}
   $addout.ana.menu.gesdac.b3 add command -label "19" \
	-command {list_output "Ana Gesdac 19"}
   $addout.ana.menu.gesdac.b3 add command -label "20" \
	-command {list_output "Ana Gesdac 20"}
   $addout.ana.menu.gesdac.b3 add command -label "21" \
	-command {list_output "Ana Gesdac 21"}
   $addout.ana.menu.gesdac.b3 add command -label "22" \
	-command {list_output "Ana Gesdac 22"}
   $addout.ana.menu.gesdac.b3 add command -label "23" \
	-command {list_output "Ana Gesdac 23"}
   $addout.ana.menu.gesdac.b3 add command -label "24" \
	-command {list_output "Ana Gesdac 24"}


   menu $addout.ana.menu.gesdac.b4
   $addout.ana.menu.gesdac.b4 add command -label "25" \
	-command {list_output "Ana Gesdac 25"}
   $addout.ana.menu.gesdac.b4 add command -label "26" \
	-command {list_output "Ana Gesdac 26"}
   $addout.ana.menu.gesdac.b4 add command -label "27" \
	-command {list_output "Ana Gesdac 27"}
   $addout.ana.menu.gesdac.b4 add command -label "28" \
	-command {list_output "Ana Gesdac 28"}   	
   $addout.ana.menu.gesdac.b4 add command -label "29" \
	-command {list_output "Ana Gesdac 29"}
   $addout.ana.menu.gesdac.b4 add command -label "30" \
	-command {list_output "Ana Gesdac 30"}
   $addout.ana.menu.gesdac.b4 add command -label "31" \
	-command {list_output "Ana Gesdac 31"}
   $addout.ana.menu.gesdac.b4 add command -label "32" \
	-command {list_output "Ana Gesdac 32"}
	
   menu $addout.dig.menu.mpl.b5	
   $addout.dig.menu.mpl.b5 add command -label "33" \
	-command {list_output "Dig MPL 33"}
   $addout.dig.menu.mpl.b5 add command -label "34" \
	-command {list_output "Dig MPL 34"}
   $addout.dig.menu.mpl.b5 add command -label "35" \
	-command {list_output "Dig MPL 35"}
   $addout.dig.menu.mpl.b5 add command -label "36" \
	-command {list_output "Dig MPL 36"}
   $addout.dig.menu.mpl.b5 add command -label "37" \
	-command {list_output "Dig MPL 37"}
   $addout.dig.menu.mpl.b5 add command -label "38" \
	-command {list_output "Dig MPL 38"}
   $addout.dig.menu.mpl.b5 add command -label "39" \
	-command {list_output "Dig MPL 39"}
   $addout.dig.menu.mpl.b5 add command -label "40" \
	-command {list_output "Dig MPL 40"}
	
   menu $addout.dig.menu.mpl.b6	
   $addout.dig.menu.mpl.b6 add command -label "41" \
	-command {list_output "Dig MPL 41"}
   $addout.dig.menu.mpl.b6 add command -label "42" \
	-command {list_output "Dig MPL 42"}
   $addout.dig.menu.mpl.b6 add command -label "43" \
	-command {list_output "Dig MPL 43"}
   $addout.dig.menu.mpl.b6 add command -label "44" \
	-command {list_output "Dig MPL 44"}
   $addout.dig.menu.mpl.b6 add command -label "45" \
	-command {list_output "Dig MPL 45"}
   $addout.dig.menu.mpl.b6 add command -label "46" \
	-command {list_output "Dig MPL 46"}
   $addout.dig.menu.mpl.b6 add command -label "47" \
	-command {list_output "Dig MPL 47"}
   $addout.dig.menu.mpl.b6 add command -label "48" \
	-command {list_output "Dig MPL 48"}
	
	
   #------------------------------------------------------------------------------------#
   # Add a Close button
   #------------------------------------------------------------------------------------#

   button $addout.close -text Close -command {destroy .add_output}

   #------------------------------------------------------------------------------------#
   # Pack things up
   #------------------------------------------------------------------------------------#
   
   pack $addout.title -side top  -fill x -padx 3 -pady 3
   pack $addout.dig $addout.ana -side top	
   pack $addout.close -side bottom -pady 3m






   # end else
   }




# end  of procedure add_output   	
}



proc list_output { out } {

      global num_of_items

      #------------------------------------------------------------------------------------#
      # It does not matter what the name is since the adding button proc
      # will also check for the connection that exists in db already
      #------------------------------------------------------------------------------------#

      set name "some_name"
      set out_val [ split $out " " ]
      set temp_elem_num [lindex $out_val 2]


      if {[lindex $out_val 1] == "Gesout" } {
      	set board 0
      }

      if {[lindex $out_val 1] == "MPL" } {
      	set board 1
      	
      }

      if {[lindex $out_val 1] == "Gesdac" } {
      	set board 2
      	
      }  else {

      	set board 3
     }

      
      #------------------------------------------------------------------------------------#
      # Check whether the board is a valid number of board ( 0, 1, 2 )
      #------------------------------------------------------------------------------------#

	
      if { [check_out_db $name $board $temp_elem_num] > 0} {
      	#puts "output.tcl: The output connection exists in other database "
      	
	set status [tk_dialog  .warning  {}  {
	The Output Connection has already been used / mapped before.\
	Please remove the old connection first. 
      	
      	} warning 0 {Close} ]
       		
       	switch $status { 0 {return} }	            	
      	
      }
      
      
      


      for {set i 0} {$i <= $num_of_items} {incr i} {
         set curline [.bpm.output_text get $i]
        # puts "curline is $curline"
        # puts "out is $out "

         if {[string compare $curline $out ] == 0 } {
            puts "the button $curline has already been selected before"
            return
         }






      # end for loop
      }

      #----------------------------------------------------------#
      # Insert output selected to the BPM listbox
      #----------------------------------------------------------#

      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			

      .bpm.output_text insert end $out
      incr num_of_items

      #puts "output.tcl: what is the element here : $surveh $diganal $elem_num"

      #----------------------------------------------------------#
      # 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]
		

   # end proc list_output
   }




#-------------------------------------------------------
# This procedure delete the selected item in the output listbox
# if the delete button is pressed without selecting anything, then by
# default the last item in the list will be deleted
#-------------------------------------------------------

proc delete_output  { selected } {

   global surveh , elem_num
   set count [.bpm.output_text index end]

   if {$count == 0} {

      puts "output.tcl: there's no more items to delete here"
      return

   } else {

      if {$selected == ""} { set selected end }
      set curline [.bpm.output_text get $selected]

      .bpm.output_text delete $selected


      #----------------------------------------------------------#
      # UPDATING THE INPUT CONNECTION FOR ANALOG OUTPUT ELEMENT
      #
      # First, we have to get the input information our Analog Out is connected to
      #----------------------------------------------------------#

      set type [lindex $selected 1]

      #----------------------------------------------------------#
      # If type is an Analog Out type
      #----------------------------------------------------------#

      if { $type != "Gesout" && $type != "MPL" && $type != "Gesdac" } {
              	set io_conn [get_io_connection $curline]
              	set elnum [lindex $io_conn 1]
              	
              	#----------------------------------------------------------#
              	# Delete the element from Analog Out input list
              	#----------------------------------------------------------#
              	
              	delete_single_input $elnum
              	
      }


   }


}





proc get_output { } {

   	global surveh
   	set output ""
   	set count_output [.bpm.output_text index end]
   	
   		
	for {set i 0} {$i < $count_output} {incr i} {
        	
                #----------------------------------------------------------#
        	# get the string "Dig Gesout 1" or something and then parse and append
        	#----------------------------------------------------------#
        	
        	set out [.bpm.output_text get $i]
        	set out1 [lindex $out 1]
        	
        	if {[lindex $out 1] == "Gesout"} {
        	 	append output "0|"
        	 	append output "[lindex $out 2]|"
        	}	
        	if {[lindex $out 1] == "MPL"} {
        	 	append output "1|"
        	 	append output "[lindex $out 2]|"
        	}
        	if {[lindex $out 1] == "Gesdac"} {
        	 	append output "2|"
        	 	append output "[lindex $out 2]|"
        	} else {
        	
			#----------------------------------------------------------#
        		# Get the analog output connection using the name
        	 	#----------------------------------------------------------#
        	 	
        	 	set io_conn [get_io_connection $out]
        	
        	  	#puts "output.tcl : io_conn : $io_conn"
        	   	append output "3|"
			append output "[lindex $io_conn 1]|"
        	 	
        	}        	
        	

        # puts "output.tcl  : what's output here $output"
         return $output

   # end for loop
   }


}



#------------------------------------------------------------------------------------#
# This procedure will create the Analog Out selections 
# Here, we also do some error checking to see if the analog out has already
# exist or not and whether it has already assigned an input or not
#------------------------------------------------------------------------------------#
 
proc create_anaout_item { index } {

	global surface surface2 surveh elem_num
	
		set name [get_element_name 1 3 $index]
		
		#------------------------------------------------------------------------------------#
		# if the Analog Output element does not exist
		#------------------------------------------------------------------------------------#
		
		if { $name == ""} {
	             	
	            #------------------------------------------------------------------------------------#
			# Create the analog output element first
			#------------------------------------------------------------------------------------#
			
	           	send_io_elem_info 1 3 $index "Ana Out $index" 0 0:0:0:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1			
			#------------------------------------------------------------------------------------#
			# Update the name in the Analog Out selection menu
			#------------------------------------------------------------------------------------#
			
	  		$surface.anaout  entryconfigure $index -label "$index Ana Out $index"
	
	  		if [winfo exists $surface2] {
		  		$surface2.anaout  entryconfigure $index -label "$index Ana Out $index"
	               }
		  	
			set name "Ana Out $index"
	
		}
		
		set elnum [lindex [split [display_output 1 3 $index] |] 1]
		
		
		#------------------------------------------------------------------------------------#
		# This means that the analog output we selected has already have one input associated 
		# Therefore, do not add it to the output list -> RETURN
		#------------------------------------------------------------------------------------#
		
		if {$elnum != ""} {
		     	
	set status [tk_dialog  .warning  {}  {
	The Output Connection has already been used / mapped before.\
	Please remove the old connection first. 
      	
      	} warning 0 {Close} ]
       		
       	switch $status { 0 {return} }	            	
 
			return
		}
		
		#------------------------------------------------------------------------------------#
		# This means that the sub analog has already have 1 OUTPUT 
		# Therefore, do not add it to the output list -> RETURN
		#------------------------------------------------------------------------------------#
		
		if {[.bpm.output_text index end] != 0 } {
				return
       		}

		list_output $name	
		
		#------------------------------------------------------------------------------------#
		# Add this input element from Analog Output in the Input list			
		#------------------------------------------------------------------------------------#
		
	        send_out_list_info  1 3 $index "$surveh|$elem_num|"
		
		
# end proc
}