// Author: Sven Weih

// ===============================================================================================================================
// MyOption
// ===============================================================================================================================
function MyOption(name, value, selected, optionIsSet) {
  this.name = name;
  this.value = value;
  this.selected = selected;
  this.optionIsSet = optionIsSet;
}

// ===============================================================================================================================
// myOptionSort
// ===============================================================================================================================
function myOptionSort(a, b) {
  result = 0;
  if (a.name < b.name) {
    result = -1;
  } else {
    result = 1;
  }
  return result;
}

// ===============================================================================================================================
// SelectBox
// ===============================================================================================================================
function MySelectBox() {
  this.unSelectedOptions = new Array(); 
  this.selectedOptions = new Array(); 
  this.unSelectedBoxSorted = true; 
  this.optionsEnabled = false;
  this.optionsElement = null;
  
  // FunnktionReferenzierung
  this.addOption = addOption;
  this.fillSelectBoxUnselected = fillSelectBoxUnselected;
  this.fillSelectBoxSelected = fillSelectBoxSelected;
  this.sortUnselectedBox = sortUnselectedBox;
  this.clearBox = clearBox;
  this.selectOptions = selectOptions;
  this.removeOptions = removeOptions;
  this.addKey = addKey;
  this.removeKey = removeKey;
  this.moveUp = moveMyOptionUp;
  this.moveDown = moveMyOptionDown;
  this.saveData = saveData;
  this.enableOptions = enableOptions;
  this.setOptionsBox = setOptionsBox;
  this.optionsBoxClick = optionsBoxClick;
  this.findOption = findOption; 
  this.drawOptionsBox = drawOptionsBox;
}


// ===============================================================================================================================
// setOptionsBox
// ===============================================================================================================================
function setOptionsBox(source) { 
 if (this.optionsEnabled) {
   for (i=0; i < source.options.length; i++) {
      if (source.options[i].selected) {
        theOption = this.findOption(source.options[i].value, this.selectedOptions);
        if (theOption.optionIsSet) {
          this.drawOptionsBox(1);
          return true;
        } else {
          this.drawOptionsBox(0);
          return true;
        }
      }
    }  
    this.drawOptionsBox(2);
  }
}

// ===============================================================================================================================
// setOptionsBox
// ===============================================================================================================================
function drawOptionsBox(state) { 
  if (state==2) {
    this.optionsElement.disabled = true;
    this.optionsElement.checked = false;
  } else {
    this.optionsElement.disabled = false;
    checked = (state == 1);
    this.optionsElement.checked = checked;
  }
}

// ===============================================================================================================================
// findOption
// ===============================================================================================================================
function findOption(key, haystack) {  
  for (k=0; k < haystack.length; k++) {
     if (haystack[k].value == key) 
       return haystack[k];
  }
}

// ===============================================================================================================================
// optionsBoxClick
// ===============================================================================================================================
function optionsBoxClick(source) {
  theOption = this.findOption(source.options[source.options.selectedIndex].value, this.selectedOptions);
  theOption.optionIsSet = ! theOption.optionIsSet;
  return true;
}

// ===============================================================================================================================
// enableOptions
// ===============================================================================================================================
function enableOptions(optionsElement) {
  this.optionsEnabled = true;
  this.optionsElement = optionsElement;
}

// ===============================================================================================================================
// moveMyOptionDown
// ===============================================================================================================================
function moveMyOptionDown(source) {
  max = source.options.length;
  for (i = max-1; i>=0; i--) {
    if (source.options[i].selected && i< max-1) {
      swapMyOptions(source, i, i+1);
      myKey = this.selectedOptions[i];
      this.selectedOptions[i] = this.selectedOptions[i+1]; 
      this.selectedOptions[i+1] = myKey;
    }
  }
}

// ===============================================================================================================================
// moveMyOptionUp
// ===============================================================================================================================
function moveMyOptionUp(source) {
  max = source.options.length;
  for (i = 0; i< max; i++) {
    if (source.options[i].selected && i>0) {
      swapMyOptions(source, i, i-1);
      myKey = this.selectedOptions[i];
      this.selectedOptions[i] = this.selectedOptions[i-1]; 
      this.selectedOptions[i-1] = myKey;      
    }
  }
}

// ===============================================================================================================================
// selectOptions
// ===============================================================================================================================
function selectOptions(source, target) {
  for (j=0; j < source.options.length; j++) {
    if (source.options[j].selected) {      
      this.addKey(source.options[j].value);
    }
  }
  this.clearBox(source);
  this.clearBox(target);
  this.fillSelectBoxSelected(target);
  this.fillSelectBoxUnselected(source);
}

