function removeSelectedOptions(from) {

	first=document.forms['inregistrare']['subcat_main'];
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
	}
	else {
		for (var i=(from.options.length-1); i>=0; i--) {
			var o=from.options[i];
			if (o.selected) {
				from.options[i] = null;
				first.options[i] = null;
			}
		}
	}
	from.selectedIndex = -1;
	return false;
}

// Compare two options within a list by VALUES
function compareOptionValues(a, b){ 
	// Radix 10: for numeric values
	// Radix 36: for alphanumeric values
	
	var sA = parseInt(a.value, 36);  
	var sB = parseInt(b.value, 36);  
	
	return sA - sB;
}

// Compare two options within a list by TEXT
function compareOptionText(a, b){ 
	var x = a.text.toLowerCase();
    var y = b.text.toLowerCase();
    
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function checkAll(list){
	for(var j = 0; j < list.length; j++){
		if(list[j] != null)	{
			list.options[j].selected = true;
		}
	}
}

function contains_value(list, obj) {
    var i = list.length;
    while (i--) {
        if (list[i].value == obj) {
            return true;
        }
    }
    
    return false;
}

// Dual list move function
function moveDualList(srcList, destList, max_length, moveFromLeftToRight){
	if(moveFromLeftToRight){
		if(srcList.selectedIndex == -1 || contains_value(destList, srcList.options[srcList.selectedIndex].value)){
			return false;
		}
		
		if(destList.options.length >= max_length){
			alert("Maxim " + max_length + " categorii pot fi selectate!");
			return false;
		}
		
		newDestList = new Array(destList.options.length);
		
		var len = 0;
		for(len = 0; len < destList.options.length; len++){
			if(destList.options[len] != null){
				newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, false);
			}
		}
		
		for(var i = 0; i < srcList.options.length; i++){ 
			if(srcList.options[i] != null && srcList.options[i].selected == true){
				// Statements to perform if option is selected
				// Incorporate into new list
				newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, false);
				len++;
			}
		}
		
		// Sort out the new destination list
		//newDestList.sort( compareOptionValues ); // BY VALUES
		newDestList.sort( compareOptionText ); // BY TEXT
		
		// Populate the destination with the items from the new array
		for(var j = 0; j < newDestList.length; j++){
			if(newDestList[j] != null){
				destList.options[j] = newDestList[j];
			}
		}
	}else{
		if(destList.selectedIndex == -1){
			return false;
		}
	
		// Erase source list selected elements
		//length = destList.options.length;
		if(destList.options.length > 0){
			for(var i = destList.options.length - 1; i >= 0; i--){ 
				if(destList.options[i] != null && (destList.options[i].selected == true)){
					// Erase Source
					//destList.options[i].value = "";
					//destList.options[i].text  = "";
					destList.options[i] = null;
				}
			}
		}
	}
	
	return true;
}

function removeOption(list, option){
	//alert(list.options.length);
	//length = list.options.length;
	if(list.options.length > 0){
		for(var i = list.options.length - 1; i >= 0; i--){
			if(list.options[i] != null && list.options[i].value == option.value){
				list.remove(i);
				//list.options[i] = null;
			}
		}
	}
}

function appendOption(list, option){
	//alert(list.options.length);
	list.options[list.options.length] = new Option(option.text, option.value, false, false);
	
	newList = new Array(list.options.length);
	for(len = 0; len < list.options.length; len++){
		if(list.options[len] != null){
			newList[len] = new Option(list.options[len].text, list.options[len].value, list.options[len].defaultSelected, list.options[len].selected);
		}
	}
	
	newList.sort( compareOptionText ); // BY TEXT
	
	// Populate the destination with the items from the new array
	for(var j = 0; j < list.length; j++){
		if(list[j] != null){
			list.options[j] = newList[j];
		}
	}
}

