﻿function validaCPF(oSrc, args) {
    s = args.Value;

    if (isNaN(s)) {
        return args.IsValid = false;
    }

    var i;
    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) {
        return args.IsValid = false;
    }

    d1 = 11 - (d1 % 11);

    if (d1 > 9) {
        d1 = 0;
    }

    if (dv.charAt(0) != d1) {
        return args.IsValid = 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) {
        return args.IsValid = false;
    }

    return args.IsValid = true;
} 




function validaDataNascimento(source, clientside_arguments){
    
    if(clientside_arguments.Value.length == 0){
        clientside_arguments.IsValid=true;
        return;
    }
    else
    {
        if(clientside_arguments.Value.length != 10)
        {
            clientside_arguments.IsValid=false;
            return;
        }
    }
    
	var dia = (clientside_arguments.Value.substring(0,2)); 
    var mes = (clientside_arguments.Value.substring(3,5)); 
	var ano = (clientside_arguments.Value.substring(6,10)); 

	var cons = true; 
	
	// verifica se foram digitados números
	if (isNaN(dia) || isNaN(mes) || isNaN(ano)){
		clientside_arguments.IsValid=false;
	}
		
    // verifica o dia valido para cada mes 
    if ((dia < 1)||(dia < 1 || dia > 30) && 
		(mes == 4 || mes == 06 || 
		 mes == 09 || mes == 11 ) || 
		 dia > 31) { 
    	cons = false; 
	} 

	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		cons = false; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || 
	   ( dia > 28 && 
	   (parseInt(ano / 4) != ano / 4)))) { 
		cons = false; 
	} 
    
    var dataAtual = new Date();
    
    //Verifica se ano informado é menor que (ano atual - 100)
    if (ano < (parseInt(dataAtual.getFullYear()) - 100))
    {
        cons = false;
    }
    else
    {
        //Verifica se ano informado é maior que (ano atual)
        if (ano > parseInt(dataAtual.getFullYear()))
        {
            cons = false;
        }
    }
    
   
    
    if (cons == false) { 
		clientside_arguments.IsValid=false;
	} 
	else{
	    clientside_arguments.IsValid=true;
	}
}


function v_NR(tecla)
{
    if(typeof(tecla) == 'undefined')

    var tecla = window.event;

    var codigo = (tecla.which ? tecla.which : tecla.keyCode ? tecla.keyCode : tecla.charCode);

    // permite números, 8=backspace, 46=del e 9=tab, shift, home, esquerda, direita

    if ( (codigo >= 48 && codigo <= 57) || (codigo >= 96 && codigo <= 105) || codigo == 8 || codigo == 46 || codigo == 9 || codigo == 16 || codigo == 36 || codigo == 37 || codigo == 39)
    {
        return true;
    }
    else
    {
        return false; 
    } 
}
function soNumero(evt){
	var code = 0;
	var e = window.event;
	try
		{if (e.keyCode) code = e.keyCode;} catch(e) {}
	try
		{if (e.which) code = e.which;}  catch(e) {} // Netscape 4.?   
	try
		{if (e.charCode) code = e.charCode;}  catch(e) {} // Mozilla  
	
	if (!(code>47 && code<58)){
		try
			{e.keyCode=0;} catch(e) {}
		try
			{e.which=0;}  catch(e) {} // Netscape 4.?   
		try
			{e.charCode=0;}  catch(e) {} // Mozilla 
	}
	
}

