//
// NewListBox.js
//

function GetMultipleListBox(oListBox) {
   // step through each entry and see if it's selected
   oListBox = eval(oListBox);
   var len = oListBox.length;
   var str;
   var selectionString = "";
   var spaces="      ";

   for (i=0; i < len; i++) {
      if(oListBox.options[i].selected == true) {
         // add to string
         str = oListBox.options[i].value;
         if (str.length <5) {
            // pad with spaces to make it lengh 5
            str += spaces.substring(0, 5 - str.length);
            }
         else if (5< str.length) {
            // truncate to length 5
            str = str.substring(0,4);
            }
         selectionString += str;
        }
      }

   if(selectionString == "")
      selectionString = "-";
   return selectionString;
   }

function GetSelectedListBoxText(oListBox) {
   // step through each entry and see if it's selected
   oListBox = eval(oListBox);
   var vLabels = new String("");

   for (var i = 0; i < oListBox.length; i++) {
      if (oListBox.options[i].selected == true) {
         // add to string
         vLabels += oListBox.options[i].text + ", ";
        }
      }

   // Remove the last comma from the list
   var l = vLabels.length - 2;
   vLabels = vLabels.substring(0, l);

   return vLabels;
   }

function GetSelectedListBoxValues(oListBox, bQuoted) {
   // step through each entry and see if it's selected
   oListBox = eval(oListBox);
   var vLabels = new String("");

   if (bQuoted != true) {
      for (var i = 0; i < oListBox.length; i++) {
         if (oListBox.options[i].selected == true) {
            // add to string
            vLabels += oListBox.options[i].value + ", ";
           }
         }
      }
   else {
      for (var i = 0; i < oListBox.length; i++) {
         if (oListBox.options[i].selected == true) {
            // add to string
            vLabels += "'" + oListBox.options[i].value + "', ";
           }
         }
      }

   // Remove the last comma from the list
   var l = vLabels.length - 2;
   vLabels = vLabels.substring(0, l);

   return vLabels;
   }


function ListBoxGetValues(parameter){
   var temp1 = new Array(), j=0, i;

   for(i=0; i< parameter.length; i++){
      if(parameter[i].selected==true) {
         temp1[j]='"' + parameter[i].value + '"';
         j++;
         }
      }
   return(temp1);
   }

function SetBooleanListBox(fieldName, fieldValue) {
   if( fieldValue == "-" || fieldValue == " -" || fieldValue == " - " || 
      fieldValue == null || fieldValue == "" )  {

      eval("document.Form."  +fieldName+ ".selectedIndex=0"); //fieldValue = "-";
      return;
      }

   var 
      err = true,
      len = eval("document.Form." +fieldName+ ".length"),
      val,i;

   for (i=0; i< len; i++) {
      val = eval("document.Form." +fieldName+ ".options[" +i+ "].value");
      if (val == fieldValue) {
         // found value in list box so select it
         eval("document.Form."  +fieldName+ ".selectedIndex=" + i );
         err=false;
         break;
         }     
      }

   if (err) {
      alert("ERROR: "+fieldName+ "='"+ fieldValue +"'");
      }
   }

function SetListBox(fieldName, fieldValue) {
   fieldValue = StringTrimLeft(StringTrimRight(fieldValue));
   if( fieldValue == "-" || fieldValue == "" ) {
      eval("document.Form."  +fieldName+ ".selectedIndex=0"); 
      return;
      }    

   var err = true;
   var len = eval("document.Form." +fieldName+ ".length");
   var val;
   for (i=0; i< len && err; i++) {
      val = eval("document.Form." +fieldName+ ".options[" +i+ "].value");
      if (val.toUpperCase() == fieldValue) {
         // found value in list box so select it
         eval("document.Form."  +fieldName+ ".selectedIndex=" + i );
         err=false;
         }     
      }

   if (err) {
      alert("ERROR: "+fieldName+ "='"+ fieldValue +"'");
      }
   }

function SetMultipleListBox(fieldName, fieldValue) {
   if( fieldValue == "-" || fieldValue == " -" || fieldValue == " - " || 
      fieldValue == null || fieldValue == "" ) {

      // nothing to be selected
      eval("document.Form."  +fieldName+ ".selectedIndex=0");  // fieldValue = "-";
      return;
      }

   // extract out each 5 character field and save in an array
   var fields= new MakeArray(fieldValue.length/5 + 1);
   var fieldIdx=0;
   for(var idx = 0; idx < fieldValue.length; idx += 5) {
      fields[fieldIdx++] = StringTrimRight(fieldValue.substring(idx, idx+5));
      }      
 
   // if the field exists in the list box, select it
   var val;
   var len = eval("document.Form." +fieldName+ ".length");
   var i=0;
   for (i=0; i < len; i++) {
      // step through each item in the listbox
      val = eval("document.Form." +fieldName+ ".options[" +i+ "].value");

      // step through each entry in the array and compare to listbox entry
      for(var k=0; k< fieldIdx; k++) {
         if (StringTrimRight(val) == fields[k]) {  
            // if we get a match, deselect inital value which is '-'
            eval("document.Form." +fieldName+ ".options[" +0+ "].selected=false");
            // and then select the value found
            eval("document.Form." +fieldName+ ".options[" +i+ "].selected=true");            
            }
         }
      } 
   }

