// Seleciona próximo elemnto do formulário
selTel = function(kCode, cp, proxCp) {
	if($(cp).val().length==$(cp).attr('maxlength') && ((kCode>=96 && kCode<=105) || (kCode>=48 && kCode<=57))) {
		$(proxCp).focus();
	}
}

$(document).ready(function() {
	
	$('#cp_ddd_tel, #cp_ddd_cel').mask('(99)',{placeholder:''});
	$('#cp_telefone, #cp_celular').mask('9999-9999',{placeholder:''});
	
	$('#cp_ddd_tel').keyup(function(event) {
		//if($(this).val().length==$(this).attr('maxlength')) $('#cp_telefone').focus();
		selTel(event.keyCode, this, '#cp_telefone');
	});
	$('#cp_telefone').keyup(function(event) {
		//if($(this).val().length==$(this).attr('maxlength')) $('#cp_ddd_cel').focus();
		selTel(event.keyCode, this, '#cp_ddd_cel');
	});
	$('#cp_ddd_cel').keyup(function(event) {
		//if($(this).val().length==$(this).attr('maxlength')) $('#cp_celular').focus();
		selTel(event.keyCode, this, '#cp_celular');
	});
	$('#cp_celular').keyup(function(event) {
		//if($(this).val().length==$(this).attr('maxlength')) $('#cp_email').focus();
		selTel(event.keyCode, this, '#cp_email');
	});
	
	$('#from_contato').submit(function() {
		
		cpsVazios = [];
		
		$('#cp_nome, #cp_email, #cp_msg').each(function() {
			vlrCp = $(this).val();
			if(vlrCp == $(this).attr('defaultValue') || vlrCp == '') cpsVazios.push($(this).attr('id'));
		});
		
		if(cpsVazios.length) {
			msgCps = 'Por favor, preencha adequadamente o(s) campo(s):';
			
			for(i=0; i<cpsVazios.length; i++) {
				nomeCp = cpsVazios[i];
				msgCps += "\n- "+$('label[for="'+nomeCp+'"]').html();
			}
			
			alert(msgCps);
			$('#'+cpsVazios[0]).focus();
		}
		
		return false;
	});
});