function soNumerosVirgula(e,campo)
{     
    var tam = campo.value.length - 1;
    var valorSemCaracterInvalido = campo.value.substr(0, tam);

    if(!(((e.keyCode >= 96) && (e.keyCode <= 105)) || ((e.keyCode >= 48) && (e.keyCode <= 57)))){
        campo.value = valorSemCaracterInvalido;
    }
      
}
function FormataValor(campo,decimais,e,milhar)
{
	try
	{
		if(typeof(e) == 'undefined')
        {
            var e = window.event;
        }
        var tecla = (e.which ? e.which : e.keyCode ? e.keyCode : e.charCode);
        
		if(milhar == null || milhar == "undefined"){ 
			milhar = false;
		}
		var virgula='1';
		var valor = campo.value;
		var tam;
        
		if ( tecla == 8 || tecla == -1 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
		{
			for(i=0;i<decimais;i++)
			{
				virgula=virgula+'0';
			}
			valor = valor.replace( ",", "" );
			while(valor.indexOf('.')>-1)
			{
				valor = valor.replace('.','');
			}
			tam = valor.length;
			valor = valor/virgula;
			if(valor!=0 || tam > 2 ){
				campo.value = valor.toString();
				campo.value = campo.value.replace(".",",");
				if (campo.value.indexOf(",")>0){
					var vir = campo.value.substr(campo.value.indexOf(","),decimais+1);
					var d=(decimais - vir.length)+1;
					if (vir.length<=decimais+1){
						for(i=0;i<d;i++) 
							campo.value = campo.value + '0';
					}
				}
				else{
					campo.value = campo.value + ',';
					for (i=1;i<=decimais;i++)
						campo.value = campo.value + '0';
				}
				if (milhar){
					if(tam>(decimais+3) && tam<=(decimais+6)){
						campo.value = campo.value.substr(0,tam-(decimais+3)) + '.' + campo.value.substr(tam-(decimais+3),tam);
					}
					if(tam>(decimais+6) && tam<=(decimais+9)){
						campo.value = campo.value.substr(0,tam-(decimais+6)) + '.' + campo.value.substr(tam-(decimais+6),3)+ '.' + campo.value.substr(tam-(decimais+3),tam);
					}
					if(tam>(decimais+9) && tam<=(decimais+12)){
						campo.value = campo.value.substr(0,tam-(decimais+9)) + '.' + campo.value.substr(tam-(decimais+9),3) + '.' + campo.value.substr(tam-(decimais+6),3)+ '.' + campo.value.substr(tam-(decimais+3),tam);
					}
				}
			} 
		}
	}
	catch(err)
	{}
}

function submeterPesquisa(e, buttonid){ 
  var evt = e ? e : window.event;
  var bt = document.getElementById(buttonid);
  if (bt){ 
      if (evt.keyCode == 13){ 
            bt.click(); 
            return false; 
      } 
  } 
}

function contaCaracters(lenMax, controle, ctrlMsg)
{
    var tam = controle.value.length;
    ctrlMsg.value = (lenMax - tam > 0) ? lenMax - tam : 0;
    if (tam >= lenMax) controle.value = controle.value.substr(0, lenMax);
}

function abreJanelaConfirmacaoCandidatura(Pagina){
       var width = 350;
       var height = 200;

       var left = 99;
       var top = 99;
       window.open(Pagina,'UploadFoto', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
       void(0);       
    }

function desabilitarPagina(controle)
{
    if (WebForm_OnSubmit())
    {
   document.getElementById('' + controle + '').style.visibility = 'visible';
   document.getElementById('' + controle + '').style.height = document.body.scrollHeight;
   }
}

function validaData(source, clientside_arguments){
    
    if(clientside_arguments.Value.length == 0){
        clientside_arguments.IsValid=true;
        return;
    }
    else
    {
        if(clientside_arguments.Value.length != 10)
        {
            clientside_arguments.IsValid=false;
            return;
        }
    }
    
	var dia = (clientside_arguments.Value.substring(0,2)); 
    var mes = (clientside_arguments.Value.substring(3,5)); 
	var ano = (clientside_arguments.Value.substring(6,10)); 

	 var cons = true; 
	
	// verifica se foram digitados números
	if (isNaN(dia) || isNaN(mes) || isNaN(ano)){
		clientside_arguments.IsValid=false;
	}
		
    // verifica o dia valido para cada mes 
    if ((dia < 1)||(dia < 1 || dia > 30) && 
		(mes == 4 || mes == 06 || 
		 mes == 09 || mes == 11 ) || 
		 dia > 31) { 
    	cons = false; 
	} 

	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		cons = false; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || 
	   ( dia > 28 && 
	   (parseInt(ano / 4) != ano / 4)))) { 
		cons = false; 
	} 
    
    if (ano < 1800){
        cons = false;
    }
    
    if (cons == false) { 
		clientside_arguments.IsValid=false;
	} 
	else{
	    clientside_arguments.IsValid=true;
	}
}

