/*
These functions assume a nested array of values has been generated
previously in the page to populate the pulldown menus.
That array might look like:

categories = new Array();
categories['BUILDINGS'] = new Array();
categories['BUILDINGS'][1] = new Array ("Church","4H");
categories['BUILDINGS'][2] = new Array ("City","4O");
categories['BUILDINGS'][3] = new Array ("Door","4P");
categories['BUILDINGS'][4] = new Array ("House","4T");
categories['BUILDINGS'][5] = new Array ("Window","4I");

...which would generate one category, "BUILDINGS",
with the option text displayed and the option value defined as
new Array ("Church","4H");
"Church" is option's displayed text and "4H" is the value of option

The call from a web form's pulldown menu, using the onchange
event handler, might look like:

onchange="popsubcat(this.options[this.selectedIndex].value,'subcat','theForm')"
*/

//Arguments: 
//menu = name of pulldown menu to clear
//fname = name of web form
function clearmenu(menu,fname) {
  //st = 1 keeps first menu option intact
  //st = 0 clears all menu options
  var st = 1;
  fieldobj = eval("document." + fname + "." + menu);
  if (navigator.appName.indexOf("Netscape") != -1) {
    while (fieldobj.length > st) {
      fieldobj.options[st] = null;
    }
  }
  if (navigator.appName.indexOf("Explorer") != -1) {
    while (fieldobj.length > st) {
      fieldobj.options.remove(st);
    }
  }   
}

//Arguments: 
//cat = option's displayed text
//val = options's value
//menu = name of pulldown menu
//c = index of pulldown menu to populate
//fname = form name
function popmenu (cat,val,menu,c,fname) {
  fieldobj = eval("document." + fname + "." + menu);
  if (navigator.appName.indexOf("Netscape") != -1) {
    fieldobj.options[c] = new Option(cat,val,false,false);
  }
  if (navigator.appName.indexOf("Explorer") != -1) {
    var el = document.createElement("OPTION");
    el.text = cat;
    el.value = val;
    fieldobj.options.add(el,c);
  }
}

//Arguments: 
//cat = category chosen in first pulldown menu
//menutoclear = second pulldown menu that responds to first menu
//fname = form name
function popsubcat(cat,menutoclear,fname) {
  //Clear the responding menu of present options first
  clearmenu(menutoclear,fname);
  for (c = 1; c < categories[cat].length; c++) {
    popmenu(categories[cat][c][0],categories[cat][c][1],menutoclear,c,fname);
  }
  c = 1;
}

function allsubcats() {
  ething = new Array();
  c = 0;
  for (cat in categories) {
    for (subcat in categories[cat]) {
      if (!ething[categories[cat][subcat][0]]) {
        ething[c] = categories[cat][subcat][0] + "," + categories[cat][subcat][1];
        c++;
      }
    }
  }
  ething.sort();
  elements = new Array();
  c = 1;
  for (e in ething) {
    elements = ething[e].split(",");
    popmenu(elements[0],elements[1],"subcat",c,"theForm");
    c++;
    elements = "";
  }
}

verbose = new Array();
verbose['Ministry Links'] = "M";
verbose['Secretary Links'] = "S";
verbose['Personal Links'] = "P";

verbose2 = new Array();
verbose2['M'] = "Ministry Links";
verbose2['S'] = "Secretary Links";
verbose2['P'] = "Personal Links";

//In each call to popmenu() below, the arguments passed are:
//(text of option,value of option,name of pulldown menu,index of option populated,name of form)

//Start with the option whose index is "1" (0 is the first option)
var c = 1;
for (title in categories) {
  popmenu(title,verbose[title],"category",c,"theForm");
  c++;
}
c = 1;

//Populate the subcategory menu with subcats from chosen category
if (incomingcat != "" && incomingcat != "A") {
  for (subcat in categories[verbose2[incomingcat]]) {
    popmenu(categories[verbose2[incomingcat]][c][0],categories[verbose2[incomingcat]][c][1],"subcat",c,"theForm");
    c++;
  }
}
//Populate subcat menu with all subcategories by default
else {
  allsubcats();
}
c = 1;