// ===============================================================================================================================
// saveData
// ===============================================================================================================================
function saveData(source, saveToField, saveLinksToField) {
  linksString = '';
  
  if (source.options.length > 0) {
    saveString = '';    
    for (j=0; j < source.options.length; j++) {
      if (saveString.length > 0 ) {
        saveString = saveString + ',';
      }
      
      // String der Leerzeichen enthält in Anführungszeichen setzen.
      if (source.options[j].value.indexOf(' ') > -1) {
        source.options[j].value = '"' + source.options[j].value + '"';
      }
          
      saveString = saveString + source.options[j].value;
      
      if (this.optionsEnabled) {               
        theOption = this.findOption(source.options[j].value, this.selectedOptions);
        if (theOption.optionIsSet) {
          if (linksString.length > 0) {
            linksString = linksString + ',';
          }
          linksString = linksString + source.options[j].value;          
        }
      }
    }
  } else {
    saveString = '-1';
  }
   
  saveToField.value = saveString; 
  if (this.optionsEnabled)
    saveLinksToField.value = linksString;  
}

// ===============================================================================================================================
// removeOptions
// ===============================================================================================================================
function removeOptions(source, target) {
  for (j=0; j < source.options.length; j++) {
    if (source.options[j].selected) {      
      this.removeKey(source.options[j].value);
    }
  }
  this.clearBox(source);
  this.clearBox(target);
  this.fillSelectBoxSelected(source);  
  this.fillSelectBoxUnselected(target);    
}

// ===============================================================================================================================
// addKey
// ===============================================================================================================================
function addKey(key) {
  for (i=0; i< this.unSelectedOptions.length; i++) {
    if (this.unSelectedOptions[i].value == key) {      
      this.selectedOptions[this.selectedOptions.length] = this.unSelectedOptions[i];
      this.unSelectedOptions = removeArrayElement(i, this.unSelectedOptions);  
      return 1;
    }
  }
}

// ===============================================================================================================================
// removeKey
// ===============================================================================================================================
function removeKey(key) {
  for (i=0; i< this.selectedOptions.length; i++) {
    if (this.selectedOptions[i].value == key) {      
      this.unSelectedOptions[this.unSelectedOptions.length] = this.selectedOptions[i];
      this.selectedOptions = removeArrayElement(i, this.selectedOptions);  
      return 1;
    }
  } 
}

// ===============================================================================================================================
// removeArrayElement
// ===============================================================================================================================
function removeArrayElement(nr, myArray) { 
  for(x=nr;x<myArray.length-1;x++) {
    myArray[x] = myArray[x+1]
  }
  myArray.length += -1
  return myArray;
}

// ===============================================================================================================================
// addOption
// ===============================================================================================================================
function addOption(name, value, selected, optionIsSet) {
  if (selected == true) {
    this.selectedOptions[this.selectedOptions.length] = new MyOption(name, value, selected, optionIsSet);
  } else {
    this.unSelectedOptions[this.unSelectedOptions.length] = new MyOption(name, value, selected, optionIsSet);
  }   
}

// ===============================================================================================================================
// fillSelectBoxUnselected
// ===============================================================================================================================        
function fillSelectBoxUnselected(selectBox) {  
   if (this.unSelectedBoxSorted) { 
    this.sortUnselectedBox();
  }
  for (i=0; i < this.unSelectedOptions.length; i++) {
     selectBox.options[i] = new Option(this.unSelectedOptions[i].name, this.unSelectedOptions[i].value, false, false);     
  }
}

// ===============================================================================================================================
// fillSelectBoxselected
// ===============================================================================================================================        
function fillSelectBoxSelected(selectBox) {     
  for (i=0; i < this.selectedOptions.length; i++) {
     
     selectBox.options[i] = new Option(this.selectedOptions[i].name, this.selectedOptions[i].value, false, false);     
  }
  if (this.optionsEnabled) 
    this.setOptionsBox(selectBox);
}

// ===============================================================================================================================
// sortUnselectedBox
// ===============================================================================================================================        
function sortUnselectedBox() {
  this.unSelectedOptions.sort(myOptionSort);
}

// ===============================================================================================================================
// clearBox
// ===============================================================================================================================        
function clearBox(selectBox) {
  selectBox.options.length = 0;
}


// ===============================================================================================================================
// swapOptions
// ===============================================================================================================================
function swapMyOptions(obj,i,j) {
  if (i != -1) {
    var o = obj.options;
    var i_selected = o[i].selected;
    var j_selected = o[j].selected;


    var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
    var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
    o[i] = temp2;
    o[j] = temp;
    o[i].selected = j_selected;
    o[j].selected = i_selected;
  }
}