function _AP_Reload(new_ma_tech_id,card_fmt_list) {
	clearLastFound();
	loadJavaScriptDoc( 'querystring', 'mainmenu.cgi', 'cmd=srv+srch/scrollfl_relate_reload.html&srch_ma_tech_id='+new_ma_tech_id + (typeof(additional_ajax_params) != 'undefined' && additional_ajax_params ? additional_ajax_params : '') + (typeof(card_fmt_list)!='undefined'?"&card_fmt_list="+card_fmt_list:'') );
}

//  pushes a number of parents and children to the list at once to define basic field relationships
function _AP()
{

   for( var i = 0; i < arguments.length; i+= 2 )
   {
      var found = -1;
      for( var j = 0; j < Parents.length; j++ )
      {
         if( Parents[j].field_id == arguments[i] )
         {
            found = j;
            break;
         }
      }
      if( found == -1 )
      {
         found = Parents.length;
         Parents.push( new ParentType( arguments[i] ) );
      }

      var thisparent = Parents[found];
      thisparent.values.push( new ParentItemType( '~', '~' ) ); //  add blank value to define the relationship between parent & child fields

      var thischildren = thisparent.values[thisparent.values.length-1].children;
      found = -1;
      for( var j = 0; j < thischildren.length; j++ )
      {
         if( thischildren[j].field_id == arguments[i+1] )
         {
            found = j;
            break;
         }
      }
      if( found == -1 )
      {
         found = thischildren.length;
         thischildren.push( new ChildType( thisparent.field_id, thisparent.values, arguments[i+1] ) );
      }
   }
}

function find_parent( field_id )
{
   for(var i=0; i < Parents.length; ++i)
   {
      var values = Parents[i].values;
      for(var j=0; j < values.length; ++j)
      {
         var children = values[j].children;
         for(var k=0; k < children.length; ++k)
         {
            if(children[k].field_id == field_id)
               return children[k].parent_field_id;
         }
      }
   }
   return false;
}

/*  Functions using field_id_list */

// Returns an array of select ids.
// If mode == 'id', it is assumed that search_str is the id of the parent select.
// Otherwise, it is assumed to be the field_id of the parent.

function find_children( search_str, mode ) {

    if (mode == 'id')
      search_str = getFieldID(search_str);

    if (!getID(search_str))
      return;

    var childFields = new Array();

    // Find the parent
    for (var x = 0; x < Parents.length; x++) {
      if (Parents[x].field_id == search_str) {

      // Then, get the field_id of each child
      for (var y = 0; y < Parents[x].values.length; y++) {
          if (Parents[x].values[y].children[0] && getID(Parents[x].values[y].children[0].field_id))
            childFields[childFields.length] = getID(Parents[x].values[y].children[0].field_id);
      }
      break;
     }
    }

    return childFields;
}

function getParentObject(field_id) {
  var parent_field_id = find_parent(field_id);
  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][1] == parent_field_id)
      return document.getElementById(field_id_list[x][0]);
  }
  return false;
}

function getParentID(field_id) {
  var parent_field_id = find_parent(field_id);
  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][1] == parent_field_id)
      return field_id_list[x][0];
  }
  return false;
}

function getID(field_id) {
  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][1] == field_id)
      return field_id_list[x][0];

  }
  return false;
}

function getFieldID(real_id) {
  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][0] == real_id)
      return field_id_list[x][1];

  }
  return false;
}


function getObject(field_id) {
  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][1] == field_id)
      return document.getElementById(field_id_list[x][0]);
  }
  return false;
}

// Adds a new item to the field_id_list.
// Saves the options of the added item for restoration later, if desired.
function fieldIDListAdd(field_id, real_id) {
  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][1] == field_id) {
        return
    }
  }

  field_id_list[field_id_list.length] = new Array(3);
  field_id_list[field_id_list.length - 1][0] = real_id;
  field_id_list[field_id_list.length - 1][1] = field_id;
  field_id_list[field_id_list.length - 1][2] = new Array();

  if (document.getElementById(real_id)) {
	  var childOpts = document.getElementById(real_id).options;
	  for (var x = 0; x < childOpts.length; x++) {
	    field_id_list[field_id_list.length - 1][2][x] = new Object();
	    field_id_list[field_id_list.length - 1][2][x].inner = childOpts[x].innerHTML;
	    field_id_list[field_id_list.length - 1][2][x].val = childOpts[x].value;
	  }
	}

}

