/**  * Função para aplicar máscara em campos de texto
     
     * APLICAÇÃO:
         1.  CPF: onkeyup="maskIt(this,event,'###.###.###-##')" 
         2.  TELEFONE: onkeyup="maskIt(this,event,'(##)####-####')"
         3.  DATA: onkeyup="maskIt(this,event,'##/##/####')"
         4.  DINHEIRO: onkeyup="maskIt(this,event,'###.###.###,##',true)"
         5.  CEP: onkeyup="maskIt(this,event,'#####-###')" 
         6.  CNPJ: onkeyup="maskIt(this,event,'##.###.###/####-##')" 
     */
     
     function maskIt(w,e,m,r,a){        
     	// Cancela se o evento for Backspace    
     	if (!e) var e = window.event    
     	if (e.keyCode) code = e.keyCode;    
     	else if (e.which) code = e.which;        
     	
     	// Variáveis da função    
     	var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();    
     	var mask = (!r) ? m : m.reverse();    var pre  = (a ) ? a.pre : "";    
     	var pos  = (a ) ? a.pos : "";   
     	var ret  = "";    
     	
     	if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;   
     	 
     	// Loop na máscara para aplicar os caracteres    
     	for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){        
     		if(mask.charAt(x)!='#'){            
     	   	    ret += mask.charAt(x); x++;        
     	 	} else{            
     	 		ret += txt.charAt(y); y++; x++;        
     	 	 }
     	   }       

     	 // Retorno da função    
     	 ret = (!r) ? ret : ret.reverse()        
     	 w.value = pre+ret+pos;
     }
        // Novo método para o objeto 'String'
      String.prototype.reverse = function(){    
      	return this.split('').reverse().join('');
  };


function excluir(texto){
    if(confirm('Deseja excluir o registro ' + texto + '?')){
        return true;
    }else{
        return false;
    }
}

function alterar(texto){
    if(confirm('Deseja alterar a exibição deste registro?')){
        return true;
    }else{
        return false;
    }
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}

function FormataCampos(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;


    if(document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode;
    } else if(document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    } else {
        nTecla = evtKeyPress.which;
        if (nTecla == 8) {
            return true;
        }
    }

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++; }
        else {
            sCod += sValue.charAt(nCount);
            nCount++;
        }

        i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
        if (sMask.charAt(i-1) == "9") { // apenas números...
            return ((nTecla > 47) && (nTecla < 58)); } 
        else { // qualquer caracter...
            return true;
        } 
    }
    else {
        return true;
    }
}
function select_all(){
    obj = document.form1.selecionados;
    for(var x=0; x < obj.options.length; x++)
        obj.options[x].selected = true;
    obj2 = document.form1.select_cursos;
    for(var x=0; x < obj2.options.length; x++)
        obj2.options[x].selected = true;
}           
function select_list(obj){
    //obj = document.form1.selecionados;
    for(var x=0; x < obj.options.length; x++)
        obj.options[x].selected = true;
}           
function select_list_cadastro(obj){
    obj = document.form.selecionados_emprego;
    for(var x=0; x < obj.options.length; x++)
        obj.options[x].selected = true;
}           
try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function doBusca(tuf){

    //limpa o select
    var c=document.getElementById("cargo")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")

    //Monta a url com a uf
    xmlhttp.open("GET", "cargos.php?idarea="+tuf,true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var n=0
            var c=document.getElementById("cargo")
            while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCidades=eval((xmlhttp.responseText))
            var bCidades=eval((xmlhttp.responseText))
            //popula o select com a lista de cidades obtida
            for(var i=0;i<aCidades.length;i++){
                n=i%2
                if(n==0){
                    aCidades[i]=unescape(aCidades[i])
                    cCidade = unescape(aCidades[i])
                }else{
                    bCidades[i]=unescape(bCidades[i])
                    c.options[c.options.length]=new Option(cCidade,bCidades[i])
                }
                
            }
        }
    }

    xmlhttp.send(null)
}
function doBuscaEmprego(tuf){

    //limpa o select
    var c=document.getElementById("select_emprego")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")

    //Monta a url com a uf
    xmlhttp.open("GET", "cargos.php?idarea="+tuf,true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var n=0
            var c=document.getElementById("select_emprego")
            while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCidades=eval((xmlhttp.responseText))
            var bCidades=eval((xmlhttp.responseText))
            //popula o select com a lista de cidades obtida
            for(var i=0;i<aCidades.length;i++){
                n=i%2
                if(n==0){
                    aCidades[i]=unescape(aCidades[i])
                    cCidade = unescape(aCidades[i])
                }else{
                    bCidades[i]=unescape(bCidades[i])
                    c.options[c.options.length]=new Option(cCidade,bCidades[i])
                }
                
            }
        }
    }

    xmlhttp.send(null)
}

