/**
 * HTMLOptGroup class.
 * Implements interface for creating optgroup elements.
 * @version 1.0
 */


/**
 * Constructor
 * @param string label
 * @return void
 */
function HTMLOptGroup(label, value){
	this.label = label;
	this.value = value;

	this._optel = document.createElement('optgroup');
	this._optel.setAttribute("label", this.label);
}


HTMLOptGroup.prototype._optel;
HTMLOptGroup.prototype.label;
HTMLOptGroup.prototype.value;


/**
 * Append options.
 * @param string name
 * @param string value
 * @param bool disabled
 * @param bool selected
 * @return void
 */
HTMLOptGroup.prototype.appendOption = function(text, value, disabled, selected){
	var option = document.createElement('option');
	option.setAttribute("value", value);
	option.innerHTML = text;
	option.disabled = disabled;
	option.selected = selected;

	this._optel.appendChild(option);
}


/**
 * Appends generated HTMLOptGroup to target object.
 * @param object target (usually HTMLSelectElement)
 * @return void
 */
HTMLOptGroup.prototype.appendTo = function(target, mode, indent_length, indent_prefix){

	if(mode == null) mode = 0;

	switch(mode){
		case 1:
			if(indent_length == null) indent_length = 3;
			if(indent_prefix == null) indent_prefix = "&nbsp;";

			var prefix = "";
			for(j=0; j<indent_length; j++){
				prefix += indent_prefix;
			}

			var option = document.createElement('option');
			option.setAttribute("value", this.value);
			option.innerHTML = this.label;
			target.appendChild(option);

			while(this._optel.hasChildNodes()){
				var child = this._optel.removeChild(this._optel.firstChild);
				child.innerHTML = prefix + child.innerHTML;
				target.appendChild(child);
			}
			break;
		default:
			target.appendChild(this.getElement());
			break;
	}
}


/**
 * Getter for generated optgroup element.
 * @return object HTMLOptgroupElement
 */
HTMLOptGroup.prototype.getElement = function(){
	return this._optel;
}