// Returns the html for the options of the select element to be restored.
// Return value expected to be assigned to select.innerHTML
function fieldIDListRestoreOptions(real_id) {

  var scrollsel = document.getElementById(real_id);

  if (!scrollsel)
  	return;

	var selArray = new Array();
	for (var x = 0; x < scrollsel.options.length; x++) {
		if (scrollsel.options[x].selected)
			selArray.push(scrollsel.options[x].value);
	}

  scrollsel.innerHTML = '';

  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][0] == real_id) {
      for (var y = 0; y < field_id_list[x][2].length; y++) {
      	var newOpt = document.createElement('option');
      	newOpt.value = field_id_list[x][2][y].val;
      	newOpt.innerHTML = field_id_list[x][2][y].inner;
      	for (var z = 0; z < selArray.length; z++) {
      		if (selArray[z] == newOpt.value) {
      			newOpt.selected = true;
      			break;
      		}
      	}
      	scrollsel.appendChild(newOpt);
      }
      break;
    }
  }
}

// Removes an element from field_id_list.
// If mode is id, compare string should the the id of the element to be removed.
// Otherwise, compare_str should be field_id
function fieldIDListRemove(compare_str, mode) {
  var newFieldIDList = new Array();

  var check_index = 1;
  if (mode == 'id') {
    check_index = 0;
  }

  for (var x = 0; x < field_id_list.length; x++) {
    if (field_id_list[x][check_index] != compare_str) {
      newFieldIDList[newFieldIDList.length] = new Array(2);
      newFieldIDList[newFieldIDList.length - 1][0] = field_id_list[x][0];
      newFieldIDList[newFieldIDList.length - 1][1] = field_id_list[x][1];
      newFieldIDList[newFieldIDList.length - 1][2] = new Array();
      for (var y = 0; y < field_id_list[x][2].length; y++) {
      	newFieldIDList[newFieldIDList.length - 1][2][y] = new Object();
      	newFieldIDList[newFieldIDList.length - 1][2][y].inner = field_id_list[x][2][y].inner;
      	newFieldIDList[newFieldIDList.length - 1][2][y].val = field_id_list[x][2][y].val;

      }
    }
  }

  field_id_list = newFieldIDList;

}

/* End functions for field_id_list */


function ParentType( field_id )
{
   this.field_id = field_id;
   this.values = new Array();  //  Array of ParentItemType
   this.empty = false;
}

function ParentItemType( value, description )
{
   this.value = value;
   this.value_dbquote = quote + dbquotes( value ) + quote;
   this.description = description;
   this.children = new Array();  //  Array of ChildType
}

function ChildType( parent_field_id, parent_value_obj, field_id )
{
   this.parent_field_id = parent_field_id;
   this.parent_value_obj = parent_value_obj;
   this.field_id = field_id;
   this.items = new Array(); //  array of ChildItemType
}

function ChildItemType( value, description )
{
   this.value = value;
   this.value_dbquote = quote + dbquotes( value ) + quote;
   this.description = description;
}

// Insert a new ChildItem into arr, if a ChildItem with the same value
// doesn't already exist. Assumes arr is sorted, and uses binary
// search to find the correct insertion point.
function insertChildItem( arr, new_item )
{
   var l = -1;
   var r = arr.length;
   while(r - l > 1)
   {
      var mid = Math.floor((r + l)/2);
      if(new_item.value > arr[mid].value)
         l = mid;
      else if(new_item.value < arr[mid].value)
         r = mid;
      else
      {
         r = mid;
         break;
      }
   }

   if(arr[r] == new_item)
      return;
   arr.splice(r, 0, new_item);
}

var last_found_parent_field_id = '';
var last_found_parent_field_id_index = -1;

var last_found_parent_value = '';
var last_found_parent_value_index = -1;

var last_found_child_field_id = '';
var last_found_child_field_id_index = -1;

