if (navigator.appName.indexOf('Microsoft') != -1){   
    clientNavigator = "IE";   
}else{   
    clientNavigator = "Other";   
} 

function validacpf(obj){
	var i;
 	a = obj.value;
	s = a;//a.substr(0,3) + a.substr(4,3) + a.substr(8,3) + a.substr(12, 2);
	
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
 
	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(10-i); 
	}
 
	if (d1 == 0){
		alert("CPF Invalido");
		return false; 
	}
 
	d1 = 11 - (d1 % 11);
 
	if (d1 > 9) d1 = 0;
 
	if (dv.charAt(0) != d1){
		alert("CPF Invalido");
		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){
		alert("CPF Invalido");
		return false;
	}
 
	return true; 
}

function validar(obj){
	if (obj.value==''){
		document.getElementById("msg" + obj.name).style.display = 'block';
	}else{
		document.getElementById("msg" + obj.name).style.display = 'none';
	}
}

function combo(obj) {
	if (obj.value == "?") {
		document.getElementById("msg" + obj.name).style.display = 'block';
   }else{
		document.getElementById("msg" + obj.name).style.display = 'none';
   }
}

function validaform(form){
	var campos   = document.curriculo.getElementsByTagName('input');
	var selects  = document.curriculo.getElementsByTagName('select');
	var nomes    = "";
	var msg_pwd  = "";
	var mensagem = "";
	var count    = 0;
	var datas    = "";

	for (var x = 0; x < campos.length; x++){
		if(campos[x].id == 'o_cpf'){
			if( ! validacpf(campos[x]) ){
				return false;
			}
		}
		
		if(campos[x].id.substring(0,4) == 'o_dt' || campos[x].id.substring(0,3) == 'dt_' ){
			datas = validarData(campos[x]);

			if( datas != "" ){
				msg_pwd += "<br \/><b>" + datas + "<\/b>";
			}
		}
		
		if(campos[x].id.substring(0,2) == 'o_'){
			if( campos[x].id == 'o_senha' ){
				if( document.curriculo.senha.value != document.curriculo.rounds.value ){
					if( document.curriculo.senha.value != document.curriculo.re_senha.value ){
						msg_pwd += "<br \/>A senha e a confirma&ccedil;&atilde;o de senha devem ser iguais.";
					}
				}	
			}
			if(campos[x].value == ""){
				nomes += ", " + campos[x].name.toUpperCase();
				count++;
			}
		}
	}
	for (var x = 0; x < selects.length; x++){
		if(selects[x].id.substring(0,2) == 'o_'){
			if(selects[x].value == "?"){
				nomes += ", " + selects[x].name.toUpperCase();
				count++;
			}
		}
	}
	
	if( (nomes.length > 0) || (msg_pwd.length > 0) ){
		if(nomes.length > 0){
			if( count == 1 ){
				mensagem += "O campo <b>" + nomes.substring(1) + "<\/b> deve ser preenchido.";
			}else if( count > 1 ){
				mensagem += "Os campos <b>" + nomes.substring(1) + "<\/b> devem ser preenchidos.";
			}
		}
		document.getElementById('msg_erros').innerHTML = mensagem + msg_pwd;
		document.getElementById('msg_erros').style.display = "";
		return false;
	}
}

function formatar(src, mask, tipo, evt){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	
	if (texto.substring(0,1) != saida){
    	src.value += texto.substring(0,1);
  	}
  
  if( tipo == 'N' ){
	  return Bloqueia_Caracteres(evt);
  }
}

function Bloqueia_Caracteres(evnt){   
//Função permite digitação de números   
    if (clientNavigator == "IE"){   
        if (evnt.keyCode < 48 || evnt.keyCode > 57){   
            return false   
        }   
    }else{   
        if ((evnt.charCode < 48 || evnt.charCode > 57) && evnt.keyCode == 0){   
            return false   
        }   
    }   
}

function validarData(campo){
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	var erro = campo.name.toUpperCase() + " - ";
	
	if(campo.value==''){
		return '';
	}else{
		if (campo.value.match(expReg)){
			var dia = campo.value.substring(0,2);
			var mes = campo.value.substring(3,5);
			var ano = campo.value.substring(6,10);
			
			if( (mes==4 || mes==6 || mes==9 || mes==11) && dia > 30){
				return erro + "Dia incorreto !!! O mês especificado contém no máximo 30 dias.";
			}else{
				if(ano%4!=0 && mes==2 && dia>28){
					return erro + "Data incorreta!! O mês especificado contém no máximo 28 dias.";
				}else{
					if(ano%4==0 && mes==2 && dia>29){
						return erro + "Data incorreta!! O mês especificado contém no máximo 29 dias.";
					}else{
						return '';
					}
				}
			}
		}
	}
	return erro + "Formato de data inválido.";
}