/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function nonumbers(e, extra) {
    var key;
    var keychar;

    if (window.event) {
        key = window.event.keyCode;
    }
    else if (e) {
        key = e.which;
    }
    else {
        return true;
    }
    keychar = String.fromCharCode(key);

    if ((("0123456789").indexOf(keychar) > -1)) {
        return false;
    }
    else if (extra != null && (keychar == extra)) {
        return true;
    }
    else
        return true;
}
function numbersonly(e, decimal) {
    var key;
    var keychar;
    if (window.event) {
        key = window.event.keyCode;
    }
    else if (e) {
        key = e.which;
    }
    else {
        return true;
    }
    keychar = String.fromCharCode(key);

    if ((key==null) || key == 0x2E || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
        return true;
    }

    else if ((("0123456789").indexOf(keychar) > -1)) {
        return true;
    }
    else if (decimal && (keychar == ".")) {
        return true;
    }
    else
        return false;
}

function ismaxlength(mlength, obj){
    if (obj.value.length>mlength) {
        obj.value=obj.value.substring(0,mlength);
    }
}
function roundNumber (rnum) {

    return Math.round(rnum*Math.pow(10,2))/Math.pow(10,2);

}
/*
2 - float2moeda

A partir de um valor float ela retorna o valor formatado com separador de milhar e vírgula nos centavos.

 */
function float2moeda(num) {

    x = 0;

    if(num<0) {
        num = Math.abs(num);
        x = 1;
    } if(isNaN(num)) num = "0";
    cents = Math.floor((num*100+0.5)%100);

    num = Math.floor((num*100+0.5)/100).toString();

    if(cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+'.'
        +num.substring(num.length-(4*i+3)); ret = num + ',' + cents; if (x == 1) ret = ' - ' + ret;return ret;

}
/*
3 - moeda2float

Pega um valor formatado com virgula e separador de milha e o transforma em float
 */
function moeda2float(moeda){

    moeda = moeda.replace(".","");

    moeda = moeda.replace(",",".");

    return parseFloat(moeda);

}

function currencyFormat(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
        num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function formatCurrency(param){
            
    param2 = Number(Math.round(Number(param)*100));
            
    param2 = param2/100;
            
    param2 = new String(param2);
    pos = param2.indexOf(".");
    a1 = param2.substring(0,pos);
    a2 = param2.substring(pos,param2.length);
    if (pos == -1) {
        param2 = param2 + ".00";
    } else if (a2.length == 2) {
        param2 = param2 + "0";
    } else if (a2.length > 3) {
        param2 = a1 + a2.substring(0,3);
    }
            
    return param2;
}
function FormataData(Campo, teclapres){
    var tecla = teclapres.keyCode;
    var vr = new String(Campo.value);
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    tam = vr.length + 1;
	
    if (tecla != 9 && tecla != 8){
        if (tam > 2 && tam < 5)
            Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
        if (tam >= 5 && tam <=10)
            Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
    }
}
/////////////////////////////////////////////////////////////////
function FormataHora(Campo, teclapres){
    var tecla = teclapres.keyCode;
	
    var vr = new String(Campo.value);
    vr = vr.replace(":", "");
    vr = vr.replace(":", "");

    tam = vr.length + 1;
	
    if (tecla != 9 && tecla != 8){
        if (tam > 2 && tam < 5)
            Campo.value = vr.substr(0, 2) + ':' + vr.substr(2, tam);
        if (tam > 5 && tam < 9)
            Campo.value = vr.substr(0,2) + ':' + vr.substr(2,2) + ':' + vr.substr(4,2);
    }
}
/////////////////////////////////////////////////////////////////
function funVerificaCaracterAlfabetico(campo)
{	
    tamanho_parametro = campo.value.length;
    if (tamanho_parametro != 0)
    {
        if ((campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="0") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="1") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="2") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="3") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="4") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="5") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="6") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="7") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="8") && (campo.value.substring(tamanho_parametro-1,tamanho_parametro)!="9"))
        {
            alert("Digite somente números. Ex: 1000, 500, 850, 2500, etc.");
            campo.value =  campo.value.substring(0,tamanho_parametro-1);
            campo.focus();
            return false;
        }
    }
}