function clearLastFound() {
	last_found_parent_field_id = '';
	last_found_parent_field_id_index = -1;
	last_found_parent_value = '';
	last_found_parent_value_index = -1;
	last_found_child_field_id = '';
	last_found_child_field_id_index = -1;
}

//  adds a new related field to the array
function N( repeat )
{
   if( repeat ) {
        AddNewChildItem(e,f);
   }
   else {
        AddNew(a,b,c,d,e,f);
   }
}

function AddNew( parent_field_id, child_field_id, parent_value, parent_description, child_value, child_description )
{
   var new_found_parent_field_id_index = -1;
   var new_found_parent_value_index = -1;
   var new_found_child_field_id_index = -1;

   var found = -1;
   if( parent_field_id != last_found_parent_field_id )
   {
      for( var i = 0; i < Parents.length; i++ )
      {
         if( parent_field_id == Parents[i].field_id )
         {
            found = i;
            break;
         }
      }
      if( found == -1 )
      {
         found = Parents.length;
         Parents.push( new ParentType( parent_field_id ) );
      }
   }
   else
      found = last_found_parent_field_id_index;

   new_found_parent_field_id_index = found;

   var parent = Parents[found];

   var values = parent.values;
   found = -1;
   var value;

   if( parent_field_id == last_found_parent_field_id && parent_value == last_found_parent_value )
   {
      found = last_found_parent_value_index;
      value = values[found];
   }
   else
   {
      for( var i = 0; i < values.length; i++ )
         if( parent_value == values[i].value )
         {
            found = i;
            break;
         }
      if( found == -1 )
      {
         found = values.length;
         values.push( new ParentItemType( parent_value, parent_description ) );
      }
      value = values[found];
   }
   new_found_parent_value_index = found;

	 if (!value || !value.children) return;

   var children = value.children;

   found = -1;
   var child;
   if( parent_field_id == last_found_parent_field_id && parent_value == last_found_parent_value &&
       child_field_id == last_found_child_field_id )
   {
      found = last_found_child_field_id_index;
      child = children[found];
   }
   else
   {
      for( var i = 0; i < children.length; i++ )
         if( child_field_id == children[i].field_id )
         {
            found = i;
            break;
         }
      if( found == -1 )
      {
         found = children.length;
         children.push( new ChildType( parent.field_id, value, child_field_id ) );
      }
      child = children[found];
   }
   new_found_child_field_id_index = found;

   insertChildItem(child.items, new ChildItemType( child_value, child_description ) );

   last_found_parent_field_id = parent_field_id;
   last_found_parent_field_id_index = new_found_parent_field_id_index;

   last_found_parent_value = parent_value;
   last_found_parent_value_index = new_found_parent_value_index;

   last_found_child_field_id = child_field_id;
   last_found_child_field_id_index = new_found_child_field_id_index;
}

// Performs same function as AddNew() when only the child
// value/description pair has changed
function AddNewChildItem( child_value, child_description )
{
   var values = Parents[last_found_parent_field_id_index].values;
   var children = values[last_found_parent_value_index].children;
   var child = children[last_found_child_field_id_index];

   insertChildItem(child.items, new ChildItemType( child_value, child_description ) );
}

function ItemType( parent_field_id, child_field_id, parent_value, parent_description, child_value, child_description )
{
   this.parent_field_id = parent_field_id;
   this.child_field_id = child_field_id;
   this.parent_value = parent_value;
   this.parent_value_dbquote = quote + dbquotes(parent_value) + quote;
   this.parent_description = parent_description;
   this.child_value = child_value;
   this.child_value_dbquote = quote + dbquotes(child_value) + quote;
   this.child_description= child_description;
}

function Child( parent_field_id, field_id )
{
   this.parent_field_id = parent_field_id;
   this.field_id = field_id;
   this.items = new Array();
}

function ChildItem( value, description, value_dbquote )
{
   this.value = value;
   this.value_dbquote = value_dbquote;
   this.description = description;
}

