$(function(){
	$('.multiselect').multiselect({
		nonSelectedText: 'Invia a tutti',
		numberDisplayed: 0,
		nSelectedText: 'selezionati',
		buttonWidth:'100%',
		maxHeight: window.innerHeight * 0.5,
	});
	
	$('#text-msg').on('keypress paste focusout',function(event){ 
		var textarea = $(this);			
		setTimeout(function(){			
			
			var text = textarea.val();
			var n = text.length;
			
			if(n > 160){
				textarea.val( text.substr(0,160) );
				n = 160;
			}
			$('#text-count').html(n);
			
		},100);
	})
});
function populateGrp(val){
	$.get('./?populate='+val,function(data){

		var obj = $.parseJSON(data);
		var $sel = $('#sel-grp');
	
		if(obj != null){
			$sel.multiselect('dataprovider', obj);
			$sel.multiselect('enable');
		}else{
			$sel.find('option, optgroup').remove();
			$sel.multiselect('rebuild');
			$sel.multiselect('disable');
		}
	});

}
function confirmSMS(){
	if($('#text-msg').val() == ''){
		alert('Inserire il testo del messaggio');
		return;
	}
	confirm("Procedo con l'invio degli SMS?", function(){
		$('#sms-grp').hide();
		$('#f-sms-result').html('<div class="alert alert-warning"><img src="../../../../images/sel-loader.gif" /> Invio SMS in corso... attendere</div>');
		$('#SMSform').submit(); 
		closeDialog();
	});
}
