function doPrint() {
	window.print();
}

// ___ character limiting

function update($prompt, limit, count) {
	var html = 'No more than ' 
		+ limit 
		+ ' characters, please. '
		+ (limit - count)
		+ ' still available.';
		
	$prompt.html(html);
	
	if(limit - count < 10)
		$prompt.addClass('limit-prompt-alert');
	else
		$prompt.removeClass('limit-prompt-alert');
}

function enforce(field, amount, $prompt) {
	var $field = $(field);
	var value = $field.val();
	if(value.length > amount)
		$field.val(value.substr(0, amount));
	
	update($prompt, amount, $field.val().length);
}

$('.formline').each(function() {
	$textarea = $('textarea', this);
	if($textarea.length) {
		var limit = $textarea.attr('limit');
		if(limit) {
			var html = '<div class="limit-prompt"></div>';
			var $prompt = $(html).insertBefore($textarea);
			update($prompt, limit, 0);
			$textarea.keyup(function() {
				enforce(this, limit, $prompt);
			});
		}
	}
});


// ___ textarea auto-grow

function setupGrow() {
	function resize() {
		fitToContent($(this));
	}
	
	$('textarea').each(function() {
		var $this = $(this);
		$this
	        .data('original-height', $this.height())
	        .css('overflow', 'hidden')
	 		.keyup(resize)
	 		.keydown(resize);
	});
	
	setInterval(function() {
		$('textarea').each(function() {
			var $this = $(this);
			fitToContent($this);
		});
	}, 100);	
}

function fitToContent(/* JQuery */text) {
    var adjustedHeight = Math.max(text[0].scrollHeight, text.data('original-height'));
    text.css('height', adjustedHeight + 'px');
}

setupGrow();