// make sure listbox selections don't exceed numSelections
function ValidateListBox(fieldName, numSelections) {
   // step through each entry and see if it's selected
   var len = eval("document.Form." +fieldName+ ".length");
   var count = 0;
 
   for (i=0; i < len; i++) {
      if(eval("document.Form." +fieldName+ ".options[" +i+ "].selected")) count++;
      }
        
   if(count > numSelections) { 
      alert("Please select no more than " + numSelections + " item from the listbox.");         
      return false;
      }
   return true;
   }

//-------------------------------------------------------------------
//
// SetListBox2
//
// Select the items in the listbox corresponding to the values
// in vValues. vValues may either be a single value string or
// an array of strings. This function does NOT reset any previous
// values in the listbox.
//
function SetListBox2(oListbox, vValues) {
//   vValues = eval(vValues);
   oListbox = eval(oListbox);  // Needed to force Netscape to treat as a select object

   if (typeof(vValues) == "object") {
      for (i = 0; i < vValues.length; i++) {
         for (j = 0; j < oListbox.options.length; j++) {
            if (vValues[i].toLowerCase() == oListbox.options[j].value.toLowerCase()) {
               oListbox.options[j].selected = true;
               break;
               }
            }
         }
      }
   else { // treat it as a string value
      for (i = 0; i < oListbox.options.length; i++) {
         if (vValues.toLowerCase() == oListbox.options[i].value.toLowerCase()) {
            oListbox.options[i].selected = true;
            break;
            }
         }
      } 
   }



function DeleteSelectedFromList(vTargetBox) {
   var iIdx = vTargetBox.selectedIndex;

   while (iIdx != -1) {
      vTargetBox.options[iIdx] = null;
      iIdx = vTargetBox.selectedIndex;
      }
   }

function DeleteOptionByValue(vTargetBox, szVal) {
   var i;
   
   for (i = 0; i < vTargetBox.options.length; i++) {
      if (vTargetBox.options[i].value == szVal) {
         vTargetBox.options[i] = null;
         }
      }
   }

function ScrollItem(vTargetBox, vDirection) {
   var i;
   if (vDirection == -1) {
      for(i = 0; i < vTargetBox.length; i++) {
         if(vTargetBox.options[i].selected) {
            fSwapItems(vTargetBox, i, i+vDirection);            
            }
         }
      }
   else {
      for(i = vTargetBox.length-1; i >= 0; i--) {
         if(vTargetBox.options[i].selected) {
            fSwapItems(vTargetBox, i, i+vDirection);            
            }
         }
      }
   }

// MoveItem()
//
// This function moves an item from one listbox to another
// listbox. It removes the item from the source listbox. It
// will move all selected items.
//
// vSource  --  The source listbox
// vTarget  --  The target listbox
//
function MoveItem(vSource, vTarget){
   var j;

   if (vSource.selectedIndex < 0) {   
      alert("No item is selected.");
      return false;
      }

   for (j = 0; j < vSource.length; j++) {
      if(vSource.options[j].selected) {
         vTarget.options[vTarget.length] = new Option(vSource.options[j].text,
                                                      vSource.options[j].value);
         vSource.options[j] = null;
         }
      }
   return true;
   }

// LoadListbox()
//
// This function is used client side to load the listbox values.   
// It clears the listbox and then loads the Option objects from the
// given array.
//
// aData    -- Array object holding Option objects
// oListbox -- Pointer to the listbox to load
//
function LoadListbox( aData, oListbox ) {
   aData = eval(aData);
   oListbox = eval(oListbox);  // Needed to force Netscape to treat as a select object
   oListbox.options.length = aData.length; // Clear the listbox
   
   for (i = 0; i < aData.length; i++) {
      // Hack Alert! Must create a new Option each time for Netscape, else if
      // you try to reload the same Option, it's undefined. Netscape seems to
      // consume it in the select box loading.
// !IE // oListbox.options[i] = new Option(aData[i].text, aData[i].value);
      oListbox.options[i].text  = aData[i].text;
      oListbox.options[i].value = aData[i].value;
      }
   }

// LoadListboxQuotes()
//
// This function is used client side to load the listbox values.   
// It clears the listbox and then loads the Option objects from the
// given array.
// The data elements will contain quotes.
//
// aData    -- Array object holding Option objects
// oListbox -- Pointer to the listbox to load
//
function LoadListboxQuotes( aData, oListbox ) {
   aData = eval(aData);
   oListbox = eval(oListbox);  // Needed to force Netscape to treat as a select object
   oListbox.options.length = aData.length; // Clear the listbox
   
   for (i = 0; i < aData.length; i++) {
      // Hack Alert! Must create a new Option each time for Netscape, else if
      // you try to reload the same Option, it's undefined. Netscape seems to
      // consume it in the select box loading.
// !IE // oListbox.options[i] = new Option(aData[i].text, aData[i].value);
      oListbox.options[i].text  = aData[i].text;
      oListbox.options[i].value = '"'+aData[i].value+'"';
      }
   }


