// Prototype Extensions - Select
// Version:               0.1
// Depends on:            Jquery 1.3.x, Browser Detect 2.1.6
// Author:                Wagner Rodrigues

var Select = {

  getOptions: function(element) {
	element = $('#'+element);
	return element[0].options;
  },

  getSelectedOptions: function(element) {
	element = $('#'+element);
	for(i=0;i<element[0].options.length;i++){
		if(element[0].options[i].selected)
			return element[0].options[i];
	}
  },

  getUnselectedOptions: function(element) {
	return this.getOptions(element).findAll(function(element){ if(!element.selected) return element; });
  },

  // Select first option of one select element
  selectFirst: function (element) {
	element = $('#'+element);
    element[0].options[0].selected = true;
  },

  // Update text with *new_value* of option with *index* of select *element*
  updateOption: function (element, index, new_value) {
	  element = $('#'+element);
	  element[0].options[index].text = new_value;
  },

  // Select all options from select element 
  selectAll: function (element) {
    element = $('#'+element)
	for (i=0, l=element[0].options.length;i<l;i++) {
		element[0].options[i].selected = true;
    }
  },

  // Unselect all options from select element
  unselectAll: function (element, ignore_empty) {
    element = $('#'+element);
    for (i=0, l=element[0].options.length;i<l;i++) {
    	element[0].options[i].selected = false;
	}
  },

  // Select all empty options from select element 
  selectEmpties: function (element) {
    element = $('#'+element)
	for (i=0, l=element[0].options.length;i<l;i++) {
      if ('' == element[0].options[i].value)
    	  element[0].options[i].selected = true;
    }
  },

  // Unselect all empty options from select element
  unselectEmpties: function (element, ignore_empty) {
    element = $('#'+element);
    for (i=0, l=element[0].options.length;i<l;i++) {
      if ('' == element[0].options[i].value)
    	  element[0].options[i].selected = false;
	}
  },

  // Select all empty options from select element 
  selectNonEmpties: function (element) {
    element = $('#'+element)
	for (i=0, l=element[0].options.length;i<l;i++) {
      if ('' != element[0].options[i].value)
    	  element[0].options[i].selected = true;
    }
  },

  // Unselect all empty options from select element
  unselectNonEmpties: function (element, ignore_empty) {
    element = $('#'+element);
    for (i=0, l=element[0].options.length;i<l;i++) {
      if ('' != element[0].options[i].value)
    	  element[0].options[i].selected = false;
	}
  },

  selectOption: function (element, value){
    element = $('#'+element);
	for (i=0, l=element[0].options.length;i<l;i++) {
      if (value == element[0].options[i].value){
    	  element[0].options[i].selected = true;
	  }	
		
	}
  },

  //  Add an option to a select or optgroup
  addOption: function(element, text, value, selected) {
	if (null == selected) selected = true;
	option_element = new Option(text, value, false, selected);
	if (Browser.isIE) option_element.innerHTML = text;
	select_element = $('#'+element)
	select_element[0].appendChild(option_element);
  },
  
  //  Add an option to a select or optgroup from input object
  addOptionFromInput: function(select_element, input_element, selected) {
    select_element = $('#'+select_element);
	input_element = $('#'+input_element);
	if (input_element.value == "") return;
    self.addOption(select_element[0],input_element[0].value,input_element[0].value, selected);
  },
  
  //  Add an optgroup to a select
  addGroup: function (select_element,label,id,allow_duplicates) {
    select_element = $('#'+select_element);
	if (null == allow_duplicates) allow_duplicates = false
    if (false == allow_duplicates) {
      for (gi=0, gl=select_element[0].childNodes.length;gi<gl;gi++) {
        if ('optgroup' == select_element[0].childNodes[gi].nodeName.toLowerCase() && label == select_element[0].childNodes[gi].label) {
          return select_element[0].childNodes[gi];
        }
      }
    }
	var group_element = document.createElement('optgroup');
	group_element.label = label;
	group_element.id = id;
	select_element.appendChild(group_element);
	return group_element;
  },

  //  Delete an option from a select
  deleteOption: function (select_element,index,remove_optgroup) {
    select_element = $('#'+select_element);
	if (null != index) {
		select_element[0].options[index] = null;
      if(!Browser.isIE5x && remove_optgroup) {
        cont_children_nodes=0;
        for (i=0, l=parent_node.childNodes.length;i<l;i++) {
          if ('option' == parent_node.childNodes[i].nodeName.toLowerCase())
            cont_children_nodes++;
        }
        if (null != parent_node && 'optgroup' == parent_node.nodeName.toLowerCase() && 0 == cont_children_nodes) {
        	select_element[0].removeChild(parent_node);
          parent_node = null;
		}
      }
    }
    if ('undefined' != select_element[0].options[index] && null != select_element[0].options[index])
    	select_element[0].options[index].selected = true;
  },

  //  Delete all options from a select
  //  If ignore_empty is true it will not dele options with empty value
  deleteAll: function (select_element, ignore_empty) {
    select_element = $('#'+select_element);
    for (var i=select_element[0].options.length-1;i>-1;i--) {
    	alert(select_element[0].options[i]);
    	if (!ignore_empty || select_element[0].options[i].value != '')
    		this.deleteOption(select_element[0].id, i);
    }
  },

  //  Delete all selected options from a select.
  //  If ignore_empty is true it will not dele options with empty value
  deleteSelected: function (select_element, ignore_empty) {
    select_element = $('#'+select_element);
    for (var i=select_element[0].options.length-1;i>-1;i--) {
      if (select_element[0].options[i].selected && (!ignore_empty || select_element[0].options[i].value != ''))
        this.deleteOption(select_element[0], i);
    }
  },

  //  Copy all selected options from one select object to another creating the optgroups if necessary
  copySelected: function (from_element, to_element, ignore_empty) {
    from_element = $('#'+from_element);
	to_element = $('#'+to_element);
    for (i=0, l=from_element[0].options.length;i<l;i++) {
      if (true == from_element[0].options[i].selected && from_element[0].options[i].value != '') {
        if ('select' == from_element[0].options[i].parentNode.nodeName.toLowerCase())
          this.addOption(to_element[0],from_element[0].options[i].text,from_element[0].options[i].value);
        else if ('optgroup' == from_element[0].options[i].parentNode.nodeName.toLowerCase()) {
          group_element = this.addGroup(to_element[0],from_element[0].options[i].parentNode.label,from_element[0].options[i].parentNode.id+'to');
          if (Browser.isOpera)
            this.addOption(to_element[0],from_element[0].options[i].text,from_element[0].options[i].value);
          else
            this.addOption(group_element,from_element[0].options[i].text,from_element[0].options[i].value);
        }
      }
    }
  },

  //  Copy all options from one select object to another creating the optgroups if necessary
  copyAll: function (from_element,to_element) {
	from_element = $('#'+from_element);
	to_element = $('#'+to_element);
    this.selectAll(from_element[0]);
    this.copySelected(from_element[0],to_element[0]);
  },

  //  Copy selected options from one select object to another creating the optgroups if necessary
  moveSelected: function (from_element,to_element) {
    from_element = $('#'+from_element);
	to_element = $('#'+to_element);
    this.copySelected(from_element[0], to_element[0]);
	this.deleteSelected(from_element[0]);
  },

  //  Copy all options from one select object to another creating the optgroups if necessary
  moveAll: function (from_element,to_element) {
	from_element = $('#'+from_element);
	to_element = $('#'+to_element);
    this.copyAll(from_element[0], to_element[0]);
	this.deleteAll(from_element[0]);
  },

  // Serialize select values and populate input element
  serializeAllTo: function (select_element, to_element, delimiter, ignore_empty) {
    select_element = $('#'+select_element);
	to_element = $('#'+to_element);

    joinned = '';
    for (i=0, l=select_element[0].options.length;i<l;i++) {
      if (!ignore_empty || select_element[0].options[i].value != '') {
        joinned = joinned+select_element[0].options[i].value;
        if (i != l-1)
          joinned = joinned+delimiter;
      }
    }
    to_element[0].value = joinned;
  },
  
  serializeSelectedTo: function (select_element, to_element, delimiter, ignore_empty) {
    select_element = $('#'+select_element);
	to_element = $('#'+to_element);

    joinned = '';
    for (i=0, l=select_element[0].options.length;i<l;i++) {
      if (!ignore_empty || select_element[0].options[i].value != '') {
        if(select_element[0].options[i].selected){
			joinned = joinned+select_element[0].options[i].value;
	        if (i != l-1)
	          joinned = joinned+delimiter;
		}
      }
    }
    to_element[0].value = joinned;
  }
};
