Help me draw a dashed border?

Here’s my latest attempt at rendering a dynamic rectange (w/ width, height, border_width, border_color & background_color) I’m trying to render a dashed border, and can’t seem to get the code right…

I’ve reworked my logic about five times, and can get the dashes to render right (almost) but the fill color disappears. It’s rendered using actionscript. You can see the issues if you change the border width… Anyone mind taking a look to see what I’m doing wrong? It seems like the moveTo function isn’t behaving right when I get to the end of each line. Thanks for any help!
I’ve attached the .fla, here’s my code:


var w:Number = 300;
var h:Number = 300;

var border_width:Number = 6;
var border_color:String = '0xcccc99';
var background_color:String = '0x336699';
var dash_width:Number = 10;
var dash_space:Number = (border_width*3);

// figure out how many horizontal dashes to render
// this is crude, but tried other ways and still the fill isn't right...
var h_num_dashes:Number = (w/(dash_width+dash_space));
var v_num_dashes:Number = (h/(dash_width+dash_space));

with(movBox) { // empty movie clip on the stage
	if(background_color) {
		beginFill(background_color,100);
	}
	lineStyle(border_width, border_color, 100 );
	// start
	moveTo(0,0);
	
	// top line
	//lineTo(w,0); // draws the next line without dashes for sanity's sake (debug)
	var xpos:Number = 0;
	for(i=0;i<h_num_dashes;i++) {
		lineTo((xpos+dash_width),0);
		moveTo((xpos+dash_width+dash_space),0);
		xpos += (dash_width+dash_space);
	}
	lineTo(w,0);	
	
	// right line
	//lineTo(w,h);
	var ypos:Number = 0;
	for(i=0;i<v_num_dashes;i++) {
		lineTo(w,(ypos+dash_width));
		moveTo(w,(ypos+dash_width+dash_space));
		ypos += (dash_width+dash_space);
	}
	lineTo(w,h);
	
	// bottom line
	//lineTo(0,h);
	var xpos:Number = w;
	for(i=h_num_dashes;i>0;i--) {
		lineTo((xpos-dash_width),h);
		moveTo((xpos-dash_width-dash_space),h);
		xpos -= (dash_width+dash_space);
	}
	lineTo(0,h);	
	
	// left line
	//lineTo(0,0);
	var ypos:Number = h;
	for(i=v_num_dashes;i>0;i--) {
		lineTo(0,(ypos-dash_width));
		moveTo(0,(ypos-dash_width-dash_space));
		ypos -= (dash_width+dash_space);
	}
	// finish
	lineTo(0,0);

	
}