Help with polygon drawing & editing

Hey All,
I’m working on an editor that will allow you to draw polygons by placing points. This works fine. What i’m having trouble with is adding more points once you’ve created a shape.

Currently I’m using a formula to try to calculate which line segment the mouse is closest to when you click to add the point in the appropriate order. The points are all stored in an array based on either when you added them (or for the new ones, where they were inserted). It seems to work fine some of the time, but certainly not all of the time… help???

If you have ideas for other ways to sort the poly, please let me know. I’m pretty stuck and have tried many different approaches!

I’ve attached a flash mx2004 file below. I’m trying to code it with AS1 to maintain player v6 compatibility.