function funMascaraValor(campo)
{
	
    var tecla = window.event.keyCode;
    if( !(tecla >= 48 && tecla <= 57) || eval(campo + ".length") > 17)
        window.event.returnValue = false;
    else{
        if (eval(campo + ".length") <=20)
        {
            if (eval(campo + ".length") > 2)
            {
                valor =eval(campo);
		    
                indice= valor.indexOf(","); //pegando o índice onde se encontra a vírgula se esta existir
                //retirando a vírgula se tiver, para atualizar o novo valor
                if (indice!= -1)
                {
				
                    parteInicial = valor.substring(0,indice); //pegando tudo que vem antes da vírgula
                    parteFinal = valor.substr(indice+1); //pegando tudo que vem depois da vírgula
                    valor = parteInicial + parteFinal; //atualizando o novo valor sem vírgula
                }
                indice=valor.indexOf("."); //pegando o índice onde se encontra o primeiro ponto se existir
                //retirando os pontos se tiver
                while (indice!=-1)
                {
                    parteInicial = valor.substring(0,indice); //pegando tudo que vem antes do ponto(localizado na posição de índice)
                    parteFinal = valor.substr(indice+1); //pegando tudo que vem depois do ponto(localizado na posição de índice)
                    valor = parteInicial + parteFinal; //atualizando o novo valor sem o ponto determinado
                    indice=valor.indexOf("."); //pegando o índice do próximo ponto se existir
                }
			
                //atualizando o tamanho do valor, agora sem vírgula e sem ponto
                tamanho = valor.length;
			
                //colocando a vírgula    
                parteSemPonto = valor.substring(0, tamanho-1);
                parteDecimal = "," + valor.substr(tamanho-1);
                tamanho = parteSemPonto.length;
				
                //colocando os pontos
                if(tamanho > 3)
                {
                    parteComPonto= "";
				
                    //A quantidade de pontos que o valor vai ter
                    quantPontos = tamanho/3;
                    //colocando os pontos no número
                    for (varInd=1; varInd <= quantPontos; varInd++)
                    {	
                        if(tamanho > 3) //vai ter ponto
                        {
                            //concatenando ponto com os últimos três dígitos da parte que ainda não tem ponto. E depois concatenando com a parte com ponto existente
                            parteComPonto= "." + parteSemPonto.substr(tamanho - 3) + parteComPonto; 
                            if (tamanho==4) //fica sobrando só um dígito para a parte sem ponto
                                parteSemPonto = parteSemPonto.substr(0,1);
                            else
                                //a parte sem ponto vai ser tudo q sobrou, iniciando do primeiro dígito tirando os últimos três dígitos (estes como foi dito anteriormente, fazem parte agora da parte com ponto)
                                parteSemPonto = parteSemPonto.substring(0, tamanho-3);
                            tamanho = parteSemPonto.length; //atualizando o tamanho da parte sem ponto
                        }
                        else
                        {
                            //não vai mais ter ponto na parte sem ponto.
                            parteSemPonto = parteSemPonto.substr(0,tamanho); 
                            break;
                        }
                    }
                    valor = parteSemPonto + parteComPonto + parteDecimal;
                }
                else
                {	
                    if (parteSemPonto == "")
                    {
                        indice= parteDecimal.indexOf(","); 
                        //retirando a vírgula se tiver
                        if (indice!= -1)
                            parteDecimal = parteDecimal.substr(1);
                    }
                    valor = parteSemPonto + parteDecimal;
					
                }
                eval(campo + "='" + valor + "'");//IMPORTANTE:Devido ao uso do sqlencode do SIFU, esta linha está recebendo dois plics antes e depois do campo valor, para a função funcionar corretamente, deixar apenas um.
            }
        }
    }	
}

function FormataValor2(Campo, teclapres){
    var tecla = teclapres.keyCode;
	
    var vr = new String(Campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");  
    vr = vr.replace(".", "");  
    vr = vr.replace(".", "");  
	
    tam = vr.length + 1;
	
    if (tecla != 9 && tecla != 8){
        if (tam > 3 && tam < 5)
            Campo.value = vr.substr(0, 1) + '.' + vr.substr(1, tam);
        if (tam >= 5 && tam < 6)
            Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);      
        if (tam >= 6 && tam < 7)
            Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);            
        if (tam >= 7 && tam < 8)
            Campo.value = vr.substr(0, 1) + '.' + vr.substr(1, 3) + '.' + vr.substr(4, tam);                  
        if (tam >= 8 && tam < 9)
            Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, tam);
    }
}
/////////////////////////////////////////////////////////////////
function FormataNum(Campo, teclapres){
    var tecla = teclapres.keyCode;
	
    var vr = new String(Campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
	
    tam = vr.length + 1;
	
    if (tecla != 9 && tecla != 8){
        if (tam > 3 && tam < 8)
            Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
        if (tam >= 8 && tam <10)
            Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3);
    }
}
/////////////////////////////////////////////////////////////////
function FormataValor(fld, milSep, decSep, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;  // Enter
    key = String.fromCharCode(whichCode);  // Get key value from key code
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
    len = fld.value.length;
    for(i = 0; i < len; i++)
        if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += milSep;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        fld.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            fld.value += aux2.charAt(i);
        fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}
