function mascara(o,f){
	v_obj=o
	v_fun=f
	setTimeout("execmascara()",1)
}
function execmascara(){
	v_obj.value=v_fun(v_obj.value)
}
function validarCPF(CPF){ 
	if (CPF != ''){ 
		if (!verificarCPF(CPF)){ 
			alert('CPF Inválido!'); 
			document.form.txtCPF.value=''; 
			document.form.txtCPF.focus(); 
		} 
	} 
}  

function verificarCPF(c){ 
	c = c.replace('.','');	
	c = c.replace('.','');	
	c = c.replace('-','');	
	var i;  
	s = c; 
	var c = s.substr(0,9);  
	var dv = s.substr(9,2);  
	var d1 = 0;  
	var v = false; 
	for (i = 0; i < 9; i++)  
	{  
		d1 += c.charAt(i)*(10-i);  
	}  
	if (d1 == 0){  
		v = true;  
		return false;  
	}  
	d1 = 11 - (d1 % 11);  
	if (d1 > 9) d1 = 0;  
	if (dv.charAt(0) != d1)  
	{  
		v = true; 
		return false;  
	}  
	d1 *= 2;  
	for (i = 0; i < 9; i++) {  
		d1 += c.charAt(i)*(11-i);  
	}  
	d1 = 11 - (d1 % 11);  
	if (d1 > 9) d1 = 0;  
	if (dv.charAt(1) != d1)  
	{  
		v = true; 
		return false;  
	}  
	if (!v) { 
		return true; 
	} 
}

function formatarCPF (campo,teclapres) { 
	var tecla = teclapres.keyCode;  
	if ((tecla < 48) || (tecla > 57)) {
		return false;
	} 
	vr = campo.value;  
	vr = vr.replace( '-', '' );  
	vr = vr.replace( '.', '' );  
	vr = vr.replace( '.', '' );  
	tam = vr.length; 

	if (tam < 11 && tecla != 8){ tam = vr.length + 1 ; }  
	if (tecla == 8 ){	tam = tam - 1 ; }  
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){  
		if ( tam <= 2 ){  
			campo.value = vr ; }  
		if ( (tam > 2) && (tam <= 5) ){  
			campo.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }  
		if ( (tam >= 6) && (tam <= 8) ){  
			campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }  
		if ( (tam >= 9) && (tam <= 11) ){  
			campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }  
	}
}
function cpf(v){
	v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
	v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
	v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
	//de novo (para o segundo bloco de números)
	v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hífen entre o terceiro e o quarto dígitos
	return v
}
function cnpj(v){
	v=v.replace(/\D/g,"")                           //Remove tudo o que não é dígito
	v=v.replace(/^(\d{2})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro dígitos
	v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
	v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           //Coloca uma barra entre o oitavo e o nono dígitos
	v=v.replace(/(\d{4})(\d)/,"$1-$2")              //Coloca um hífen depois do bloco de quatro dígitos
	return v
}
function ajaxInit(){
	var xmlhttp ;
	try {
		xmlhttp = new XMLHttpRequest ( ) ;
	}catch (ee){
		try { 
			xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP") ;
		}catch( e ) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP" ) ;
			}catch(E) {
				xmlhttp= false ;
			}
		}
	}
	return xmlhttp ;
}
function atualizacampo(valor){
	document.getElementById("txtRd").value = valor; 
	ajax=ajaxInit();
	document.getElementById("DivFormDhl").innerHTML="Carregando...";
	if(ajax){
		ajax.open('GET','?ac=carregar_form&id='+valor,true);
		ajax.onreadystatechange=function(){
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("DivFormDhl").innerHTML=ajax.responseText;
				}
			}
		}
	}
	ajax.send(null);
}
function validarRadio(){
	var snnull = 'S';
	for (i=0;i<document.form.elements.length;i++){
		if (document.form.elements[i].type == "radio"){
			if (document.form.elements[i].checked == true){
				snnull = 'N';
			}
		}
	}
	return snnull;
}
function emitirDeclaracao(){
	if(document.form.txtrdpro.value=='2' || document.form.txtrdpro.value=='4' ){
		if( validarRadio() == 'S'){
			alert('Informe o tipo de profissional');
		}
		else if(document.form.txtCPF.value == '' && document.form.txtCNPJ.value == '' ){
			alert('Informe o campo CPF ou o CNPJ.');
			document.form.txtCNPJ.focus();
		}
		else if(document.form.txtCodigo.value == ''){
			alert('Informe o número da inscrição');
			document.form.txtCodigo.focus();
		}
		else{
			document.form.action='reldhl';
			document.form.submit();
		}
	}
	else{
		if( validarRadio() == 'S'){
			alert('Informe o tipo de profissional');
		}
		else if(document.form.txtCPF.value == ''){
			alert('Informe o campo CPF.');
			document.form.txtCNPJ.focus();
		}
		else if(document.form.txtCodigo.value == ''){
			alert('Informe o número da inscrição');
			document.form.txtCodigo.focus();
		}
		else{
			document.form.action='reldhl';
			document.form.submit();
		}
	}
}
function consultarDeclaracao(){
	document.form.action='crodf?ac=consultaDhl&cod='+document.form.txtConsulta.value;
	document.form.submit();
}
