//Modified the initLists and fillBaseList functions to handle values in addition to text - Aug, 2001
//Added the getDefaultListItems() function - Aug 10, 2001

var v = false;

function get(elt)
{
   // loop thru document.forms property and exit w/ current element index

   var num = -1;
   for (var i = 0; i < elt.form.elements.length; i++) {
      if (elt.form.elements[i].name == elt.name) {
        num = i;  // save element index
        break;
      }
   }
   return num;  // returns current element index
}


function sindex(num, offset, elt)
{  //***modified next line to get the right element index***
   // sel finds selected index or value of num + offset's form element
   var sel = elt.form.elements[num+offset].selectedIndex;
   if (sel < 0) sel = 0;

   return sel;
}

function relate(elt, tree, depth)
{ // relate submenus based on sel of form - calls update to redef submenus
// elt - option list
//alert (tree);
// depth - starting at 1 for first list
   if (v) {
      var num = get(elt); // fetch the current elt index
      var a = tree;        // set a to be the tree array

      while (a != null && --depth != -1) {
        // traverse menu tree until we reach the corresponding option record
        a = a[sindex(num, -depth, elt)];
      }

      // at depth 3, should end up w/ something like a[i][j][k]
      // where each index holds the value of s(elected )index in related form select elts
      if (a != null && a.length) {
         // if a array exists and it has elements,
         // feed update() w/ this record reference
         update(num, elt, a);
         return;
      }
   }
   // if a hasn't any array elements or new Option unsupported then end up here ;)
   //jmp(form, elt); // make like a live popup
}


function update(num, elt, m)
{
   // updates submenus - element(num)'s menu options, and all submenus
   // if refd element exists

   if (num != -1) {
      num++; // reference next element, assume it follows in HTML
      //***modified next line to refer to elements within the same form***
      with (elt.form.elements[num]) {

	 // null out options in reverse order (bug work-around)
         for (var i = options.length - 1; 0 < i; i--) options[i] = null;

         // fill up next menu's items
         for (var i = 0; i < m.length; i++)
           options[i] = (m[i].value != null) ? new Option(m[i].text, m[i].value) : new Option(m[i].text);

         if (document.getElementById) {
             options.selectedIndex = 0;
         } else {
             options[0].selected = true; // default to 1st menu item, windows bug kludge
         }
      }
      if (m[0].length != 0) {
         update(num, elt, m[0]); // update subsequent form if any grandchild menu exists
      }
    }
}

// Internet Explorer 4+ bug fix:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each SELECT,
// so it gets it wrong.

function resetIE() {
   for (var i = 0; i < document.forms.length; i++) {
      document.forms[i].reset();
   }
}

if (document.all)
   window.onload = resetIE;

function initLists() {

  if (!v) return false;

  var args = initLists.arguments;
  var argLen = args.length - 1;
  var temp = new Array();
  var baseList = args[0];
  fillBaseList( baseList, args[1] );

  //this loop iterates through each 2nd argument (arrays)
  for(var arg=argLen; arg>2; arg-=2) {
    var list = args[arg-1];
    var a = args[arg];
    var newSubI = 0, oldSubI = 0;
    var subMenu = null;

    for(var i=0; i<a.length;i++) {
     if (a[i].length) {
        //create a new Option with the appropriate submenu
        subMenu = arg==argLen ? null : m[oldSubI++];
        temp[temp.length] = typeof(a[i]) == "object"
                            ? new O(a[i][0], a[i][1], subMenu )
           	                : new O(a[i]   , null   , subMenu );
	}
      else {
        temp = temp.sort(compare);
        m[newSubI++] = temp;
        temp = new Array();
      }
    }
    //reset the length of array
    m.length = newSubI;
  }

  //set up the listboxes
  relate(baseList, m, 1);

  return true;
}

function fillBaseList(elt, a) {
  if (v) {
    //create a new Option for each array element
    for(var i=0; i<a.length; i++)
    {
       a[i] = typeof(a[i]) == "object" ? new O( a[i][0], a[i][1], null ) : new O( a[i], null, null );
    }
    //fill the list
    // null out options in reverse order (bug work-around)
    for (var i = elt.options.length - 1; 0 < i; i--)
      elt.options[i] = null;

    // fill up menu's items
    for (var i = 0; i < a.length; i++) {
      elt.options[i] = a[i].value != null
                       ? new Option(a[i].text, a[i].value)
                       : new Option(a[i].text);
    }

    if (document.getElementById) {
        elt.options.selectedIndex = 0;
    } else {
        elt.options[0].selected = true; // default to 1st menu item, windows bug kludge
    }
  }
}

//this function is used to size the listboxes in Netscape
function getDefaultListItems()
{ 
  if (v && navigator.appName == 'Netscape')
  {
    var options = ""
    for (var i=0; i<10; i++) options += "<Option>.......................</Option>";

    return options;
  }
}

// Internet Explorer 4+ bug fix:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each SELECT,
// so it gets it wrong.
//
// Thanks to Peter Belesis (pbel@internet.com) for pointing this out.

function resetIE() {
   if (document.all) {
       for (var i = 0; i < document.forms.length; i++) {
          document.forms[i].reset();
       }
   }
}

if (document.all)
   window.onload = resetIE;