/////////////////////////////////////////////////////////////////
function FormataTel(Campo, teclapres){
    var tecla = teclapres.keyCode;
	
    var vr = new String(Campo.value);
    vr = vr.replace("-", "");
	
    tam = vr.length + 1;
    if (tecla >= 36 && tecla <= 105){
        if (tam == 4 )
            Campo.value = '('+vr.substr(0, 3)+')';
        if (tam == 9  || tam == 13)
            Campo.value = vr.substr(0,9) + '-' + vr.substr(9,9);
    }
}
function FormataTelSemDDD(Campo, teclapres) {
    var tecla = teclapres.keyCode;

    var vr = new String(Campo.value);
    vr = vr.replace("-", "");

    tam = vr.length + 1;
    if (tecla >= 36 && tecla <= 105){
        if (tam == 5  || tam == 9)
            Campo.value = vr.substr(0,4) + '-' + vr.substr(5,9);
    }
}
/////////////////////////////////////////////////////////////////
function FormataCEP(Campo, teclapres){
    var tecla = teclapres.keyCode;
    if(!numbersonly(teclapres, false)) {
        return false;
    }
    var vr = new String(Campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");

    tam = vr.length + 1;

    if(tecla != 46 && tecla != 9 && tecla != 8 && tam > 8) {
        return false;
    }
    if (tecla != 9 && tecla != 8){
        if (tam > 2 && tam < 8)
            Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
        if (tam >= 5 && tam < 13)
            Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '-' + vr.substr(5,tam);
    }
    return true;
}


/////////////////////////////////////////////////////////////////
function FormataCPF(Campo, teclapres){
    var tecla = teclapres.keyCode;

    if(!numbersonly(teclapres, false)) {
        return false;
    }
    var vr = new String(Campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
	
    tam = vr.length + 1;
    
		
    if (tecla != 9 && tecla != 8){
        if (tam > 3 && tam < 7)
            Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
        if (tam >= 7 && tam <10)
            Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
        if (tam >= 10 && tam < 12)
            Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
    }
    return true;
}
/////////////////////////////////////////////////////////////////
function FormataCNPJ(Campo, teclapres){

    var tecla = teclapres.keyCode;

    var vr = new String(Campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace("/", "");
    vr = vr.replace("-", "");

    tam = vr.length + 1 ;

	
    if (tecla != 9 && tecla != 8){
        if (tam > 2 && tam < 6)
            Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
        if (tam >= 6 && tam < 9)
            Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
        if (tam >= 9 && tam < 13)
            Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
        if (tam >= 13 && tam < 15)
            Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
    }
}
/////////////////////////////////////////////////////////////////
function proximoCampo(atual, prox,tammax)
{
    if (atual.value.length >= tammax)
        prox.focus();
		
}

function SaltaCampo(campo,prox,tammax,teclapres){

    var tecla = teclapres.keyCode;
    vr = document.forms[0].elements[campo].value;
		
    if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108){
        document.form[campo].value = vr.substr(0, vr.length - 1);
    }
    else {
        vr = vr.replace("-","");
        vr = vr.replace("/","");
        vr = vr.replace("/","");
        vr = vr.replace(",","");
        vr = vr.replace(".","");
        vr = vr.replace(".","");
        vr = vr.replace(".","");
        vr = vr.replace(".","");
        tam = vr.length;

        if (tecla != 0 && tecla != 9 && tecla != 16 )
            if ( tam == tammax )
                document.forms[0].elements[prox].focus();
    }
}
//////////////////////////////////////////////////////////////////
function LimpaCampo(sValor,iBase){
    var tam = sValor.length
    var saida = new String
    for (i=0;i<tam;i++)
        if (!isNaN(parseInt(sValor.substr(i,1),iBase)))
            saida = saida + String(sValor.substr(i,1));
    return (saida);		
}
/////////////////////////////////////////////////////////////////
function TestaFormItr(theForm,iTipo){
    if (!(TestaNI (theForm.NI,iTipo)))
        return (false);
	
    if (!(TestaControle (theForm.Controle)))  
        return (false);
	
    return (true);
}
/////////////////////////////////////////////////////////////////
function TestaNI(cNI,iTipo){
    var NI 
    NI = LimpaCampo(cNI.value,10);
    switch (iTipo) {
        case 1:
            if (NI.length != 14){
                alert('O número do CNPJ informado está incorreto');
                cNI.value = "";
                cNI.focus();
                return(false);
            }

            if (NI.substr(12,2) != CalcularDV(NI.substr(0,12), 9)){
                alert('O número do CNPJ informado está incorreto');
                cNI.value = "";
                cNI.focus();
                return(false);
            }
            break;

        case 2:
            if (NI.length != 11){
                alert('O número do CPF informado está incorreto');
                cNI.value = "";
                cNI.focus();
                return(false);
            }

            if (NI.substr(9,2) != CalcularDV(NI.substr(0,9), 11)){
                alert('O número do CPF informado está incorreto');
                cNI.value = "";
                cNI.focus();
                return(false);
            }
            break;
	
        case 3:
            if (NI.length != 8){
                alert('O número do ITR informado está incorreto');
                cNI.value = "";
                cNI.focus();
                return(false);
            }
            var dv = new String(); 
            dv = CalcularDV(NI.substr(0,7), 9);
            dv = dv.substr(0,1);
            if (NI.substr(7,1) != dv){
                alert('O número do ITR informado está incorreto');
                cNI.value = "";
                cNI.focus();
                return(false);
            }
            break;

        default:
            return(false);
    }
    return (true);	
}  
/////////////////////////////////////////////////////////////////
function TestaHora(cHora){
    var hora; 
    hora = LimpaCampo(cHora.value,10);
    var tam = hora.length;
    if	(tam != 4){
        alert('A hora está incorreta');
        cHora.value = "";
        cHora.focus();
        return(false);
    }
	
    var hr = hora.substr(0,2)
    var min = hora.substr (2,2)
    var seg = hora.substr (4,2)	
    if ((hr > 23) || (min > 59) || (seg > 59)){
        alert('A hora está incorreta');
        cHora.value = "";
        cHora.focus();
        return(false);
    }
    return(true);
}
/////////////////////////////////////////////////////////////////
function TestaData_sd(cData){
    var data; 
    data = LimpaCampo(cData.value,6);
    var tam = data.length;
    if	(tam != 6){
        alert('A data está incorreta');
        cData.value = "";
        cData.focus();
        return(false);
    }
    //	var dia = data.substr(0,2)
    var mes = data.substr (0,2)
    var ano = data.substr (2,4)	
    if (mes > 12) {
        alert('A data está incorreta');
        cData.value = "";
        cData.focus();
        return(false);
    } 
    return(true); 
}
///////////////////////////////////////////////////////////////////
function TestaData(cData){
    var data; 
    data = LimpaCampo(cData.value,10);
    var tam = data.length;
    if	(tam != 8){
        alert('A data está incorreta');
        cData.value = "";
        cData.focus();
        return(false);
    }
    var dia = data.substr(0,2)
    var mes = data.substr (2,2)
    var ano = data.substr (4,4)	
    switch (mes){
        case '01':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '02':
            if  (dia > 29){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '03':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '04':
            if  (dia > 30){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '05':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '06':
            if  (dia > 30) {
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '07':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '08':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '09':
            if  (dia > 30){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '10':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '11':
            if  (dia > 30) {
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        case '12':
            if  (dia > 31){
                alert('A data está incorreta');
                cData.value = "";
                cData.focus();
                return(false);
            }
            break;
        default:
            alert('A data está incorreta');
            cData.value = "";
            cData.focus();
            return(false);
            break;			
    } 

    return(true); 
}
///////////////////////////////////////////////////////////////////
function VerAlfaNumerico(pInd){
    var pValor = document.forms[0].elements[pInd].value
    var AuxTam = pValor.length  
    for(var j=0;j<AuxTam;j++)
        if ((!IndAlfaNumerico(pValor.charAt(j))) || (pValor.charAt(j) == " ")){
            document.forms[0].elements[pInd].focus();  
            document.forms[0].elements[pInd].value = pValor = pValor.substring(0,j)           
        } 
}
////////////////////////////////////////////////////////////////////
function IndAlfaNumerico(N){
    for(var i=0;i<10;i++)
        if(N == i)
            return true;
    return false;    
}
////////////////////////////////////////////////////////////////////
function TestaDataAno(cData){
    var data; 
    data = LimpaCampo(cData.value,10);
    var tam = data.length;
    if	(tam != 6){
        alert('A data está incorreta');
        cData.value = "";
        cData.focus();
        return(false);
    }
    var mes = data.substr(0,2);
    var ano = data.substr(2,4);	
    if (mes > 12) {
        alert('A data está incorreta');
        cData.value = "";
        cData.focus();
        return(false);
    }
    return(true); 
}
/////////////////////////////////////////////////////////////////


//-->
