	// Aplica mascara de Número
	function mask_num(campo, teclapres) {
		var vr = campo.value;
		var tecla = teclapres.keyCode;
		var tam = 10;
			if ( tecla ) {
			vr = vr.replace(/[^0-9]/ig, '');
			campo.value = vr;
		}
	}
	
	jQuery.noConflict();
	jQuery(document).ready(function() {
        
        function inputVal() {
			jQuery('form.validate_form input').blur(function() {
				var text = jQuery(this).attr('desc');
				if(jQuery(this).val() == '')
					jQuery(this).val(text);
			}).focus(function() {
				var text = jQuery(this).attr('desc');
				jQuery(this).css('background','#fff');
				if(jQuery(this).val() == text)
					jQuery(this).val('');
			});
		}
		
		inputVal();

		// Confirm delete
		jQuery('#dependentes_form .del').click(function () {
			if(confirm('Deseja realmente apagar este registro?')) {
				var cod = jQuery(this).prev().val();
				jQuery('#del_form input:first').val(cod);
				jQuery('#del_form').submit();
			}
		});

		// Verify if input is blanck
		jQuery('form.validate_form').submit(function() {
			var este = jQuery(this).attr('id');
			var answer = null;
			jQuery('#'+este+' input.obrigatorio').each(function() {
				var i = jQuery(this);
				var text = i.attr('desc');
				if (i.val() == '' || i.val() == text) {
					answer = false;
					i.css('background','red');
				} else {
					i.parent('p.info').removeClass('validate');
				};
			});
			if (answer == false) {
				alert('Verifique os campos obrigátorios');
				return answer;
			}
		});

		// Add input
		jQuery('span#mais').click(function() {
			var html = null;
			html = '<dl>';
			html += '<dd>';
			html += '<input type="text" desc="Nome" name="nomeDependente[]" id="nomeDependente" class="obrigatorio" size="20" value="Nome" />';
			html += '<input type="text" desc="Parentesco" name="parentesco[]" id="parentesco" class="obrigatorio" size="20" value="Parentesco" />';
			
			html += '<select name="dia[]">';
			html += '<option value="">Dia</option>';
			for(i=1;i<32;i++) {
				html += '<option value="'+i+'">'+i+'</option>';
			}
			html += '</select>';
			
			html += '<select name="mes[]">';
			html += '<option value="">Mês</option>';
			for(i=1;i<13;i++) {
				html += '<option value="'+i+'">'+i+'</option>';
			}
			html += '</select>';
			
			html += '<select name="ano[]">';
			html += '<option value="">Ano</option>';
			var date=new Date();
			var y=date.getFullYear();
			for(i=y-100;i<=y;i++) {
				html += '<option value="'+i+'">'+i+'</option>';
			}
			html += '</select>';
			
			html += '<span class="apagar" title="Apagar" style="cursor:pointer;">X</span>';
			html += '</dd>';
			html += '</dl>';
			
			jQuery('form dl:last').after(html);
			inputVal();
			
			
			// Confirm delete
			jQuery('#dependentes_form .apagar').click(function () {
					jQuery(this).parent().parent().remove();
			});
			
		});

		jQuery('input:text, textarea').blur(function() {
			var valure = jQuery(this).val();
			jQuery(this).attr({ 
	          defaultValue: valure,
	          value: valure
	        });
            return true;
		});
		
		jQuery('input:checkbox, input:radio').click(function() {
			var valure = jQuery(this).attr('checked');
			if(valure) {
				jQuery(this).attr({ 
		          defaultChecked: true,
		          checked: true
		        });
		    }
		    return true;
		});

		jQuery('select').change(function() { 
			var val = jQuery(this).children("option:selected").val();
			var text = jQuery(this).children("option:selected").text();
			jQuery(this).children("option").each(function(i) {
				if (this.value!=val) {
					jQuery(this).replaceWith("<option value='"+this.value+"'>"+this.text+"</option>");
				} else {
					jQuery(this).replaceWith("<option value='"+val+"' selected='selected'>"+text+"</option>");
				}
			});
		});

		// Desabilita campo de apoio
		function desabilita(campo) {
			id = jQuery(campo).attr("name");
			if(campo.value == 'Sim' || campo.value == 'Sim, por danos?') {
				jQuery("."+id+"-input, #"+id+"-input").attr("disabled",false);				
			} else {
				jQuery("."+id+"-input, #"+id+"-input").attr("disabled",true);
				
			}
		}
		
		// Aplica mascara de CPF
		function mask_cpf(campo) {
			var vr = campo.value;
			var tam = 11;
			vr = vr.replace(/[^0-9]/ig, '');
			tam = vr.length;
			if ( tam > 3 ) {
				vr = vr.substr( 0, 3 ) + '.' + vr.substr( 3, tam );
				if ( tam > 6 ) {
					vr = vr.substr( 0, 7 ) + '.' + vr.substr( 7, tam );
					if ( tam > 9 ) {
						vr = vr.substr( 0, 11 ) + '-' + vr.substr( 11, tam );
						if ( tam > 11 ) {
							vr = vr.substr( 0, 14 );
						}
					}
				}
			}
			campo.value = vr;
		}
		
		// Mask of amount
		function maskAmount (campo) {
			campo.value = campo.value.replace(/[^0-9]/ig, '');
		};

		// Aplica mascara de CNPJ
		function mask_cnpj(campo) {
			var vr = campo.value;
			var tam;
			//Format: ##.###.###/####-##
			vr = vr.replace(/[^0-9]/ig, '');
			tam = vr.length;
			if ( tam > 2 ) {
				vr = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam );
				if ( tam > 5 ) {
					vr = vr.substr( 0, 6 ) + '.' + vr.substr( 6, tam );
					if ( tam > 8 ) {
						vr = vr.substr( 0, 10 ) + '/' + vr.substr( 10, tam );
						if ( tam > 12 ) {
							vr = vr.substr( 0, 15 ) + '-' + vr.substr( 15, tam );
							if ( tam > 14 ) {
								vr = vr.substr( 0, 18 );
							}
						}
					}
				}
			}
			campo.value = vr;
		}
		
		// Mask of Phone and Mobile
		function maskPhone (campo) {
			var tammax = 10;
			var vr = campo.value;
			var tam = 0;
			var ddd = 2;
			vr = vr.replace(/[^0-9]/ig, '');
			tam = vr.length;
			if ( tam < tammax) {
				if ( tam <= ddd ) {
					campo.value = vr;
				}
				if ( (tam > ddd) && (tam <= 6) && tam < tammax ) {
					campo.value = "(" + vr.substr( 0, ddd ) + ") " + vr.substr( ddd, tam );
				}
				if ( (tam >= 7) && tam < tammax ) {
					campo.value = "(" + vr.substr( 0, ddd ) + ") " + vr.substr( ddd, 4 ) + "-" + vr.substr( 6, 4 );
				}
			}
		else
			{
			campo.value = "(" + vr.substr( 0, ddd ) + ") " + vr.substr( ddd, 4 ) + "-" + vr.substr( 6, 4 );
			}
		};
		
		// Mask of Phone and Mobile without DDD
		function maskPhone_w_ddd(campo) {
			var tammax = 8;
			var vr = campo.value;
			var tam = 0;
			vr = vr.replace(/[^0-9]/ig, '');
			tam = vr.length;
			if ( tam < tammax) {
				if ( (tam >= 4) && tam < tammax ) {
					campo.value = vr.substr( 0, 4 ) + "-" + vr.substr( 4, 4 );
				}
			}
		else
			{
			campo.value = vr.substr( 0, 4 ) + "-" + vr.substr( 4, 4 );
			}
		};

		// Aplica mascara de Data
		function mask_date(campo) {
		var vr = campo.value;
		var tam = 10;
			vr = vr.replace(/[^0-9]/ig, '');
			tam = vr.length;
			if ( tam > 2 ) {
				vr = vr.substr( 0, 2 ) + '/' + vr.substr( 2, tam );
				if ( tam > 4 ) {
					vr = vr.substr( 0, 5 ) + '/' + vr.substr( 5, tam );
					if ( tam > 8 ) {
						vr = vr.substr( 0, 10 );
					}
				}
			}
			campo.value = vr;
		}
		
		// Aplica mascara de CEP
		function mask_cep(campo) {
			var vr = campo.value;
			var tam = 9;
			vr = vr.replace(/[^0-9]/ig, '');
			tam = vr.length;
			if ( tam > 2 ) {
				vr = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam );
				if ( tam > 5 ) {
					vr = vr.substr( 0, 6 ) + '-' + vr.substr( 6, tam );
					if ( tam > 8 ) {
						vr = vr.substr( 0, 10 );
					}
				}
			}
			campo.value = vr;
		};
		
		// Aplica mascara de Placa
		function mask_placa(campo) {
			var vr = campo.value;
			var tam = 9;
			vr = vr.replace(/[^0-9A-Za-z]/ig, '');
			tam = vr.length;
			if ( tam <= 3 ) {
				vr = vr.replace(/[^A-Za-z]/ig, '');
			} else {
				if ( tam > 3 ) {
					vr_p1 = vr.substr( 0, 3 );
					vr_p1 = vr_p1.replace(/[^A-Za-z]/ig, '');
					vr_p2 = vr.substr( 3, tam );
					vr_p2 = vr_p2.replace(/[^0-9]/ig, '');
					if ( vr_p1.length = 3 ) {
						vr =  vr_p1 + '-' + vr_p2;
						if ( tam > 7 ) {
							vr_p2 = vr.substr( 4, 4 );
							vr_p2 = vr_p2.replace(/[^0-9]/ig, '');
							vr =  vr_p1 + '-' + vr_p2;
						}
					} else {
						vr = vr_p1;
					}
				}
			}
			campo.value = vr;
		};
		
		// Desabilita campo de apoio
		jQuery('.desab input:radio').click(function() {
			desabilita(this);
		});
		
		// Apply mask of data
		jQuery('.data').keyup(function() {
			mask_date(this);
		});
		
		// Apply mask of placa
		jQuery('.placa').keyup(function() {
			mask_placa(this);
		});
		
		// Apply mask of CEP
		jQuery('.cep').keyup(function() {
			mask_cep(this);
		});
		
		// Apply mask of amount
		jQuery('.num').keyup(function() {
			maskAmount(this);
		});
		
		// Apply mask of tel
		jQuery('.tel').keyup(function() {
			maskPhone(this);
		});
		
		// Apply mask of tel
		jQuery('.tel-w-ddd').keyup(function() {
			maskPhone_w_ddd(this);
		});
		
		// Apply mask of cpf
		jQuery('.cpf').keyup(function() {
			mask_cpf(this);
		});
		
		// Apply mask of cnpj
		jQuery('.cnpj').keyup(function() {
			mask_cnpj(this);
		});
		
		// Apply mask of cnpj
		jQuery('.relacao input:radio').change(function() {
			if(this.value == 'Próprio') {
				jQuery('#relacao-proprio').hide();
			} else {
				jQuery('#relacao-proprio').show();
			}
		});
		
		// Change mask of cnpj / cpf
		jQuery('.tipo_pessoa').change(function() {
			if(this.value == 'Física') {
				jQuery('.cnpj_cpf').keyup(function() {
					mask_cpf(this);
				});
			}
			else if(this.value == 'Jurídica') {
				jQuery('.cnpj_cpf').keyup(function() {
					mask_cnpj(this);
				});
			}
		});
	});

	function printSelection(content) {
		var pwin=window.open('','print_content','width=1,height=1');
		pwin.document.open();
		pwin.document.write('<html><head><link rel="stylesheet" href="http://www.afbdmg.com.br/afbdmg/wp-content/themes/afbdmg/style.css" type="text/css" /></head><body onload="window.print()" style="text-align:left">'+content+'</body></html>');
		pwin.document.close();
		setTimeout(function(){pwin.close();},1000);
        return true;
	}