function getSelValue( select ) {
	if ( select.selectedIndex != -1 ) {
		return select.options[ select.selectedIndex ].value;
	} else return false;
}

function getSelText( select ) {
	if ( select.selectedIndex != -1 ) {
		return select.options[ select.selectedIndex ].text;
	} else return false;
}

function addOption( select, text, value ) {
	select.options[ select.options.length ] = new Option( text, value, false, false );
}

function insertOption( select, value, text, index ) {
	if ( index == null ) {
		addOption( select, text, value );
	} else {
		if ( index > select.options.length ) index = select.options.length;
		addOption( select, "", "" );
		for(var i = select.options.length - 2; i >= index; i--) {
			select.options[ i + 1 ].text = select.options[ i ].text;
			select.options[ i + 1 ].value = select.options[ i ].value;
		}
		select.options[ index ].text = text;
		select.options[ index ].value = value;
	}
}

function changeOptionById( select, id, text, value ) {
	for(var i = 0; i < select.options.length; i++ ) {
		if ( id == select.options[ i ].value ) {
			select.options[ i ].text = text;
			if ( value != null ) select.options[ i ].value = value;
			return i;
		}
	}
	return false;
}

function delOptionById( select, id ) {
	for(var i = 0; i < select.options.length; i++ ) {
		if ( id == select.options[ i ].value ) {
			select.options[ i ] = null;
			return i;
		}
	}
	return false;
}

function delAllOptions( select ) {
	if (select.options.length) {
		for(var i = select.options.length-1; i >= 0; i--) {
			select.options[i] = null;
		}
	}
}

function selOptionById( select, id ) {
	for(var i = 0; i < select.options.length; i++ ) {
		if ( id == select.options[ i ].value ) {
			select.selectedIndex = i;
			return i;
		}
	}
	return false;
}

function delOptionByIndex( select, index ) {
	select.options[ index ] = null;
}

function sortOptions( select, order, type ) {
	if ( order != "asc" && order != "desc" ) order = "asc";
	if ( type != "text" && type != "value" ) type = "text";

	if ( select.options.length > 1 ) {
		for( var i = 0; i < select.options.length; i++) {
			for( var j = i; j < select.options.length; j++) {
				if ( order == 'asc' ) {
					var op1 = select.options[i];
					var op2 = select.options[j];
				} else {
					var op1 = select.options[j];
					var op2 = select.options[i];
				}
				if ( op1[ type ] > op2[ type ] ) {
					var text = op1.text;
					var value = op1.value;
					op1.text = op2.text;
					op1.value = op2.value;
					op2.text = text;
					op2.value = value;
				}
			}
		}
	}
}

