/* Javascripts para validações de formulários */

// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd
// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.
function whichBrs() {
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1) return 'Opera';
    if (agt.indexOf("staroffice") != -1) return 'Star Office';
    if (agt.indexOf("webtv") != -1) return 'WebTV';
    if (agt.indexOf("beonex") != -1) return 'Beonex';
    if (agt.indexOf("chimera") != -1) return 'Chimera';
    if (agt.indexOf("netpositive") != -1) return 'NetPositive';
    if (agt.indexOf("phoenix") != -1) return 'Phoenix';
    if (agt.indexOf("firefox") != -1) return 'Firefox';
    if (agt.indexOf("safari") != -1) return 'Safari';
    if (agt.indexOf("skipstone") != -1) return 'SkipStone';
    if (agt.indexOf("msie") != -1) return 'Internet Explorer';
    if (agt.indexOf("netscape") != -1) return 'Netscape';
    if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
    if (agt.indexOf('\/') != -1) {
    if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
    return navigator.userAgent.substr(0,agt.indexOf('\/'));}
    else return 'Netscape';} else if (agt.indexOf(' ') != -1)
    return navigator.userAgent.substr(0,agt.indexOf(' '));
    else return navigator.userAgent;
}

function isIE(){
     if ('Internet Explorer' == whichBrs())
        return true;
    
    return false;
}

function isFireFox(){
     if ('Firefox' == whichBrs())
        return true;
    
    return false;
}



document.onkeydown = showDown;

function showDown(evt) { 
	evt = (evt)? evt : ((event)? event : null); 
	if (evt) { 
			//if (event.keyCode == 8 && (event.srcElement.type!= "text" && event.srcElement.type!= "textarea" && event.srcElement.type!= "password")) { 
			// When backspace is pressed but not in form element 
			//cancelKey(evt); 
			//} 
			//else if (event.keyCode == 116) { 
			if (event.keyCode == 116) { // When f5 is pres	sed 
				cancelKey(evt); 
				} 
			//else if (event.keyCode == 122) { 
			// When F11 is pressed 
			//cancelKey(evt); 
			//} 
		//else if (event.ctrlKey && (event.keyCode == 78 ¦¦ event.keyCode == 82)) { 
		// When ctrl is pressed with R or N 
		//cancelKey(evt); 
		//} 
		//else if (event.altKey && event.keyCode==37 ) { 
		// stop Alt left cursor 
		//return false; 
		//} 
	} 
} 

function cancelKey(evt) { 
	if (evt.preventDefault) { 
		evt.preventDefault(); 
		return false; 
	}else { 
		evt.keyCode = 0; 
		evt.returnValue = false; 
	} 
} 


/*Função  Pai de Mascaras*/
function Mascara(o, f) {
    v_obj = o
    v_fun = f
    setTimeout("execmascara()", 1)
}

/*Função que Executa os objetos*/
function execmascara() {
    v_obj.value = v_fun(v_obj.value)
}

/*Função que Determina as expressões regulares dos objetos*/
function leech(v) {
    v = v.replace(/o/gi, "0")
    v = v.replace(/i/gi, "1")
    v = v.replace(/z/gi, "2")
    v = v.replace(/e/gi, "3")
    v = v.replace(/a/gi, "4")
    v = v.replace(/s/gi, "5")
    v = v.replace(/t/gi, "7")
    return v
}


/*Função que formata  CPF*/
function Cpf(v) {
    v = v.replace(/\D/g, "")
    v = v.replace(/(\d{3})(\d)/, "$1.$2")
    v = v.replace(/(\d{3})(\d)/, "$1.$2")
    v = v.replace(/(\d{3})(\d{1,2})$/, "$1-$2")
    return v
}

/* Função que retira formatação do cpf (ponto e hífen) */
function replaceAll(str) {
    de = "." //irá retirar os pontos
    var pos = str.indexOf(de);
    while (pos > -1) {
        str = str.replace(de, "");
        pos = str.indexOf(de);
    }
    de = "-" //irá retirar a virgula
    var pos = str.indexOf(de);
    while (pos > -1) {
        str = str.replace(de, "");
        pos = str.indexOf(de);
    }

    return (str);
}

/*Função que verifica CPF*/
function verificaCPF(ctrlCpf) {
	if (ctrlCpf.value.length != 0){
		if (verCpf(replaceAll(ctrlCpf.value))) { //Se a validação do CPF sem formatação for TRUE (cpf válido)
			//return true;
		} else {        
			alert('Número de CPF inválido.');
			ctrlCpf.focus();
			return false;        
		}
	}
}