function validaDataAno(source, clientside_arguments)
   {
      if (clientside_arguments.Value.length!=4)
      {
        clientside_arguments.IsValid=false;
      }
      else 
      {
        if (clientside_arguments.Value < 1900)        
            clientside_arguments.IsValid=false;
        else
            clientside_arguments.IsValid=true;
      }
   }
   
   function validaDataMesAno(source, clientside_arguments)
   {   
      if (clientside_arguments.Value.substr(0,2)>0 && clientside_arguments.Value.substr(0,2)<13)
      {
        if (clientside_arguments.Value.substr(3,1)>0)
        {
            clientside_arguments.IsValid=true;
        }
        else
        {
            clientside_arguments.IsValid=false;
        }
      }
      else 
      {
        clientside_arguments.IsValid=false;
      }
   }
   
   function validaDataMesAnoPermitindoNulo(source, clientside_arguments)
   { 
      if (clientside_arguments.Value == '__/____')
      {
            clientside_arguments.IsValid=true;
      }
      else
      {  
          if (clientside_arguments.Value.substr(0,2)>0 && clientside_arguments.Value.substr(0,2)<13)
          {
            if (clientside_arguments.Value.substr(3,1)>0)
            {
                clientside_arguments.IsValid=true;
            }
            else
            {
                clientside_arguments.IsValid=false;
            }
          }
          else 
          {
            clientside_arguments.IsValid=false;
          }
      }
   }
   
