
// Optional utility function. Discussed later
function goTo(src) {
  window.location = src
}

// Returns a string filled with the specified number of spaces
function Spaces(size) {
 var str = ""
 for (;size>0;size--)
  str+=" "
 return str
}

// Creates a blank option
function BlankSpace(el, size) {
 var newItem = new Option
 newItem.text = Spaces(size)
 el[0] = newItem 
}

// Clears a list and adds a single empty item to retain the size.
function ClearList(el, end) {
 var cnt = el.options.length
 for (var i = cnt-1; i>=end; i--) el.options[i] = null
 el.selectedIndex = -1
 if ((el.options.length==0) && (arguments[2])) BlankSpace(el,arguments[2])
}

// Adds an item to the the list
// itemName - The text to display 
// itemValue - The internal value used with form submission or 
//   accessible through script
// itemChild - (Optional) A link to a child list
function OptionList_AddItem(itemName, itemValue, itemChild) {
 var newItem = new Object
 newItem.value = itemValue
 newItem.text = itemName
 newItem.child = itemChild
 this.items[this.items.length] = newItem    
}

// The onchange event handler for the list. Everytime an option
// is selected, check if there is a child list to populate.
function OptionList_Change(id) {
 var el = this.options[this.selectedIndex]
 if (el.child) el.child.Populate(id)      
}

// Fills the SELECT element from a list
function OptionList_Populate(ID) {
 var ct=0
 if (this.items.length==0) {
  BlankSpace(this.src, this.size)
  ct++
 }
 else {
  ct=this.items.length
  // Remove an leftover items
  ClearList(this.src, ct)
  for (var i = 0; i < ct; i++) {
   var newItem = new Option
   newItem.value = this.items[i].value
   newItem.text = this.items[i].text
   newItem.child = this.items[i].child
   this.src.options[i] = newItem
   if (ID == this.items[i].value){this.src.options[i].selected = true}
  }
 }
 
}

// The constructor for the option list
function OptionList(src,size) {
 src.OptionList = this
 this.src = src
 this.size = size
 this.onchange = this.src.onchange = OptionList_Change
 this.AddItem = OptionList_AddItem    
 this.items = new Array
 this.Populate = OptionList_Populate
}


