$().ready(function(){
	
	$('*[data-mask=telefone]').on('keypress', function(e){
		
		range = $(this).getSelection();
		
		string = $(this).val().substr(0, range.start).replace(/[^0-9]/g, '')
				+ String.fromCharCode(e.which) 
				+ $(this).val().substr(range.end, $(this).val().length).replace(/[^0-9]/g, '');
		
		excessoes = (
			e.keyCode != 8 // backspace
			&& e.keyCode != 9 // tab
			&& e.keyCode != 13 // enter
			&& e.keyCode != 16 // shift
			&& e.keyCode != 17 // ctrl 
			&& e.keyCode != 27 // esc
			&& !(e.keyCode >= 33 && e.keyCode <= 40) // page up, page down, end, home, up, left, up, right, down
		);
		
		if (
			(isNaN(parseInt(String.fromCharCode(e.which), 10)) && excessoes) || string.length > 10
		) {
			return false;
		} else {
			
			if (string.length >= 2 && string.length < 6) {
				$(this).val('(' + string.substr(0, 2) + ') ' + string.substr(2));
				return false;
			} else if (string.length >= 6) {
				$(this).val('(' + string.substr(0, 2) + ') ' + string.substr(2, 4) + '-' + string.substr(6));
				return false;
			}
			
		}
				
	}).on('focus limparForm', function(){
		if ($(this).val() == '(00) 0000-0000') {
			$(this).val('');
		}
	}).on('blur', function(){
		if ($(this).val().length == 0) {
			$(this).val('(00) 0000-0000');
		} 
	}).each(function(){
		$(this).trigger('blur');
	});
	
	$('*[data-mask=cpf]').on('keypress', function(e){
		
		range = $(this).getSelection();
		
		string = $(this).val().substr(0, range.start).replace(/[^0-9]/g, '')
				+ String.fromCharCode(e.which) 
				+ $(this).val().substr(range.end, $(this).val().length).replace(/[^0-9]/g, '');
		
		excessoes = (
			e.keyCode != 8 // backspace
			&& e.keyCode != 9 // tab
			&& e.keyCode != 13 // enter
			&& e.keyCode != 16 // shift
			&& e.keyCode != 17 // ctrl 
			&& e.keyCode != 27 // esc
			&& !(e.keyCode >= 33 && e.keyCode <= 40) // page up, page down, end, home, up, left, up, right, down
		);
		
		if (
			(isNaN(parseInt(String.fromCharCode(e.which), 10)) && excessoes) || string.length > 11
		) {
			return false;
		} else {
			
			if (string.length >= 3 && string.length < 6) {
				$(this).val(string.substr(0, 3) + '.' + string.substr(3));
				return false;
			} else if (string.length >= 6 && string.length < 9) {
				$(this).val(string.substr(0, 3) + '.' + string.substr(3, 3) + '.' + string.substr(6));
				return false;
			} else if (string.length >= 9) {
				$(this).val(string.substr(0, 3) + '.' + string.substr(3, 3) + '.' + string.substr(6, 3) + '-' + string.substr(9));
				return false;
			}
			
		}
				
	}).on('focus limparForm', function(){
		if ($(this).val() == '000.000.000-00') {
			$(this).val('');
		}
	}).on('blur', function(){
		if ($(this).val().length == 0) {
			$(this).val('000.000.000-00');
		} 
	}).each(function(){
		$(this).trigger('blur');
	});
	
	$('*[data-mask=data]').on('keypress', function(e){
		
		range = $(this).getSelection();
		
		string = $(this).val().substr(0, range.start).replace(/[^0-9]/g, '')
				+ String.fromCharCode(e.which) 
				+ $(this).val().substr(range.end, $(this).val().length).replace(/[^0-9]/g, '');
		
		excessoes = (
			e.keyCode != 8 // backspace
			&& e.keyCode != 9 // tab
			&& e.keyCode != 13 // enter
			&& e.keyCode != 16 // shift
			&& e.keyCode != 17 // ctrl 
			&& e.keyCode != 27 // esc
			&& !(e.keyCode >= 33 && e.keyCode <= 40) // page up, page down, end, home, up, left, up, right, down
		);
		
		if (
			(isNaN(parseInt(String.fromCharCode(e.which), 10)) && excessoes) || string.length > 8
		) {
			return false;
		} else {
			
			if (string.length >= 2 && string.length < 4) {
				$(this).val(string.substr(0, 2) + '/' + string.substr(2));
				return false;
			} else if (string.length >= 4) {
				$(this).val(string.substr(0, 2) + '/' + string.substr(2, 2) + '/' + string.substr(4));
				return false;
			}
			
		}
				
	}).on('focus limparForm', function(){
		if ($(this).val() == 'DD/MM/AAAA') {
			$(this).val('');
		}
	}).on('blur', function(){
		if ($(this).val().length == 0) {
			$(this).val('DD/MM/AAAA');
		} 
	}).each(function(){
		$(this).trigger('blur');
	});
	
});

