Drawing board stop draw

This code is from a drawing board, the problem is:

1- When you click outside the drawing area, a dot is drawn (thus you cant click anything except inside drawing area because it will result in undesired dots)

2- When you drag outside the drawing area the dot is drawn (bad), the pencil doesn’t draw (good), but if you drag until reaching the drawing area draws (undesired, because it should only draw when you started to drag from inside the drawing area)

Can someone tell me how to tweak the code or simple add a “deselect” tool option please?

That’s a link to the fla file ziped, and that’s the code of the drawing board:

http://www.soccerschool.com.ar/f/drawingBroadmx.zip


_root.P = 0;
_root.level = 1000;
_root.tmpx = -1;
_root.tmpy = -1;
_root.count = 0;
_root.isMouseDown = false;
_root.frontColor = 0x000000;

// don't draw if dragged from outside

mm.drawing=false;
mm.onPress=function(){this.drawing=true;}
mm.onRelease=mm.onRollOut=function(){this.drawing=false;}

// note that rolling out of the mm clip will also stop the drawing

//default brush size

_root.pt = 5;
_root.tool = "pen";
md = new Object();
mm = new Object();
mu = new Object();
actionList = new Array();
Number.prototype.HEXtoRGB = function() {
	return {rb:this >> 16, gb:(this >> 8) & 0xff, bb:this & 0xff};
};
Color.prototype.blendRGB = function(c1, c2, t) {
	if (arguments.length == 2) {
		t = c2;
		c2 = this.getRGB();
	}
	if (t<-1) {
		t = -1;
	} else if (t>1) {
		t = 1;
	}
	if (t<0) {
		t = 1+t;
	}
	c1 = c1.HEXtoRGB();
	c2 = c2.HEXtoRGB();
	var ct = (c1.rb+(c2.rb-c1.rb)*t) << 16 | (c1.gb+(c2.gb-c1.gb)*t) << 8 | (c1.bb+(c2.bb-c1.bb)*t);
	this.setRGB(ct);
	return ct;
};
md.onMouseDown = function() {
	if (_root.paintMode) {
		a = new Object();
		a.start = _root.P;
		actionList.push(a);
		_root.isMouseDown = true;
		if (_root.tool == "del") {
			for (i=0; i<_root.P; i++) {
				if (eval("P"+i).hitTest(_root.cursor)) {
					eval("P"+i).removeMovieClip();
					break;
				}
			}
		}
		if (_root.tool == "sqr") {
			Paint.duplicateMovieClip("P"+_root.P, _root.level+P);
			eval("P"+_root.P)._x = _root._xmouse;
			eval("P"+_root.P)._y = _root._ymouse;
		}
		if (_root.tool == "ova") {
			Paint.duplicateMovieClip("P"+_root.P, _root.level+P);
			eval("P"+_root.P)._x = _root._xmouse;
			eval("P"+_root.P)._y = _root._ymouse;
		}
		if (_root.tool == "pen") {
			Paint.duplicateMovieClip("P"+_root.P, _root.level+P);
			eval("P"+_root.P)._x = _root._xmouse;
			eval("P"+_root.P)._y = _root._ymouse;
			eval("P"+_root.P).beginFill(_root.frontColor, 100);
			eval("P"+_root.P).drawCircle((_root.pt/2), 0, 0);
			this.endFill();
			_root.P++;
			_root.tmpx = _root._xmouse;
			_root.tmpy = _root._ymouse;
		}
		if (_root.tool == "mak") {
			Paint.duplicateMovieClip("P"+_root.P, _root.level+P);
			eval("P"+_root.P)._x = _root._xmouse;
			eval("P"+_root.P)._y = _root._ymouse;
			eval("P"+_root.P).duplicateMovieClip(eval("P"+_root.P)._name+"shadow", _root.P+_root.level-500);
			sn = eval("P"+_root.P)._name;
			eval("P"+_root.P).onUnload = function() {
				eval(this._name+"shadow").removeMovieClip();
			};
			eval(sn+"shadow").beginFill(_root.frontColor, 10);
			eval(sn+"shadow").drawCircle((pt), 0, 0);
			eval(sn+"shadow").endFill();
			eval("P"+_root.P).beginFill(_root.frontColor*10, 5);
			eval("P"+_root.P).drawCircle((_root.pt/2), 0, 0);
			this.endFill();
			_root.P++;
			_root.tmpx = _root._xmouse;
			_root.tmpy = _root._ymouse;
		}
	}
};
md.onMouseUp = function() {
	if (_root.isMouseDown == true) {
		_root.isMouseDown = false;
		if (_root.tool == "sqr") {
			_root.P++;
		}
		if (_root.tool == "ova") {
			_root.P++;
		}
		actionList[actionList.length-1].end = _root.P;
	}
	_root.tmpx = -1;
	_root.tmpy = -1;
	_root.level += 1000;
	_root.count = 0;
};
mm.onMouseMove = function() {
	// setear area en donde debe dibujar
	if (_xmouse < 705 && _xmouse > 70 && _ymouse < 595 && _ymouse > 115) {
		_root.paintMode = !(Panel.hitTest(_root._xmouse, _root._ymouse, true));
		if (_root.paintMode) {
			//Mouse.hide();
			_root.cursor._visible = 1;
			if (_root.tool == "del") {
				for (i=0; i<_root.P && _root.isMouseDown; i++) {
					if (eval("P"+i).hitTest(_root.cursor)) {
						eval("P"+i).removeMovieClip();
					}
				}
			}
			if (_root.tool == "sqr") {
				if(this.drawing){
					sqr = eval("P"+(_root.P));
					sqr.clear();
					point = new Object();
					point.x = _root._xmouse;
					point.y = _root._ymouse;
					sqr.globalToLocal(point);
					toX = point.x;
					toY = point.y;
					sqr.beginFill(_root.frontColor, 100);
					sqr.moveTo(0, 0);
					sqr.lineTo(toX, 0);
					sqr.lineTo(toX, toY);
					sqr.lineTo(0, toY);
					sqr.endFill();
				}
			}
			if (_root.tool == "ova") {
				if(this.drawing){
					ova = eval("P"+(_root.P));
					ova.clear();
					point = new Object();
					point.x = _root._xmouse;
					point.y = _root._ymouse;
					ova.globalToLocal(point);
					toX = point.x;
					toY = point.y;
					v_far = Math.abs(0-toY);
					h_far = Math.abs(0-toX);
					far = Math.sqrt((v_far*v_far)+(h_far*h_far));
					ova.beginFill(_root.frontColor, 100);
					ova.drawCircle(far, 0, 0);
					ova.endFill();
				}
			}
			if (_root.tool == "pen") {
				_root.count++;
				if (_root.count%500 == 0) {
					_root.level += 1000;
				}
				if (_root.isMouseDown && _root.count%4 == 0) {
					if (_root.tmpx != -1 && _root.tmpy != -1) {
						Paint.duplicateMovieClip("P"+_root.P, _root.level+P);
						eval("P"+_root.P).drawLine(_root._xmouse, _root._ymouse, _root.tmpx, _root.tmpy, _root.pt);
						_root.P++;
					}
					_root.tmpx = _root._xmouse;
					_root.tmpy = _root._ymouse;
				}
			}
			if (_root.tool == "mak") {
				_root.count++;
				if (_root.count%500 == 0) {
					_root.level += 1000;
				}
				if (_root.isMouseDown && _root.count%2 == 0) {
					if (_root.tmpx != -1 && _root.tmpy != -1) {
						Paint.duplicateMovieClip("P"+_root.P, _root.level+P);
						eval("P"+_root.P).drawNeonLine(_root._xmouse, _root._ymouse, _root.tmpx, _root.tmpy, _root.pt);
						_root.P++;
					}
					_root.tmpx = _root._xmouse;
					_root.tmpy = _root._ymouse;
				}
			}
		} else {
			Mouse.show();
			_root.cursor._visible = 0;
		}
	}
};
Mouse.addListener(md);
Mouse.addListener(mm);
Mouse.addListener(mu);
// 
MovieClip.prototype.drawCircle = function(r, x, y) {
	this.moveTo(x+r, y);
	a = Math.tan(22.5*Math.PI/180);
	for (var angle = 45; angle<=360; angle += 45) {
		// endpoint:
		var endx = r*Math.cos(angle*Math.PI/180);
		var endy = r*Math.sin(angle*Math.PI/180);
		// control:
		// (angle-90 is used to give the correct sign)
		var cx = endx+r*a*Math.cos((angle-90)*Math.PI/180);
		var cy = endy+r*a*Math.sin((angle-90)*Math.PI/180);
		this.curveTo(cx+x, cy+y, endx+x, endy+y);
	}
};
MovieClip.prototype.drawLine = function(fromX, fromY, toX, toY, pt) {
	this._x = fromX;
	this._y = fromY;
	point = new Object();
	point.x = toX;
	point.y = toY;
	this.globalToLocal(point);
	// trace(point.x + " " + point.y);
	toX = point.x;
	toY = point.y;
	// way cal
	v_far = Math.abs(0-toY);
	h_far = Math.abs(0-toX);
	far = Math.sqrt((v_far*v_far)+(h_far*h_far));
	adj = (toX-0);
	Q = Math.acos(adj/far)/(Math.PI/180);
	if (0>toY) {
		rot = 90-Q;
	} else {
		rot = 90+Q;
	}
	if (rot>90) {
		way = 360-Math.abs(rot-90);
	} else {
		way = Math.abs(rot-90);
	}
	for (i=0; i<far; i += 2) {
		nx = int((i)*Math.cos((way)*(Math.PI/180)));
		ny = -(int((i)*Math.sin((way)*(Math.PI/180))));
		this.beginFill(_root.frontColor, 100);
		this.drawCircle((pt/2), nx, ny);
		this.endFill();
	}
};
MovieClip.prototype.drawNeonLine = function(fromX, fromY, toX, toY, pt) {
	this._x = fromX;
	this._y = fromY;
	point = new Object();
	point.x = toX;
	point.y = toY;
	this.globalToLocal(point);
	// trace(point.x + " " + point.y);
	toX = point.x;
	toY = point.y;
	// way cal
	v_far = Math.abs(0-toY);
	h_far = Math.abs(0-toX);
	far = Math.sqrt((v_far*v_far)+(h_far*h_far));
	adj = (toX-0);
	Q = Math.acos(adj/far)/(Math.PI/180);
	if (0>toY) {
		rot = 90-Q;
	} else {
		rot = 90+Q;
	}
	if (rot>90) {
		way = 360-Math.abs(rot-90);
	} else {
		way = Math.abs(rot-90);
	}
	this.duplicateMovieClip(this._name+"shadow", _root.P+_root.level-500);
	this.onUnload = function() {
		eval(this._name+"shadow").removeMovieClip();
	};
	for (i=0; i<far; i += 2) {
		nx = int((i)*Math.cos((way)*(Math.PI/180)));
		ny = -(int((i)*Math.sin((way)*(Math.PI/180))));
		eval(this._name+"shadow").beginFill(_root.frontColor, 10);
		eval(this._name+"shadow").drawCircle((pt), nx, ny);
		eval(+this._name+"shadow").endFill();
	}
	for (i=0; i<far; i += 2) {
		nx = int((i)*Math.cos((way)*(Math.PI/180)));
		ny = -(int((i)*Math.sin((way)*(Math.PI/180))));
		c = new Color(this);
		c.setRGB(_root.frontColor);
		c.blendRGB(0xffffff, .2);
		this.beginFill(c.getRGB(), 100);
		this.drawCircle((pt/2), nx, ny);
		this.endFill();
	}
};
function cursorReflesh() {
	this.createEmptyMovieClip("cursor", 100001);
	c = new Color(this.cursor);
	c.setRGB(_root.frontColor);
	if (_root.frontColor>(0xffffff/2)) {
		c.blendRGB(0x000000, -0.3);
	} else {
		c.blendRGB(0xffffff, -0.5);
	}
	this.cursor.lineStyle(1, 0xffffff, 100);
	this.cursor.drawCircle(pt/2, 0, 0);
	this.cursor.startDrag(true);
}
function undo() {
	if (actionList.length>0) {
		a = actionList.pop();
		if (a.start == undefined) {
			C = new Color(this.Background);
			C.setRGB(a);
			C2 = new Color(this.Panel.bubg);
			C2.setRGB(a);
		} else {
			for (i=a.start; i<=a.end; i++) {
				eval("P"+i).removeMovieClip();
			}
			_root.P = a.start;
		}
	}
}
cursorReflesh();


I hope you can help and thank you so much!