I am using the following code to limit the number of checkboxes a user can check:

Code:
var MAX_ALLOWED=5;
var clickedData=new Array(false,false,false,false);
function itemsClicked(){var i=0;for(var j=0;j<clickedData.length;j++)i+=clickedData[j]?1:0;return(i);}
function itemClicked(_v)
{
  var ALLOW_THIS=true;
  var x=itemsClicked();
  if (x>=MAX_ALLOWED && !clickedData[_v])
  {
    ALLOW_THIS=false;
  }
  else
  {
    clickedData[_v]=clickedData[_v]?false:true;
	eval("document.create_exhib.r"+_v+".clicked=false;");
  }
  <!--document.showit.s1.value=x; /* comment out */-->
  return (ALLOW_THIS); /* kill the event handler */
}
The problem comes when i use this code on dynamically a populated form:
eg. previously checked boxes are marked
Code:
checked="checked"
when they match an entry in the relevant database.

The script cannot seem to calculate the limit with this dynamic data thrown in.

Has anyone else experienced a similar problem or could anyone recommend a more suitable peice of code.

Many Thanks!