$(document).ready(function() {
	
	// First Array contains the group codes, Second Array contains the name to display on the screen.
	var groups =  new Array("partners", "managers", "sn_account", "stf_account", "assoc", "sft_con", "bizwtr", "admin");
	var names = new Array("Partners", "Managers", "Senior Accountants", "Staff Accountants", "Associates", "Software Consultants", "Business Writer", "Administration");
	
	// Loops through the option elements and finds the elements that are part of the same group, add them to the same class
	$('select#input_1_2 option').each(function (index){
		for (var i in groups){
			var pattern = "^"+groups[i]+"";
			option_elem = $(this).val().match(new RegExp(pattern))
			if(option_elem){
				$(this).addClass(groups[i]);
			}
		}
	});
	
	// wraps all the option elements of the same class in a optgroup elem.
	for(var i in groups){
		var label = "<optgroup label='" + names[i] + "'>" + names[i] + "</optgroup>";
		var selector = "." + groups[i];
		$(selector).wrapAll(label);
	}
});