function verCpf(cpf_value) {
    if (cpf_value.length != 11 || cpf_value == "00000000000" || cpf_value == "11111111111" || 
        cpf_value == "22222222222" || cpf_value == "33333333333" || cpf_value == "44444444444" ||
        cpf_value == "55555555555" || cpf_value == "66666666666" || cpf_value == "77777777777" || 
        cpf_value == "88888888888" || cpf_value == "99999999999") {
        return false;
    }
    
    add = 0;
    
    for (i = 0; i < 9; i++) {
        add += parseInt(cpf_value.charAt(i)) * (10 - i);
    }
    
    rev = 11 - (add % 11);

    if (rev == 10 || rev == 11) {
        rev = 0;
    }

    if (rev != parseInt(cpf_value.charAt(9))) {
        return false;
    }
    
    add = 0;
    
    for (i = 0; i < 10; i++) {
        add += parseInt(cpf_value.charAt(i)) * (11 - i);
    }

    rev = 11 - (add % 11);
    
    if (rev == 10 || rev == 11) {
        rev = 0;
    }

    if (rev != parseInt(cpf_value.charAt(10))) {
        return false;
    }
   
    return true;
}


function somente_numero(campo) {
    var digits = "0123456789-/"
    var campo_temp
    for (var i = 0; i < campo.value.length; i++) {
        campo_temp = campo.value.substring(i, i + 1)
        if (digits.indexOf(campo_temp) == -1) {
            campo.value = campo.value.substring(0, i);
        }
    }
}


/* Aceitação de apenas caracteres NUMÉRICOS - utilizado nas funções de máscaras */
function numeros(e) {
    if (e.keyCode) {// Internet Explorer
        var tecla = e.keyCode;
    } else if (e.which) { // Nestcape
        var tecla = e.which;
    }

    if ((tecla > 47 && tecla < 58)) { // numeros de 0 a 9
        return true;
    } else {
        if (tecla != 8) { // backspace 
            return false;
        } else {
            //event.returnValue = false; 
			return false;
        }

    }

}

/* Aceitação de apenas caracteres ALFANUMÉRICOS */
function caracteresAlfa(e) {
    var strValidos = " AÀÁÃÂBCÇDEÊÉFGHIÍJKLMNOÓÔÕPQRSTUÚVWXYZaàáãâbcçdeéêfghiíjklmnoõóõpqrstuúvwxyz";
    
    if(e.keyCode){
        var tecla = e.keyCode;
    } else if(e.which){
        var tecla = e.which;
    }
    
    /*
    if (document.all) {// Internet Explorer
        var tecla = event.keyCode;
    } else if (document.layers) { // Nestcape
        var tecla = e.which;
    }*/

    if (strValidos.indexOf(String.fromCharCode(tecla)) != -1 || tecla == 9) {
        return true;
    } else {
        if (tecla != 8) { // backspace 
            // event.keyCode = 0;
            return false;
        } else {
            return true;
        }
    }
}

function cep(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
	v=v.replace(/(\d{5})(\d)/,"$1-$2")   //Coloca hífen entre o quinto e sexto dígitos          
    
    return v;
}

function telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}

function data(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1/$2")       //Coloca uma barra o segundo e o terceiro dígitos
    v=v.replace(/(\d{2})(\d)/,"$1/$2")       //Coloca uma barra entre o quinto e sexto 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
}

/* Função para limpar todos os campos do formulário */

function inicializa_variaveis(){
	
	document.getElementById("cboCurso").value="";
	document.getElementById("cboTurno").value="";
	document.getElementById("txtNomeCandidato").value="";
	document.getElementById("txtNomePai").value="";
	document.getElementById("txtNomeMae").value="";
	document.getElementById("txtNaturalidade").value="";
	document.getElementById("txtDataNasc").value="";
	document.getElementById("txtRg").value="";
	document.getElementById("txtCpf").value="";
	document.getElementById("cboEstadoCivil").value="";
	document.getElementById("txtProfissao").value="";
	document.getElementById("cboSituacaoAtual").value="";
	document.getElementById("txtCep").value="";
	document.getElementById("txtEndereco").value="";
	document.getElementById("txtEndereco_nr").value="";
	document.getElementById("txtEndereco_comp").value="";
	document.getElementById("txtBairro").value="";
	document.getElementById("cboCidade").value="";
	document.getElementById("txtTelefoneRes").value="";
	document.getElementById("txtTelefoneCom").value="";
	document.getElementById("txtCelular").value="";
	document.getElementById("cboEscolaridade").value="";
	document.getElementById("cboOrigemEscolar").value="";
	document.getElementById("cboRenda").value="";
	document.getElementById("txtEmail").value="";	
	

}

function isDate(campo){
        var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
        var msgErro = 'Data inválida.';
        if ((campo.value.match(expReg)) || (campo.value == '')){                     
                return true;
        } else {
                alert(msgErro);
                return false;                          
        } 
    }

function toDate(valor){
	return Date(valor.split("/")[2].toString() + "-" + valor.split("/")[1].toString() + "-" + valor.split("/")[0].toString())
}


