   var classQ = new Array();
   var classMax = new Array();
   var reportCount = 0;
   var myForm;
   var checkBoxProp = 'selected';
   var araProp = 'report';


   function initializeClassQ()
   {
      // create 2 dim array: classQ
      for (var k=0; k < classMax.length; k++)
      {
         classQ[k] = new Array();
      }

      for (var i=0; i < reportCount; i++)
      {
         // only include reports that are visible
         if (myForm.elements[araProp+'['+i+'].priority'])
         {
            var productClass = myForm.elements[araProp+'['+i+'].productClassId'].value;
            var priority = (myForm.elements[araProp+'['+i+'].priority'].selectedIndex) + 1;
            if (myForm.elements[araProp+'['+i+'].' + checkBoxProp].checked)
            {
               classQ[productClass-1][priority-1] = i;
            }
         }
      }
   }

   function redrawClass(cls)
   {
      // go through all reports and clear anything with matching class
      for (var i=0; i < reportCount; i++)
      {
         if (cls == myForm.elements[araProp+'['+i+'].productClassId'].value)
         {
            clearReport(i);
         }
      }

      // go through class' queue and check & assign priorities
      var queue = classQ[cls-1];
      for (var i=0; i < queue.length; i++)
      {
         drawReport(classQ[cls-1][i], i+1, queue.length);
      }
   }

   function clearReport(num)
   {
      // clear checkbox
      myForm.elements[araProp+'['+num+'].'+checkBoxProp].checked = false;
      // clear priority
      myForm.elements[araProp+'['+num+'].priority'].options[0] = new Option("--",0);
      myForm.elements[araProp+'['+num+'].priority'].options.length = 1;
   }

   function drawReport(num, priority, maxPriority)
   {
      // set checkbox
      myForm.elements[araProp+'['+num+'].'+checkBoxProp].checked = true;
      // set up priority select list & set selected
      for (var i=0; i < maxPriority; i++)
      {
         myForm.elements[araProp+'['+num+'].priority'].options[i] = new Option(i+1,i+1);
      }
      myForm.elements[araProp+'['+num+'].priority'].selectedIndex = priority-1;
   }

   function doCheckbox(num, isDup)
   {
      var productClass = myForm.elements[araProp+'['+num+'].productClassId'].value;
      if (myForm.elements[araProp+'['+num+'].'+checkBoxProp].checked)
      {
         // report was CHECKED, check for max-out, check for dup, then append to end
         if (classMax[productClass - 1] != 0 && (classQ[productClass - 1].length >= classMax[productClass - 1]))
         {
            alert("Sorry, you are only allowed to select " + classMax[productClass - 1] + " product(s) in this product category.");
            myForm.elements[araProp+'['+num+'].'+checkBoxProp].checked = false;
            return;
         }
         if (isDup)
         {
            if (!confirm("This report has already been ordered and is still available for viewing. Charges will apply if you retrieve a new copy of this report.\n\nAre you sure you want to reorder this report?"))
            {
               myForm.elements[araProp+'['+num+'].'+checkBoxProp].checked = false;
               return;
            }
         }
         if (myForm.elements[araProp+'['+num+'].available'] && myForm.elements[araProp+'['+num+'].available'].checked==false)
         {
            myForm.elements[araProp+'['+num+'].available'].checked = true;
         }
         classQ[productClass-1].push(num);
      }
      else
      {
         // report was UNCHECKED: remove from queue
         classQ[productClass-1] = removeAra(classQ[productClass-1], num);
      }

      redrawClass(productClass);
   }

   function doPriority(num)
   {
      // grab product's class and new priority
      var productClass = myForm.elements[araProp+'['+num+'].productClassId'].value;
      var priority = myForm.elements[araProp+'['+num+'].priority'].selectedIndex + 1;
      // remove it from the queue
      classQ[productClass - 1] = removeAra(classQ[productClass - 1], num);
      // insert it into it's new position
      classQ[productClass - 1] = insertAra(classQ[productClass - 1], num, priority - 1);
      redrawClass(productClass);
   }

   // this function actually modifies the argument ara
   function removeAra(ara, value)
   {
      // create a dup ara but exclude value from it
      var tempAra = new Array();
      while(ara.length > 0)
      {
         var reportNum = ara.shift();
         if (reportNum != value)
            tempAra.push(reportNum);
      }
      return tempAra;
   }

   // this function actually modifies the argument ara
   function insertAra(ara, value, position)
   {
      // create a dup ara and copy over, while inserting in right position
      var tempAra = new Array();
      for (var i=0; i < position && ara.length > 0; i++)
      {
         tempAra.push(ara.shift());
      }
      tempAra.push(value);
      while(ara.length > 0)
      {
         tempAra.push(ara.shift());
      }

      return tempAra;
   }

   function countSelectedQ()
   {
      var count = 0;
      for (var i=0; i < classQ.length; i++)
      {
         count += classQ[i].length;
      }
      return count;
   }
