/* Author: Praekelt */

$("#timepicker_1").timePicker({step:10, startTime:"00:00", endTime:"23:50", show24Hours: true,separator: ':',});
$('#conv-message').keyup(function(){
	    	$('#charcount').text($('#conv-message').val().length);
			if($('#conv-message').val().length>140){$('#valid-twitter').hide()}else{$('#valid-twitter').show()}
			if($('#conv-message').val().length>160){$('#valid-sms').hide()}else{$('#valid-sms').show()}
			$('span.preview-message').text($('#conv-message').val());
			
});

$('.typeahead').typeahead();

$(function() { $( "#datepicker" ).datepicker({ minDate: 0, dateFormat: 'yy-mm-dd' }); });
$(function() { $( "#dob" ).datepicker({ minDate: 0, dateFormat: 'yy-mm-dd' }); });

$('#surveyUSSD').click(function() {
	if($('#surveyUSSD').attr('checked')) { $('#surveyUSSDNum').show(); } else { $('#surveyUSSDNum').hide(); }
});

$('#search-filter-input').focus(function() {
	$('input#search-filter-input').val('');
});

$('#convtype').change(function() {
	var newAction = $('#convtype').val();
	$('#newConversationType').attr('action', newAction);
});

/* show or hide the tag pool options depending on the delivery class chosen */
$('.delivery-class-radio').change(function() {
    delivery_classes = $('.delivery-class-radio[name=delivery_class]');
    delivery_classes.each(function(index, element) {
        var deliveryClass = $(element);
        var tagPoolDiv = $('#' + deliveryClass.val() + '_tag_pool_selection');
        if($(deliveryClass).attr('checked')) {
            tagPoolDiv.show();
            $('#' + deliveryClass.val() + '_tag_pool_selection select').removeAttr('disabled');
        } else {
            tagPoolDiv.hide();
            $('#' + deliveryClass.val() + '_tag_pool_selection select').attr('disabled', 'disabled');
        }
    });

})
@@ -1076,549 +1076,1207 @@
 );%0A%0A
-/* show or hide the tag pool options depending on the delivery class chosen */%0A$('.delivery-class-radio').change(function() %7B%0A    delivery_classes = $('.delivery-class-radio%5Bname=delivery_class%5D');%0A    delivery_classes.each(function(index, element) %7B%0A        var deliveryClass = $(element);%0A        var tagPoolDiv = $('#' + deliveryClass.val() + '_tag_pool_selection');%0A        if($(deliveryClass).attr('checked')) %7B%0A            tagPoolDiv.show();%0A            $('#' + deliveryClass.val() + '_tag_pool_selection select').removeAttr('disabled'
+function addOptGroupLabel(selectField) %7B%0A    stripOptGroupLabel(selectField);%0A    var selectedOpt = selectField.find('option:selected');%0A    var optGroupLabel = selectedOpt.closest('optgroup').attr('label');%0A    selectedOpt.prepend(%22%3Cspan class='optgroup-label'%3E%22%0A                        + optGroupLabel + %22: %3C/span%3E%22);%0A%7D%0A%0Afunction stripOptGroupLabel(selectField) %7B%0A    selectField.find('.optgroup-label').remove();%0A%7D%0A%0A/* show or hide the tag pool options depending on the delivery class chosen */%0A$('.delivery-class-radio').change(function() %7B%0A    delivery_classes = $('.delivery-class-radio%5Bname=delivery_class%5D');%0A    delivery_classes.each(function(index, element) %7B%0A        var deliveryClass = $(element);%0A        var tagPoolDiv = $('#' + deliveryClass.val() + '_tag_pool_selection');%0A        var selectField = $('#' + deliveryClass.val() + '_tag_pool_selection select');%0A        if($(deliveryClass).attr('checked')) %7B%0A            tagPoolDiv.show();%0A            selectField.removeAttr('disabled');%0A            selectField.blur(function() %7B addOptGroupLabel(selectField); %7D);%0A            selectField.focus(function() %7B stripOptGroupLabel(selectField); %7D);%0A            addOptGroupLabel(selectField
 );%0A 
@@ -2338,67 +2338,19 @@
     
-$('#' + deliveryClass.val() + '_tag_pool_selection select')
+selectField
 .att

Add some magic Javascript to display / hide the option group.