function AddChild( children, parent_field_id, field_id, value, description, value_dbquote )
{
   var found = -1;
   for( var i = 0; i < children.length; i++ )
      if( children[i].field_id == field_id  )
      {
         found = i;
         break;
      }
   if( found == -1 )
   {
      found = children.length;
      children[children.length] = new Child( parent_field_id, field_id );
   }
   children[found].items[children[found].items.length] = new ChildItem( value, description, value_dbquote );
}

function childitemsort( a, b )
{
   if( a.description < b.description )
      return -1;
   if( a.description > b.description )
      return 1;
   return 0;
}

//  Applies DB2-style quotes to a string if this is not brokerload code (and therefore is searching code).
//   Example: Fred's car
//   Becomes: Fred''s car
function dbquotes( str )
{
   return (brokerload_scrollfl_relate ? str : str.replace(/\'/g,"''") );
}

var check_before_init = false;
// readyToFireOnChangeEvent is used by brokerload/changelisting/step2.html (also: brokerload/addlisting/step1a.html) in the body onload to indicate when it is OK to start processing the onchange event caused by clicking on the select input.
// Internet Explorer fires the onChange event when the javascript updates the select inputs. Other browsers do not. Adding this will prevent this in any browser.
var readyToFireOnChangeEvent = true;


function loadField_done( field_id, called_from_onchange )
{
   //alert( 'done: ' + field_id );
   RelatedFields.push( field_id );

   if (use_field_id_list == true) {
    var select_id = getID(field_id);
    check_related( document.getElementById( select_id ), called_from_onchange, select_id);

    if (loadFieldFinishFunc)
      loadFieldFinishFunc(select_id);
   }
    else
     check_related( document.getElementById( field_id ), called_from_onchange );
}

function addifunique( arr, item )
{
   for( var i = 0; i < arr.length; i++ )
      if( arr[i] == item )
         return;

   arr.push( item );
}

function CombinedType( field_id )
{
   this.field_id = field_id;
   this.items = new Array(); //  array of CombinedItemType
}

function CombinedItemType( value, value_dbquote, description )
{
   this.value = value;
   this.value_dbquote = value_dbquote;
   this.description = description;
}

// real_id is optional, and will only be used if use_field_id_list == true
function check_related( select_control, called_from_onchange, real_id )
{

   if( called_from_onchange && !readyToFireOnChangeEvent )
      return;

   var card_fmt_obj = false;

   if( brokerload_update_listing )
   {
      var card_fmt = getSelectedCardFmt();
      card_fmt_obj = findCardFmtObj( card_fmt );
   }

   if( !select_control )
      return;

   var select_control_id = (use_field_id_list != true) ? select_control.id : real_id;
   var select_control_field_id;

   if (use_field_id_list != true)
    select_control_field_id = select_control.id;
   else {
    for (var x = 0; x < field_id_list.length; x++) {
      if (real_id == field_id_list[x][0]) {
        select_control_field_id = field_id_list[x][1];
      }
    }
   }

   var found = -1;
   var select_control_type = select_control.type;
   for( var i = 0; i < Parents.length; i++ )
      if( Parents[i].field_id == select_control_field_id )
      {
         found = i;
         break;
      }

   if( found == -1 )
   {
      //  if the field is not found in the parent list, the field is not related; exit.  Uncomment next line to send a request for any scroll field, empty or not.
      //loadField( select_control_id, select_control );
      return;
   }

   if( Parents[found].empty )
   {
      //  Field is designated as empty. This occurs when a request is sent to the server to find related scrolls for the field, and there are none.
      return;
   }
   var select_copy = new Array();
   var select_one_copy = '', select_one_selectedIndex = -1;

   if( select_control_type == 'select-multiple' )
   {
      var o = select_control.options;
      var len = o.length;
      for( var j = 0; j < len; j++ )
         if( o[j].selected )
            select_copy.push( o[j].value );
   }
   else if( select_control && select_control.selectedIndex != -1 && select_control.options )
      select_copy.push( select_control.options[select_control.selectedIndex].value );

   //  See if the selected value(s) in the select box are in the value list; if not, fetch them
   var values = Parents[found].values;
   var notfoundarr = new Array();
   for( var i = 0; i < select_copy.length; i++ )
   {
      var foundval = false;
      for( var j = 0; j < values.length; j++ )
      {
         if( values[j].value == select_copy[i] )
         {
            foundval = true;
            break;
         }
      }
      if( !foundval )
         notfoundarr.push( select_copy[i] );
   }

   //  Are there any values that haven't been loaded? If so, fetch them.
   if( notfoundarr.length > 0 )
   {
      loadField( select_control_field_id, select_control, notfoundarr, called_from_onchange );
      return;
   }
   var ThisParent = Parents[found];


   //  This flag is set to true when called from a search screen, so that related
   //  fields don't fire until they are clicked on, unless the field has at least
   //  one selected item.
   if( check_before_init && select_control.selectedIndex == -1 )
      return;

   //  look to see if this field has any child fields
   var children = new Array(); //  array of arrays
   var allchildren = new Array();
   var not_in_list = new Array(); //  used to store the item that starts with "Not in list: " for editing purposes, if it exists


   if( select_control_type == 'select-multiple' )
   {
      var values = ThisParent.values;
      for( var j = 0; j < values.length; j++ )
      {
         var value = values[j];
         for( var k = 0; k < select_copy.length; k++ )
         {
            if( value.value_dbquote == select_copy[k] )
               children.push( value.children );
         }
         for( var k = 0; k < value.children.length; k++ )
            addifunique( allchildren, value.children[k].field_id );
      }
   }
   else if( select_control_type == 'select-one' )
   {
      select_one_selectedIndex = select_control.selectedIndex;
      if( select_one_selectedIndex != -1 )
      {
         select_one_copy = select_control.options[select_one_selectedIndex].value;
         var values = ThisParent.values;
         for( var j = 0; j < values.length; j++ )
         {
            var value = values[j];
            if( value.value_dbquote == select_one_copy )
               children.push( value.children );

            for( var k = 0; k < value.children.length; k++ ){
            	addifunique( allchildren, value.children[k].field_id );
            }
         }
      }
   }

   //  expand the subchildren into a single array
   var expchildren = new Array();

   for( var i = 0; i < children.length; i++ )
   {
      var child = children[i];
      for( var j = 0; j < child.length; j++ )
         expchildren.push( child[j] );
   }

   var item_fields = new Array();//  array of CombinedType()

   for( var i = 0; i < expchildren.length; i++ )
   {
      var found = -1;
      for( var j = 0; j < item_fields.length; j++ )
      {
         if( expchildren[i].field_id == item_fields[j].field_id )
         {
            found = j;
            break;
         }
      }
      if( found == -1 )
      {
         found = item_fields.length;
         item_fields.push( new CombinedType( expchildren[i].field_id ) );
      }
      var item_field = item_fields[found];
      var items = item_field.items;
      var expitems = expchildren[i].items;

      for( var j = 0; j < expitems.length; j++ )
      {
         found = false;
         for( var k = 0; k < items.length; k++ )
         {
            if( expitems[j].value == items[k].value )
            {
               found = true;
               break;
            }
         }
         if( !found )
            items.push( new CombinedItemType( expitems[j].value, expitems[j].value_dbquote, expitems[j].description ) );
      }
   }

   //  If there are no children, blank out all related subscroll lists
   if( item_fields.length == 0 )
   {
      if( allchildren.length == 0 )
         return;

      //  Blank out those child lists with no related parent item
      for( var i = 0; i < allchildren.length; i++ )
      {
         var child_sel_id = (use_field_id_list != true) ? allchildren[i] : field_id_list[allchildren[i]];
         var child_sel = document.getElementById( child_sel_id ); //.field_id );
         if( child_sel && child_sel.options && !isLookupField(child_sel.name))
         {
            if( brokerload_scrollfl_relate )
            {
               // leave one blank choice, and call check_related() on
               // the child field so the field clearing cascades down all the related
               // fields
               var old_child_val = child_sel.options[0].value;
               var old_child_txt = child_sel.options[0].text;
               var retain_old_child = (scrollfl_ma_tech_id == "x'20060523190739851043000000'");
               	
               // circular lookups can cause Lookup fields used by RVA and CBR to be 
               // cleared when they shouldn't be. This check added for CUR-5824	
               if (retain_old_child && isLookupField(child_sel.name) && old_child_val == '' && old_child_txt == '')
               		continue;
               	
               	child_sel.innerHTML = '';
				child_sel.options.length = 1;
				child_sel.options[0].text = '';
				child_sel.options[0].value = '';
               	
               	
               	
				

               if( brokerload_update_listing )
               {
                  Listing[ allchildren[i] ] = '';
                  if( card_fmt_obj )
                  {
                     var fields = card_fmt_obj.allfields;
                     for( var j = 0; j < fields.length; j++ )
                     {
                        if( fields[j].field_id == allchildren[i] )
                        {
                         //  erase all items in the field list
                           var items = fields[j].items;
                           items.splice( 0, items.length );
                           // add a blank item
                           fields[j].AddItem( '', '', 'Y' );
                        }
                        
                        if( fields[j].type == 'L' && retain_old_child )
                        {
                           fields[j].AddItem( old_child_val, old_child_txt, 'Y');
                           child_sel.options[1] = new Option(old_child_val, old_child_txt);
                           child_sel.selectedIndex = 1;
                           Listing[ allchildren[i] ] = old_child_val;
                        }
                     }
                  }
               }
				
          		check_related( child_sel, called_from_onchange);


            }
            else
            {
               child_sel.innerHTML = '';
               child_sel.options.length = 0;

               if( brokerload_update_listing )
               {
                  Listing[ allchildren[i] ] = '';
                  if( card_fmt_obj )
                  {
                     var fields = card_fmt_obj.allfields;
                     for( var j = 0; j < fields.length; j++ )
                     {
                        if( fields[j].field_id == allchildren[i] )
                        {
                           // erase all items in the field list
                           var items = fields[j].items;
                           items.splice( 0, items.length );
                        }
                     }
                  }
               }
            }
         }
      }
      return;
   }


   /*  This will show an alert with every child field and item in the list
   var s = new Array();
   for( var i = 0; i < item_fields.length; i++ )
   {
      s.push('\n\n' + item_fields[i].field_id + '\n---------\n' );
      for( var j = 0; j < item_fields[i].items.length; j++ )
      {
         s.push( (j > 0 ? ', ' : '') + item_fields[i].items[j].value );
      }
   }
   alert( s.length + '\n' + s.join('') );
   //  */

   //  Sort expanded children list by field_id, then combine fields into a single array
   //  Populate the child scroll list

   for( var i = 0; i < item_fields.length; i++ )
   {
      var child = item_fields[i];
      if (use_field_id_list == true) {
         if (!document.getElementById(getID(child.field_id)))
             continue;
      }
      not_in_list.length = 0;
      var selected_items = new Array();
      var selected_items_desc = new Array();
      selected_items.length = 0;
      selected_items_desc.length = 0;

      var child_sel_id;
      if (use_field_id_list != true)
        child_sel_id = child.field_id;
      else {
        for (var ix = 0; ix < field_id_list.length; ix++) {
          if (field_id_list[ix][1] == child.field_id) {
            child_sel_id = field_id_list[ix][0];
            break;
          }
        }
      }

      var child_sel = document.getElementById( child_sel_id );   
      if( child_sel && child_sel.options )
      {
         //  Sort the child item list
         child.items.sort( childitemsort );

         if( child_sel.type == 'select-one' )
         {
				if( brokerload_add_listing && Listing[child_sel_id] )
				{
					selected_items[selected_items.length] = Listing[child_sel_id];
					selected_items_desc[selected_items_desc.length] = Listing[child_sel_id];
				}
            else if( child_sel.selectedIndex != -1 )
            {
               selected_items[selected_items.length] = child_sel.options[child_sel.selectedIndex].value;
               selected_items_desc[selected_items_desc.length] = child_sel.options[child_sel.selectedIndex].text;
            }
            if( !brokerload_add_listing )
            {
               for( var j = 0; j < child_sel.options.length; j++ )
               {
                  if( child_sel.options[j].text.indexOf( 'Not in list: ' ) != -1 )
                  {
                     not_in_list[0] = child_sel.options[j].value;
                     not_in_list[1] = child_sel.options[j].text;
                  }
               }
            }
         }
         else //  select-multiple
         {
            if( child_sel.selectedIndex != -1 )
            {
               selected_items[selected_items.length] = child_sel.options[child_sel.selectedIndex].value;
               selected_items_desc[selected_items_desc.length] = child_sel.options[child_sel.selectedIndex].text;
            }
            var o = child_sel.options;
            var len = o.length;
            for( var j = 0; j < len; j++ )
            {
               if( o[j].selected )
               {
                  selected_items.push( o[j].value );
                  selected_items_desc.push( o[j].text );
               }
               if( o[j].text.indexOf( 'Not in list: ' ) != -1 )
               {
                  not_in_list[0] = o[j].value;
                  not_in_list[1] = o[j].text;
               }
            }

         }
         child_sel.innerHTML = ''; // erase all items; equivalent to child_sel.options.length = 0, except much faster

         var field = false;
         if( brokerload_update_listing && card_fmt_obj )
         {
            var fields = card_fmt_obj.allfields;
            for( var j = 0; j < fields.length; j++ )
            {
               if( fields[j].field_id == child.field_id )
               {
                  field = fields[j];
                  // erase all items in the field list
                  var items = field.items;
                  items.splice( 0, items.length );
                  // add a blank item
                  if( brokerload_scrollfl_relate && ( child.items.length == 0 || (child.items.length > 0 && child.items[0].description != '') ) )
                     field.AddItem( '', '', 'N' );
               }
            }
         }

         //  add a blank entry if brokerload_scrollfl_relate is true and the first item isn't already blank
         if( brokerload_scrollfl_relate && (child.items.length == 0 || (child.items.length > 0 && child.items[0].description != '' ) ) )
            child_sel.options[child_sel.options.length] = new Option( '', quote + quote);

         var newlist = new Array();
         for( var j = 0; j < child.items.length; j++ )
         {
            var found = false;
            var item = child.items[j];
            var newvalue = item.value_dbquote;
            for( var k = 0; k < newlist.length; k++ )
            {
               if( newlist[k].value == newvalue && newlist[k].description == item.description )
               {
                  found = true;
                  break;
               }
            }
            if( !found )
            {

               newlist[newlist.length] = new ChildItem( newvalue, item.description, item.value_dbquote );
               if (brokerload_update_listing && not_in_list[0] == newvalue) not_in_list.length=0;
               if( item.description.indexOf( 'Not in list: ' ) != -1 )
                  not_in_list.length = 0;
               var l = child_sel.options.length;
               var newdescription = item.description.trim();
               child_sel.options[l] = new Option( newdescription, newvalue );

               var is_selected = false;
               for( var k = 0; k < selected_items.length; k++ )
                  if( selected_items[k].toUpperCase().trim() == newvalue.toUpperCase().trim() )
                  {
                     is_selected = true;
                     break;
                  }
               if( is_selected )
               {
                  child_sel.options[l].selected = true;
                  if( brokerload_update_listing && card_fmt_obj )
                  {
                     field.AddItem( newvalue, newdescription, 'Y' );
                     Listing[ child.field_id ] = newvalue;
                  }
               }
               else if( !is_selected && brokerload_update_listing && card_fmt_obj )
					{
                  field.AddItem( newvalue, newdescription, 'N' );
					}
            }
         }

         //  Make sure that the first item in selected_items[] is added to the child_sel if this function is not called from an onchange event
         //  Added "!brokerload_add_listing" condition to fix a related scroll error with Add Listings - added 10-3-2005 by Brandon Medenwald (AKMLS-41, AKMLS-51)

         if( brokerload_scrollfl_relate && !called_from_onchange && !brokerload_add_listing )
         {
            if( selected_items.length > 0 )
            {
               var found = false;
               for( var j = 0; j < newlist.length; j++ )
               {
                  if( newlist[j].value == selected_items[0] )
                  {
                     found = true;
                     break;
                  }
               }
               if( !found && selected_items_desc[0].indexOf("Not in list")!=-1)
               {
                  var l = child_sel.options.length;
                  child_sel.options[l] = new Option( selected_items_desc[0], quote + dbquotes(selected_items[0]) + quote );
                  child_sel.selectedIndex = l;
               }
            }
         }

         //  if there is a "Not in list: " item, add it and select it
         if( not_in_list.length == 2 )
         {
            var l = child_sel.options.length;
            child_sel.options[l] = new Option( not_in_list[1], quote + dbquotes(not_in_list[0]) + quote );
            child_sel.selectedIndex = l;
            if( brokerload_update_listing && card_fmt_obj )
            {
               field.AddItem( not_in_list[0], not_in_list[1], 'Y' );
               Listing[ child.field_id ] = not_in_list[0];
            }
         }

         //  if we're in brokerload, there are only 2 items in the
         // list, the first is blank, and the brokerload_1child_sel is
         // true, select the second item
         if( brokerload_scrollfl_relate && child_sel.options.length == 2 &&
             child_sel.options[0].text == '' && brokerload_1child_sel ) {
            child_sel.selectedIndex = 1;
            // The child_sel is changing, so fire onchange.
            try {
            	if (child_sel.onchange)
            		child_sel.onchange();
            } catch(e){}
         }

         //  Make sure Listing object is up to date
         if( brokerload_update_listing ) {
            Listing[ child.field_id ] = child_sel.options[child_sel.selectedIndex].value;
            if (calc_millage && ('Y' == gbl_millage_auto_calc || called_from_onchange))
            	calc_millage(child_sel);
         }

         // Fire the related fields for this child, if there are any
         check_related( child_sel, called_from_onchange );
      }
   }
}

function explain( obj, text )
{
   var str = text + '\n--------\n';
   var i = 0;
   for( prop in obj )
   {
      i++;
      str += 'name: ' + prop + '; value: ' + obj[prop] + '\n';
      if( i > 10 )
      {
        alert( str );
        i = 0;
        str = '';
      }
   }

   alert( str );
}

var quote = '';

//  called from body onload or some other similiar page init event
function init_related_items( new_quote )
{
   //  allow the value of the select box to contain a quote wrapped around it
   if( new_quote && new_quote != '' )
      quote = new_quote;

   for( var i = 0; i < Parents.length; i++ )
   {
      var select_control = thisform[Parents[i].field_id];

      if( select_control && select_control.length ) // returned an array; find the "select"
      {
         var found = false;
         for( var j = 0; j < select_control.length; j++ )
            if( select_control[j].type == 'select-one' || select_control[j].type == 'select-multiple' )
            {
               found = select_control[j];
               break;
            }
         if( found )
            select_control = found;
      }

      if( select_control )
		{
        check_related( select_control, false );
		}
   }
}

/* (AKPC-206/NEF-138)
   Called from onchange of select-one select box to save chosen value
   for use by recover_saved() to restore correct values on "Back"
   button */
function save_chosen( select_box )
{
   var hidden_input = document.getElementById('chosen_' + select_box.name);
   if(hidden_input)
      hidden_input.value = select_box.value;
}

/* Called from onload or other page init event to restore values
   in select-one select boxes. Needed because IE uses only the
   selectedIndex to set the selected item in select box when
   "Back" button is used.*/
function recover_saved()
{
   for(var j=0; j < thisform.elements.length; ++j)
   {
      var control = thisform.elements[j];
      var saved_input = document.getElementById('chosen_' + control.name);
      if(!saved_input)
         continue;

      var found = -1;
      for(var i=0; i < control.options.length; ++i)
      {
         if(control.options[i].value == saved_input.value)
         {  found = i;
            break;
         }
      }

      if(found >= 0)
         control.selectedIndex = found;
   }
}


function isLookupField (field_id){
	if (typeof(findCardFmtObj) == 'undefined') return false;
	var card_fmt_obj  = findCardFmtObj( getSelectedCardFmt() );
	if( card_fmt_obj ) {
		for( var i = 0; i < card_fmt_obj.allfields.length; i++ ){
			var f = card_fmt_obj.allfields[i];
			if( f.table_name == 'list' && f.field_id == field_id && f.type == 'L')
				return true
			else if (f.table_name == 'list' && f.field_id == field_id && f.type != 'L')
				return false;
			else continue;
		}
	}
	return false;
}
