# This has the implementation of [random $max $min]
source random.tcl




set alarmx 0
set alarmy 0
set alarm_surveh 0
set alarm_diganal  0
set alarm_elem_num 0
set alarm_name ""
set new_alarm 0


proc set_alarm_pos { x y } {
   global alarmx
   global alarmy

   set alarmx $x
   set alarmy $y

}


proc set_alarm_name {name} {
	
	global alarm_name
	set alarm_name $name
	
}


proc alarm { } {

        global al
        global al2

	global new_alarm
	global alarm_name
	
	#----------------------------------------------------
	# calling the c++ function to check whether there
	# is a new alarm came up or not
	#----------------------------------------------------
	
	set new_alarm [check_alarm]
	set alarm_name [get_alarm_name]
	
	if { $new_alarm == 1 } {
	
		
		# If there is a new alarm
		set al .ventana.editor.$alarm_name
		set al2 .ventana.editor.$alarm_name
		
		if [winfo exists $al] {
			destroy $al
		}
		
		if [winfo exists $al2] {
			destroy $al2
		}
		
		
		button $al -bg red -relief flat -text $alarm_name \
			-command {destroy $al; destroy $al2}	
		
		button $al2 -bg red -relief flat -text $alarm_name \
			-command {destroy $al; destroy $al2}	
		
		bind $al <ButtonRelease-1> {
			global al
			
			set al %W
			set name [lindex [$al configure -text] 4]
   			
			# Sending alarm name to database
       		 	add_alarm_to_db $name
       		 	
       		 	# Turn the button "Alarm List" (from "test.tcl")  red
       		 	
       		 	.ventana.bar.alarm configure -bg red
       		 	.ventana2.bar.alarm configure -bg red
		}	
		
		place $al -x 300 -y 300
		
		bind $al2 <ButtonRelease-1> {
			global al2
			
			set al2 %W
			set name [lindex [$al2 configure -text] 4]
   			
			# Sending alarm name to database
       		 	add_alarm_to_db $name
       		 	
       		 	# Turn the button "Alarm List" (from "test.tcl")  red
       		 	
       		 	.ventana.bar.alarm configure -bg red
       		 	.ventana2.bar.alarm configure -bg red
		}	
		
		place $al2 -x 300 -y 300
		
		# Flash the alarm in the beginning
		after 1 flashing $al
		after 1 flashing $al2
		
		
		# set the global alarm status in c++ back to 0
		reset_alarm
		
        }

	after 100 alarm
	
	
}


proc check_new_alarm {name} {
	
	global new_alarm
	global alarm_name
	
	set new_alarm 1
	set alarm_name $name
	
}



proc flashing {name} {

	$name flash
        $name flash
        $name flash
        $name flash
        $name flash

}