function abreJanelaPopup(Pagina){
       var width = 300;
       var height = 110;

       var left = 99;
       var top = 99;
       window.open(Pagina,'UploadFoto', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
       void(0);       
    }
    
function abreJanelaPedido(IUPedido){    
       var width = 500;
       var height = 400;

       var left = 99;
       var top = 99;
       window.open('pgPedido.aspx?IUPedido='+IUPedido,'Pedido', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
       void(0);       
    }
    
    function m_CEP(campo,tammax) {
        var vr = campo.value;
        vr = vr.replace( "-", "" );
        vr = vr.replace( ".", "" );
        var tam = vr.length;
        if (tam < tammax) { tam = vr.length + 1; }
        tam = tam - 1;
        if ( (tam > 2) && (tam <= 8) ) {
        vr = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ); }
        campo.value = vr;
    }
       
     function m_DATA(campo,tammax,tecla) {                     
            if(typeof(tecla) == 'undefined')
            var tecla = window.event;
            var codigo = (tecla.which ? tecla.which : tecla.keyCode ? tecla.keyCode : tecla.charCode);
            var vr = campo.value;
            vr = vr.replace( "/", "" );
            vr = vr.replace( "/", "" );
            var tam = vr.length;
            if (tam < tammax) { tam = vr.length + 1; }
            if (codigo == 8) { tam = tam - 1; }
            tam = tam - 1;
            if ( (tam >= 2) && (tam < 3) ) {
            vr = vr.substr( 0, tam - 0 ) + '/' + vr.substr( tam - 0, 2 ); }
            if ( (tam >= 3) && (tam < 4) ) {
            vr = vr.substr( 0, tam - 1 ) + '/' + vr.substr( tam - 1, 2 ); }
            if (tam == 4) {
            vr = vr.substr( 0, tam - 2 ) + '/' + vr.substr( tam - 2, 2 ) + '/' + vr.substr( tam - 0, 5 ); }
            if (tam == 5) {
            vr = vr.substr( 0, tam - 3 ) + '/' + vr.substr( tam - 3, 2 ) + '/' + vr.substr( tam - 1, 6 ); }
            if (tam == 6) {
            vr = vr.substr( 0, tam - 4 ) + '/' + vr.substr( tam - 4, 2 ) + '/' + vr.substr( tam - 2, 7 ); }
            if (tam == 7) {
            vr = vr.substr( 0, tam - 5 ) + '/' + vr.substr( tam - 5, 2 ) + '/' + vr.substr( tam - 3, 8 ); }
            
            
            campo.value = vr;
            
        }

    function m_CNPJ(campo,tammax) {

        var vr = campo.value;

        vr = vr.replace( "-", "" );

        vr = vr.replace( "/", "" );

        vr = vr.replace( ".", "" );

        vr = vr.replace( ".", "" );

        var tam = vr.length;

        if (tam < tammax) { tam = vr.length + 1 ; }

        tam = tam - 1;

        if ( (tam > 2) && (tam <= 5) ) {

        vr = vr.substr( 0, tam - 1 ) + '-' + vr.substr( tam - 1, tam ) ; }

        if ( (tam >= 6) && (tam <= 8) ) {

        vr = vr.substr( 0, tam - 5 ) + '/' + vr.substr( tam - 5, 4 ) + '-' + vr.substr( tam - 1, tam ) ; }

        if ( (tam >= 9) && (tam <= 11) ) {

        vr = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '/' + vr.substr( tam - 5, 4 ) + '-' + vr.substr( tam - 1, tam ) ; }

        if ( (tam >= 12) && (tam < 14) ) {

        vr = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '/' + vr.substr( tam - 5, 4 ) + '-' + vr.substr( tam - 1, tam ) ; }


        campo.value = vr; 

    }


     function m_CPF(campo,tammax) {
        var vr = campo.value;

        vr = vr.replace( "-", "" );

        vr = vr.replace( ".", "" );

        vr = vr.replace( ".", "" );

        var tam = vr.length;



        if (tam < tammax) { tam = vr.length + 1; }



        tam = tam - 1;

        if ( (tam > 2) && (tam <= 11) ) {

        vr = vr.substr( 0, tam - 1 ) + '-' + vr.substr( tam - 1, tam ); }

        if ( (tam == 10) ) {

        vr = vr.substr( 0, tam - 7 ) + '.' + vr.substr( tam - 7, 3 ) + '.' + vr.substr( tam - 4, tam ); }



        campo.value = vr;
    }

    function soNumerosData(e,campo)
    {    
        var vr = campo.value;
        var tam = vr.length;
        
        if(typeof(e) == 'undefined')
        {
            e = window.event;
        }
        var whichCode = (e.which ? e.which : e.keyCode ? e.keyCode : e.charCode);
            
        if (whichCode == 8) return true;//backspace
        if (tam == 10) return false;        
            var strCheck = '0123456789';
            var key = String.fromCharCode(whichCode); // Valor para o código da Chave
            if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    }

    function soNumeros(e)
    {
        
        if(typeof(e) == 'undefined')
        {
            e = window.event;
        }
        var whichCode = (e.which ? e.which : e.keyCode ? e.keyCode : e.charCode);
            
        if (whichCode == 8) return true;//backspace        
        var strCheck = '0123456789';
        key = String.fromCharCode(whichCode); // Valor para o código da Chave        
        if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    }

    function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
        if (objTextBox.value.length > 11) return false;
        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;
        key = String.fromCharCode(whichCode); // Valor para o código da Chave
        if (strCheck.indexOf(key) == -1) return false; // Chave inválida
        len = objTextBox.value.length;
        for(i = 0; i < len; i++)
            if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
        aux = '';
        for(; i < len; i++)
            if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
        aux += key;
        len = aux.length;
        if (len == 0) objTextBox.value = '';
        if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
        if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
        if (len > 2) {
            aux2 = '';
            for (j = 0, i = len - 3; i >= 0; i--) {
                if (j == 3) {
                    aux2 += SeparadorMilesimo;
                    j = 0;
                }
                aux2 += aux.charAt(i);
                j++;
            }
            objTextBox.value = '';
            len2 = aux2.length;
            for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
            objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
        }
        return false;
    }
    
    //************************************************************************************************************************************
    ///ALTERA O ESTADO DO FORMULÁRIO DE ACORDO COM O ARGUMENTO RECEBIDO, CHAMANDO O MÉTODO QUE ALTERA O ESTADO DE CADA ELEMENTO DESTE FORM.
    //************************************************************************************************************************************
    function alteraEstadoFormulario(form, estado)
    {
       var form = document.getElementById(form);    //Pega o id do form recebido
       var elemento = form.elements;                //Já tenho todos os elementos do form.
       var span = form.getElementsByTagName("span");
        
        
        if(elemento.length != null){
            for(var i=0; i<elemento.length; i++)         //Varre os elementos em busca dos tipos:
            {
                alteraEstadoElemento(elemento[i], estado);
            }
        }
        
        if(span != null)
            alteraEstadoElemento(span, estado);
    }
    
    //*********************************************************************************
    ///ALTERA O ESTADO DO(S) ELEMENTO(S) RECEBIDO(S) DE ACORDO COM O ARGUMENTO RECEBIDO.
    //*********************************************************************************
    function alteraEstadoElemento(elemento, estado)
    {     
        var auxElemento = document.getElementById(elemento);
                
        if(auxElemento == null)//Caso o elemento tenha sido passado pelo método "alteraEstadoFormulario", tem que descartar o getElementById, pois o que é passado é o proprio elemento.
            auxElemento = elemento;
            
        if(auxElemento.type == null){//se o elemento não for tipado:
        
            if(auxElemento.length == null){//se o elemento não tiver tamanho, serão então tipadas as variáveis 'select', 'span', 'input', pra que possam ser varridas mais abaixo.
                var select = auxElemento.getElementsByTagName("select"); 
                var span = auxElemento.getElementsByTagName("span");       
                var input = auxElemento.getElementsByTagName("input");     
            }
            else
            {
                for(var i=0;i<auxElemento.length;i++)                
                        auxElemento[i].style.visibility = estado ? "visible" : "hidden";
            }
                                              
            //Varre os elementos input
                for(var i=0; i<input.length; i++){
                    switch (input[i].type)
                    {
                        case "text": //textbox
                        {
                            input[i].style.visibility = estado ? "visible" : "hidden";
                            break;
                        }
                        case "submit": //botão
                        {
                            input[i].style.visibility = estado ? "visible" : "hidden";
                            break;
                        }
                        case "checkbox": //checkbox
                        {
                            input[i].style.visibility = estado ? "visible" : "hidden";
                            break;
                        }
                        case "radio": //radio button
                        {
                            input[i].style.visibility = estado ? "visible" : "hidden";
                            break;
                        }
                    }
                }

                //Varre os elementos labels ou RequiredFieldValidators
                for(var i=0;i<span.length;i++)
                {
                    span[i].style.visibility = estado ? "visible" : "hidden";
                    if(span[i].isvalid != null)//identificação de um required validator.
                        ValidatorEnable(span[i], estado);
                }

                //Varre os elementos do tipo select
                for(var i=0;i<select.length;i++)
                {
                    switch(select[i].type){
                        case "select-one": //dropdown
                            {  
                                select[i].style.visibility = estado ? "visible" : "hidden"; 
                                break;
                            }
                    }
                }
                      
        }
        else //caso o elemento seja tipado, faz o switch então para um tratamento.
        {   
            switch (auxElemento.type){
                case "text": //textbox
                {
                    auxElemento.style.visibility = estado ? "visible" : "hidden";
                    break;
                }
                case "submit": //botão
                {
                    auxElemento.style.visibility = estado ? "visible" : "hidden";
                    break;
                }
                case "checkbox": //checkbox
                {
                    elemento.style.visibility = estado ? "visible" : "hidden";
                    break;
                }
                case "radio": //radio button
                {
                    elemento.style.visibility = estado ? "visible" : "hidden";
                    break;
                }
                case "select-one": //dropdown
                {   
                    auxElemento.style.visibility = estado ? "visible" : "hidden";
                    break;
                }
            }
        }
    }
    
    //***********************************
    ///LIMPA O(S) ELEMENTO(S) RECEBIDO(S).
    //***********************************
    function limpaElemento(elemento)
    {
        var auxElemento = document.getElementById(elemento);
                
        if(auxElemento == null)//Caso o elemento tenha sido passado pelo método "alteraEstadoFormulario", tem que descartar o getElementById, pois o que é passado é o proprio elemento.
            auxElemento = elemento;
            
        if(auxElemento.type == null){//se o elemento não for tipado:
            
            if(auxElemento.length == null){//se o elemento não tiver tamanho, serão então tipadas as variáveis 'select', 'span', 'input', pra que possam ser varridas mais abaixo.
                var select = auxElemento.getElementsByTagName('select');     
                var input = auxElemento.getElementsByTagName('input'); 
                var textArea = auxElemento.getElementsByTagName('textarea');
            }
            
            if(input.length != null){
                for(var i=0;i<input.length;i++)
                {                    
                        switch(input[i].type){
                            case "text": //textbox
                            {
                                input[i].value = "";
                                break;
                            }
                            case "checkbox": //checkbox
                            {
                                input[i].checked = false;
                                break;
                            }
                            case "radio": //radio button
                            {
                                input[i].checked = false;
                                break;
                            }
                        }
                }
            }
            
            if(select.length != null){
                for(var i=0;i<select.length;i++)
                { 
                    switch(select[i].type){
                        case "select-one": //dropdown
                        {  
                            select[i].selectedIndex = 0;
                            break;
                        }
                    }
                }
            }
            
            if(textArea.length != null){
                for(var i=0;i<textArea.length;i++)
                {
                    switch(textArea[i].type){
                        case "textarea": //textbox com mais de uma linha:
                        {
                            textArea[i].value = "";
                            break;
                        }
                    }
                }
            }
        }
        else{
            switch(auxElemento.type){

                case "text": //textbox
                {
                    auxElemento.value = "";
                    break;
                }
                case "select-one": //dropdown
                {   
                    auxElemento.selectedIndex = 0;
                    break;
                }
                case "checkbox": //checkbox
                {
                    auxElemento.checked = false;
                    break;
                }
                case "radio": //radio button
                {
                    auxElemento.checked = false;
                    break;
                }
            }
        }
    }