function doBuscaCad(tuf,j){

    //limpa o select
    var c=document.getElementById("cargo"+j)
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")

    //Monta a url com a uf
    xmlhttp.open("GET", "cargos.php?idarea="+tuf,true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var n=0
            var c=document.getElementById("cargo"+j)
            while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCidades=eval((xmlhttp.responseText))
            var bCidades=eval((xmlhttp.responseText))
            //popula o select com a lista de cidades obtida
            for(var i=0;i<aCidades.length;i++){
                n=i%2
                if(n==0){
                    aCidades[i]=unescape(aCidades[i])
                    cCidade = unescape(aCidades[i])
                }else{
                    bCidades[i]=unescape(bCidades[i])
                    c.options[c.options.length]=new Option(cCidade,bCidades[i])
                }
                
            }
        }
    }

    xmlhttp.send(null)
}


function doBuscaCurso(tc){

    //limpa o select
    var d=document.getElementById("curso")
    while(d.options.length>0)d.options[0]=null
    d.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")

    //Monta a url com a uf
    xmlhttp.open("GET", "cursos.php?tipo="+tc,true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var m=0
            var d=document.getElementById("curso")
            while(d.options.length>0)d.options[0]=null
            //Transforma a lista de Cursos JSON em Javascript
            var aCursos=eval((xmlhttp.responseText))
            var bCursos=eval((xmlhttp.responseText))
            //popula o select com a lista de Cursos obtida
            for(var j=0;j<aCursos.length;j++){
                m=j%2
                if(m==0){
                    aCursos[j]=unescape(aCursos[j])
                    cCursos = unescape(aCursos[j])
                }else{
                    bCursos[j]=unescape(bCursos[j])
                    d.options[d.options.length]=new Option(cCursos,bCursos[j])
                }
                
            }
        }
    }

    xmlhttp.send(null)
}

