How can i stop a function in drawing ( painting ) app?

hi everybody ,

i have a problem with functions, i’m creating a painting app in flash as3. i want to draw two different style a pen and rectangle.

When i draw pen style in first frame there is no problem, but when i click rectangle style in second frame it is drawing with both style. How can i stop or kill other function?

How do I make both of the not drawing. And How can i undo my last action in drawing?

Fla is in the attachement (CS4 version)

Sorry for my English, Thanks.

in first frame drawing pen;


var clip : Shape = new Shape();
addChild (clip);

stage.addEventListener(MouseEvent.MOUSE_DOWN, _handleMouseEvent);
stage.addEventListener(MouseEvent.MOUSE_UP, _handleMouseEvent);

function _handleMouseEvent(evt:MouseEvent):void
{
   switch (String(evt.type)) {
      case "mouseDown":
         stage.addEventListener(MouseEvent.MOUSE_MOVE, _handleMouseEvent);
         clip.graphics.lineStyle(7,0x000000);
         clip.graphics.moveTo(mouseX,mouseY);         
      break;
      
      case "mouseUp":
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, _handleMouseEvent);
      break;
      
      case "mouseMove":
         clip.graphics.lineTo(mouseX,mouseY);
      break;
   }
}



in second frame;


stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler); 
var startX:Number; 
var startY:Number; 
var rects:Array = new Array(); 
var finalRects:Array = new Array(); 

function downHandler(event:MouseEvent):void {     
    stage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);     
    stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);     
    stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);     
    startX = mouseX;     
    startY = mouseY; 
} 

function moveHandler(event:MouseEvent):void {     
    removeRects();     
    var rect:Sprite = new Sprite();     
    rects.push(rect);     
    rect.graphics.beginFill(0,0.25);     
    rect.graphics.drawRect(startX,startY,mouseX - startX,mouseY - startY);     
    rect.graphics.endFill();     
    addChild(rect); 
} 

function upHandler(event:MouseEvent):void {     
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);     
    stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);     
    stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);     
    var rect:Sprite = new Sprite();     
    finalRects.push(rect);     
    var colour:Number = Math.floor( 0xda1935);     
    rect.graphics.beginFill(colour,0.5);     
    rect.graphics.drawRect(startX,startY,mouseX - startX,mouseY - startY);     
    rect.graphics.endFill();     
    addChild(rect);     
    //trace("Number of rectangles = " + finalRects.length); 
} 

function removeRects():void {     
    var len:uint = rects.length;    
    for(var i:uint = 0; i < len; i++) {         
        removeChild(rects*);         
        rects* = null;         
        rects.splice(i,1);     
    } 
}