// $Id: DDB2.js,v 1.3 2010/05/28 12:25:17 clinton Exp $
var AniteDDB = new Object ({
   // Returns true iff DDB is a select element
  isDDB: function (DDB) {
    if (DDB == null || !Object.isString(DDB.type)) return false;
    else if ((DDB.type == "select-one") || (DDB.type == "select-multiple")) return true;
    return false;
  },    
  
  // Removes all options from the DDB
  clear: function(DDB) {
    if (!this.isDDB(DDB)) return;
    /*
    while (DDB.options.length > 0)
      DDB.options[0] = null;
    */
    $(DDB).update('');
  },
    
  // Set's the DDB's optiosn to selected if they are in the supplied value Array 
  setValue: function (DDB, values) {    
    if (!this.isDDB(DDB)) return;
    
    values = $A([values]);  
    values = values.flatten()
    for (var i = 0; i < values.length; i++) values[i] = new String(values[i]);
    for (var i = 0; i < DDB.options.length; i++) {
      DDB.options[i].selected = false; 
      for (var j = 0; j < values.length; j++) {
        if (DDB.options[i].value == values[j]) DDB.options[i].selected = true;      
      }
    }
  },
  setSelectedIndex: function(DDB, index) {
    if (!this.isDDB(DDB)) return false;
    var result = false;                  
    for (var i = 0; i < DDB.options.length; i++) {
      if (i == index) {DDB.options[i].selected = true; result = true;}
      else {DDB.options[i].selected = false;}
    }
    return result;
  },
  
  // Returns the value of the first selected item in the DDB
  getValue: function (DDB) {
    if (!this.isDDB(DDB)) return null;
    var sel = this._getSelectedOptions(DDB);
    if (sel.length > 0) return sel[0].value;
    else return null;
  },
      
  // Returns the Text of the first selected item in the DDB
  getText: function(DDB) {
    if (!this.isDDB(DDB)) return null;
    var sel = this._getSelectedOptions(DDB);
    if (sel.length > 0) return sel[0].text;
    else return null;
  },
  
  // Returns the Index of the first selected item in the DDB (-1 if no selection)
  getSelectedIndex: function(DDB) {
    if (!this.isDDB(DDB)) return -1;    
    for (var i = 0; i < DDB.options.length; i++) {
      if (DDB.options[i].selected) return i;  
    }
    return -1;
  },

  // Returns a (possibly empty) array of the currently selected values in the DDB
  getValues: function (DDB) {
    if (!this.isDDB(DDB)) return null;
    var result = new Array();
    var sel = this._getSelectedOptions(DDB);
    
    for (var i = 0; i < sel.length; i++) {
      result[result.length] = sel[i].value;  
    }
    return result;
  },   
  
  // Resets the selection to the default when the page loaded  
  reset: function(DDB) {
    if (!this.isDDB(DDB)) return null;
    for (var i = 0; i < DDB.options.length; i++) {
      DDB.options[i].selected = DDB.options[i].defaultSelected;   
    }      
  },

  // Populates the DDB with options - based on the supplied source
  populate: function (DDB, source, validationFunction, baseName) {
    if (!this.isDDB(DDB)) return;
    if (source.length == 0) AniteDDB.clear(DDB);
    else if (Object.isString(source)) this._populateDDB(DDB, source, validationFunction);
    else if (source[0].name && source[0].code) this._populateDDB_json(DDB, source, validationFunction);
    else this._populateDDBAry(DDB, source, validationFunction)      
  },

  addOptionGroup: function (DDB, groupName, groupMembers) {
    var content = "<optgroup label=\"" + groupName + "\">";
    for (var i = 0; i < groupMembers.length; i++){
      content += "<option value=\"" + groupMembers[i].code + "\">" + groupMembers[i].name + "</option>";
    }
    content += "</optgroup>";
    $(DDB).insert({bottom: content});
  },
    
  // Sorts the options, by default by the text of the option
  sort: function (DDB, ignoreFirstItem, caseSensitive, sortFunction) {
    if (!this.isDDB(DDB)) return;
    if (ignoreFirstItem == null) ignoreFirstItem = false;
    if (caseSensitive == null) caseSensitive = true;
    if (sortFunction == null || !Object.isFunction(sortFunction)) {
      sortFunction = caseSensitive ? this.defaultTextSort : this.defaultTextSortIgnoreCase;
    }
    
    var curValue = this.getValues(DDB);
    var sorted = new Array();
    var startAt = (ignoreFirstItem?1:0);
    for (var i = startAt; i < DDB.options.length; i++) {
      var insertPt = sorted.length;
      for (var j = 0; j < sorted.length; j++) {
        if (sortFunction(DDB.options[i], sorted[j]) < 0) {
          insertPt = j;
          break;
        }
      }
      var opt = new Object({value: DDB.options[i].value, text: DDB.options[i].text});
      sorted.splice(insertPt, 0, opt);    
    }
    for (var j = 0; j < sorted.length; j++) {
      DDB.options[j+startAt] = new Option(sorted[j].text, sorted[j].value);  
    }  
    this.setValue(DDB, curValue);
  },
  
  // Returns the index of the option with the supplied text.
  indexOfText: function (DDB, textValue) {
    if (!this.isDDB(DDB)) return -1;
    
    for (var i = 0; i < DDB.options.length; i++) {
      if (DDB.options[i].text == textValue) {
        return i;
      }
    }
    return -1;
  },
    
  // Returns the index of the option with the supplied value.
  indexOf: function (DDB, value) {
    if (!this.isDDB(DDB)) return -1;
  
    for (var i = 0; i < DDB.options.length; i++) {
      if (DDB.options[i].value == value) {
        return i;
      }
    }
    return -1;
  },  

  next: function (DDB) {
    if (!this.isDDB(DDB)) return false;
    var current = this.getSelectedIndex(DDB);
    return this.setSelectedIndex(DDB, current+1);
  },

  first: function(DDB) {
    return this.setSelectedIndex(DDB, 0);
  },
  
  // ------------------------------------------------------------------------//
  // Default sort functions 
  defaultTextSort: function (left, right) {
    if (left.text < right.text) return -1;
    else if (left.text > right.text) return 1;
    else return 0;
  },

  defaultTextSortIgnoreCase:function (left, right) {
    if (left.text.toLowerCase() < right.text.toLowerCase()) return -1;
    else if (left.text.toLowerCase() > right.text.toLowerCase()) return 1;
    else return 0;
  },

  defaultValueSort: function (left, right) {
    if (left.value < right.value) return -1;
    else if (left.value > right.value) return 1;
    else return 0;
  },
  
  defaultValueSortIgnoreCase: function (left, right) {
    if (left.value.toLowerCase() < right.value.toLowerCase()) return -1;
    else if (left.value.toLowerCase() > right.value.toLowerCase()) return 1;
    else return 0;
  },
  
  // ------------------------------------------------------------------------//
  
  // Private methods - not intended for user use      
  _getSelectedOptions: function (DDB) {
    if (!this.isDDB(DDB)) return null;
    var result = new Array();
    for (var i = 0; i < DDB.options.length; i++) {
      if (DDB.options[i].selected) result[result.length] = new Object({value: DDB.options[i].value, text: DDB.options[i].text});  
    }
    return result;
  }, 

  _populateDDB_json: function (DDB, jsonArray, validationFunction) {
    if (!this.isDDB(DDB)) return;
    if (jsonArray == null) return;    
    var curSel = this.getValues(DDB);
    this.clear(DDB);
    
    for (var i = 0, j = 0; i < jsonArray.length; i++) {
      var curItem = jsonArray[i];
      if (!Object.isFunction(validationFunction) || (validationFunction(curItem))) {
        DDB.options[j] = new Option(curItem.name, curItem.code);
        j++;
      }
    }
  
    this.setValue(DDB, curSel);
  },
  
  _getSelectedArray:function (DDB, selected, baseName) {
    if (!this.isDDB(DDB)) return;
    if (baseName == null) baseName = DDB.name;    
    var aryName = baseName + selected + "Array";
    var exists = eval("typeof " + aryName);   
    if (exists) return eval(aryName);
    else return null;
  },
   
  _populateDDB:function  (DDB, selected, validationFunction, baseName) {
    if (!this.isDDB(DDB)) return;
    this._populateDDBAry(DDB, this._getSelectedArray(DDB, selected, baseName), validationFunction);
  },
  
  _populateDDBAry: function (DDB, selectedArray, validationFunction) {
    if (!this.isDDB(DDB)) return;
    if (selectedArray == null) return;   
  
    var curSel = this.getValues(DDB);
    this.clear(DDB);
  
    for (var i = 0, j = 0; i < selectedArray.length; i++) {
      var curItem = eval("new Array "+ selectedArray[i]);
      if (!Object.isFunction(validationFunction) || (validationFunction(curItem[1]))) {
        eval("DDB.options[j]=" + "new Option" + selectedArray[i]);
        j++;
      }
    }
    this.setValue(DDB, curSel);
  }
 
});

function sortDropDown(DDB, ignoreFirstItem, caseSensitive, sortFunction) {
  AniteDDB.sort(DDB, ignoreFirstItem, caseSensitive, sortFunction);
}

function getDropDownValue(DDB) {
  return AniteDDB.getValue(DDB);
}
function setDropDown(DDB, value) {
  AniteDDB.setValue(DDB, value);
}