function Limpar(valor, validos) {

    // retira caracteres invalidos da string
    var result = "";
    var aux;
    for (var i=0; i < valor.length; i++) {
        aux = validos.indexOf(valor.substring(i, i+1));
        if (aux>=0) {
            result += aux;
        }
    }
    return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {
    var tecla = teclapres.keyCode;
    vr = Limpar(campo.value,"0123456789");
    tam = vr.length;
    dec=decimal

    if (tam < tammax && 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 <= dec )
        { campo.value = vr ; }

        if ( (tam > dec) && (tam <= 5) ){
            campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
        if ( (tam >= 6) && (tam <= 8) ){
            campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, tam ) ; }
        if ( (tam >= 12) && (tam <= 14) ){
            campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
        if ( (tam >= 15) && (tam <= 17) ){
            campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
    } 

}

function IsNumeric(sText)

{
    var ValidChars = "0123456789.,";
    var IsNumber=true;
    var Char;

 
    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function toggle(el) {
    if ( el.style.display != 'none' ) {
        el.style.display = 'none';
    }
    else {
        el.style.display = '';
    }
}
var i = 1;
var j = 0;
		
    function adiciona_curso(){
    var conteudo = document.getElementById('outros_cursos'+i);
			
    //Aqui começa a criação do elemento
			
    var radio = '<p><input name="outros'+i+'" type="text" id="outros'+i+'" maxlength="100"><input type="radio" name="routros'+i+'" value="1" >&nbsp;B&aacute;sico&nbsp;<input type="radio" name="routros'+i+'" value="2" >&nbsp;Intermedi&aacute;rio&nbsp;<input type="radio" name="routros'+i+'" value="3" >&nbsp;Fluente/Avan&ccedil;ado&nbsp;<img src="admin/imagens/bts/exclui.gif" width="15" height="14" border="0" id="remover" onClick="esconde_div(\'outros_cursos'+i+'\', \'outros'+i+'\')" ></p>'; 
    conteudo.innerHTML += radio;
    document.form.cont.value = i;
    i++;
    var div = '<div id="outros_cursos'+i+'"></div>';
    conteudo.innerHTML += div;
    //Aqui termina a criação do elemento
    button = document.getElementById('toggle');
    var texto = conteudo.getElementsByTagName('div')[0];
    //button.onclick = function(){ toggle(texto); }
    button.onkeypress = function(e){
        var keynum;
        if(window.event) // para o IE
            keynum = window.event.keyCode;
        else if(e.keyCode) // Netscape/Firefox/Opera
            keynum = e.keyCode;
        if (keynum == 13) {
            toggle(texto);
        }
    }
}

function esconde_div(id,text) {
    //tem que tratar browsers diferentemente  
    if (document.getElementById) { // DOM3 = IE5, NS6
        //document.getElementById(id).style.display = 'none';
        document.getElementById(text).value = ''; //limpa o valor do text pra não gravar de novo o que foi escondido
    }
    else {
        if 
        (document.layers) { // Netscape 4
            //  document.id.display = 'none';
            document.id.value = '';
        }
        else { // IE 4
            //document.all.id.style.display = 'none';
            document.all.id.value = '';
        }
    }
}

function excluir_emprego(id) {
    if (document.getElementById) { // DOM3 = IE5, NS6
        //  var div = document.getElementById(id);
        document.getElementById('empresa'+id+'').value = ''; //limpa o valor do text pra não gravar de novo o que foi escondido
        document.getElementById('entrada'+id+'').value = '';
        document.getElementById('saida'+id+'').value = '';
        while(document.getElementById('cargo'+id+'').options.length>0)document.getElementById('cargo'+id+'').options[0]=null
        document.getElementById('motivo'+id+'').value = '';
        document.getElementById('atividades'+id+'').value = '';
        //document.getElementById('salario'+id+'').value = '';
        document.getElementById('atual'+id+'').checked = 0;
    }
}

function habilita_campo(){
    var nivel = document.getElementById('nivel');
    var curso = document.getElementById('curso');
    (nivel.value >= 4)? curso.disabled=false : curso.disabled=true;	
}

function desabilita_servico(){
    var emprego = document.getElementById('emprego');
    //var curso = document.getElementById('curso');
    (emprego.checked == true)? document.getElementById('lista_empregos').style.display = 'none' : document.getElementById('lista_empregos').style.display = 'block';	
}

function exclui_foto(){
    if(confirm('Deseja excluir a imagem?')){
        var foto = document.getElementById('foto');
        document.getElementById('foto').style.display = 'none';	
        document.getElementById('mostra_foto').value = 0;
    }else{
        return false;
    }
}

function testa_data(data, campo){
    var campo = document.getElementById(campo);
    var dia1 = data.substr(0,2);
    //var dia1=parseInt(window.document.form1.dia.value);
    var mes1 = data.substr(3,2);
    //var mes1=parseInt(window.document.form1.mes.value);
    var ano1 = data.substr(6,4);
    // var ano1=parseInt(window.document.form1.ano.value);

    var datadigitada= new Date(ano1,(mes1-1),dia1);
    var miliqq=datadigitada.getTime();

    var mydate= new Date()
    var mili=mydate.getTime();

    var diaqq=parseInt(datadigitada.getDate());
    var mesqq=parseInt(datadigitada.getMonth())+1;

    if((dia1!=diaqq) || (mes1!=mesqq)){
        alert("Data inválida - "+dia1+'/'+mes1+'/'+ano1);
        campo.value='dia/mes/ano';
        campo.focus();
        return false;
    }
    else if(miliqq > mili){
        alert("Data Digitada maior que data atual");
        campo.value='dia/mes/ano';
        campo.focus();
        return false;
    }
}

function alerta_tecla(){
    alert('Os valores devem ser digitados!');
    return false;
}

function rejeitaTecla(oEvent){
    var oEvent = oEvent ? oEvent : window.event;
    var tecla = (oEvent.keyCode) ? oEvent.keyCode : oEvent.which;
    if(tecla == 17 ){
        alerta_tecla();
    }
}