function fSwapItems(vTargetBox, FirstItemNum, SecondItemNum) {
   // Check input parameters
   if((FirstItemNum < 0) || (FirstItemNum >= vTargetBox.options.length)) {
      return;
      }
   if((SecondItemNum < 0) || (SecondItemNum >= vTargetBox.options.length)) {
      return;
      }

   var vTempInfo = new cDataField(vTargetBox.options[FirstItemNum].text, 
                                  vTargetBox.options[FirstItemNum].value);
   var vTempSelected =            vTargetBox.options[FirstItemNum].selected;

   vTargetBox.options[FirstItemNum].text     = vTargetBox.options[SecondItemNum].text;
   vTargetBox.options[FirstItemNum].value    = vTargetBox.options[SecondItemNum].value;
   vTargetBox.options[FirstItemNum].selected = vTargetBox.options[SecondItemNum].selected;

   vTargetBox.options[SecondItemNum].text     = vTempInfo.pItemName;
   vTargetBox.options[SecondItemNum].value    = vTempInfo.pItemValue;
   vTargetBox.options[SecondItemNum].selected = vTempSelected;

   return;
   }

// The next 4 functions are located in ListBox.js because they manipulate Option
// arrays which is what the ListBox handles.

// GetText()
//
// This function will return the name associated with the
// given value. This function operates on an array of Option
// objects.
//
// aPairs  --  An array of Option objects.
// szValue  --  The name to key on.
//   
function GetText( aPairs, szValue ) {
   var i;
   
   for (i = 0; i < aPairs.length; i++) {
      if (szValue == aPairs[i].value) {
         return (aPairs[i].text);
         }
      }
   return "";  // Nothing found, return empty string.
   }

// WriteText()
//
// Write out the name associated with a given value.
//
function WriteText(aPairs, szValue) {
   document.write( GetText(aPairs, szValue) );
   }

// GetValue()
//
// This function will return the value associated with the
// given text.
//
// aPairs  --  An array of Option objects.
// szText  --  The text to key on.
//   
function GetValue( aPairs, szText ) {
   var i;
   
   for (i = 0; i < aPairs.length; i++) {
      if (szText == aPairs[i].text) {
         return (aPairs[i].value);
         }
      }
   return "";  // Nothing found, return empty string.
   }

// WriteValue()
//
// Write out the value associated with a given name.
//
function WriteValue(aPairs, szText) {
   document.write( GetValue(aPairs, szText) );
   }

//-------------------------------------------------
// Functions below this line should not be used anymore, but they're
// left here for now just in case.
//
///////////////////////////////// private
function MakeArray(n) {
   this.length = n;
   for(var k=1; k <=n; k++) {
      this[k]=0;
      }
   return this;
   }

//-------------------------------------------------
////////////// new functions
function cDataField(Name, Value, id) {
   this.pItemName   = Name;
   this.pItemValue  = Value;
   this.id          = id;
   }

function xxfBuildList(vSourceArray, vTargetBox) {
   vTargetBox.options.length = vSourceArray.length;
   for (i=0;i < vSourceArray.length;i++) {
      vTargetBox.options[i].selected = false; // Fix invalid selection status
      vTargetBox.options[i].value = vSourceArray[i].pItemValue;
      vTargetBox.options[i].text  = vSourceArray[i].pItemName;
      }
   }

function xxfBuildSearchListBox(vSourceArray, vTargetBox) {
   vTargetBox.options.length = vSourceArray.length;
   for (i=1; i< vSourceArray.length; i++) {
      vTargetBox.options[i].value = vSourceArray[i].pItemValue;
      vTargetBox.options[i].text  = vSourceArray[i].pItemName;
      }
   }

///////////////////////////////// private
function fFindInArray(vArray, vValue) {
   var vReturnBool = false;

   if (vValue == " Available Fields "){
      vReturnBool = true; // We do not want to move over the default value
   }
   else {
      for (i = 0; i < vArray.length; i++) {
         if (vArray[i].value == vValue){
            vReturnBool = true;
            }
         }
      }
   return vReturnBool;
   }

function fGetBoxList(vSourceBox) {
   var vReturnArray = new Array();

   for (i=0; i < vSourceBox.options.length; i++) {
      var vText       = vSourceBox.options[i].text;
      var vValue      = vSourceBox.options[i].value;
      vReturnArray[i] = new cDataField(vText,vValue);
      }
   return vReturnArray
   }

// Used by Search to keep search lists on the utility frame accurate.

function DelElementByValue(theArray, theValue) {
   if(theArray.length==0) return(theArray);

   var vText, vValue, ind;
   var vReturnArray = new Array();

   ind=0;
   while(ind< theArray.length) {
      if(theArray[ind].value!=theValue) {
         vText       = theArray[ind].text;
         vValue      = theArray[ind].value;
         vReturnArray[vReturnArray.length] = new Option(vText,vValue);
         }
      ind++;
      }
   return(vReturnArray);
   }
