function RemoveOptGroupByLabel(select, label) {
	try {
		var eachGroup = select.firstChild;
		while (label != eachGroup.label) {
			eachGroup = eachGroup.nextSibling;
		}
		var o = select.removeChild(eachGroup);
		var sel = document.getElementById('removed_ghost_services*');
		sel.appendChild(o);
	} catch(er) {
		alert(er);
	}
}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) { 
	for (var i=(from.options.length-1); i>=0; i--) {
		var o=from.options[i];
		if (o.selected) {
			if (o.parentNode.tagName == 'OPTGROUP') {
				RemoveOptGroupByLabel(from, o.parentNode.label);
			} else {
				o = from.removeChild(o);
				var sel = document.getElementById('removed_services*');
				sel.appendChild(o);
			}
		}
	}
	from.selectedIndex = -1; 
}
	
// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
	var options = {};
	var i;
	for (i=0; i<to.options.length; i++) {
		options[to.options[i].value] = to.options[i].text;
	}
	for (i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] === null || options[o.value] === undefined || options[o.value] != o.text) {
				var optionText = o.text;
				var optionValue = o.value;
				if ($("select[name='pmt_tracking']").val() == 'on' && startsWith("service:", optionValue)) {
					optionText += " PMT";
					optionValue += "^pmt_dynamic_tracking_state=Enable"
				}
				to.options[to.options.length] = new Option(optionText, optionValue, false, false);
			}
		}
	}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if(obj) {
		for (var i=0; i<obj.options.length; i++) {
			obj.options[i].selected = true;
		}
	}
}


	
// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	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;
}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	// If > 1 option selected, do nothing
	var selectedCount=0, i;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
		}
	}
	if (selectedCount!=1) {
		return;
	}
	// If this is the first item in the list, do nothing
	i = obj.selectedIndex;
	if (i === 0) {
		return;
	}
	swapOptions(obj,i,i-1);
	obj.options[i-1].selected = true;
}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	// If > 1 option selected, do nothing
	var selectedCount=0, i;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
		}
	}
	if (selectedCount != 1) {
		return;
	}
	// If this is the last item in the list, do nothing
	i = obj.selectedIndex;
	if (i == (obj.options.length-1)) {
		return;
	}
	swapOptions(obj,i,i+1);
	obj.options[i+1].selected = true;
}