// these vars are needed for the transfer
var targetValues;
var targetTexts;

// transferElements 
function transferElements(donor, target, bRemove){
  loadArrays(target);
  for (var x=(donor.length - 1); x >= 0; x--) {
    if (donor.options[x].selected ) {
      addIt = 1;
      for (var y=0; y < targetValues.length; y++) {
        if (donor.options[x].value == targetValues[y]) {
          addIt = 0;
          break;
        }
      }
      if (addIt == 1) {
        targetValues[targetValues.length] = donor.options[x].value;
        targetTexts[targetTexts.length] = donor.options[x].text;
      }
      if (bRemove) donor.remove(x);
    }
  }
  loadTarget(target);
}

// loadArrays
function loadArrays(target) {
  targetValues = new Array();
  targetTexts = new Array();
  for (var x=0; x < target.length; x++) {
    targetValues[x] = target.options[x].value;
    targetTexts[x] = target.options[x].text;
  }
}

// loadTarget - empties the target select element and then fills it using text and values stored in two arrays, also sorts the arrays before adding the values
function loadTarget(target){
  while (target.length > 0) target.remove(0);
  for (var x=0; x < targetValues.length; x++) target.options[x] = new Option(targetTexts[x], targetValues[x]);
}


// setTextValue - converts a multiple select element into a delimited text element
function setTextValue(element) {
  target = document.getElementById(element + "Box[]");
  if (target != null) {
    var newValue = new String("");
    for (var x=0; x < target.length; x++) {
      if (x != 0) newValue += ",";
      newValue += target.options[x].value;
    }
    document.getElementById(element).value = newValue;
  }
}

function giveWarningAndFixValue(field) { 
  // Removes all non-numeric values
  var value = parseInt(field.value); 
  if(!isNaN(value)) field.value = value; 
  else field.value = ""; 
  //used for NS4.7 because when changing value the cursor is placed in front of the value, instead of behind 
  if(document.layers) field.select(); 
} 

function isNumericValue(str) { 
  var isValid = true; 
  if (str.search(/^[0-9]+$/) == -1) { 
    isValid = false; 
  }
  return isValid; 
} 

function limitField(field) { 
  if(!isNumericValue(field.value)) { 
    giveWarningAndFixValue(field); 
  } 
}

function selectAll(i) {
  var list1 = i;
  for (var x=0; x < list1.length; x++) {
    if (list1.options[x].value != "") {
      list1.options[x].selected = true;
   }
  }
}
